Search code examples
pythongoogle-sheetsgdata

insert row python gdata.spreadsheets.client


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


Solution

  • 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