Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
插入行python gdata.spreadsheets.client_Python_Google Sheets_Gdata - Fatal编程技术网

插入行python gdata.spreadsheets.client

插入行python gdata.spreadsheets.client,python,google-sheets,gdata,Python,Google Sheets,Gdata,我对专门用于谷歌电子表格的python gdata API有点着迷。使用gdata.spreadsheet.service,很容易拼凑出一个dict并将其作为新行插入google电子表格,如下所示: 现在我需要使用gdata.spreadsheets.client模块,因为我需要Oauth的东西。我能够进行身份验证并编辑现有单元格,但是我无法根据上述列中的值插入新单元格或行 就我所知: import gdata.spreadsheets.client import gdata.gauth to

我对专门用于谷歌电子表格的python gdata API有点着迷。使用gdata.spreadsheet.service,很容易拼凑出一个dict并将其作为新行插入google电子表格,如下所示:

现在我需要使用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)
这只是覆盖“权重”列中的第一个值,而不是在该列下面的行中追加另一个值
任何帮助都将是巨大的

我相信你想要的是:


spr_client.InsertRow(entry.to_dict(),'SPREADSHEETID','od6')
spr_client.InsertRow将不工作,因为gdata.spreadsheets.client.SpreadsheetsClient没有InsertRow,如下所示:

(Pdb)p页\u服务

(Pdb)p dir(图纸服务)

['AddListEntry','AddRecord','AddTable','AddWorksheet','Batch','ClientLogin','Delete','Get','GetAccessToken','GetCell','GetCells','GetEntry','GetFeed','GetListFeed','GetNext','GetOAuthToken','GetRecords','GetSpreadsheets','GetTables','GetWorksheets','ModifyRequest','Post','Put','Request请求'“我的GD客户端”和“UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU","设置属性","大小","str","子类钩子","weakref","添加列表"try、add_record、add_table、add_worksheet、alt_auth_service、api_version、auth_scopes、auth_service、auth_token、batch、client_login、delete、get、get、get访问_token、get_cell、get_cells、get_entry、get_feed、get_list_feed、get_next、get_token、get_记录、get_电子表格、get_table“获取工作表”、“获取工作表”、“主机”、“http客户端”、“修改请求”、“post”、“put”、“请求”、“请求客户端登录令牌”、“撤销令牌”、“源”、“ssl”、“更新”、“升级令牌”、“xoauth\u请求者id”]


它所拥有的是AddRecord,这是最接近您(和我)正在寻找的内容。但是,AddRecord需要一个表id,我还没有弄清楚如何获取。我看到的所有示例似乎都在使用gdata.spreadsheet.service.SpreadsheetsService,它确实有InsertRow。

您需要使用CellQuery:

cell_query = gdata.spreadsheets.client.CellQuery( min_row=1, max_row=1, min_col=1, max_col=1, return_empty=True) cells = gd_client.GetCells(sprd_key, wrksht_key, q=cell_query) cell_entry = cells.entry[0] cell_entry.cell.input_value = 'Address' gd_client.update(cell_entry) # This is the call to Google Server to update cell\u query=gdata.spreadsheets.client.CellQuery( 最小行=1,最大行=1,最小列=1,最大列=1,返回值为空=True) cells=gd\u client.GetCells(sprd\u键、wrksht\u键、q=cell\u查询) 单元格\条目=单元格。条目[0] cell\u entry.cell.input\u值='Address' gd_client.update(cell_条目)#这是对Google服务器的更新调用 要执行批更新,请使用:

range = "A6:D1113" cellq = gdata.spreadsheets.client.CellQuery(range=range, return_empty='true') cells = gd_client.GetCells(sprd_key, wrksht_key, q=cellq) batch = gdata.spreadsheets.data.BuildBatchCellsUpdate(sprd_key, wrksht_key) n = 1 for cell in cells.entry: cell.cell.input_value = str(n) batch.add_batch_entry(cell, cell.id.text, batch_id_string=cell.title.text, operation_string='update') n = n + 1 gd_client.batch(batch, force=True) # A single call to Google Server to update all cells. range=“A6:D1113” cellq=gdata.spreadsheets.client.CellQuery(范围=range,返回为\'u empty='true') cells=gd_client.GetCells(sprd_键,wrksht_键,q=cellq) batch=gdata.spreadsheets.data.BuildBatchCellsUpdate(sprd\u键,wrksht\u键) n=1 对于cells.entry中的单元格: cell.cell.input_值=str(n) batch.add_batch_条目(cell,cell.id.text,batch_id_string=cell.title.text,operation_string='update') n=n+1 gd_client.batch(batch,force=True)#对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可以帮助:

好的,看来谷歌目前只在gdata python客户端上使用OAuth2。我还想提一下,python的这个API的可用性有点太低了。如果其他人有问题,请尽管抱歉,但正如前面的回答gdata.spreadsheets.client.SpreadsheetsClient没有InsertRow()函数
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')