Reputation: 194
When processing results of Google forms with Google Apps Script accessing the form by …
let formID = FormApp.getActiveForm().getId();
… sometimes fails with an exception like "Form data could not be retrieved." Manually started just a minute later it works properly.
To handle those errors the best way I want to catch the exception and retry the method one minute later. I came up with this:
function foo() {
const maxTries = 3;
let formID;
let tries = 1;
while(true) {
try {
formID = FormApp.getActiveForm().getId();
break;
} catch (e) {
console.log("Retrieving form data failed (" + tries + ")");
if (tries >= maxTries) {
console.log("Retrieving form data not possible"); // and/or …
throw(e);
return;
} else {
tries++;
};
};
};
// Do things with form stuff
};
How can I insert a 60 second pause between the tries? And I'm not sure anyway, if there isn't a better way to overcome those errors (at all or within Google Apps Script).
Upvotes: 0
Views: 1019
Reputation: 1333
In this case why don't you try using Utilities.sleep
. This method will pretty much puts the script to sleep for a set amount of time. Please note the maximum amount is of 5 minutes as the maximum execution time for a script is of 6 minutes. You can check runtime limits in the documentation.
Upvotes: 1