我整个上午都在用 python 脚本填充 google 电子表格中的单元格。 在完成几个过时的教程后,我可以使用此脚本打开电子表格
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('Apps Script Execution APIxxxxxxx.json', scope)
c = gspread.authorize(credentials)
wks = c.open_by_key ('1rwPLv6jZBhPGpf38DO6MLxx......')
wks.update_acell('A2','abc')
我已经尝试了来自不同教程的几个示例,但我找不到为什么会出现此错误:
AttributeError: 'Spreadsheet' 对象没有属性 'update_acell'
有什么建议吗?
请您参考如下方法:
我自己找到了答案。
您还需要定义工作表(使用 sheet1 而不是自己给定的名称)
ws = wks.get_worksheet(1)
ws.update_acell('A2','abc')
如果您选择了错误的工作表或不存在的工作表,则会出现这样的错误。 AttributeError: 'NoneType' 对象没有属性 'update_acell'
2016 年 7 月更新
如果你只有一张纸,你应该使用这个:
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('YOUR_SECRET_AUTH_JSON_FILE_HERE.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open("NAME OF YOUR SPREADSHEET")
ws = wks.get_worksheet(0)
ws.update_acell('A1','IT WORKS!')
