Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sheet ID in Google Sheets API?

Google Sheets document can contain some sheets. First is default and '0'. Generally for any sheet there is address like this:

https://docs.google.com/spreadsheets/d/(spreadsheetId)/edit#gid=(sheetId)

with both spreadsheetId and sheetId.

But in API documentation there is no mention of how to use sheetId. I can only read and edit default sheet for given spreadsheetId.

If in request from code presented in exemplary link I added sheetId property I got error:

{ 
    message: 'Invalid JSON payload received. Unknown name "sheetId": Cannot bind query parameter. Field \'sheetId\' could not be found in request message.',
    domain: 'global',
    reason: 'badRequest' 
}

How to get access to other sheets than default in Google Sheets API and read or update fields in them?

like image 292
Daniel Avatar asked Oct 22 '18 17:10

Daniel


People also ask

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.

How do I add ID to Google Sheets?

You can generate a unique value using a formula in the spreadsheet. An ID must be a value, not a formula, though, so copy (Ctrl+C) and paste as plain text (Shift+Ctrl+V) the result of the formula calculation into the cell meant to contain the new ID. That's all there is to it!

What is spreadsheet ID in Google Sheets?

The spreadsheet ID serves as a unique identifier for a Google Sheet. The ID is the value between '/d/' and '/edit' in the URL of the Google Sheet. In the sample Google Sheet URL, the part that references the spreadsheet ID is highlighted in red https://docs.google.com/spreadsheets/d/spreadsheetId/edit#gid=0.


1 Answers

Sheet name is the easiest way to access a specific sheet. As written here, range parameter can include sheet names like,

Sheet1!A1

If you must use a sheet id instead of sheet name, You can use any of the alternate end points which uses dataFilter, like spreadsheets.values.batchUpdateByDataFilter instead of spreadsheets.values.batchUpdate. You can then use sheetId in request body at data.dataFilter.gridRange.sheetId. An example of using such a filter with sheetId is provided by another answer here by ztrat4dkyle.

However, developer metadata is the preferred method of permanently associating objects(sheets/ranges/columns) to variables, where user modifications are expected on such objects.

like image 150
TheMaster Avatar answered Oct 15 '22 06:10

TheMaster