Search code examples
pythongoogle-sheetsgoogle-sheets-apigspread

Why do i get TypeError in deleteDimision request using gspread (Python)


I'm trying to add rows and delete columns trough gspread, but i keep getting TypeError: string indices must be integers

Here's the code:

writeTo = client.open_by_key(file['id']).worksheet('Sheet1')

request = {
    "requests": [
        {
            "deleteDimension": {
                "range": {
                    "sheetId": 0,
                    "dimension": "COLUMNS",
                    "startIndex": 8,
                    "endIndex": 26
                }
            }
        }
    ]
}

result = writeTo.batch_update(request)

I'm using sheets API v3


Solution

  • Modification points:

    • It seems that batch_update method is the method of Class gspread.spreadsheet.Spreadsheet. Ref When I saw your script, client.open_by_key(file['id']).worksheet('Sheet1') returnd Class gspread.worksheet.Worksheet. I thought that this was the reason of your issue.
      • When I tested your script, I could confirm that the same error of TypeError: string indices must be integers occurs.

    When these points are reflected in your script, it becomes as follows.

    From:

    writeTo = client.open_by_key(file['id']).worksheet('Sheet1')
    

    To:

    writeTo = client.open_by_key(file['id'])
    

    Note:

    • In this modification, it supposes that the value of file['id'] is the valid Spreadsheet ID. Please be careful about this. When an error like Requested entity was not found. occurs, please confirm your Spreadsheet ID again.

    • And, I think that your request body is correct. When I tested your request body using the modified script, I confirmed that no error occurs.

    Reference: