1

I have wrote some codes in Google App Script and I get recurrently exceptions because I reach execution quota limits.

I want to avoid this exceptions but I not found any class or method to query the current status of the quotas.

evilside
  • 33
  • 3
  • 1
    Possible duplicate of [Exceeded maximum execution time in Google Apps Script](https://stackoverflow.com/questions/7854573/exceeded-maximum-execution-time-in-google-apps-script) – Rubén Apr 30 '18 at 20:49
  • Define "execution quota limits" - are these time-based ("Maximum execution time exceeded"), or API usage quotas, e.g. "Exceeded rate limit for _____ " or "Sent too many emails today" – tehhowch May 01 '18 at 13:19
  • Both. I would like to avoid all those exceptions. – evilside May 06 '18 at 17:05
  • I seen the other thread, It is a solution but not very "elegant", demand too much code for very simple issue, I was hope exist a Google specially Class for this. – evilside May 06 '18 at 17:11

1 Answers1

1

Unfortunately, there's no single function that would allow you to skip any quota limitations outright.

However, you can build a simple architecture where once you know what kind of rate limits you'd run into (say, either time or the count of hits etc.), you can build the logic of your code to accommodate such scenarios.

Example: here's how you can bypass script execution timeout, programmatically -

Code to detect the threshold -

// var today = new Date();
// 'today' is declard in the original script

function isTimeUp(today) {
  var now = new Date();
  return now.getTime() - today.getTime() > 30000;
  // 30000 = 30 seconds; this is the threshold limit
  // you are free to setup your own threshold limit
}

Code to intentionally break the code (before the script does it for you) so you can programatically setup triggers to pick things up right where it were broken off -

function firstFunction() {
    var today = new Date();
    for (var i = 0; i < something.length; i++) {
        // check if the script has exceeded threshold
        if (isTimeUp(today)) {
            // schedule a trigger for a different function
            ScriptApp.newTrigger("repeatFunction")
                .timeBased()
                .everyMinutes(1)
                .create();
            break;
        }
    }
}

function repeatFunction() {
    for (/* condition */) {
        // do stuff; maybe not everything from firstFunction()
        if (/* test condition that defines the last action */) {
            var triggers = ScriptApp.getProjectTriggers();
            for (var i = 0; i < triggers.length; i++) {
                // delete all triggers
                ScriptApp.deleteTrigger(triggers[i]);
            }
            break;
        }
    }
}

I realise you might've not needed too much of the coding part of this but I'd be happy to help as required :)

Sourabh Choraria
  • 2,255
  • 25
  • 64