简体   繁体   English

“工作表” object 没有属性“电子表格”

[英]'Worksheet' object has no attribute 'spreadsheets'

This is my first time working with google sheets api.这是我第一次使用谷歌表格 api。 So, I'm trying to bold the first row of a google sheet.所以,我试图加粗谷歌表格的第一行。 But I get the error 'Worksheet' object has no attribute 'spreadsheets' while doing so.但这样做时我收到错误“工作表”object 没有属性“电子表格” I have mentioned the code below.我已经提到了下面的代码。 Please point out what am I doing wrong.请指出我做错了什么。

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()

The error occurred on this line -错误发生在这一行 -

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

  • You want to use the method of batchUpdate in Sheets API.您想使用 Sheets API 中的 batchUpdate 方法。
  • You want to achieve this using gspread with python.您想使用带有 python 的 gspread 来实现此目的。
  • You have already been able to get and put values for Google Spreadsheet.您已经能够获取和放置 Google 电子表格的值。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?

Modification points:修改点:

  • In gspread, there are no method of spreadsheets() in client.open("SheetsAPI Practice").sheet1 .在 gspread 中, client.open("SheetsAPI Practice").sheet1中没有spreadsheets()的方法。 In your script, you are trying to use the method of googleapis of python with gspread.在您的脚本中,您尝试使用带有 gspread 的 python 的 googleapis 方法。 The reason of error message is this.错误消息的原因是这样的。
  • The gspread has the method for using batchUpdate in Sheets API. gspread 有在 Sheets API 中使用 batchUpdate 的方法。 So you can use this, when client = gspread.authorize(creds) can be used for the Google Spreadsheet you want to use.因此,当client = gspread.authorize(creds)可用于您要使用的 Google 电子表格时,您可以使用它。
  • Your request body is required to be modified.您的请求正文需要修改。 When you use req to the batchUpdate method, please put in the array like {"requests": [req]} .当您对 batchUpdate 方法使用req时,请放入类似{"requests": [req]}的数组中。

When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

From: 从:
 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()
To: 至:
 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)
  • When this modified script is run, the texts of 1st row become the bold.运行此修改后的脚本时,第一行的文本变为粗体。

Note:笔记:

  • This modified script supposes that the Google Spreadsheet can be used by the service account.此修改后的脚本假定服务帐户可以使用 Google 电子表格。 Please be careful this.请注意这一点。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “工作表”对象没有属性“单元格” - 'Worksheet' object has no attribute 'cell' openpyxl'工作表'对象没有属性'写'(python) - openpyxl 'Worksheet' object has no attribute 'write'(python) '工作表' object 没有属性 'set_column' - 'Worksheet' object has no attribute 'set_column' Openpyxl:“工作表”对象没有属性“值” - Openpyxl: 'Worksheet' object has no attribute 'values' “ AttributeError:“工作表”对象没有属性“工作表””是什么意思? - What does “AttributeError: 'Worksheet' object has no attribute 'worksheet' ” mean? Google Spreadsheets API:'SpreadsheetsClient'对象没有属性'update_cell' - Google Spreadsheets API: 'SpreadsheetsClient' object has no attribute 'update_cell' AttributeError: 'Worksheet' 对象没有属性 'hide_gridlines' - AttributeError: 'Worksheet' object has no attribute 'hide_gridlines' AttributeError:“工作表”object 没有属性“插入行” - AttributeError: 'Worksheet' object has no attribute 'insert_row' `iter_cols` 给出一个工作表对象没有属性错误? - `iter_cols` gives a worksheet object has no attribute error? 无法写入excel AttributeError:'Worksheet'对象没有属性'write' - Cannot write to an excel AttributeError: 'Worksheet' object has no attribute 'write'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM