52

I know that Google Apps Script has a getSheetId() method for the Sheet Class, but is there any way to select a sheet within a spreadsheet by referencing the ID?

I don't see anything like getSheetById() in the Spreadsheet Class documentation.

6 Answers 6

60

You can use something like this :

function getSheetById(id) {
  return SpreadsheetApp.getActive().getSheets().filter(
    function(s) {return s.getSheetId() === id;}
  )[0];
}

var sheet = getSheetById(123456789);

And then to find the sheet ID to use for the active sheet, run this and check the Logs or use the debugger.

function getActiveSheetId(){
  var id  = SpreadsheetApp.getActiveSheet().getSheetId();
  Logger.log(id.toString());
  return id;
}
6
  • 12
    Thanks, I've been doing that. It just seems very clumsy that you can do SpreadsheetApp.openById() and you can getSheetId(), but you can't select a sheet by ID.
    – Finn Smith
    Oct 31, 2014 at 21:48
  • 3
    Careful if you have saved a Sheet id with Properties Service. It may not be exactly the same as the actual Sheet id when you call getProperty(). Make sure you pass Sheet.getSheetId().toString() to the setProperty method instead of just Sheet.getSheetId(). Note: you may need to change the method to deal with Strings.
    – Trigaten
    Oct 29, 2020 at 22:41
  • You can create a one liner by using return sheets.find(sheet => sheet.getSheetId() === id). Provided that you created a variable named sheets. Apr 18, 2021 at 16:19
  • When I use the getSheetById() function proposed by Serge insas or the one-liner proposed by melledijkstra I'm not able to perform any functions like e.g. .getRange() on the returned sheet. Can you tell me what I have to do to make it possible? Jul 20, 2021 at 7:03
  • It didn't work for me until I added toString() after s.getSheetId(). It's because Gogole Script was using ID in scientific notation instead of regular number or string. Apr 3, 2022 at 18:10
14
var sheetActive = SpreadsheetApp.openById("ID");
var sheet = sheetActive.getSheetByName("Name");
4
  • 11
    How does this help select a sheet if you know the sheet ID, but not the sheet name? SpreadsheetApp.openByID opens a spreadsheet, not a sheet.
    – browly
    Jan 9, 2017 at 23:53
  • @browly This helps because the ID never changes, but if you explicitly write the sheet name and the sheet is renamed the script will no longer work.
    – Xzila
    Oct 27, 2017 at 15:21
  • 8
    @Xzila SpreadsheetApp.openById() opens a Spreadsheet, not a Sheet, so it doesn't help answer the actual question asked.
    – talljosh
    Jun 27, 2018 at 2:38
  • How in the world does this answer have 14 upvotes when it should be deleted? Oct 31 at 20:36
10

Look at your URL for query parameter #gid

https://docs.google.com/spreadsheets/d/18K3KY2veYSQGaku8DxEI_a8V1ODEQyIGQCTgwP3uqg4/edit#gid=1962246736

In example above gid=1962246736, so you can do something like this:

function getSheetNameById_test() {
  Logger.log(getSheetNameById(19622467362));
}

function getSheetNameById(gid) {
  var sheet = getSheetById(gid ?? 0);
  if (null != sheet) {
    return sheet.getName();
  } else {
    return "#N/D";
  }
}

/** 
  * Searches within Active (or a given) Google Spreadsheet for a provided Sheet ID and returns
  * the Sheet if the sheet exists; otherwise it will return undefined if not found.
  *
  * @param {Integer} gid - the ID of a Google Sheet 
  * @param {Spreadsheet} ss - [OPTIONAL] a Google Spreadsheet object (https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet)
  * @return {Sheet} the Google Sheet object if found; otherwise undefined (https://developers.google.com/apps-script/reference/spreadsheet/sheet)
  */
function getSheetById(gid, ss) {
  var foundSheets = (ss ?? SpreadsheetApp.getActive()).getSheets().filter(sheet => sheet.getSheetId() === gid);
  return foundSheets.length ? foundSheets[0] : undefined;
}
4
  • 1
    For an unknown reason, the accepted answer doesn't work for me. This classic for each does the magic!
    – Rivo
    Jan 16, 2020 at 12:31
  • @Rivo Maybe try what I commented on the first solution?
    – Trigaten
    Oct 29, 2020 at 22:26
  • @Rodolpho, Please update the link as it seems invalid now.
    – Dhamo
    May 28, 2021 at 19:21
  • 1
    @Dhamo sorry for delay, link and source updated Mar 8, 2022 at 22:58
2

I'm surprised this API doesn't exist... It seems essential. In any case, this is what I use in my GAS Utility library:

/** 
  * Searches within a given Google Spreadsheet for a provided Sheet ID and returns
  * the Sheet if the sheet exists; otherwise it will return undefined if not found.
  *
  * @param {Spreadsheet} ss - a Google Spreadsheet object (https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet)
  * @param {Integer} sheetId - the ID of a Google Sheet 
  * @return {Sheet} the Google Sheet object if found; otherwise undefined (https://developers.google.com/apps-script/reference/spreadsheet/sheet)
  */
function getSheetById(ss, sheetId) {
  var foundSheets = ss.getSheets().filter(sheet => sheet.getSheetId() === sheetId);
  return foundSheets.length ? foundSheets[0] : undefined;
}
1

Simple and shortest method for oneliners:

const getSheetById = (spreadsheet, sheet_id) => spreadsheet.getSheets().find(sheet => sheet.getSheetId() === sheet_id)

Array.prototype.find returns undefined if there is no match

-5

Not sure about ID but you can set by sheet name:

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("your_sheet_name"));

The SpreadsheetApp Class has a setActiveSheet method and getSheetByName method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.