Showing posts with label spreadsheets. Show all posts
Showing posts with label spreadsheets. Show all posts

Getting the worksheet feed for a given spreadsheet


This code snippet demonstrates how to obtain a feed of worksheet entries, given an authenticated gdata.spreadsheet.service.SpreadsheetsService() client object and a worksheet key. For the sake of the example, we are going to extract the worksheets feed for the first spreadsheet in our spreadsheets feed. The example uses the interactive Python interpreter. To obtain the worksheet feed for a private spreadsheet:
>>> import gdata
>>> import gdata.docs
>>> import gdata.spreadsheet
>>> import gdata.spreadsheet.service
>>> client = gdata.spreadsheet.service.SpreadsheetsService()
>>> client.email = 'me@gmail.com'
>>> client.password = 'mypassword'
>>> client.ProgrammaticLogin()
>>> spreadsheet_feed = client.GetFeed('http://spreadsheets.google.com/feeds/spreadsheets/private/full')
>>> first_entry = spreadsheet_feed.entry[0]
>>> key = first_entry.id.text.rsplit('/')[-1]
>>> worksheets_feed = client.GetWorksheetsFeed(key)
>>> for entry in worksheets_feed.entry:
...     print entry.title.text
... 
To do the same without authentication for public spreadsheets: To obtain the worksheet feed for a private spreadsheet:
>>> import gdata.spreadsheet
>>> import gdata.spreadsheet.service
>>> client = gdata.spreadsheet.service.SpreadsheetsService()
>>> key = 'p123345abcDEF'
>>> worksheets_feed = client.GetWorksheetsFeed(key, visibility='public', projection='values')
>>> for entry in worksheets_feed.entry:
...     print entry.title.text
... 

Batch update spreadsheet cells using Python


Using the gdata-python-client, you can change the value in multiple cells with one HTTP request. Here is an example:
import gdata.spreadsheet
import gdata.spreadsheet.service

client = gdata.spreadsheet.service.SpreadsheetsService()
client.email = 'your email address'
client.password = 'your password'
client.ProgrammaticLogin()

# Use the spreadsheet key and worksheet ID for the worksheet you want to edit.
cells = client.GetCellsFeed('pKq0C...', wksht_id='od6')

batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()

# This sample changes the first four cells in the spreadsheet.
cells.entry[0].cell.inputValue = 'x'
batchRequest.AddUpdate(cells.entry[0])
cells.entry[1].cell.inputValue = 'y'
batchRequest.AddUpdate(cells.entry[1])
cells.entry[2].cell.inputValue = 'z'
batchRequest.AddUpdate(cells.entry[2])
cells.entry[3].cell.inputValue = '=sum(3,5)'
batchRequest.AddUpdate(cells.entry[3])

updated = client.ExecuteBatch(batchRequest, cells.GetBatchLink().href)

Access Spreadsheets list feed elements in Python


# print out dictionary of row information for each row
for entry in worksheet_list_feed.entry:
  for key in entry.custom:
    print '%s: %s' % (key, entry.custom[key].text)