3

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

Cleptus
  • 3,446
  • 4
  • 28
  • 34

1 Answers1

1
  • 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:

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

Tanaike
  • 181,128
  • 11
  • 97
  • 165