Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Google Sheet by ID?

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.

like image 616
Finn Smith Avatar asked Oct 31 '14 19:10

Finn Smith


People also ask

How do I find a Google Sheet ID?

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".

How do I find the sheet ID in Google Sheets API?

The spreadsheet ID can be discovered from the spreadsheet URL; the sheet ID can be obtained from the spreadsheet.

What is sheet ID in Google Sheet?

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 .

How do I find my Google Docs ID?

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.


2 Answers

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; } 
like image 76
Serge insas Avatar answered Oct 13 '22 11:10

Serge insas


var sheetActive = SpreadsheetApp.openById("ID"); var sheet = sheetActive.getSheetByName("Name"); 
like image 40
Javier Avatar answered Oct 13 '22 12:10

Javier