Retuning a GMail script to prevent "Exceeded maximum execution time" -
i use gmail account receive status updates devices have listed in google spreadsheet user , serial number. status update email have serial number of device in it's email subject , sent once week per device. due number of devices (600+), pretty constant influx of status updates various devices every day. i'm trying setup script search inbox , create list of devices haven't received status update email prompt me figure out why device isn't reporting in.
i've modified script found online at: http://forums.mozillazine.org/viewtopic.php?f=46&t=2740775. works obtaining device serial number listed in spreadsheet , searching inbox email subject serial number sent within past 15 days. script works fine when spreadsheet has 200 items, beyond start getting "exceeded maximum execution time" error , need 600+ items , more time goes on. had add utilities.sleep(1000) line avoid "service invoked many times in short time: gmail ratemax" error. there better way write script avoid these errors?
function searchemail() { var sheet = spreadsheetapp.getactivesheet(); var data = sheet.getdatarange().getvalues(); var waittime = 15; var emailto = "temp@email.com"; var emailsubject = "no email in " + waittime + " days following devices"; var emailtext = ""; (var = 1; < data.length; i++) { utilities.sleep(1000); var j = + 1; var gsearch = "in:inbox subject:(" + data[i][1] + ") newer_than:" + waittime + "d"; var threads = gmailapp.search(gsearch, 0, 1); if (threads.length == 0) { var emailtext = emailtext + j + " device: " + data[i][1] + " user: " + data[i][0] + "\n"; } } mailapp.sendemail(emailto, emailsubject, emailtext); }
i'd suggest see problem point of view.
could find common string emails report sending ? "device status" in subject or in body...
this way search inbox string, put label on avoid rechecking them , use simple function check in spreadsheet if number in list.
function checklist(device){ var sheet = spreadsheetapp.getactivesheet(); var data = sheet.getdatarange().getvalues(); for(var n=0;n<data.length;n++){ if(data[n][0]==device){ return true};// assuming number in column } return false; }
then send mail if(checklist(device)!=true){
this far more efficient think.
Comments
Post a Comment