getActiveSheet Method

232 views
Skip to first unread message

Fernando DosSantos

unread,
Mar 18, 2021, 1:32:49 PM3/18/21
to Google Apps Script Community
Good day,

I believe that I am missing a piece of information to fully grasp the getActiveSheet method of the SpreadsheetApp class. Referring to the Apps Script documentation, its usage seems to be very straight forward: 

The active sheet in a spreadsheet is the sheet that is being displayed in the spreadsheet UI.

However, I find that no matter which sheet tab is displayed in the UI, the getActiveSheet method always chooses the first sheet (tab) of a multi-sheet spreadsheet as the 'active' sheet. I've even tried hiding the first sheet to trick it - no, it still insists that first sheet is the active sheet. 

Please help me to be understand this method.

Alan Wells

unread,
Mar 18, 2021, 1:46:16 PM3/18/21
to Google Apps Script Community
How is the sheet tab shown?  User clicks on the sheet tab?
Are you logging the sheet tab name in order to verify what sheet tab is being retrieved?

  var sh;
  
  sh = SpreadsheetApp.getActiveSheet();
  Logger.log(sh.getName())

fernando....@dish.com

unread,
Mar 18, 2021, 2:15:02 PM3/18/21
to Google Apps Script Community
Funny enough, the sheet tab is being shown in the UI (Google Sheets) by a function which you had posted to this group discussionhttps://developers.google.com/apps-script/community?place=msg%2Fgoogle-apps-script-community%2FqRG_SKp316s%2FmeR6Dcr6BwAJ

I'm using that function to only show only the sheet which corresponds to the day of week. I want to then grab the active sheet (which is showing in the UI,) and do something else with it. 

But I have tried the getActiveSheet method outside of this scenario, so that I can better understand it. So to your first question, even when I (the user) set a sheet as active (bring it in to view in the UI,) the getActiveSheet method always chooses the first tab in the spreadsheet. I say it 'chooses', because Logger.log (sh.getName()) prints the name of that first tab, never the tab which I make active in the UI.

Reply all
Reply to author
Forward
0 new messages