email - How to avoid exceeding maximum execution time with Google Apps Script & Gmail? -


i trying build table including following data emails sent/received within specific time frame gmail account: (1) message id, (2) sender email address, (3) recipient email address, (4) day , time, (5) subject

the following script seems work exceeds maximum execution time allowed google apps script. able me amend can run batches?

function datetostring(date) {   return date.getfullyear() + "/" + (date.getmonth() + 1) + "/" + date.getdate(); }  function searchbytimeframe(from, to) {   var threads = gmailapp.search("after:"+datetostring(from) +" before:"+datetostring(to)+" in:anywhere");   var results = [];   (var = 0; < threads.length; i++) {     logger.log(threads[i].getfirstmessagesubject() + " (count: " + threads[i].getmessagecount() + ")");     var messages = threads[i].getmessages();     (var j = 0; j < messages.length; j++) {       var message = messages[j];       var sender = message.getfrom();       var recipientsstr = message.getto()       results.push([message.getid(), sender, recipientsstr, message.getdate(), message.getsubject()]);       continue;     }   }    return results; }  function main() {   var ss = spreadsheetapp.getactivespreadsheet();   var = ss.getrange("b4").getvalue();   var = ss.getrange("b5").getvalue();   var results = searchbytimeframe(from, to);    if(results.length > 0) {     var sheet = ss.insertsheet();     var header = sheet.getrange("a1:e1").setvalues([['id', 'from', 'to', 'date', 'subject']]).setfontweight("bold");;     var cell = sheet.getrange("a2:e"+(results.length+1));     cell.setvalues(results);   } else {     var ui = spreadsheetapp.getui(); // same variations.     ui.alert('no email found...');   } } 

use batch library cbl (mdcb85ns0dbpktakfkuamwzpmfj86rila).

  • cbl initiates trigger go off in 7 mins (startorresumecontinousexecutioninstance())
  • once close 5 mins (istimerunningout()) present index thread stored
  • the next time runs index loaded batch key

i've not tested can see general idea - you'll need update main() append rows rather overwrite each time.

var function_name = "main"; var email_recipient = "!!! put email address here !!!";  function datetostring(date) {   return date.getfullyear() + "/" + (date.getmonth() + 1) + "/" + date.getdate(); }  function searchbytimeframe(from, to) {    cbl.startorresumecontinousexecutioninstance(function_name)    var threads = gmailapp.search("after:" + datetostring(from) + " before:" + datetostring(to) + " in:anywhere");   var results = [];   var = cbl.getbatchkey(function_name) || 0;    (; < threads.length; i++) {     logger.log(threads[i].getfirstmessagesubject() + " (count: " + threads[i].getmessagecount() + ")");     var messages = threads[i].getmessages();     (var j = 0; j < messages.length; j++) {       var message = messages[j];       var sender = message.getfrom();       var recipientsstr = message.getto()       results.push([message.getid(), sender, recipientsstr, message.getdate(), message.getsubject()]);       continue; // ajr: what's for?       if (cbl.istimerunningout(function_name)) {         cbl.setbatchkey(function_name, i)         break;       }     }   }    if (i === threads.length) {     cbl.endcontinuousexecutioninstance(function_name, email_recipient, "gmail search finished")   }    return results; }  function main() {    var ss = spreadsheetapp.getactivespreadsheet();   var = ss.getrange("b4").getvalue();   var = ss.getrange("b5").getvalue();   var results = searchbytimeframe(from, to);    if(results.length > 0) {     var sheet = ss.insertsheet();     var header = sheet.getrange("a1:e1").setvalues([['id', 'from', 'to', 'date', 'subject']]).setfontweight("bold");;     var cell = sheet.getrange("a2:e"+(results.length+1));     cell.setvalues(results);   } else {     var ui = spreadsheetapp.getui(); // same variations.     ui.alert('no email found...');   } } 

Comments