Apps Script

Spreadsheets timed out — How to Fix in Apps Script

Exact Error Message: Service Spreadsheets timed out while accessing document with id ...

When Apps Script displays Spreadsheets timed out, Apps Script communicates with the Google Sheets calculation backend via RPC. When the target workbook has tens of thousands of volatile formulas (INDIRECT, NOW, TODAY, IMPORTRANGE, heavy conditional formatting), reading or writing even one range waits on full sheet recalculation until the RPC call times out.

Step-by-Step Resolution

  1. Replace per-cell loop calls (getRange(i, j).getValue() / setValue()) with single bulk array operations: getDataRange().getValues() and setValues().
  2. Audit the target spreadsheet for heavy conditional formatting rules across full columns (e.g. A:Z), unused empty trailing rows, and chained IMPORTRANGE calls, and delete blank rows.
  3. Convert historical static formula columns to plain values once periods close so every Apps Script write does not re-trigger thousands of recalculations.
  4. Batch writes in chunks (e.g., 500–2,000 rows per batch) with SpreadsheetApp.flush() and exponential backoff retries if transient backend contention occurs.

Common Edge Cases

Calling SpreadsheetApp.openById() simultaneously from multiple time-driven triggers or webhooks creates lock contention on the spreadsheet recalculation engine, triggering timeouts even on medium files.

When This Doesn't Work

If a workbook approaches Google Sheets calculation ceilings (hundreds of thousands of formula cells), archive older years into separate Spreadsheets or offload storage to BigQuery / SQL and keep only rolling reporting windows in Sheets.

Prevention Tips

Always acquire LockService.getScriptLock() for concurrent scripts, batch I/O outside loops, and minimize volatile functions in script-connected sheets.

Relevant Tools & Guides

Related Errors

Frequently Asked Questions

Does closing unused empty rows actually speed up Apps Script getValues()?

Yes. Deleting thousands of trailing blank rows and excess conditional formatting rules dramatically cuts backend load time.

Why does the timeout happen only on Monday mornings or peak hours?

Concurrent collaborators, triggered scripts, and dashboard reloads opening the workbook simultaneously cause temporary lock contention on the spreadsheet calculation thread.

Should I call SpreadsheetApp.flush() inside every loop iteration?

No! Calling flush() forces synchronous write synchronization and slows execution; only call flush() once at logical batch boundaries.

What is the maximum spreadsheet size before timeouts become frequent?

While Sheets supports 10 million cells, Apps Script latency typically climbs noticeably once heavy formula workbooks exceed ~500,000 calculated cells.