Search code examples
javascriptgoogle-sheetsgoogle-apps-script

onChange(e) in Google Sheets Apps Script refuses to trigger when told to check for specific cell


I used ChatGPT's assistance to try and get a function that changes a value of a set cell (cell C) when a target cell (cell B) changes its value from 5. This is the function I ended up with.

function onChange(e) {
  var cell = e.range;
  if(cell.getSheet().getName() == "Tester" && cell.getColumn() == 4 && cell.getRow() == 10) {
    var value = cell.getValue();
    if(value != 5) {
      cell.offset(9,0).setValue(value);
    }
  }
}

This function is located in a .gs file alongside various other simple formulas I use for custom buttons.

I use a different script with setValue (a button) to change the value of condition cell (cell A). When cell A's value becomes above a certain threshold (governed by other cells), it changes the results of the function contained in cell B, which, if the function above worked, should change the value in cell C. However, nothing is happening. I replaced the value with a set text, I tried changing the name of the sheet - it did nothing. I am unsure of how to proceed here.


Solution

  • onChange is not a reserved function name for a simple trigger. That is the reason why it doesn't work.


    If you are unfamiliar with Google Apps Script, using the basic ChatGPT is not a good idea, as you cannot easily notice when ChatGPT has hallucinated. Several users have reported non-working code suggested by this tool, in many cases because the code includes things that don't exist.

    To learn about the existing simple triggers in Google Apps Script, check out https://developers.google.com/apps-script/guides/triggers.