Google-sheets – How to trigger a script in Google sheets when a value changes

google sheetsgoogle-apps-scriptgoogle-apps-script-triggers

I try to fire a script when a value in a certain cell or range of cells changes.
(it's not changed by the user, so I can't use onEdit)

I've tried to use onChange, but this doesn't fire my script at all

function onChange() {
//  verwijder verwerkte 'klaar' regels

  var actieSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Actie");
  var asRange = actieSheet.getRange("F3:F25") 
  var asValues = asRange.getValues();
    for (var i = 0; i < 22; i++) {
         x1='false';
         asValues[i][0]= false;
      }
      actieSheet.getRange("F3:F25" ) .setValue (asValues);
}

Is there a way to trigger a script if a Cell changes or even if there is any change in a sheet, it will do for me?

Best Answer

onChange isn't a reserved word for a simple trigger on Google Apps Script but there is an "on change" installable trigger that could execute a specified function regardless of it's name. Unfortunately for this case, this installable trigger, the same way that onEdit works, only is triggered by changes made by the user.

If the update is made by a script, make that script run the desired function or "merge" them. If that is not possible, the "workaround" is to use a time driven trigger or the HTML Service and setTimeout to poll the spreadsheet for changes.

The maximum frequency that a time driven trigger could be triggered is every minute (30 minutes for add-ons)

The maximum frecuency for setTimout is every 4ms or every 10ms depending on the web browser.

Be aware that there are quotas and limits that could be or not relevant for your case.

References

Related