Google Apps Script: A Gentle Guide to the “DEADLINE_EXCEEDED” Error

* If you need help with the content of this article for work or development, individual support is available.

When working with Google Apps Script (GAS), you may sometimes see the “DEADLINE_EXCEEDED” error, which means your script stopped before completing. This happens because GAS has strict limits on how long a script can run. Although the message looks intimidating, the cause is simple, and the solutions are easy to apply.

GAS allows a maximum of about 6 minutes for manual runs or time-driven triggers, and about 30 seconds for Web Apps or webhook executions (such as doPost). If your script cannot finish within those limits, GAS forces it to stop and shows this error.

The most common reason for this issue is heavy or inefficient processing. A typical example is writing to a spreadsheet row by row. Code like the following tends to be very slow:

// Inefficient: writing one row at a time
for (let i = 1; i <= 1000; i++) {
  sheet.getRange(i, 1).setValue("OK");
}

A much faster approach is batching operations:

// Efficient: writing all rows at once
const values = Array.from({length: 1000}, () => ["OK"]);
sheet.getRange(1, 1, 1000, 1).setValues(values);

This simple change reduces execution time dramatically.

For large datasets, it’s also helpful to divide the process into smaller chunks using PropertiesService to save progress:

function processInChunks() {
  const prop = PropertiesService.getScriptProperties();
  let start = Number(prop.getProperty("startRow") || 1);
  const chunkSize = 200;

  const sheet = SpreadsheetApp.getActiveSheet();
  const lastRow = sheet.getLastRow();

  for (let i = start; i < start + chunkSize && i <= lastRow; i++) {
    // Your task here
  }

  prop.setProperty("startRow", start + chunkSize);

  if (start + chunkSize <= lastRow) {
    console.log("Continuing next run");
  } else {
    console.log("Complete");
    prop.deleteProperty("startRow");
  }
}

This technique helps keep each execution short and safe within time limits.

If your script calls external APIs frequently (for example via UrlFetchApp), reducing the number of requests or using caching often helps.

In short, DEADLINE_EXCEEDED is not a dangerous error—it’s a sign that your script can become faster and more stable with a few simple optimizations. If you’d like me to review your specific script, feel free to share it anytime.


Ref.Ref.

  1. Google Apps Script Quotas
    https://developers.google.com/apps-script/guides/services/quotas
  2. Apps Script Best Practices
    https://developers.google.com/apps-script/guides/support/best-practices
  3. UrlFetchApp Reference
    https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

ZIDOOKA!

Need help with the content of this article?

I provide individual technical support related to the issues described in this article, as a freelance developer. If the problem is blocking your work or internal tasks, feel free to reach out.

Support starts from $30 USD (Estimate provided in advance)
Thank you for reading

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Policy on AI Usage

Some articles on this site are written with the assistance of AI. However, we do not rely entirely on AI for writing; it is used strictly as a support tool.