【问题标题】:'Worksheet' object has no attribute 'spreadsheets'“工作表”对象没有属性“电子表格”
【发布时间】:2023-04-02 20:50:01
【问题描述】:

这是我第一次使用 google sheet api。所以,我试图加粗谷歌工作表的第一行。但这样做时我得到错误 'Worksheet' object has no attribute 'spreadsheets'。我已经提到了下面的代码。请指出我做错了什么。

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets",
         "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json",scope)

client = gspread.authorize(creds)
sheet = client.open("SheetsAPI Practice").sheet1

req = { "repeatCell": {
        "range": {
            "sheetId": 0,
            "startRowIndex": 0,
            "endRowIndex": 1
        },
        "cell": {
            "userEnteredFormat": {
                "textFormat": {
                    "bold": True        
                }
            }
        },
        "fields": "userEnteredFormat.textFormat.bold"
    }
}

sheet.spreadsheets().batchUpdate(spreadsheetId=sheet.id,body=req).execute()

此行发生错误-

sheet.spreadsheets().batchUpdate(spreadsheetId=sheet.id,body=req).execute()

【问题讨论】:

  • 我的回答是否向您展示了您想要的结果?你能告诉我吗?这对我学习也很有用。如果这可行,与您有相同问题的其他人也可以将您的问题作为可以解决的问题。如果您对我的回答有疑问,我深表歉意。到时候,可以问一下你现在的情况吗?我想学习解决你的问题。
  • 请检查您答案的评论部分。非常感谢您的关心。

标签: python excel google-sheets-api


【解决方案1】:
  • 您想使用 Sheets API 中的 batchUpdate 方法。
  • 您希望使用 gspread 和 python 来实现此目的。
  • 您已经能够获取和输入 Google 电子表格的值。

如果我的理解是正确的,那么这个答案呢?

修改点:

  • 在gspread中,client.open("SheetsAPI Practice").sheet1中没有spreadsheets()的方法。在您的脚本中,您正在尝试将 python 的 googleapis 方法与 gspread 一起使用。错误信息的原因是这样的。
  • gspread 具有在 Sheets API 中使用 batchUpdate 的方法。因此,当client = gspread.authorize(creds) 可用于您要使用的 Google 电子表格时,您可以使用它。
  • 您的请求正文需要修改。当你对batchUpdate方法使用req时,请像{"requests": [req]}一样放入数组中。

当以上几点反映到你的脚本中时,它变成如下。

修改脚本:

从:
sheet = client.open("SheetsAPI Practice").sheet1

req = { "repeatCell": {
        "range": {
            "sheetId": 0,
            "startRowIndex": 0,
            "endRowIndex": 1
        },
        "cell": {
            "userEnteredFormat": {
                "textFormat": {
                    "bold": True
                }
            }
        },
        "fields": "userEnteredFormat.textFormat.bold"
    }
}

sheet.spreadsheets().batchUpdate(spreadsheetId=sheet.id,body=req).execute()
到:
spreadsheet = client.open("SheetsAPI Practice")
req = {
    "requests": [
      {
          "repeatCell": {
              "range": {
                  "sheetId": 0,
                  "startRowIndex": 0,
                  "endRowIndex": 1
              },
              "cell": {
                  "userEnteredFormat": {
                      "textFormat": {
                          "bold": True
                      }
                  }
              },
              "fields": "userEnteredFormat.textFormat.bold"
          }
      }
    ]
}
spreadsheet.batch_update(req)
  • 运行此修改后的脚本时,第一行的文本变为粗体。

注意:

  • 此修改后的脚本假定服务帐户可以使用 Google 电子表格。请注意这一点。

参考资料:

【讨论】:

  • 感谢您抽出宝贵时间进行如此精彩的解释。你说的其实都是对的。但是,当我运行您的修改版本时,它给出了这样的错误 - 字符串索引必须是整数。因此,我将参数作为这样的列表发送 - spreadsheet.batch_update([req])。这次它给了我这样的错误 - KeyError: 'range'。如果您能指出问题以及我应该如何解决它,那将是一个很大的帮助。
  • 对不起!你的回答确实起到了作用。发生错误是因为我打开了第一张工作表,如下表 = client.open("SheetsAPI Practice").sheet1。但我应该像你展示的那样打开它——电子表格 = client.open("SheetsAPI Practice")。然后在请求正文中提及 sheetId。非常感谢@Tanaike
  • @Wall-E 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多