I am trying to run the following script to get around Google script's maximum execution time. I have the function run for 4 minutes and set a trigger for it to run again in 5 minutes starting where it left off in processing. The first time through it runs smoothly and sets the trigger. When the trigger runs the first time, it processes very slow and then does not run again. Could anyone tell me what I'm doing wrong and if there is a way around this?
function updateAll() {
var startTime= (new Date()).getTime();
var startRow = ScriptProperties.getProperty("start_row");
//Start on row 2 if null
if (!startRow) {
startRow = "2";
}
// This code deletes all the triggers
var triggers = ScriptApp.getProjectTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
for (var i = startRow; i < 3000; i++) {
var currTime = (new Date()).getTime();
if(currTime - startTime >= 240000) {
ScriptProperties.setProperty("start_row", i)
//Set new trigger to run in 5 minutes
ScriptApp.newTrigger("updateAll")
.timeBased()
.at(new Date(currTime+300000))
.create();
break;
} else {
//Do logic and set values in spreadsheet
//Run sleep to avoid maximum script execution error
Utilities.sleep(100);
}
};
}
Date.now()
does your(new Date()).getTime()
. – Henrique G. Abreu