13
votes

I am trying to use RESTful API to gather the data from a Google spreadsheet spreadsheet.

I read the document at https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get but I cannot a way that allows me to specify a specific GID (tabs) of the spreadsheet.

After call the API, I was able to get a value from the first tab of the spreadsheet, but I want to change it to the second page (another GID)

Any help would be appreciated. Thanks.

Edit: Added example:

I have a sample spreadsheet here: https://docs.google.com/spreadsheets/d/1da3e6R08S8OoibbOgshyAQy7m9VRGPOrSGzVIvMSXf4/edit#gid=0

When I want to get the value on A1, I can call the RESTful API:

https://content-sheets.googleapis.com/v4/spreadsheets/1da3e6R08S8OoibbOgshyAQy7m9VRGPOrSGzVIvMSXf4/values/A1?key=API_KEY

And I will get:

{
  "range": "Sheet1!A1",
  "majorDimension": "ROWS",
  "values": [
    [
      "This is on the first tab!"
    ]
  ]
}

But as you see in my spreadsheet, I have to tabs (sheet2), how can I get that value of "This is on the second tab!"?

2
What are you trying to do? Are you trying to read data from specific cells? - noogui
@noogui Yes, but my spreadsheet have different tabs (which specify by gid in the URL), but when I execute the RESTful call, it only allow to get the value from the first tab, and the API I found have no way to read another tabs' data. I added the example, which should make things more clear. - Jamie Phan
By tab, you mean sheets right? like Sheet1, Sheet2, Sheet3, etc? - noogui

2 Answers

21
votes

To specify a specific GID (tabs) of the spreadsheet. Enter your 'sheet_name' with single quotes. Don't miss to add double quotes for range. Hope it helps

var query = {
        auth: authClient,
        spreadsheetId: 'spreadsheetID', 
        range: "'sheet1'!A1:D3",
    };
0
votes
$data[] = new Google_Service_Sheets_ValueRange([
    'range' => 'MGM!A1:Z' . $rowNum,
    'values' => $values
]);

MGM - is your tag name