I have an Apps Script that constantly gets this error. Judge by the notification email, it looks like the time limit is 5 minutes. Does anyone know if there is a way to extend this time limit? Or perhaps is there a way to call the Apps Script again and it pick up from where it left off? Any help is appreciated.
问题:
回答1:
One thing you could do (this of course depends on what you are trying to accomplish) is:
- Store the necessary information (i.e. like a loop counter) in a spreadsheet or another permanent store(i.e. ScriptProperties).
- Have your script terminate every five minutes or so.
- Set up a time driven trigger to run the script every five minutes(or create a trigger programmatically using the Script service).
- On each run read the saved data from the permanent store you\'ve used and continue to run the script from where it left off.
This is not a one-size-fit-all solution, if you post your code people would be able to better assist you.
Here is a simplified code excerpt from a script that I use every day:
function runMe() {
var startTime= (new Date()).getTime();
//do some work here
var scriptProperties = PropertiesService.getScriptProperties();
var startRow= scriptProperties.getProperty(\'start_row\');
for(var ii = startRow; ii <= size; ii++) {
var currTime = (new Date()).getTime();
if(currTime - startTime >= MAX_RUNNING_TIME) {
scriptProperties.setProperty(\"start_row\", ii);
ScriptApp.newTrigger(\"runMe\")
.timeBased()
.at(new Date(currTime+REASONABLE_TIME_TO_WAIT))
.create();
break;
} else {
doSomeWork();
}
}
//do some more work here
}
NOTE#1: The variable REASONABLE_TIME_TO_WAIT
should be should be large enough for the new trigger to fire. (I set it to 5 minutes but I think it could be less than that).
NOTE#2: doSomeWork()
must be a function that executes relatively quick( I would say less than 1 minute ).
NOTE#3 : Google has deprecated Script Properties
, and introduced Properties Service
in its stead. The function has been modified accordingly.
回答2:
Also, try to minimize the amount of calls to google services. For example, if you want to change a range of cells in the spreadsheets, don\'t read each one, mutate it and store it back. Instead read the whole range (using Range.getValues()) into memory, mutate it and store all of it at once (using Range.setValues()).
This should save you a lot of execution time.
回答3:
Anton Soradoi\'s answer seems OK but consider using Cache Service instead of storing data into a temporary sheet.
function getRssFeed() {
var cache = CacheService.getPublicCache();
var cached = cache.get(\"rss-feed-contents\");
if (cached != null) {
return cached;
}
var result = UrlFetchApp.fetch(\"http://example.com/my-slow-rss-feed.xml\"); // takes 20 seconds
var contents = result.getContentText();
cache.put(\"rss-feed-contents\", contents, 1500); // cache for 25 minutes
return contents;
}
Also note that as of April 2014 the limitation of script runtime is 6 minutes.
回答4:
I have used the ScriptDB to save my place while processing a large amount of information in a loop. The script can/does exceed the 5 minute limit. By updating the ScriptDb during each run, the script can read the state from the db and pick up where it left off until all processing is complete. Give this strategy a try and I think you\'ll be pleased with the results.
回答5:
If you are using G Suite Business or Enterprise edition. You can register early access for App Maker after App maker enabled your script run runtime will increase run time from 6 minutes to 30 minutes :)
More details about app maker Click here