My google app script is iterating through the user's google drive files and copying and sometimes moving files to other folders. The script is always stopped after 5 minutes with no error message in the log.
I am sorting tens or sometimes thousands files in one run.
Are there any settings or workarounds?
A single execution of Apps script can last no longer than 6 minutes and you're hitting this limit.
Use batch operations Alternating read and write commands is slow. To speed up a script, read all data into an array with one command, perform any operations on the data in the array, and write the data out with one command.
If you are in the code editor, and want to stop a script from running, you can click the "cancel" link in the toast display. Or you can click "View" -> "Executions" from the code editor and then terminate the script.
While Google Apps Script implements a subset of ECMAScript 5, there's nothing forcing it to be asynchronous. While it is true that JavaScript's major power is its asynchronous nature, the Google developers appear to have given that up in favor of a simpler, more straightforward API. UrlFetchApp methods are synchronous.
One thing you could do (this of course depends on what you are trying to accomplish) is:
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 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.
NOTE#4: 2nd time when the function is called, it takes the ith value of for loop as a string. so you have to convert it into an integer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With