Activating a sheet in Google Apps Script doesn't work

1,022 views
Skip to first unread message

Nate P

unread,
Mar 7, 2022, 5:07:08 AM3/7/22
to Google Apps Script Community

I have created a Google Apps Script that is bound to a spreadsheet. I am trying to programmatically change the active sheet to Sheet2 when a user clicks on a particular cell in Sheet1.

From the documentation, this should be simple; there is a Sheet method called activate(). However, calling this function via a trigger does not change the active sheet - Sheet1 stays in focus. Other functions like hideSheet() and showSheet() work immediately, but not activate().

Any idea what I might be doing wrong? Am I using this function incorrectly?

Thank you in advance for your help!

-Nate

Clark Lind

unread,
Mar 7, 2022, 9:26:29 AM3/7/22
to Google Apps Script Community
This may help. Sometimes when I don't know the exact way to do a thing, I just record a simple Macro to see how Sheets handles it. 

function UntitledMacro() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A1').activate();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2), true);
};

Greg Noeninckx

unread,
Feb 8, 2023, 10:47:34 AM2/8/23
to Google Apps Script Community
This worked for me. Thanks.
Reply all
Reply to author
Forward
0 new messages