27

I am trying to use Google Drive API (Python) to download some tabs of a spreadsheets file. Are the gids information in the file's metadata? What I am trying to do is (this may not be correct, please suggest) :

file_metadata = self.service.files().get(file_id=file_id).execute()
# in the following line, how can I get a download url with a gid (a tab in a spreadsheet file)
download_url = file_metadata.get('exportLinks')['text/csv']
# download the file.
Darshit
  • 15
  • 1
  • 4
lucky_start_izumi
  • 2,265
  • 13
  • 39
  • 59

2 Answers2

0

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
Community
  • 1
  • 1
sorin
  • 149,293
  • 163
  • 498
  • 754
  • This doesn't seem to work anymore (perhaps due to the new google spreadsheets). For example, a worksheet_id of omlf5j6 is giving me 53609033112 using this method, when the actual gid is 1366234904 – Chris Apr 06 '16 at 17:21
0

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.

kiwidrew
  • 2,895
  • 1
  • 15
  • 22