【问题标题】:Sheets API batchupdate gives TypeError when trying to change background color尝试更改背景颜色时,Sheets API batchupdate 会出现 TypeError
【发布时间】:2018-10-14 01:04:01
【问题描述】:

更具体地说,我正在遵循 google 的基本教程,尝试为另一个应用程序更改单元格的背景颜色,但现在我正在单独运行它以进行测试。正在运行的代码是

    def highlight(sheetId):
    from googleapiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client as gclient, tools

    SCOPES = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = gclient.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    servicebot = build('sheets', 'v4', http=creds.authorize(Http()))

    reqs = {"requests": [
    {
      "repeatCell": {
        "range": {
          "sheetId": sheetId,
          "startColumnIndex": 2,
          "endColumnIndex": 4,
          "startRowIndex": 0,
          "endRowIndex": 1,
        },
        "cell": {
          "userEnteredFormat": {
            "backgroundColor": {
              "red": 1.0,
              "green": 0.0,
              "blue": 0.0
            },
          }
        },
        "fields": "userEnteredFormat(backgroundColor)"
      }
    }
    ]}



    servicebot.spreadsheets().batchUpdate('19G_4_m-H_jLjXHKxb5Q4PyHSf1Tv9GgbQP-13F95tjQ', body=reqs).execute

highlight(0)

当我运行它时,我得到了错误

    Traceback (most recent call last):
  File "C:\Users\rexfo\Desktop\autosparring\test.py", line 51, in <module>
    highlight(0)
  File "C:\Users\rexfo\Desktop\autosparring\test.py", line 49, in highlight
    servicebot.spreadsheets().batchUpdate('19G_4_m-H_jLjXHKxb5Q4PyHSf1Tv9GgbQP-13F95tjQ', body=reqs).execute
TypeError: method() takes 1 positional argument but 2 were given

那么我该如何解决呢?我做错了什么

【问题讨论】:

  • 所有参数都是关键字参数 - 您使用位置参数传递 spreadsheetId 这是不正确的。 (第一个也是唯一允许的位置是与所有类方法关联的隐式self
  • @tehhowch 好吧,错误已经消失,所以感谢您指出这一点,但是,即使它应该改变背景颜色,它似乎也没有对工作表做任何事情。你觉得我这样做有什么问题吗?
  • 适当编辑您的问题并包含相关背景信息,包括相关研究和您尝试过的所有测试/调整以及相应的结果。考虑使用 APIs Explorer 来确定正确的 JSON

标签: python google-sheets-api google-api-python-client


【解决方案1】:

已经有一段时间了,希望这会有所帮助。 我相信执行应该是一种方法。这意味着您需要执行servicebot.spreadsheets().batchUpdate('19G_4_m-H_jLjXHKxb5Q4PyHSf1Tv9GgbQP-13F95tjQ', body=reqs).execute() 才能运行该功能。

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    相关资源
    最近更新 更多