首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在GoogleSheets接口v4中使用findReplace函数需要什么作用域?

在GoogleSheets接口v4中使用findReplace函数需要什么作用域?
EN

Stack Overflow用户
提问于 2019-06-11 05:00:11
回答 1查看 286关注 0票数 1

我想在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.">
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-11 06:35:58

  • 您想要通过python使用findReplace request of Sheets API。
  • 您已经能够使用Sheets API修改电子表格。

如果我的理解是正确的,那么这个修改如何?我认为Invalid requests[0].findReplace: scope not set.错误的原因是没有定义替换值的范围。在您的请求体中,没有使用rangesheetIdallSheets的属性。那么下面的修改如何呢?

修改后的脚本:

发自:

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

参考资料:

如果我误解了你的问题,这不是你想要的结果,我道歉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56533468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档