python
  • gdata
  • google-spreadsheet-api
  • 2012-10-13 49 views 0 likes 
    0

    讀取到更新在結束第一 這裏是我的代碼:更新電子表格使用GDATA Python API中

    spreadsheet_key = "0AhBYO002ygGgdDZQTW5pTVhLdjM4NlhHbXJ1cVRCd3c" 
    worksheet_id = "od6" 
    spr_client = gdata.spreadsheet.service.SpreadsheetsService() 
    spr_client.email = '[email protected]' 
    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) 
    

    UPDATE:此代碼固定在此之後:

    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'} 
    

    回答

    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值時也是如此。希望可以幫助避免你的一些混亂:)

    +0

    @RacketDonkey 謝謝你這麼多 我現在這個錯誤: {「狀態」 400,「體」:「屬性名稱"與元素類型" NS1相關的名稱":公制"必須遵循由' = '字符。','reason':'Bad Request'} 請你幫我解決......我已經失眠太久,試圖讓這個工作.. :( –

    +0

    @AniqueAkhtar尋找在錯誤代碼中,是否使用XML爲字典賦值? – RocketDonkey

    +0

    Nop ... 我很難編碼它... Jus t像我給你看的方式.. 但我使用的是Django ... –

    1

    你只需要輸入列名:

    Spreadsheet('name','metricname','completedunits','teamsize') 
    

    你可以閱讀更多關於它here

    相關問題