【发布时间】:2017-11-26 03:46:26
【问题描述】:
我正在尝试使用 google sheet API 将新行附加到现有的 google sheet。身份验证已经成功进行,并且代码能够读取工作表的内容。
但是,当我尝试使用 service.spreadsheets().values().append(...) 时,我得到以下 HttpError:"Invalid JSON payload received. Unknown name "": Root element must be a message."
这是我的代码:
from json import dumps as jsdumps
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?version=v4')
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)
spreadsheetId = '{...}'
rangeName = "Sheet1"
value_input_option = 'RAW'
insert_data_option = 'INSERT_ROWS'
value_range_body = {
"range": rangeName,
"majorDimension:": "ROWS",
"values": list(rd.values()), # rd is just a dictionary with some stuff in it
}
那么当我尝试发送它时:
request = service.spreadsheets().values().append(spreadsheetId=spreadsheetId, range=rangeName, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=jsdumps(value_range_body))
运行request.execute()后,我得到了上面列出的错误。
【问题讨论】:
-
您的“rangeName”变量似乎不完整。范围应类似于“Sheet1!A1:C3”。你试过那种格式吗?
-
[meta] 我发现这个页面是这个页面的中文副本:xbuba.com/questions/44706584问题ID也与Stackoverflow上的匹配。很遗憾他们可能无法访问原始的 Stackoverflow ,这可能是他们唯一的选择。由于政治造成了如此多的浪费。
标签: django python-3.5 google-sheets-api