コンテンツにスキップ
Zidooka
スポンサーリンク

RangeError: Maximum call stack size exceeded in Google Apps Script – Causes and Fixes

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

When developing with Google Apps Script (GAS), you may suddenly encounter the error:

RangeError: Maximum call stack size exceeded

At first glance, this error looks obscure, but in practice, its causes are very limited.
In most cases, it signals a logical design issue rather than a GAS bug.

In this article, we’ll walk through:

  • What this error actually means
  • GAS code examples that reproduce it
  • Why it occurs so often in GAS
  • Practical ways to prevent it in real projects

This is useful not only for GAS beginners, but also for developers working with triggers, recursion, and spreadsheet-heavy logic.


What “RangeError: Maximum call stack size exceeded” Means

This error occurs when function calls are nested too deeply, exceeding the JavaScript call stack limit.

In simple terms:

A function keeps calling other functions (often itself) until the runtime can no longer keep track of the call history.

This is a JavaScript error in general, but it appears frequently in GAS due to:

  • Recursive functions without proper termination
  • Triggers calling functions that modify spreadsheets
  • Recursive processing of large datasets
  • Circular callback structures

In most cases, something is almost infinite, even if it wasn’t intended to be.


GAS Code Examples That Trigger This Error

Recursion Without a Stop Condition (Most Common)

function sampleRecursion() {
  sampleRecursion();
}

This function calls itself endlessly, immediately overflowing the call stack.


Incorrect Termination Condition

function countDown(n) {
  if (n === 0) return;
  countDown(n++);
}
countDown(5);

Although it looks like a countdown, n++ increases the value.
The termination condition is never reached, leading to a stack overflow.


Recursive Array Processing on Large Data

function sumArray(arr) {
  if (arr.length === 0) return 0;
  return arr[0] + sumArray(arr.slice(1));
}

This may work for small arrays, but when used with large spreadsheet data, it can easily exceed the stack limit.


Trigger Functions Calling Themselves (GAS-Specific)

function onEdit(e) {
  SpreadsheetApp.getActiveSheet()
    .getRange("A1")
    .setValue("test");
  onEdit(e);
}

Here’s what happens:

  • onEdit runs
  • setValue modifies the sheet
  • Another edit is detected
  • onEdit runs again

This loop quickly exhausts the call stack.


Why This Error Is Common in Google Apps Script

GAS encourages patterns that make this error more likely:

  • Developers try to structure logic using recursion
  • Spreadsheet operations inside triggers are common
  • Data size is often dynamic and unpredictable
  • Call stacks are harder to visualize in trigger-based execution

The biggest issue is the assumption that:

“This function should only run once.”

When that assumption breaks, stack overflows follow.


How to Prevent It (Best Practices)

Always Use a Reachable Termination Condition in Recursion

function safeRecursion(n) {
  if (n <= 0) return;
  safeRecursion(n - 1);
}

Make sure the function state actually moves toward termination.


Prefer Loops Over Recursion in GAS

function sumArray(arr) {
  let total = 0;
  for (const v of arr) {
    total += v;
  }
  return total;
}

In GAS, for and while loops are usually safer and easier to debug than recursion.


Avoid Self-Invocation in Trigger Functions

  • Never directly call the same trigger function
  • Avoid unnecessary setValue calls inside triggers
  • Use PropertiesService to manage execution flags if needed

These measures alone prevent many production incidents.


Practical Debugging Checklist

If you see Maximum call stack size exceeded, check the following:

  • Are you using recursion anywhere?
  • Does the function state reliably progress toward termination?
  • Are triggers indirectly re-invoking themselves?
  • Are you processing large datasets recursively?

Most stack overflow issues in GAS can be resolved by answering these questions.


Summary

RangeError: Maximum call stack size exceeded is not a GAS bug.
It’s a design warning.

Common root causes include:

  • Infinite or near-infinite recursion
  • Missing or incorrect termination conditions
  • Trigger functions causing self-invocation

By restructuring logic and favoring iterative patterns, you can eliminate this error from real-world GAS projects.

スポンサーリンク

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)

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. We believe that if using AI improves productivity and helps convey the message more effectively, it should be utilized.

コメントを残す

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