创建新的电子表格(Google API/Python)

Pas*_*nes 7 google-docs-api python-2.7

我已设法使用以下代码创建新的电子表格文档:

# 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)
Run Code Online (Sandbox Code Playgroud)

我的理解是,您必须使用电子表格服务进行身份验证才能操作电子表格.

# 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()
Run Code Online (Sandbox Code Playgroud)

我的问题是如何从上面第一步中的创建中获取电子表格密钥,以便使用gdata.spreadsheet api访问该电子表格?

Pav*_*hov 10

我发现document.GetId()返回的值包含了我们需要的密钥.我不知道这是否是获得密钥的正确方法,但它确实有效.

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)
Run Code Online (Sandbox Code Playgroud)