Why is this changed value read as the original after Utilities.sleep?

I have Google Sheets running in OnEdit (). When a cell in a specific row is changed to “completed,” an email will be sent to the job owner. While it displays a browser message.

But I want to avoid sending an email in case of accidental selection and changing it after a few seconds.

The amazing problem is that after running Utilities.sleep (10000) and re-reading the cell value to make sure that it is the same, even if it has been changed, it will read the value again as "completed". Is this something related to sleep function?

  if (changedColumn == stageColumnIndex) {
    var newValue = sheet.getRange(changedRow, changedColumn).getValue(); // read changed cell value before the sleep call

  if (newValue.toLowerCase() == "completed") {
    //  check again in a few seconds in case it was an accidental selection which was quickly changed
    Logger.log('Sleeping');
    Utilities.sleep(10000);
    var checkedValue = sheet.getRange(changedRow, changedColumn).getValue();  //  read same cell value after sleep call but it is the same as newValue even when the cell was changed while sleeping
    Logger.log('newValue: ' + newValue);
    Logger.log('checkedValue: ' + checkedValue);

    var announceChange = (checkedValue == newValue);
    if (announceChange) {
      var streetIndex = 2;
      var street = sheet.getRange(changedRow, streetIndex).getValue();
      Browser.msgBox("Job completed at " + street + ".");
    }
  }
}
+4
source share

Source: https://habr.com/ru/post/1679620/


All Articles