gpt4 book ai didi

python - 插入行 python gdata.spreadsheets.client

转载 作者:行者123 更新时间:2023-11-28 18:43:39 26 4
gpt4 key购买 nike

我对专门用于 google 电子表格的 python gdata API 有点着迷。使用 gdata.spreadsheet.service 可以很容易地将一个字典放在一起并将其作为新行插入到谷歌电子表格中,如下所示 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)

这只是覆盖权重列中的第一个值,而不是在该列下方的行中附加另一个值任何帮助都会很棒

最佳答案

我又看了一遍,现在 Google 终于从 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 这个 url 可以帮助: https://spreadsheets.google.com/feeds/worksheets/YOUR_SPREADSHEET_ID/private/full

关于python - 插入行 python gdata.spreadsheets.client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com