python-3.x - Pygsheets update_cells() 不工作

标签 python-3.x google-sheets google-sheets-api pygsheets

我正在尝试使用 pygsheets 水平更新 3 个单元格的范围,但我遇到了一些问题

错误:

Traceback (most recent call last):
  File "C:\Users\Art\Desktop\Python\Plain\General_testing.py", line 12, in <module>
    wks.update_cells('I{0}:K{0}'.format(rows),[output['Name'], output['Age'], output['State']])
  File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\pygsheets\worksheet.py", line 431, in update_cells
    self.client.sh_update_range(self.spreadsheet.id, body, self.spreadsheet.batch_mode, parse=parse)
  File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\pygsheets\client.py", line 352, in sh_update_range
    self._execute_request(spreadsheet_id, final_request, batch)
  File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\pygsheets\client.py", line 418, in _execute_request
    response = request.execute()
  File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\Art\AppData\Local\Programs\Python\Python36\lib\site-packages\googleapiclient\http.py", line 842, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1P2FvtuOLWZGgVIp-D9ROeBqexXQt1eNv8tSxZdwGrlw/values/Sheet1%21I19%3AK19?valueInputOption=USER_ENTERED&alt=json returned "Invalid value at 'data.values[0]' (type.googleapis.com/google.protobuf.ListValue), "Joe"
Invalid value at 'data.values[1]' (type.googleapis.com/google.protobuf.ListValue), "19"
Invalid value at 'data.values[2]' (type.googleapis.com/google.protobuf.ListValue), "NY"">

产生相同错误的我的代码的简化版本:

import pygsheets

gc = pygsheets.authorize(service_file='secret.json')
sh = gc.open('TestSheet')
wks = sh.sheet1

row = 1
output = {}
output['Name'] = 'Joe'
output['Age'] = '19'
output['State'] = 'NY'
wks.update_cells('I{0}:K{0}'.format(row),[output['Name'], output['Age'], output['State']])

最佳答案

更新单元采用二维矩阵(列表的列表)作为值而非列表的输入

wks.update_cells('I{0}:K{0}'.format(row),[[output['Name'], output['Age'], output['State']] ] )

查看文档 here

关于python-3.x - Pygsheets update_cells() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47558532/

相关文章:

python-3.x - 如何保持变量和包含它的字典一致?

python - 太多值无法解包 for 循环计数列表中的值时出现错误

python - 使用 Google Sheets API 公开电子表格?

java - 如何在谷歌电子表格 API 中复制工作表

python - 使用 Python 从 Google Drive/Workspace 下载电子表格

python - 在python中通过json递归

Python单元测试: Ensure that objects do not contain data from previous run

javascript - Google 脚本 - 将 Gmail 中的数据获取到表格中

if-statement - 数字范围之间的多个 IF 语句

node.js - 在文件夹中创建 Google Sheets 文档(无需移动和删除旧文档)