Pastor Bones
Pastor Bones

Reputation: 7371

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?

Upvotes: 7

Views: 3644

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

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)

Upvotes: 10

Related Questions