I have a Google Sheet with different SheetName for each year, in YYYY format.(e.g. 2023, 2024) Based on a date input, I want to get the YYYY and go to the specific Sheet.
I have converted the date into YYYY:
var checkInYear = Utilities.formatDate(new Date(guestCInDate),'GMT','yyyy');
But when I try
var hterCalendar = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(checkInYear).getActiveRange().getValue();
It does not go to the specific sheet for that year, but goes to the first sheet in the file.
I suspect it's because my checkInYear is a date variable and my Sheet name is a string, so it could not find a match.
How can I convert my checkInYear to a string ?
I have tried:
var checkInYear = guestCInDate.Substring(1,4);
But got an error:
TypeError: guestCInDate.Substring is not a function


console.log(typeof guestCInDate);(Also it'ssubstringnotSubstring)checkInYearis a string. I'd suspect that the issue is running the code in a context where.getActiveRange()always defaults to the first tab. Please provide a minimal reproducible example and explain how you run the code.setActiveSheet(sheet)if the goal isto get the YYYY and go to the specific Sheetthat'sBased on a date input.guestCInDate)How can I convert my checkInYear to a string ?"checkInYear" is a string:formatDate(date, timeZone, format)returns a string. One can prove this using the suggestion by @TheMasterconsole.log(typeof checkInYear);returns "string". As requested, please supply an MRE AND please be specific about how this function is triggered, and the location of the cursor at the time of execution.