16

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!"?

Jamie Phan
  • 796
  • 1
  • 7
  • 26
  • What are you trying to do? Are you trying to read data from specific cells? – ReyAnthonyRenacia May 02 '17 at 15:38
  • @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 May 02 '17 at 17:11
  • By tab, you mean sheets right? like Sheet1, Sheet2, Sheet3, etc? – ReyAnthonyRenacia May 03 '17 at 10:17

2 Answers2

26

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",
    };
RAMESHKUMAR
  • 316
  • 4
  • 11
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Furthermore, I would suggest to post code as code (instead of text). – Robin Topper Aug 03 '17 at 13:52
  • 2
    Thank's a lot, I cannot find this information in the official documentation. You save my night ! – A. STEFANI Apr 04 '18 at 14:05
  • @ASTEFANI Happy that i saved your night ;) – RAMESHKUMAR Sep 17 '18 at 07:04
  • but what if there are two sheets(GRID) with same name? – omkar yadav Oct 11 '21 at 10:10
0
$data[] = new Google_Service_Sheets_ValueRange([
    'range' => 'MGM!A1:Z' . $rowNum,
    'values' => $values
]);

MGM - is your tag name

w4kskl
  • 141
  • 1
  • 6