简体   繁体   English

Google Spreadsheet API错误

[英]Google Spreadsheet API error

I'm trying to access a spreadsheet in google docs with a python script. 我正在尝试使用python脚本访问google docs中的电子表格。 But I always get errors when executing the following "standard" example: 但是在执行以下“标准”示例时,我总是会出错:

#!/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."

It simply does not work. 它根本行不通。 I always get the following error 我总是收到以下错误

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

What is going wrong here? 这是怎么了? Email and password are correct and I extracted the spreadsheet key from the url of the document itself. 电子邮件和密码正确,我从文档本身的网址中提取了电子表格密钥。

Make sure that your columns in the dictionary match the columns in your spreadsheet. 确保字典中的列与电子表格中的列匹配。 If they aren't present in the sheet, you will get an error. 如果表中没有它们,您将得到一个错误。 Also, although this isn't your problem here, you want to make sure that your dictionary keys are the lowercased column name with all spaces replaced with _ 's. 另外,尽管这不是您的问题,但您仍要确保字典键是小写的列名,所有空格都用_代替。 That is another source of potential error (and one that has tripped me up more than once :) ). 那是潜在错误的另一个来源(并且使我不止一次跳了起来:))。

Try a library named python-google-spreadsheet (which I wrote). 尝试一个名为python-google-spreadsheet (我写过)的库。 It's designed to make suff like what you are trying to do much simpler than using the API directly. 与直接使用API​​相比,它可以使您像尝试做的那样简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM