gspread 到 google 张,需要文本换行帮助

gspread to google sheets, text wrapping help need

我正在尝试将 CSV 文件导入 Google sheets(工作正常)

在此之后,我希望 sheet 中的所有单元格都将文本换行。

我尝试了多种不同的方法,但是,我总是有某种错误。

这是我的代码 运行:

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"})

我似乎得到的错误是: gspread.exceptions.APIError:

 {'code': 400, 'message': 'Invalid requests[0].repeatCell: No grid with id: 828077185', 'status': 'INVALID_ARGUMENT'}

我尝试重命名 google sheet,重命名作品 sheet,尝试了不同的模块,也尝试导入 gspread-formatting,但似乎没有任何效果

我认为在您的脚本中,gc.import_csv(sh.id, content) 可能是您遇到问题的原因。当 gc.import_csv(sh.id, content) 为 运行 时,似乎通过包含 content 的值插入了新的 sheet,并删除了现有的 sheet。这样,worksheet = sh.get_worksheet(0)worksheet 就被删除了。我认为这就是您 No grid with id: 828077185.

问题的原因

为了避免这种情况,下面修改脚本怎么样?

修改后的脚本:

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"})

参考: