我对专门用于谷歌电子表格的 python gdata API 有点困惑。使用 gdata.spreadsheet.service 可以很容易地组合一个字典并将其作为新行插入到谷歌电子表格中,如下所示http://www.mattcutts.com/blog/write-google-spreadsheet-from-python/ http://www.mattcutts.com/blog/write-google-spreadsheet-from-python/:
Dict = {'weight':'600'}
spr_client.InsertRow(Dict, spreadsheet_key, worksheet_id)
现在我需要使用模块 gdata.spreadsheets.client 因为我需要 Oauth 的东西。我能够进行身份验证并编辑现有单元格,但是我无法根据上述列中的值插入新单元格或行。
据我所知:
import gdata.spreadsheets.client
import gdata.gauth
token = gdata.gauth.OAuth2Token(client_id='CLIENTID',
client_secret='CLIENTSECRET',
scope='https://spreadsheets.google.com/feeds/',
user_agent='blah.blah',
access_token='ACCESSTOKEN',
refresh_token='REFRESHTOKEN')
spr_client = gdata.spreadsheets.client.SpreadsheetsClient()
token.authorize(spr_client)
for entry in spr_client.get_list_feed('SPREADSHEETID', 'od6').entry:
print entry.to_dict()
entry.set_value('weight', '600')
spr_client.update(entry)
这只是覆盖权重列中的第一个值,而不是在列下方的行中附加另一个值
任何帮助都会令人惊叹的
我又看了一遍,现在谷歌终于从 gdata.spreadsheet.service.SpreadsheetsService() (基于用户名、密码的身份验证)中删除了 ProgrammaticLogin() 。使用 OAuth2 和更新版本的 gdata python API 时,答案相对简单。
import gdata.spreadsheets.client
import gdata.spreadsheets.data
import gdata.gauth
# create the OAuth2 token
token = gdata.gauth.OAuth2Token(client_id='CLIENTID',
client_secret='CLIENTSECRET',
scope='https://spreadsheets.google.com/feeds/',
user_agent='blah.blah',
access_token='ACCESSTOKEN',
refresh_token='REFRESHTOKEN')
# create the spreadsheet client and authenticate
spr_client = gdata.spreadsheets.client.SpreadsheetsClient()
token.authorize(spr_client)
#create a ListEntry. the first item of the list corresponds to the first 'header' row
entry = gdata.spreadsheets.data.ListEntry()
entry.set_value('ham', 'gary')
entry.set_value('crab', 'sack')
# add the ListEntry you just made
spr_client.add_list_entry(entry, 'SPREADSHEETID', 'od6')
这将在最后使用的行之后追加一个包含数据的新行。请小心空行,因为“ListFeed”仅计入最后使用的行。
还有更优雅的方法来获取电子表格密钥和工作表 ID,但是电子表格密钥位于要编辑的工作表的 URL 中,并且第一个工作表通常是 od6。如果不是 od6,此网址可以提供帮助:https://spreadsheets.google.com/feeds/worksheets/YOUR_SPREADSHEET_ID/private/full https://spreadsheets.google.com/feeds/worksheets/YOUR_SPREADSHEET_ID/private/full
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)