I am encountering a persistent TypeError: targetSheet.appendRows is not a function
in my Google Apps Script, even in a brand new Google Sheet.
My overall goal is to copy data from multiple sheets (representing appliance types) in a Google Sheets workbook named "Appliances" to a summary sheet named "appliancesGPT".
The following simplified script, which I've run in a brand new Google Sheet with a sheet named "appliancesGPT" and another named "Utilities" containing some dummy data in the first column, consistently produces this error when trying to append data:
function copyApplianceModelsSimplified() {
const targetSheetName = "appliancesGPT";
const ss = SpreadsheetApp.getActiveSpreadsheet();
let targetSheet = ss.getSheetByName(targetSheetName);
if (!targetSheet) {
targetSheet = ss.insertSheet(targetSheetName);
targetSheet.appendRow(["Test Type", "Test Model", "No"]);
Logger.log("Target sheet created and test row added.");
} else {
Logger.log("Target sheet found.");
try {
targetSheet.appendRow(["Another Type", "Another Model", "Maybe"]);
Logger.log("Test row appended successfully.");
} catch (error) {
Logger.log("Error appending test row: " + error);
}
}
const sourceSheet = ss.getSheetByName("Utilities");
if (sourceSheet) {
Logger.log('Source sheet "Utilities" found.');
const dataToAppend = [["From Utilities 1", "Model A", "Maybe"], ["From Utilities 2", "Model B", "Yes"]];
try {
targetSheet.appendRows(dataToAppend);
Logger.log("Error appending hardcoded data: " + error); // This line gets logged
} catch (error) {
Logger.log("Error appending hardcoded data: " + error); // And this line
}
} else {
Logger.log('Source sheet "Utilities" not found.');
}
}
The Execution log from this simplified script in the new Google Sheet is:
[Your Full Execution Log Here - Copy and paste the entire log] Interestingly, the appendRow() method works successfully to add the initial test row. However, the subsequent appendRows() with hardcoded data fails with the TypeError: targetSheet.appendRows is not a function.
I have tried the following troubleshooting steps:
- Verifying that the "appliancesGPT" sheet exists and that the targetSheet variable holds a Sheet object (confirmed by logging typeof targetSheet which returns "object" and targetSheet.getName() which returns "appliancesGPT").
- Testing this simplified script in a brand new Google Sheet, which still results in the same TypeError for appendRows().
- Running the original, more complex script in both my original "Appliances" workbook and a new test workbook, with the same TypeError occurring when attempting to use appendRows().
- This consistent failure in a clean environment suggests the issue is likely not within the logic of my scripts or the data in my spreadsheets. It seems to be a problem with how my Google Apps Script environment is interacting with the Sheet object and its appendRows() method.
I am seeking help to understand why appendRows() is being reported as not a function in this context, even though appendRow() works and the targetSheet appears to be a valid Sheet object. Any insights or suggestions for further troubleshooting would be greatly appreciated.
appendRows
method. First thing you need to do is check the official documentation and not rely on hallucinations of AI. See tag info page for official documentation, free resources and more details.