I'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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top