0

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
6
  • 2
    console.log(typeof guestCInDate); (Also it's substring not Substring) Commented Nov 10, 2024 at 16:31
  • 1
    checkInYear is 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. Commented Nov 10, 2024 at 18:04
  • I think it's easier to use setActiveSheet(sheet) if the goal is to get the YYYY and go to the specific Sheet that's Based on a date input. Commented Nov 10, 2024 at 19:00
  • 1
    Please add a minimal reproducible example (There is no mention of what is the value of guestCInDate) Commented Nov 10, 2024 at 22:54
  • 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 @TheMaster console.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. Commented Nov 11, 2024 at 3:32

1 Answer 1

-1

Converting Date to String on Apps Script and navigate to the specific sheet using a ternary operator.

Doing what @TheMaster recommended to console.log(typeof guestCInDate);

It should provide something like this:

Output 1

From what was provided, it's assumed that guestCInDate is a string which is the one on the left. Unless the input is making it an object or other data types, you'll get something similar to the one on the right.


The script below uses a conditional (ternary) operator that navigates to the specific sheet based on what year is written on the script and based on @Saddles suggestion, I opted to usesetActiveSheet(sheet).

const myFunction = () => {
  var guestCInDate = new Date("August 15, 2020");
  console.log(typeof guestCInDate);
  var checkInYear = Utilities.formatDate(new Date(guestCInDate), 'GMT', 'yyyy');
  console.log(checkInYear);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var hterCalendar = ss.getSheetByName(checkInYear);
  hterCalendar ? ss.setActiveSheet(hterCalendar) : null;
}

OUTPUT:

Sample Output


References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.