Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current worksheet gid?

I use Python and Gspread to create a gsheet with about 40 worksheet containing datas about accounts. On the first worksheet, I want to put something like a summary with a link to the account's worksheet with something like =LIEN_HYPERTEXTE("#gid=xxxxxxxxxxxx","Account_42") but for this i need the worksheet gid/url. I've tried things with gspread or with pygsheets but the only thing i get is the worksheet id.

Is there a simple way to get it ?

Thanks

like image 709
Corentin LEGROS Avatar asked Oct 17 '25 20:10

Corentin LEGROS


1 Answers

  • You want to retrieve the sheet IDs and sheet names in the Spreadsheet.
    • You want to retrieve the sheet IDs of 40 sheets in the created Spreadsheet.
  • You want to achieve this using gspread with python.
  • You have already been able to get and put values for Spreadsheet using Sheets API.

If my understanding is correct, how about this answer? GID is the sheet ID of a sheet in a Spreadsheet. This is the same with the worksheet ID using at gspread.

In order to retrieve all worksheet IDs in a Spreadsheet, I think that the method of worksheets() can be used.

Sample script:

spreadsheetId = "###"  # Please set the Spreadsheet Id.

client = gspread.authorize(credentials)
sh = client.open_by_key(spreadsheetId)
worksheet_list = sh.worksheets()
for sheet in worksheet_list:
    print('sheetName: {}, sheetId(GID): {}'.format(sheet.title, sheet.id))
  • In this script, it supposes that you have already used credentials.

Reference:

  • Selecting a Worksheet

If I misunderstood your question and this was not the direction you want, I apologize.

like image 99
Tanaike Avatar answered Oct 20 '25 09:10

Tanaike