插入行python gdata.spreadsheets.client [英] insert row python gdata.spreadsheets.client
问题描述
我对于专门用于Google电子表格的python gdata API有点困惑。使用gdata.spreadsheet.service很容易将一个字典放在一起,并将它作为一个新行插入到谷歌电子表格中,例如 http://www.mattcutts.com/blog/write-google-spreadsheet-from-python/ :
Dict = {'weight':'600'}
pre>
spr_client.InsertRow(Dict,spreadsheet_key,worksheet_id)
现在我需要使用模块gdata.spreadsheets.client,因为我需要Oauth的东西。我能够进行身份验证并编辑现有的单元格,但是我无法根据上述列中的值插入新的单元格或行。
this就我而言:
import gdata.spreadsheets.client
import gdata.gauth
token = gdata.gauth.OAuth2Token(client_id ='CLIENTID',
client_secret ='CLIENTSECRET',$ b $ 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)
用于在spr_client.get_list_feed('SPREADSHEETID','od6')中输入条目:
打印entry.to_dict()
entry.set_value('weight',' 600')
sp r_client.update(entry)
这会覆盖权重列中的第一个值,而不是附加其他值在下面一行中的
中,任何帮助都会被amasing解决方案我现在再看一遍Google最终从gdata.spreadsheet.service.SpreadsheetsService()(基于密码的用户名,身份验证)中剔除了ProgrammaticLogin()。使用OAuth2和更新版本的gdata python API时,答案相对简单。
import gdata.spreadsheets.client
import gdata.spreadsheets.data
import gdata.gauth
$ b $创建OAuth2令牌
令牌= 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)
#创建一个ListEntry。列表中的第一项对应于第一个'header'行
entry = gdata.spreadsheets.data.ListEntry()
entry.set_value('ham','gary')
entry .set_value('crab','sack')
#添加刚刚创建的ListEntry
spr_client.add_list_entry(entry,'SPREADSHEETID','od6')
这会在最后一次使用的行之后追加一个包含数据的新行。由于'ListFeed'只计数到最后一次使用的行,因此请注意空行。
还有更优雅的方法来获取电子表格键和工作表ID,但电子表格键位于要编辑的工作表的URL中,第一个工作表通常是od6。如果该网址不是od6,则可以提供帮助:
https:// spreadsheets。 google.com/feeds/worksheets/YOUR_SPREADSHEET_ID/private/fullI'm a bit stuck with the python gdata API for specifically google spreadsheets. Using gdata.spreadsheet.service it was easy to throw together a dict and insert that as a new row in a google spreadsheet like this http://www.mattcutts.com/blog/write-google-spreadsheet-from-python/:
Dict = {'weight':'600'} spr_client.InsertRow(Dict, spreadsheet_key, worksheet_id)
Now i need to use the module gdata.spreadsheets.client as i require the Oauth stuff. I was able to do the authentication and edit exisiting cells however i have not been able to insert new cells or rows based on the value in the column like the above.
this is as far as i got:
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)
This just overwrites the first value in the weight column rather than appending another value in the row below in the column any help would be amasing
解决方案I've looked at this again now Google has finally axed the ProgrammaticLogin() from gdata.spreadsheet.service.SpreadsheetsService() (username, password based authentication). The answer is relatively simple when using OAuth2 and newer versions of the 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')
This will append a new row with data after the last used row. be careful with empty rows as the 'ListFeed' only counts to the last used row. also there are more elegant ways to get the spreadsheet key and worksheet id however the spreadsheet key is in the URL of the sheet you want to edit and the first worksheet is usually od6. If its not od6 this url can help: https://spreadsheets.google.com/feeds/worksheets/YOUR_SPREADSHEET_ID/private/full
这篇关于插入行python gdata.spreadsheets.client的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!