问题
I have an google app script for Google Sheets. Recently I came across "Script runtime" limitations when my function was inserting data into a spreadsheet over 6 min. My modal window just hung and in the Dev Console I got “Exceeded maximum execution time”.
After some researching I figured how to re-implement my function so it would execute in batches and less likely hit the limit.
However, at this point I would like add some logic to my script which could centralized catch this limitation and all other possible and simply show some error message to a user in comparison just hanging and doing nothing.
Are there any approaches to accomplish that ?
回答1:
I'd recommend creating a trigger using ScriptApp.newTrigger
, which could be triggered by introducing another function which checks whether or not the script has exceeded the predetermined execution time (if the max is 6, you could set it to be 5 to be safe).
Referring to something like this -
// var today = new Date();
// 'today' is declard in the original script
function isTimeUp(today) {
var now = new Date();
return now.getTime() - today.getTime() > 30000;
// 30000 = 30 seconds; this is the threshold limit
// you are free to setup your own threshold limit
}
I've written more on this topic here where i've used a .timeBased()
trigger; however, you may use a different kind of trigger altogether.
来源:https://stackoverflow.com/questions/48689551/catch-google-app-script-quotas-and-limitations-errors