Google api 通过电子表格api用大量数据更新google电子表格的最快方法是什么?

Google api 通过电子表格api用大量数据更新google电子表格的最快方法是什么?,google-api,google-spreadsheet-api,Google Api,Google Spreadsheet Api,我正在使用GoogleSpreadsheet API更新包含大量数据的电子表格(数百行,约20列) 我已经测试过批量调用来更新2500个单元格。完成呼叫大约需要40秒,请求约为1mb,响应约为2mb 有什么方法可以让它更快地工作吗?如果要更新整行内容,可以尝试使用基于列表的提要: 它将允许您更新值(而不是公式) 如果您仍然存在性能问题,您应该切换到关系数据库服务器或google的数据存储(如果您使用的是google app engine)我可以通过在更新之前跳过查询部分来加速官方API中提供的

我正在使用GoogleSpreadsheet API更新包含大量数据的电子表格(数百行,约20列)

我已经测试过批量调用来更新2500个单元格。完成呼叫大约需要40秒,请求约为1mb,响应约为2mb


有什么方法可以让它更快地工作吗?

如果要更新整行内容,可以尝试使用基于列表的提要:

它将允许您更新值(而不是公式)


如果您仍然存在性能问题,您应该切换到关系数据库服务器或google的数据存储(如果您使用的是google app engine)

我可以通过在更新之前跳过查询部分来加速官方API中提供的批处理请求。这就是他们在示例中所做的:

// Prepare the update
    // getCellEntryMap is what makes the update fast.
    Map cellEntries = getCellEntryMap(ssSvc, cellFeedUrl, cellAddrs);

    CellFeed batchRequest = new CellFeed();
    for (CellAddress cellAddr : cellAddrs) {
      URL entryUrl = new URL(cellFeedUrl.toString() + "/" + cellAddr.idString);
      CellEntry batchEntry = new CellEntry(cellEntries.get(cellAddr.idString));
      batchEntry.changeInputValueLocal(cellAddr.idString);
      BatchUtils.setBatchId(batchEntry, cellAddr.idString);
      BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);
      batchRequest.getEntries().add(batchEntry);
    }
  // Submit the update
    Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
    CellFeed batchResponse = ssSvc.batch(new URL(batchLink.getHref()), batchRequest);
/**
 * Returns a CellEntry with batch id and operation type that will tell the
 * server to update the specified cell with the given value. The entry is
 * fetched from the server in order to get the current edit link (for
 * optimistic concurrency).
 * 
 * @param row the row number of the cell to operate on
 * @param col the column number of the cell to operate on
 * @param value the value to set in case of an update the cell to operate on
 * 
 * @throws ServiceException when the request causes an error in the Google
 *         Spreadsheets service.
 * @throws IOException when an error occurs in communication with the Google
 *         Spreadsheets service.
 */
private CellEntry createUpdateOperation(int row, int col, String value)
    throws ServiceException, IOException {
  String batchId = "R" + row + "C" + col;
  URL entryUrl = new URL(cellFeedUrl.toString() + "/" + batchId);
  CellEntry entry = service.getEntry(entryUrl, CellEntry.class);
  entry.changeInputValueLocal(value);
  BatchUtils.setBatchId(entry, batchId);
  BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);

  return entry;
}
这就是我把它改成的

CellFeed batchRequest = new CellFeed();
        for (CellInfo cellAddr : cellsInfo) {
             CellEntry batchEntry = new CellEntry(cellAddr.row, cellAddr.col, cellAddr.idString);
              batchEntry.setId(String.format("%s/%s", worksheet.getCellFeedUrl().toString(), cellAddr.idString));         
              BatchUtils.setBatchId(batchEntry, cellAddr.idString);
              BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);  
              batchRequest.getEntries().add(batchEntry);



        }

        CellFeed cellFeed = ssSvc.getFeed(worksheet.getCellFeedUrl(), CellFeed.class);      
        Link batchLink =  cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);

        ssSvc.setHeader("If-Match", "*");
        CellFeed batchResponse = ssSvc.batch(new URL(batchLink.getHref()), batchRequest);
        ssSvc.setHeader("If-Match", null);

请注意,标题应该更改以使其工作。

加速:由David Tolioupov发布-它工作。一些有帮助的额外信息

有关如何使用CellFeed的示例,请参见CellDemo.java

这个例子有很多细节,足以帮助我优化代码

如David Tolioupov所述,通过以下方式创建CellEntry:

CellEntry batchEntry = new CellEntry(cellAddr.row, cellAddr.col, cellAddr.idString);
batchEntry.setId(String.format("%s/%s", cellFeedUrl.toString(), cellAddr.idString)); 
从示例中可以看出:

// Prepare the update
    // getCellEntryMap is what makes the update fast.
    Map cellEntries = getCellEntryMap(ssSvc, cellFeedUrl, cellAddrs);

    CellFeed batchRequest = new CellFeed();
    for (CellAddress cellAddr : cellAddrs) {
      URL entryUrl = new URL(cellFeedUrl.toString() + "/" + cellAddr.idString);
      CellEntry batchEntry = new CellEntry(cellEntries.get(cellAddr.idString));
      batchEntry.changeInputValueLocal(cellAddr.idString);
      BatchUtils.setBatchId(batchEntry, cellAddr.idString);
      BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);
      batchRequest.getEntries().add(batchEntry);
    }
  // Submit the update
    Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
    CellFeed batchResponse = ssSvc.batch(new URL(batchLink.getHref()), batchRequest);
/**
 * Returns a CellEntry with batch id and operation type that will tell the
 * server to update the specified cell with the given value. The entry is
 * fetched from the server in order to get the current edit link (for
 * optimistic concurrency).
 * 
 * @param row the row number of the cell to operate on
 * @param col the column number of the cell to operate on
 * @param value the value to set in case of an update the cell to operate on
 * 
 * @throws ServiceException when the request causes an error in the Google
 *         Spreadsheets service.
 * @throws IOException when an error occurs in communication with the Google
 *         Spreadsheets service.
 */
private CellEntry createUpdateOperation(int row, int col, String value)
    throws ServiceException, IOException {
  String batchId = "R" + row + "C" + col;
  URL entryUrl = new URL(cellFeedUrl.toString() + "/" + batchId);
  CellEntry entry = service.getEntry(entryUrl, CellEntry.class);
  entry.changeInputValueLocal(value);
  BatchUtils.setBatchId(entry, batchId);
  BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);

  return entry;
}

所需的只是cellFeedUrl,然后创建并发送请求。

您使用的是CellFeeds还是list feeds?谢谢。但我知道没有办法批量更新行,这意味着会有数百个请求。使用不同的数据存储对我来说不是一个选择,因为我的应用程序是为了操作谷歌电子表格而不是存储这些数据而构建的。答案很好。谢谢插入时间下降到4.5秒。如果匹配头是关键!谢谢