【问题标题】:Batch inserts into spreadsheet using python-gdata使用 python-gdata 批量插入电子表格
【发布时间】:2012-07-10 11:28:49
【问题描述】:

我正在尝试使用 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,而这应该适用于更改。非常感谢任何帮助。

【问题讨论】:

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


    【解决方案1】:

    我最近也遇到过这个问题(尝试删除时),但根据文档 here,似乎不支持批处理 insertdelete 操作:

    多个批处理操作可以组合成一个请求。 支持的两种批处理操作是查询和更新。 不支持插入和删除,因为单元格供稿不能 用于插入或删除单元格。请记住,工作表提要必须 习惯这样做。

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

    【讨论】:

      【解决方案2】:

      As of Google I/O 2016,最新的Google Sheets API 支持批量单元更新(和读取)。但请注意,GData 以及大多数 GData-based APIs 现已弃用,包括上面的示例,因为新 API 不是 GData。将电子邮件地址和密码以纯文本形式放入代码中也存在安全风险,因此new(er) Google APIs 使用OAuth2 进行授权。您需要获取最新的Google APIs Client Library for Python。就像pip install -U google-api-python-client [或pip3 for Python 3] 一样简单。

      就批量插入而言,这里有一个简单的代码示例。假设您在rows 中有多行数据。要将其大量注入工作表中,例如文件 ID SHEET_ID 并从单元格 A1 的左上角开始,您可以像这样进行一次调用:

      SHEETS.spreadsheets().values().update(spreadsheetId=SHEET_ID, range='A1',
              body={'values': rows}, valueInputOption='RAW').execute()
      

      如果您需要更长的示例,请观看下面的第一个视频,其中这些行是从关系数据库中读取的。对于此 API 的新手,这里是来自官方文档的one code sample,可帮助您入门。如需更长、更“真实”的示例,请参阅以下视频和博客文章:

      最新的 Sheets API 提供了旧版本中不具备的功能,即让开发人员能够以编程方式以文档为导向访问 Sheet,就好像您在使用用户界面一样(创建冻结行、执行单元格格式化、调整行/列的大小、添加数据透视表、创建图表等)

      但是,要对表格执行文件级别的访问,例如导入/导出、复制、移动、重命名等,您需要使用Google Drive API。 Drive API 使用示例:

      • 将 Google 表格导出为 CSV (blogpost)
      • “穷人的纯文本到 PDF”转换器 (blogpost) (*)

      (*) - TL;DR:将纯文本文件上传到云端硬盘,导入/转换为 Google 文档格式,然后将该文档导出为 PDF。上面的帖子使用 Drive API v2; this follow-up post 描述了将其迁移到 Drive API v3,这是一个 developer video 结合了两个“穷人的转换器”帖子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        • 2013-12-13
        • 2012-07-27
        相关资源
        最近更新 更多