我想在Google Sheets APIv4中使用findReplace请求。我按照Google quickstart guide for the Sheets API in Python中的定义设置了我的作用域和请求,并确认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 Sheets读写的作用域,但是我不明白为什么我得到一个错误,说没有设置作用域。
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.">
发布于 2019-06-11 06:35:58
如果我的理解是正确的,那么这个修改如何?我认为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
mysheetid
表中搜索find
。如果要从所有工作表中搜索值,请使用sheetId
.而不是allSheets
的属性
注意:
False
。adminService.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID,body=body).execute() = {} body.setdefault('requests',[]).append(findreplace_request('@mydate@','TODAY')) response = body
参考资料:
如果我误解了你的问题,这不是你想要的结果,我道歉。
https://stackoverflow.com/questions/56533468
复制相似问题