2

Pythonスクリプトを使用してGoogleドキュメントのスプレッドシートにアクセスしようとしています。しかし、次の「標準」の例を実行すると、常にエラーが発生します。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import gdata.spreadsheet.service

if __name__ == "__main__":

    email = 'email@gmail.com'
    password = 'secret'
    weight = '180'
    # Find this value in the url with 'key=XXX' and copy XXX below
    spreadsheet_key = '0AiKRVC_dEsdfafdC1Id2FGdGVMR1pHb3E4UGlmbUYxS1E'
    # All spreadsheets have worksheets. I think worksheet #1 by default always
    # has a value of 'od6'
    worksheet_id = 'od6'

    spr_client = gdata.spreadsheet.service.SpreadsheetsService()
    spr_client.email = email
    spr_client.password = password
    spr_client.source = 'Example Spreadsheet Writing Application'
    spr_client.ProgrammaticLogin()

    # Prepare the dictionary to write
    dict = {}
    dict['date'] = time.strftime('%m/%d/%Y')
    dict['time'] = time.strftime('%H:%M:%S')
    dict['weight'] = weight
    print dict

    entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
    if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
        print "Insert row succeeded."
    else:
        print "Insert row failed."

それは単に機能しません。私は常に次のエラーが発生します

{'date': '01/07/2013', 'weight': '180', 'time': '19:58:44'}

Traceback (most recent call last):
  File "/Path/to/Project//Python2GoogleDocs/src/P2GTest.py", line 31, in <module>
    entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
  File "/Library/Python/2.7/site-packages/gdata/spreadsheet/service.py", line 339, in InsertRow
    converter=gdata.spreadsheet.SpreadsheetsListFromString)
  File "/Library/Python/2.7/site-packages/gdata/service.py", line 1236, in Post
    media_source=media_source, converter=converter)
  File "/Library/Python/2.7/site-packages/gdata/service.py", line 1358, in PostOrPut
    'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'We&#39;re sorry, a server error occurred. Please wait a bit and try reloading your spreadsheet.', 'reason': 'Bad Request'}

ここで何がうまくいかないのですか?電子メールとパスワードは正しく、ドキュメント自体の URL からスプレッドシート キーを抽出しました。

4

2 に答える 2

5

辞書の列がスプレッドシートの列と一致していることを確認してください。それらがシートに存在しない場合、エラーが発生します。また、これはここでは問題ではありませんが、辞書のキーがすべてのスペースが_'s に置き換えられた小文字の列名であることを確認する必要があります。これは、潜在的なエラーの別の原因です (そして、私を何度もつまずかせたものです:))。

于 2013-01-07T23:56:23.547 に答える
0

python-google-spreadsheet (私が書いた)という名前のライブラリを試してください。これは、API を直接使用するよりもはるかに簡単に、やろうとしていることのようなものを作成できるように設計されています。

于 2013-01-08T11:10:30.703 に答える