python - 使用 python-gdata 批量插入电子表格

标签 python google-docs gdata google-docs-api

我正在尝试使用 python-gdata 来填充电子表格中的工作表。问题是,更新单个单元格非常慢。 (通过一次一个地执行它们,每个请求大约需要 500 毫秒!)因此,我正在尝试使用内置于 gdata 中的批处理机制来加快处理速度。

问题是,我似乎无法插入新单元格。我在网上搜索了示例,但找不到任何示例。这是我的代码,我从文档中的示例改编而来。 (该文档实际上并未说明如何插入单元格,但它确实显示了如何更新单元格。由于这是一个新工作表,因此它没有单元格。)

此外,在启用调试的情况下,我可以看到我的请求返回 HTTP 200 OK。


import time
import gdata.spreadsheet
import gdata.spreadsheet.service
import gdata.spreadsheets.data

email = '<snip>'
password = '<snip>'
spreadsheet_key = '<snip>'
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()

#  create a cells feed and batch request
cells = spr_client.GetCellsFeed(spreadsheet_key, worksheet_id)
batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()

#  create a cell entry
cell_entry = gdata.spreadsheet.SpreadsheetsCell()
cell_entry.cell = gdata.spreadsheet.Cell(inputValue="foo", text="bar", row='1', col='1')

#  add the cell entry to the batch request
batchRequest.AddInsert(cell_entry)

#  submit the batch request
updated = spr_client.ExecuteBatch(batchRequest, cells.GetBatchLink().href)

我的直觉是我只是误解了 API,而这应该适用于更改。非常感谢任何帮助。

最佳答案

我最近也遇到过这个(当试图删除时)但是根据文档 here似乎不支持批量 insertdelete 操作:

A number of batch operations can be combined into a single request. The two types of batch operations supported are query and update. insert and delete are not supported because the cells feed cannot be used to insert or delete cells. Remember that the worksheets feed must be used to do that.

我不确定您的用例,但使用 ListFeed 会有帮助吗?它仍然不会让您进行批处理操作,因此会有相关的延迟,但它可能比您现在(或当时)正在处理的更能容忍。

关于python - 使用 python-gdata 批量插入电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11422612/

相关文章:

python - 发送到断开连接的套接字第一次成功

linux - 如何将 vi 编辑器与 google 文档集成

android - kindle fire 中的 google drive/docs api

java - 有关 google-api-java-client 的帮助

python - python中的gdata分析客户端问题

python - 在 xml.etree.ElementTree、Python 中获取 "sibling"的 "child",

python - 如何使用python打印excel中的所有列值

google-apps-script - 通过高级驱动器服务还原Google文档

php - 如何使用 Gdata 从 Picasa 获取实际图像

python - 如何在healpix贴图上有效地执行顶帽(类似圆盘)平滑?