Google sheets API worksheet id issue / python

Viewed 176

I want to ask a question related to this: Is there a way to autofit a column in Google Sheets using gspread API for Python?

While creating a new sheet the first worksheet or "sheet1" has a gid of: edit#gid=0

This happens every time you create a new sheet. Only if you add a new worksheet "sheet2" you'll get a sheet gid withs some actual numbers for example: 12345.

spreadsheetName = "###"  # Please set Spreadsheet name.
sheetName = "###"  # Please set sheet name.

ss = client.open(spreadsheetName)
sheetId = ss.worksheet(sheetName)._properties['sheetId']
body = {
    "requests": [
        {
            "autoResizeDimensions": {
                "dimensions": {
                    "sheetId": sheetId,
                    "dimension": "COLUMNS",
                    "startIndex": 0,  # Please set the column index.
                    "endIndex": 2  # Please set the column index.
                }
            }
        }
    ]
}
res = ss.batch_update(body)

The problem is that "sheetId" : takes only integers. So how do I give the argument "sheetId" corresponding value "edit#gid=0"? And the integer: 0 which comes up if you run the code above does not work.

Thank you!

0 Answers
Related