我在OnEdit()中运行Google表格。当特定行中的单元格更改为“已完成”时,将向作业所有者发送电子邮件。现在它会弹出一条浏览器消息。
但我希望避免在选择意外的情况下发送电子邮件,并在几秒钟后再次更改。
令人惊讶的问题是,在运行Utilities.sleep(10000)并重新读取单元格的值以确保它是相同的,即使它已被更改,它也会再次读取该值为“completed”。这与睡眠功能有关吗?
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 + ".");
}
}
}