Delayed Execution on Edit with Google Apps Script

穿精又带淫゛_ 提交于 2019-12-07 23:23:37

问题


I have a relatively large spreadsheet (300 rows, 30 columns) that I color based on the values in the spreadsheet. I'm doing accessing the API minimally using only two accesses:

  • getValues(...) to access all the values of the data range.
  • setBackgrounds(...) to set all the backgrounds of the data range.

This runs in about half a second or less. However, it gets in the way if I make it run on every edit using onEdit(), but I also don't want it to be updated at regular time intervals when I'm not editing it, seems like a waste. Is there a good way to make the script run in a "delayed" way, updating at regular time intervals while I'm editing?


回答1:


Firstly, I would say you should look at Google Sheets' conditional formatting (Format > Conditional formatting menu item in Sheets) -- you may be able to do much of what you need without involving Apps Script at all.

Failing that, you can set up a regular time-based trigger to check for edits and change the backgrounds appropriately. You can support this trigger with a separate onEdit() trigger to record what has changed internally. The flow goes like this:

  1. A change is made and onEdit() triggers
  2. The onEdit() trigger only records the changed cell locations to a local variable or Cache
  3. A time-based trigger fires every minute/hour/whenever
  4. The time-based trigger checks the cache for edited cells, alters their backgrounds, then clears them from the cache

That said, depending on your workflow this approach may not be much better than simply using a time trigger to change the cells directly.



来源:https://stackoverflow.com/questions/26450158/delayed-execution-on-edit-with-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!