I'm having a very strange and persistent issue with Google Apps Script and getSheetByName()
.
I have a Google Apps Script script connected to a Google Sheets spreadsheet. The script is supposed to read data from a specific sheet (which I've named various things during my attempts: Tasks
, PROJECTS
, TEMP_PROJECTS
, and finally DATA
) and create events in my Google Calendar.
The problem is that even though I specify the sheet name correctly with SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEET_NAME")
, the execution log still says that it's finding a sheet called Sheet
(which I assume is the default name of the first tab). As a result, the script can't find the correct data and doesn't create the events in the calendar.
What I've already tried (detailed list):
Checking the sheet name: I've checked and double-checked the name of the tab in my spreadsheet, making sure it matches exactly (including case) with the name passed to getSheetByName()
. I've also tried different names.
Editing the code: I've checked that the sheet name is consistent throughout the script (both in getSheetByName()
and in the log variables).
Audit log: I've used Logger.log()
to track the name of the sheet searched for and the result of getSheetByName()
. The log always shows that it searches for the correct name but finds Sheet
.
Creating a new script: I've created a new Apps Script project and copied the code, but the problem persists.
Creating a new sheet: I've created a new worksheet and a new script, but the problem persists.
Renaming the tabs: I've tried renaming the tabs in various ways.
Restarting the browser: I've completely closed and restarted the browser.
Permissions Check: I have checked and re-granted permissions to the script to access the spreadsheet and calendar.
Simplified Script: I tried a very simple script to just search for the DATA
sheet and log the result, but it keeps saying it found Sheet
.
Google Account Sign Out and Back In: I have tried signing out and back in to my Google account.
Questions:
Are there any settings at the spreadsheet or account level that could affect how getSheetByName()
works? Could there be a very persistent "caching" or syncing issue? If so, how could I fix it?
The problem is that even though I specify the sheet name correctly with
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEET_NAME")
, the execution log still says that it's finding a sheet calledSheet
(which I assume is the default name of the first tab)
The default name of the first tab in a new spreadsheet is Sheet1
in the United States locale.
Chances are that your code looks like this:
var sheet = ss.getSheetByName('x');
Logger.log(sheet);
That means that you're not logging the sheet's name but a Sheet
object. A sheet object is a JavaScript class that provides methods for accessing and manipulating the object's properties. When you blindly coerce it to a text string as in the above example, it will always render as Sheet
. To log the sheet's name, use sheet.getName()
, like this:
const sheetName = 'x';
const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName(sheetName);
if (sheet) {
console.log(sheet.getName());
} else {
console.log(`Cannot find sheet ${sheetName}`);
}
When no sheet by the name given is found in the spreadsheet, sheet.getName()
returns null
.
See Sheet.getName().