I am trying to import a CSV to Google sheets (this is working correctly)
After this I want all the cells in the sheet to wrap the text.
I have tried it in multiple different ways, however, I always have some kind of error.
This is the code I am running:
import gspread
gc = gspread.service_account(filename='C:/pathtomyfile.json')
sh = gc.open("gsheetsname")
worksheet = sh.get_worksheet(0)
content = open('csvfilename.csv', 'rb').read()
gc.import_csv(sh.id, content)
worksheet.format("A:E", {"wrapStrategy": "WRAP"})
The errors I seem to be getting is something along the lines of: gspread.exceptions.APIError:
{'code': 400, 'message': 'Invalid requests[0].repeatCell: No grid with id: 828077185', 'status': 'INVALID_ARGUMENT'}
I tried renaming the google sheet, renaming the worksheet, tried different modules, also tried importing gspread-formatting, however nothing seems to really be working
I thought that in your script, gc.import_csv(sh.id, content)
might be the reason of your issue. When gc.import_csv(sh.id, content)
is run, it seems that new sheet is inserted by including the values of content
and the existing sheets are deleted. By this, worksheet
of worksheet = sh.get_worksheet(0)
is removed. I thought that this is the reason of your issue of No grid with id: 828077185
.
In order to avoid this, how about the following modified script?
gc = gspread.service_account(filename='C:/pathtomyfile.json')
sh = gc.open("gsheetsname")
content = open('csvfilename.csv', 'rb').read()
gc.import_csv(sh.id, content)
worksheet = sh.get_worksheet(0) # I moved this line here.
worksheet.format("A:E", {"wrapStrategy": "WRAP"})