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:
onEditrunssetValuemodifies the sheet- Another edit is detected
onEditruns 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
setValuecalls inside triggers - Use
PropertiesServiceto 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.