如何将一个电子表格单元格复制到另一个电子表格

时间:2013-12-09 05:14:20

标签: google-sheets google-spreadsheet-api

我尝试使用电子表格API批量复制https://developers.google.com/google-apps/spreadsheets/#updating_multiple_cells_with_a_batch_request进行编码,该示例基于相同的电子表格,我添加了一个目标单元格,但总是得到相同的错误

com.google.gdata.client.batch.BatchInterruptedException: Batch Interrupted (some operations might have succeeded) : a response has already been sent for batch operation update id='R1C1'

我的代码就像这样

SpreadsheetService spreadsheetService = getSpreadsheetService(currentEmail);


    WorksheetFeed feed = spreadsheetService.getFeed(getWorksheetFeedURL(sourceId), WorksheetFeed.class);
    SpreadsheetEntry targetFeed = spreadsheetService.getEntry(getSpreadsheetFeedURL(targetId), SpreadsheetEntry.class);
    SpreadsheetEntry sourceFeed = spreadsheetService.getEntry(getSpreadsheetFeedURL(sourceId), SpreadsheetEntry.class);

    for(WorksheetEntry entry:feed.getEntries()){
        WorksheetEntry targetWorksheet = spreadsheetService.insert(targetFeed.getWorksheetFeedUrl(), entry);
        FeedURLFactory urlFactory = FeedURLFactory.getDefault();
        URL cellFeedUrl = urlFactory.getCellFeedUrl(sourceFeed.getKey(), "od6", "private", "full");
        URL targetFeedUrl = urlFactory.getCellFeedUrl(targetFeed.getKey(), "od6", "private", "full");
        CellFeed cellFeed = spreadsheetService.getFeed(targetFeedUrl, CellFeed.class);
        List<CellAddress> cellAddrs = new ArrayList<CellAddress>();
        for (int row = 1; row <= entry.getRowCount(); ++row) {
            for (int col = 1; col <= entry.getColCount(); ++col) {
                cellAddrs.add(new CellAddress(row, col));
            }
        }
        Map<String, CellEntry> cellEntries = getCellEntryMap(spreadsheetService, cellFeedUrl, cellAddrs);

        CellFeed batchRequest = new CellFeed();

        for (CellAddress cellAddr : cellAddrs) {
            URL entryUrl = new URL(targetFeedUrl.toString() + "/" + cellAddr.idString);
            CellEntry batchEntry = new CellEntry(cellAddr.row, cellAddr.col, cellAddr.idString);
            String inputValue = cellEntries.get(cellAddr.idString).getCell().getInputValue();
            batchEntry.changeInputValueLocal(inputValue);
            batchEntry.setId(String.format("%s/%s", targetFeedUrl.toString(), cellAddr.idString));
            System.out.println(targetFeedUrl.toString()+": "+cellAddr.idString+" "+ inputValue);
            BatchUtils.setBatchId(batchEntry, cellAddr.idString);
            BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);
            batchRequest.getEntries().add(batchEntry);
        }
        spreadsheetService.setHeader("If-Match", "*");
        // Submit the update
        Link batchLink = cellFeed.getLink(ILink.Rel.FEED_BATCH, ILink.Type.ATOM);
        CellFeed batchResponse = spreadsheetService.batch(new URL(batchLink.getHref()), batchRequest);

        boolean isSuccess = true;
        for (CellEntry entry1 : batchResponse.getEntries()) {
            String batchId = BatchUtils.getBatchId(entry);
            if (!BatchUtils.isSuccess(entry1)) {
                isSuccess = false;
                BatchStatus status = BatchUtils.getBatchStatus(entry);
            }
        }
        spreadsheetService.setHeader("If-Match", null);

1 个答案:

答案 0 :(得分:0)

批量复制: What is the fastest way to update a google spreadsheet with a lot of data through the spreadsheet api?

Bug 包含细胞参考,例如$ A5。  他们不会写入电子表格。虽然A5和$ A $ 5都有效,但只有一个$ in的引用会导致问题。我忘记了细节。