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
CodePudding user response:
I believe the error is in file['id']
You can't slice a string by a character, they must be sliced via integers, like file[5] for example to get the first 4 letters in file.
CodePudding user response:
Modification points:
- It seems that
batch_updatemethod is the method of Classgspread.spreadsheet.Spreadsheet. Ref When I saw your script,client.open_by_key(file['id']).worksheet('Sheet1')returnd Classgspread.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 integersoccurs.
- When I tested your script, I could confirm that the same error of
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 likeRequested 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.
