0

I'll try to be straight forward into this.

I've got a spreadsheet URL like this: https://docs.google.com/spreadsheets/d/1231SDFS3afer3SDfsdf-34SDFSDgfdsd42/edit#gid=5439787534

where the part gid=5439787534 points to the exact sheet inside the spreadsheet I want to access. I'm using python + gspred to access it, but I'm not finding a way to get to the GID I stated above.

My code is like this for now:

wb = client.open_by_url('https://docs.google.com/spreadsheets/d/1231SDFS3afer3SDfsdf-34SDFSDgfdsd42/edit#gid=5439787534')

But it doesn't access the sheet, it just access the spreadsheet in general, not accessing the exact tab I need..

Any help please?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
João Casarin
  • 674
  • 2
  • 9
  • 27

1 Answers1

0

I believe your goal and situation as follows.

  • You want to retrieve the worksheet using the sheet ID (gid) using gspread of python.
  • You have already bee able to get and put values using Sheets API.

Unfortunately, it seems that there are no methods for directly retrieving the worksheet from the sheet ID in gspread. So in this case, I would like to propose a script for achieving your goal.

Sample script:

In this script, the script of authorization is not shown. So please use your script for it. And please set the variables of spreadsheetId and sheetId.

spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetId = 12345 # Please set the sheet ID (the value of "gid")

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheets = [s for s in spreadsheet.worksheets() if s.id == sheetId]
if sheets:
    sheet = sheets[0]
    values = sheet.get_all_values()
    print(values)
  • When your Spreadsheet URL of https://docs.google.com/spreadsheets/d/1231SDFS3afer3SDfsdf-34SDFSDgfdsd42/edit#gid=5439787534 is used, the Spreadsheet ID and sheet ID are 1231SDFS3afer3SDfsdf-34SDFSDgfdsd42 and 5439787534, respectively.
  • When above script is run, when the sheet of sheetId is included in the Spreadsheet, the values are returned.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Could you explain the logic of `sheets = [s for s in spreadsheet.worksheets() if s.id == sheetId}` and also it's return content Thanks – João Casarin Nov 19 '20 at 05:04
  • @João Casarin Thank you for replying. About `sheets = [s for s in spreadsheet.worksheets() if s.id == sheetId]`, this is the list comprehension. In this case, the sheet object is retrieved from all sheet objects using the inputted `sheetId`. When the sheet object of `sheetId` is found, the object is put in the list. I think that [this thread](https://stackoverflow.com/q/4260280) will help to understand about it. If my answer was not useful for your situation, please tell me. At that time, I apologize and I would like to modify it. – Tanaike Nov 19 '20 at 06:12
  • Hmmm, list comprehension is actually a new thing for me, I haven't even saw this on my graduation... I think this is the problem, neither the thread you stated could help, I think I'll need to study it deeper, because I don't understand the part `[s for s in...]` Also, congrats for the way of handling the sheetId, I haven't thought of doing that... That worked perfectly! Thanks buddy! – João Casarin Nov 19 '20 at 22:07
  • I'm sorry for this, but is it possible to you trying to help me on my other issue? It's also about python + gspread, but now it's about handling merged cells. If necessary, I can post the link here – João Casarin Nov 19 '20 at 22:14
  • @João Casarin Thank you for replying. I'm glad your issue was resolved. About your new question, when you posted it, can you show the URL of the question? I would like to confirm it. – Tanaike Nov 19 '20 at 23:17
  • Sure I can! I'm glad you're interested onto this :) https://stackoverflow.com/questions/64901345/get-combined-merged-cells-value this is the post! – João Casarin Nov 19 '20 at 23:47
  • @João Casarin Thank you for replying. I will check it. – Tanaike Nov 19 '20 at 23:50