0

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

}
adamsportstech
  • 287
  • 2
  • 5
  • 14
  • Please see this link: [Exceeded Maximum Execution Time In Google](http://stackoverflow.com/questions/7854573/exceeded-maximum-execution-time-in-google-apps-script?rq=1) – Br. Sayan Feb 23 '16 at 11:24
  • Hi I tried that, if you see the code its the same but I couldn't get it to work. Any suggestions? I've added in the time (5 mins worth of milliseconds) and I've added in the function. @Br.Sayan – adamsportstech Feb 23 '16 at 12:03
  • Well, please insert a `Logger.log()` for checking `currTime - startTime` before the `if` statement `if(currTime - startTime >= MAX_RUNNING_TIME)`. Now you `MAX_RUNNING_TIME` contains nothing. Put the `Maximum(currTime - startTime)` value there. I think it should be ~ 360 secs. – Br. Sayan Feb 24 '16 at 03:16
  • I'm sorry, Google says `ScriptProperties API is deprecated.` So we need to find another way out. – Br. Sayan Feb 24 '16 at 03:19

2 Answers2

1

As ScriptProperties has been deprecated by Google, the above script should not work. As mentioned by Sujay, we should use the Properties Service instead. The following script is working fine.

    function runMe() {

  var startTime= (new Date()).getTime();

  var scriptProperties = PropertiesService.getScriptProperties();
  var startRow= scriptProperties.getProperty('start_row');
  for(var ii = startRow; ii <= 300000; ii++) {
    var currTime = (new Date()).getTime();

    Logger.log(currTime - startTime);

    if(currTime - startTime > 300000) {
      Logger.log("Inside Loop :" + currTime);
      scriptProperties.setProperty("start_row", ii);
      ScriptApp.newTrigger("runMe")
               .timeBased()
               .at(new Date(currTime + 300000))
               .create();
      break;
    } else {
      doSomeWork();
      Logger.log("Running on : " + new Date(currTime));
    }
  }

 Logger.log("Do some more work...");

}

The Execution Transcript entries are as follows:

[16-02-25 09:53:12:326 IST] Logger.log([300000.0, []]) [0 seconds]
[16-02-25 09:53:12:327 IST] Logger.log([Running on : Thu Feb 25 2016 09:53:12 GMT+0530 (IST), []]) [0 seconds]
[16-02-25 09:53:12:328 IST] Logger.log([300001.0, []]) [0 seconds]
[16-02-25 09:53:12:329 IST] Logger.log([Inside Loop :1456374192327, []]) [0 seconds]
[16-02-25 09:53:12:361 IST] Properties.setProperty([start_row, 173281.0]) [0.031 seconds]
[16-02-25 09:53:12:362 IST] ScriptApp.newTrigger([runMe]) [0 seconds]
[16-02-25 09:53:12:363 IST] TriggerBuilder.timeBased() [0 seconds]
[16-02-25 09:53:12:365 IST] ClockTriggerBuilder.at([Wed Feb 24 20:28:12 PST 2016]) [0.002 seconds]
[16-02-25 09:53:12:623 IST] ClockTriggerBuilder.create() [0.257 seconds]
[16-02-25 09:53:12:624 IST] Logger.log([Do some more work..., []]) [0 seconds]
[16-02-25 09:53:12:626 IST] Execution succeeded [10.298 seconds total runtime]

Don't get confused with the Trigger time, it is set at PST, but is working perfectly.

Br. Sayan
  • 486
  • 1
  • 7
  • 22
0

Use the Properties Service instead.

var scriptProperties = PropertiesService.getScriptProperties();
var startRow= scriptProperties.getProperty('start_row');
scriptProperties.setProperty("start_row", ii);

ref: Properties Service

Sujay Phadke
  • 2,145
  • 1
  • 22
  • 41
  • The problem is Google has deprecated Script Properties. That's the thing we're stuck with. Please read :[Class ScriptProperties](https://developers.google.com/apps-script/reference/properties/script-properties) Do you know any other options? – Br. Sayan Feb 24 '16 at 06:15
  • 2
    @Br.Sayan ScriptProperties is deprecated. not the properties service :) `getScriptProperties` is the replacement for `ScriptProperties`. read the link above – Sujay Phadke Feb 24 '16 at 08:47
  • @Sujay : you have written ‘userProperties‘, it's probably a typo...? – Serge insas Feb 24 '16 at 09:05
  • @SujayPhadke : Thank you!...You're right...`Properties Service` is the replacement. – Br. Sayan Feb 25 '16 at 04:28