insert row python gdata.spreadsheets.client

妖精的绣舞 提交于 2019-12-04 13:05:17

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

I believe what you want is:

spr_client.InsertRow(entry.to_dict(), 'SPREADSHEETID', 'od6')

spr_client.InsertRow will not work because gdata.spreadsheets.client.SpreadsheetsClient does not have InsertRow, as shown below:

(Pdb) p sheets_service

<gdata.spreadsheets.client.SpreadsheetsClient object at 0x7fa3b0bd0390>

(Pdb) p dir(sheets_service)

['AddListEntry', 'AddRecord', 'AddTable', 'AddWorksheet', 'Batch', 'ClientLogin', 'Delete', 'Get', 'GetAccessToken', 'GetCell', 'GetCells', 'GetEntry', 'GetFeed', 'GetListFeed', 'GetNext', 'GetOAuthToken', 'GetRecord', 'GetRecords', 'GetSpreadsheets', 'GetTables', 'GetWorksheet', 'GetWorksheets', 'ModifyRequest', 'Post', 'Put', 'Request', 'RequestClientLoginToken', 'RevokeToken', 'Update', 'UpgradeToken', '_GDClient__gsessionid', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add_list_entry', 'add_record', 'add_table', 'add_worksheet', 'alt_auth_service', 'api_version', 'auth_scopes', 'auth_service', 'auth_token', 'batch', 'client_login', 'delete', 'get', 'get_access_token', 'get_cell', 'get_cells', 'get_entry', 'get_feed', 'get_list_feed', 'get_next', 'get_oauth_token', 'get_record', 'get_records', 'get_spreadsheets', 'get_tables', 'get_worksheet', 'get_worksheets', 'host', 'http_client', 'modify_request', 'post', 'put', 'request', 'request_client_login_token', 'revoke_token', 'source', 'ssl', 'update', 'upgrade_token', 'xoauth_requestor_id']

What it does have is AddRecord, which is the closest thing to what you're (and I'm) looking for. However, AddRecord requires a table id that I've not yet figured out how to obtain. All the examples I've seen seem to be using gdata.spreadsheet.service.SpreadsheetsService, which does have InsertRow.

You need to use the 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

To do batch update, use:


    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.

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!