Error fix
Fix "Exceeded maximum execution time" in Apps Script
Exceeded maximum execution time
Google documents a six minute limit per execution. Stop at 4.5 minutes, save the next index, and let the next triggered run continue.
Set it up
- Move your row loop into processInBatches.
- Add a time-driven trigger every 10 minutes for the function that calls it.
- The progress is stored in script properties and cleared when finished.
The script
/**
* Fix: "Exceeded maximum execution time".
* Work in a time budget, remember where you stopped, and let the next run continue.
*/
var BUDGET_MS = 4.5 * 60 * 1000;
function processInBatches(items, handle, now) {
now = now || function () { return Date.now(); };
var props = PropertiesService.getScriptProperties();
var start = now();
var i = Number(props.getProperty('nextIndex') || 0);
while (i < items.length) {
if (now() - start > BUDGET_MS) {
props.setProperty('nextIndex', String(i));
return { done: false, next: i };
}
handle(items[i]);
i++;
}
props.deleteProperty('nextIndex');
return { done: true, next: i };
}What you should see
| run | processed | next index |
|---|---|---|
| 1 | 812 | 812 |
| 2 | 2,188 | done |
Run 1 stops at 812 of 3,000. Run 2 resumes. Sample values, not output from a real account.
Common errors and fixes
- The job never finishes
- Each run must make progress. Check that handle(item) does not throw on the same row forever.
- It restarts from row one
- nextIndex was deleted. Do not clear script properties while it runs.
Permissions. Running this script asks you to grant Google permissions for the services listed above, to your own script. TriggerShelf never receives your Google data. See the permissions table. Google will show "Google hasn't verified this app"; that is expected for a script you paste yourself.
TriggerShelf is independent and not affiliated with or endorsed by Google LLC. Google Sheets, Gmail, Google Drive and Apps Script are trademarks of Google LLC. Quota figures come from Google's Apps Script quotas page. Test on a copy first.