When developing with Google Apps Script (GAS), one error that appears surprisingly often—and yet feels vague—is:
Exception: Invalid argument
Unlike TypeError or ReferenceError, this error is usually not about syntax.
Instead, it indicates that a Google-provided API was called with an argument that does not meet its requirements.
In this article, we’ll cover:
- What
Exception: Invalid argumentactually means - GAS code examples that reproduce the error
- Why it occurs so frequently in GAS
- Practical ways to prevent it in real-world projects
This is especially useful for developers working with SpreadsheetApp, DriveApp, and Utilities.
What “Exception: Invalid argument” Means
This error literally means:
An argument passed to a function is invalid.
In other words:
- The function exists
- The syntax is correct
- But the type, value, or format of the argument is not acceptable
In GAS, this error is commonly thrown by Google APIs such as:
SpreadsheetAppDriveAppUtilitiesUrlFetchApp
These APIs perform strict runtime validation on their arguments.
GAS Code Examples That Trigger This Error
Invalid Range Index (1-based Indexing)
function sampleRange() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(0, 1); // Rows and columns are 1-based
}
getRange(row, column) starts from 1, not 0.
Passing 0 results in Exception: Invalid argument.
Passing an Empty String or null to getRange
function sampleRangeString() {
const sheet = SpreadsheetApp.getActiveSheet();
const rangeName = "";
sheet.getRange(rangeName);
}
An empty string is not a valid range reference, so GAS rejects it as an invalid argument.
Passing a String Instead of a Date Object
function sampleDate() {
const date = "2025-12-16";
Utilities.formatDate(date, "Asia/Tokyo", "yyyy/MM/dd");
}
Utilities.formatDate expects a Date object, not a string.
Even if the string looks like a date, this causes an invalid argument error.
Using a Non-Existent Drive File ID
function sampleDrive() {
DriveApp.getFileById("dummy-id");
}
Although the argument type is a string, the ID does not correspond to an existing file, so GAS treats it as invalid.
setValues with Incorrect Array Structure
function sampleSetValues() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1:B2").setValues([1, 2, 3]);
}
setValues requires a two-dimensional array.
Passing a one-dimensional array results in Exception: Invalid argument.
Why This Error Is Common in Google Apps Script
Several characteristics of GAS make this error frequent:
- Argument validation happens only at runtime
- Error messages are generic and lack detail
- API methods are strict about data shape and type
- Spreadsheet and Drive data are often dynamic
A common misconception is:
“The value exists, so it should work.”
In GAS, existence is not enough—the structure must be exact.
How to Prevent It (Best Practices)
Explicitly Validate Arguments Before Calling APIs
if (!rangeName) return;
if (!(date instanceof Date)) return;
Always Match the API’s Expected Types
const date = new Date("2025-12-16");
Utilities.formatDate(date, "Asia/Tokyo", "yyyy/MM/dd");
Always Use 2D Arrays with setValues
sheet.getRange("A1:B2").setValues([
[1, 2],
[3, 4]
]);
Log and Inspect Arguments During Debugging
Logger.log(typeof value);
Logger.log(value);
With Invalid argument, it’s usually faster to inspect the shape of the data, not just the data itself.
Practical Debugging Checklist
If you encounter Exception: Invalid argument, check:
- Are any arguments
null,undefined, or empty strings? - Do arrays have the correct dimensions?
- Are
Date,Number, andStringtypes used correctly? - Do IDs and range references actually exist?
Most cases can be resolved by validating these points.
Summary
Exception: Invalid argument is not a GAS malfunction.
It’s a runtime signal that an API is being used incorrectly.
Typical characteristics include:
- The code runs until execution
- The function exists
- But the argument format does not meet API expectations
Once you adopt the habit of validating argument types and structures, this error becomes easy to eliminate.