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.
A spreadsheet ID can be extracted from its URL. For example, the spreadsheet ID in the URL https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 is "abc1234567".
The spreadsheet ID can be discovered from the spreadsheet URL; the sheet ID can be obtained from the spreadsheet.
Sheet ID is an identifier of a Sheet in a Google Spreadsheet file. To obtain it, open the file for the desired Sheet, and locate it at the end of the URL after 'gid=' : https://docs.google.com/spreadsheets/d/1EwXZweUNKKGSC6k6_6IkukyRFeNzi7qEIIjOAC9_vGA/edit#gid=ThisIsSheetID .
The easiest way to find this ID is to look in the address bar on your web browser when you open the Sheet. You can find the ID for any Google Sheet or Doc in the address bar when that Sheet or Doc is open in your web browser.
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; }
var sheetActive = SpreadsheetApp.openById("ID"); var sheet = sheetActive.getSheetByName("Name");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With