Diagnostic reference
Exceeded maximum execution time
Why Google Apps Script stops after 6 minutes with Exceeded maximum execution time, how to batch slow loops with getValues(), and how to checkpoint work.
Updated November 26, 2026
Apps Script error
Exceeded maximum execution time
What it means
Google Apps Script limits each execution to 6 minutes, or 30 seconds for simple triggers and custom functions. When a run hits that limit, Apps Script stops it and keeps any edits or emails already completed. The usual cause is calling getValue(), setValue(), or another Google service inside a loop.
Related error messages
- Exception: Exceeded maximum execution time
- Custom function timed out after 30 seconds
Before and after
The code before and after the fix.
function markOverdueRows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Tasks');
if (!sheet) throw new Error('Missing Tasks tab.');
const lastRow = sheet.getLastRow();
for (let row = 2; row <= lastRow; row += 1) {
const status = sheet.getRange(row, 3).getValue();
if (status === 'Open') {
sheet.getRange(row, 4).setValue('Check');
}
}
}function markOverdueRows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Tasks');
if (!sheet) throw new Error('Missing Tasks tab.');
const lastRow = sheet.getLastRow();
if (lastRow < 2) return;
const numRows = lastRow - 1;
const statuses = sheet.getRange(2, 3, numRows, 1).getValues();
const flagRange = sheet.getRange(2, 4, numRows, 1);
const existingValues = flagRange.getValues();
const existingFormulas = flagRange.getFormulas();
const updated = existingValues.map((row, index) => {
if (statuses[index][0] === 'Open') return ['Check'];
return [existingFormulas[index][0] || row[0]];
});
flagRange.setValues(updated);
}How to fix it
- Read input columns once with getValues(), compute updates in memory, and write back only the target output column with setValues(). Read getFormulas() on the output column too so unchanged formula cells stay intact.
- Remove SpreadsheetApp.flush() from inside loops. Flushing on every row forces a server round trip each iteration.
- For loops that call GmailApp or UrlFetchApp, measure elapsed time and stop after 4 to 5 minutes. Save the next row number in PropertiesService so the next trigger run resumes there.
- Write a completion marker to each row right after an external action so a resumed run skips finished rows.
Things to check
- Apps Script does not undo writes made before the timeout. Inspect the sheet or Gmail Sent folder before running the script again.
- Batching fixes slow Sheet loops, but large API pulls or Drive exports still need to split work across runs.
In the Apps Script editor
Catch errors like this before clicking Run.
myFunction (published as Error Checker for Google Apps Script on the Chrome Web Store) checks your code while you edit at script.google.com. Diagnostics, autocomplete, and rule-based quick fixes are free without an account, run in your browser, and request no Google Drive, Gmail, or Apps Script project OAuth scopes.