使用gdata python api更新电子表格

时间:2012-10-13 00:13:47

标签: python gdata google-spreadsheet-api

首先阅读最新消息 这是我的代码:

spreadsheet_key = "0AhBYO002ygGgdDZQTW5pTVhLdjM4NlhHbXJ1cVRCd3c"
worksheet_id = "od6"
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = 'myemail@gmail.com'
spr_client.password = '<pwd>'
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()

dicti = {}
dicti['Name'] = 'A'
dicti['Metric Name'] = 'A2' 
dicti['Completed Units'] = 10
dicti['Team Size'] = 2
entry = spr_client.InsertRow(dicti, spreadsheet_key, worksheet_id)

每次运行此代码时,最后一次出现此错误:

'int' object has no attribute 'decode'

请告诉我应该怎么做......

这是InsertRow函数中出现错误的地方:

/usr/local/lib/python2.7/site-packages/gdata/spreadsheet/service.py in InsertRow
      new_custom.column = k
      new_custom.text = v
      new_entry.custom[new_custom.column] = new_custom
    # Generate the post URL for the worksheet which will receive the new entry.
    post_url = 'https://spreadsheets.google.com/feeds/list/%s/%s/private/full'%(
        key, wksht_id) 
    return self.Post(new_entry, post_url, 
        converter=gdata.spreadsheet.SpreadsheetsListFromString) 

更新:将此代码修改为:

dicti = {}
dicti['Name'] = 'A'
dicti['Metric Name'] = 'A2' 
dicti['Completed Units'] = '10'
dicti['Team Size'] = '2'

我现在收到此错误:

{'status': 400, 'body': 'Attribute name &quot;Name&quot; associated with an element type &quot;ns1:Metric&quot; must be followed by the &#39; = &#39; character.', 'reason': 'Bad Request'}

2 个答案:

答案 0 :(得分:3)

您需要在使用gdata API时编写字符串值,因此除非将它们转换为字符串,否则“团队大小”和“已完成单位”变量将导致错误。此外,您应该知道API引用的列名称不会保留您的大小写等。(您的Metric Name列需要称为metricname)。因此,您还必须更改它们在字典中的显示方式(请注意,这假设您的列标题已存在,因为API需要知道如何编写字典):

dicti = {}
dicti['name'] = 'A'
dicti['metricname'] = 'A2' 
dicti['completedunits'] = '10'
dicti['teamsize'] = '2'
entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)

作为旁注(因为这让我感到沮丧),使用CellQuery个对象并声明范围的maxmin值时也是如此。希望有助于避免一些混淆:)

答案 1 :(得分:1)

您只需在:

中输入列名
Spreadsheet('name','metricname','completedunits','teamsize')

您可以详细了解here