我想在Google Sheets APIv4中使用findReplace请求。我按照Google快速入门指南中关于Python的Sheets API的定义设置了范围和请求,并确认该API可以与我的电子表格进行通讯。
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#FindReplaceRequest
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SHEET_ID = myspreadsheetid
creds = None
store = file.Storage('sheets_token.json')
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = client.flow_from_clientsecrets('client_2_https.json', SCOPES)
creds = tools.run_flow(flow, store, http=Http(disable_ssl_certificate_validation=True))
with open('sheets_token.pickle', 'wb') as token:
pickle.dump(creds, token)
adminService = build('sheets', 'v4', http=creds.authorize(Http(disable_ssl_certificate_validation=True)))
def findreplace_request(find, replacement):
findreplace_request = {}
findreplace_request['find'] = find
findreplace_request['replacement'] = replacement
findreplace_request['matchCase'] = True
findreplace_request['matchEntireCell'] = True
findreplace_request['searchByRegex'] = False
findreplace_request['includeFormulas'] = False
findreplace_request['sheetId'] = mysheetid
allSheets = False
request = {}
request['findReplace'] = findreplace_request
return request
body = {}
body.setdefault('requests',[]).append(findreplace_request('@mydate@','TODAY'))
response = adminService.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID, body=my_request).execute()
我清楚地设置了一个范围来读写Google表格,但我不明白为什么会收到错误消息,指出未设置范围。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\wnh659\AppData\Local\Continuum\anaconda3\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\wnh659\AppData\Local\Continuum\anaconda3\lib\site-packages\googleapiclient\http.py", line 851, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1Sx0CJJo-b6Z6JUaQEQ6cJ3Yxtjv3z9BtHN9EHl0-0jU:batchUpdate?alt=json return
ed "Invalid requests[0].findReplace: scope not set.">
答案 0 :(得分:0)
如果我的理解是正确的,那么该修改如何?我认为Invalid requests[0].findReplace: scope not set.
错误的原因是替换值的范围未定义。在您的请求正文中,未使用range
,sheetId
和allSheets
的属性。那么下面的修改如何?
def findreplace_request(find, replacement):
findreplace_request = {}
findreplace_request['find'] = find
findreplace_request['replacement'] = replacement
findreplace_request['matchCase'] = True
findreplace_request['matchEntireCell'] = True
findreplace_request['searchByRegex'] = False
findreplace_request['includeFormulas'] = False
sheetId = mysheetid
allSheets = False
request = {}
request['findReplace'] = findreplace_request
return request
至:
def findreplace_request(find, replacement):
findreplace_request = {}
findreplace_request['find'] = find
findreplace_request['replacement'] = replacement
findreplace_request['matchCase'] = True
findreplace_request['matchEntireCell'] = True
findreplace_request['searchByRegex'] = False
findreplace_request['includeFormulas'] = False
findreplace_request['sheetId'] = mysheetid # Added
allSheets = False
request = {}
request['findReplace'] = findreplace_request
return request
find
的表中搜索mysheetid
。如果要从所有工作表中搜索值,请使用allSheets
而不是sheetId
的属性。False
。在您的脚本中,我认为以下修改也可以进行。
body = {}
body.setdefault('requests',[]).append(findreplace_request('@mydate@','TODAY'))
response = adminService.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID, body=body).execute()
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。