This script saves the relevant emails I need from an inbox, but due to the size of the inbox I keep getting a timeout from google "Max execution time" error. As the script is taking longer than the 5 mins allowed. It works perfectly on a smaller inbox.
var SEARCH_QUERY = "label:League Full";
function getEmails_(q) {
var output = [];
var threads = GmailApp.search(q);
for (var i in threads) {
var msgs = threads[i].getMessages();
for (var j in msgs) {
output.push([msgs[j].getSubject(),msgs[j].getFrom(),msgs[j].getDate()]);
}
}
return output;
}
function appendData_(sheet, array2d) {
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}
function saveEmails() {
var array2d = getEmails_(SEARCH_QUERY,1,100);
if (array2d) {
appendData_(SpreadsheetApp.getActiveSheet(), array2d);
}
}
I've found a script which I think may work online but I'm struggling to implement it, does anyone have any experience with ScriptProperties that could point me in the right direction?
function runMe() {
var startTime= (new Date()).getTime();
saveEmails();
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+300000))
.create();
break;
} else {
saveEmails();
}
}
//do some more work here
}