When working with JavaScript or Google Apps Script (GAS), you may encounter the following error:
Exception: Cannot convert 'xxx' to Object
This article explains why this error occurs and how to fix it, focusing on patterns commonly seen in GAS.
The goal is not just to fix the error, but to prevent it from happening again.
What “Exception: Cannot convert ‘xxx’ to Object” Means
In short, this error means:
You are trying to treat a value as an Object even though it cannot be converted into one.
In GAS and JavaScript, this typically happens when you assume a variable is an object, but it is actually:
- a string
- a number
- null or undefined
- an unexpected return value from a spreadsheet or API
Common Cause ①: Passing a Non-Object Value
Many Object-related methods only work with real objects.
Example: Using Object.keys on a string
function sample() {
const value = "test";
const keys = Object.keys(value);
}
Object.keys() expects an object.
Passing a string or number causes this exception.
Fix
Only use Object methods on actual objects.
function sample() {
const value = { a: 1, b: 2 };
const keys = Object.keys(value);
}
Common Cause ②: Treating null / undefined as an Object
This is extremely common in GAS.
Example: Ignoring possible null values
function sample() {
const sheet = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("Sheet1");
const value = sheet.getRange("A1").getValue();
Object.keys(value);
}
If the cell is empty or unexpected, value may not be an object at all.
Fix
Always check for null and type safety.
if (value && typeof value === "object") {
Object.keys(value);
}
Common Cause ③: Misusing JSON.parse / JSON.stringify
JSON conversion mistakes are a major source of this error.
Example: Parsing an object instead of a string
function sample() {
const obj = { a: 1 };
JSON.parse(obj);
}
JSON.parse() only works on strings.
Passing an object causes a conversion failure.
Fix
Use JSON methods correctly.
const json = JSON.stringify({ a: 1 });
const obj = JSON.parse(json);
Common Cause ④: Misunderstanding Spreadsheet Value Types
In GAS, spreadsheet values are usually returned as strings, even if they look like JSON.
Example: Treating a JSON-like cell value as an object
function sample() {
const value = sheet.getRange("A1").getValue();
Object.keys(value);
}
If the cell contains { "a": 1 }, it is still a string.
Fix
Explicitly parse JSON strings.
const value = sheet.getRange("A1").getValue();
const obj = JSON.parse(value);
Object.keys(obj);
Common Cause ⑤: Confusing Arrays with Objects
Arrays and objects are related, but not interchangeable.
Example: Treating an array like an object
function sample() {
const list = [1, 2, 3];
Object.keys(list);
}
This may not throw immediately, but often leads to logic errors.
Fix
Use array methods for arrays.
list.forEach(v => {
Logger.log(v);
});
Debugging Checklist
If this error occurs, check the following:
- Is the value really an object?
- Could it be null or undefined?
- Are JSON.parse / JSON.stringify used correctly?
- Is the value coming from a spreadsheet as a string?
- Are arrays and objects handled separately?
In most cases, one of these points reveals the cause.
How to Prevent This Error
function safeProcess(value) {
if (!value) return;
if (typeof value === "string") {
try {
value = JSON.parse(value);
} catch (e) {
return;
}
}
if (typeof value === "object") {
Object.keys(value).forEach(key => {
Logger.log(key);
});
}
}
Best practices:
- Always check types explicitly
- Assume unexpected values in GAS
- Log intermediate values with
Logger.log()
Summary
The error “Exception: Cannot convert ‘xxx’ to Object” occurs when:
A value that is not an object is treated as one.
In Google Apps Script, this often involves:
- Spreadsheet values
- null / undefined
- JSON strings
- Type assumptions
When the error appears, the first question should always be:
“What type is this variable right now?”
Answering that usually leads directly to the fix.