我尝试使用Google工作表API将新行追加到现有的Google工作表中。身份验证已成功发生,代码可以读取工作表的内容。
但是,当我尝试使用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()
后,我收到上面列出的错误。
答案 0 :(得分:0)
我遇到了类似的问题。这对我有用:
spreadsheetId = 'foobar'
range = 'Sheet1!A1'
body = {
'values': [data],
"majorDimension:": "ROWS",
}
self.service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range,
valueInputOption='RAW',
insertDataOption='INSERT_ROWS',
body=body).execute()