【发布时间】:2021-05-20 05:52:18
【问题描述】:
我需要使用 Python 从工作表中删除特定行。 我们的工作表上有数千条记录,并且数据会定期更新,而且由于一切都是使用 Python 完成的,因此这项任务也需要使用 Python 完成。
现在这是我从文档和其他教程中得到的:
def connect_to_sheet():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
return sheet
必须在不同的函数中建立与工作表的连接,因为我在其他地方多次使用它,但在下一个函数中我发出请求并收到错误:
request_body = {
"requests": [
{
"deleteDimension": {
"range": {
"sheetId": SheetID,
"dimension": "ROWS",
"startIndex": startIndex,
"endIndex": endIndex
}
}
}
]
}
result = sheet.values().batchUpdate(spreadsheetId=SPREADSHEET_ID_4G,body=request_body).execute()
错误:
https://sheets.googleapis.com/v4/spreadsheets/sheetID/values:batchUpdate?alt=json 返回“ 收到无效的 JSON 有效负载。未知名称“请求”: 找不到字段。”。详细信息:“[{'@type': 'type.googleapis.com/google.rpc.BadRequest'、'fieldViolations': [{'description': '收到无效的 JSON 有效负载。未知名称 “请求”:找不到字段。'}]}]">
我搜索过的关于 SOF 的每个示例和类似问题都使用完全相同的请求!但我仍然无法确定导致此错误的问题。
任何帮助将不胜感激,谢谢。
【问题讨论】: