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

You can use an old visualization API URL

f'https://docs.google.com/spreadsheets/d/{doc_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}'

to download a sheet by its name. I just posted this here together with code for the Google API Python library. This is also mentioned in another answer which links to the Charts docs where it is shown how to do this with a gid. Alternatively, you can download all sheets by exporting the spreadsheet as an e.g. Excel file,

class MimeTypes:
    EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

drive_api.files().export_media(fileId=file_id, mimeType=MimeTypes.EXCEL)

If you still want to go with getting sheets by gid, the gid is the sheetId in the Google Sheets API (see here).

And you should be able to use the Sheets API to get all the sheets – and their corresponding sheetIds.

Leave a Comment

techhipbettruvabetnorabahisbahis forumu