Last active
December 16, 2015 21:09
-
-
Save edliaw/5497330 to your computer and use it in GitHub Desktop.
Example to demonstrate how to do batched cell updates in Google Spreadsheets using the gdata API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gdata.spreadsheet as ss | |
import gdata.spreadsheet.service as service | |
YOUR_EMAIL = "" | |
YOUR_PASSWORD = "" | |
DOCUMENT_NAME = "" | |
# Login! | |
client = service.SpreadsheetsService() | |
client.email = YOUR_EMAIL | |
client.password = YOUR_PASSWORD | |
client.ProgrammaticLogin() | |
# Find the spreadsheet we want | |
q = service.DocumentQuery() | |
q['title'] = DOCUMENT_NAME | |
q['title-exact'] = 'true' | |
feed = client.GetSpreadsheetsFeed(query=q) | |
# Ugly parsing of the document id and worksheet from the raw url | |
spreadsheet_id = feed.entry[0].id.text.rsplit('/', 1)[1] | |
feed = client.GetWorksheetsFeed(spreadsheet_id) | |
worksheet_id = feed.entry[0].id.text.rsplit('/', 1)[1] # You can change which worksheet you want to edit by changing feed.entry[N] where N is the index of the worksheet. | |
# Create a batch request for cell updates. Batches can only be used in this way; interacting with rows or inserting cells can only be pushed individually. | |
batchRequest = ss.SpreadsheetsCellsFeed() | |
cells = client.GetCellsFeed(spreadsheet_id, worksheet_id) | |
for c in cells.entry: | |
c.cell.inputValue = c.cell.inputValue.strip() | |
batchRequest.AddUpdate(c) | |
# To push updates individually: | |
#client.UpdateCell(c.cell.row, c.cell.col, c.cell.inputValue, spreadsheet_id, worksheet_id) | |
# Update the spreadsheet | |
client.ExecuteBatch(batchRequest, cells.GetBatchLink().href) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment