Search code examples
google-sheetsgoogle-sheets-apigoogle-query-language

Specify target worksheet in structured query of Google Sheets data


I am trying to query data from one of my google spreadsheets, and I have read the docs here.

My spreadsheet has two tabs/sheets. I can query data from the first sheet, but I can't figure out how to query data from the second sheet.

My query url looks like this:

https://docs.google.com/spreadsheets/d/[SpreadSheetId]/gviz/tq?tq=[query]

If I open the sheet in a browser I can see the sheet Id in the browser url:

https://docs.google.com/spreadsheets/d/[SpreadSheetId]/edit#gid=[SheetId]

Is there any way to specify this sheetId in the query url? I have tried adding various versions of /#gid=[sheetId] to my query url but it returns the full html of the page, not the json I expect.


Solution

  • The answer is:

    https://docs.google.com/spreadsheets/d/[spreadSheetId]/gviz/tq?gid=[sheetId]&tq=[query]
    

    Credits to tehhowch for pointing me in the right direction!