[英]'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()
If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样?
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.错误消息的原因是这样的。client = gspread.authorize(creds)
can be used for the Google Spreadsheet you want to use.因此,当client = gspread.authorize(creds)
可用于您要使用的 Google 电子表格时,您可以使用它。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.当以上几点反映到您的脚本时,它变成如下。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.