首页 > 解决方案 > 尝试更改背景颜色时,Sheets API batchupdate 会出现 TypeError

问题描述

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

    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

那么我怎么能解决这个问题呢?我做错了什么

标签: pythongoogle-sheets-apigoogle-api-python-client

解决方案


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

:)


推荐阅读