How can I get a list of sheets (name and "gid") in a Google spreadsheet using the Drive API? How can I get a list of sheets (name and "gid") in a Google spreadsheet using the Drive API? python python

How can I get a list of sheets (name and "gid") in a Google spreadsheet using the Drive API?


Indeed this seems to be a duplicate of How to convert Google spreadsheet's worksheet string id to integer index (GID)?

Here is the quick answer:

def to_gid(worksheet_id):    return int(worksheet_id, 36) ^ 31578


You can do this using the Spreadsheet service in Google Apps Script. It's not as convenient as having a direct API call, but at least it is possible:

  1. Call the SpreadsheetApp.openById(id) method to open the spreadsheet using the Drive file_id.
  2. Call the Spreadsheet.getSheets() method to retrieve a list of Sheet objects.
  3. For each sheet, call the Sheet.getSheetId() and Sheet.getSheetName() methods to get the name (which is what the spreadsheet user sees) and the sheetId (which is what needs to be provided in the gid= parameter when exporting).

You can use the recently-announced Apps Script Execution API to provide a REST API for your Apps Script project, which you can then call in a similar manner to the Drive API.