Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new spreadsheet (Google API / Python)

I've managed to create a new spreadsheet doc using the following code:

# Authorize
client = gdata.docs.client.DocsClient(source='TestDoc')
client.http_client.debug = False
client.client_login(self.cfg.get('google', 'email'), self.cfg.get('google', 'password'), source='TestDoc', service='writely')

# Create our doc
document = gdata.docs.data.Resource(type='spreadsheet', title='Test Report')
document = client.CreateResource(document)

It is my understanding that you have to authenticate with the spreadsheet service in order to manipulate a spreadsheet.

# Connect to spreadsheet API
client = gdata.spreadsheet.service.SpreadsheetsService()
client.email = self.cfg.get('google', 'email')
client.password = self.cfg.get('google', 'password')
client.source = 'TestDoc'
client.ProgrammaticLogin()

My question is how do I obtain the spreadsheet key from the creation in the first step above in order to access that spreadsheet with the gdata.spreadsheet api?

like image 801
Pastor Bones Avatar asked May 10 '12 04:05

Pastor Bones


1 Answers

I found that document.GetId()'s returned value contains the key that we need. I don't know if it is the right way to get the key, but it works.

spreadsheet_key = document.GetId().split("%3A")[1]
print "Key = %s" % spreadsheet_key

#example of using this key
w = client.AddWorksheet("Sheet 42", 5, 5, spreadsheet_key)
like image 127
Pavel Strakhov Avatar answered Nov 24 '22 03:11

Pavel Strakhov