【发布时间】:2021-02-07 21:37:30
【问题描述】:
我正在尝试使用 Google Sheets API 删除电子表格中的一行。运行代码时,我收到 400 错误。完整代码如下:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
def main():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# The ID and range of a spreadsheet.
SPREADSHEET_ID = '1GrO8NHRKZzTVMRnG9FskjzhWLKJySWQxT2VSXcuRixw'
RANGE_NAME = "B:F"
batch_update_spreadsheet_request_body = {
"requests": [
{
"deleteDimension": {
"range": {
"sheetId": SPREADSHEET_ID,
"dimension": "ROWS",
"startIndex": 26,
"endIndex": 26
}
}
}
],
}
request = service.spreadsheets().batchUpdate(spreadsheetId=SPREADSHEET_ID, body=batch_update_spreadsheet_request_body)
response = request.execute()
if __name__ == '__main__':
main()
这是我得到的错误:
Traceback (most recent call last):
File "f:/owcsc/Documents/GitHub/aotw/stackoverflow.py", line 59, in <module>
main()
File "f:/owcsc/Documents/GitHub/aotw/stackoverflow.py", line 54, in main
response = request.execute()
File "C:\Python38\lib\site-packages\googleapiclient\_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python38\lib\site-packages\googleapiclient\http.py", line 915, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1GrO8NHRKZzTVMRnG9FskjzhWLKJySWQxT2VSXcuRixw:batchUpdate?alt=json returned "Invalid value at 'requests[0].delete_dimension.range.sheet_id' (TYPE_INT32), "1GrO8NHRKZzTVMRnG9FskjzhWLKJySWQxT2VSXcuRixw"". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'requests[0].delete_dimension.range.sheet_id', 'description': 'Invalid value at \'requests[0].delete_dimension.range.sheet_id\' (TYPE_INT32), "1GrO8NHRKZzTVMRnG9FskjzhWLKJySWQxT2VSXcuRixw"'}]}]">
我确定这可能是一个简单的解决方法,但我找不到任何答案。
【问题讨论】:
-
@Rhys 我看到了那个帖子,问题似乎是需要字典,而不是 json。据我所知,我的不是 json 格式,但我对此了解不多,所以我可能错了。它主要是从 API 文档中复制的,因此如果文档有误,则可能是问题所在。
标签: python google-sheets google-api google-sheets-api