3

我正在尝试将批处理数据插入谷歌电子表格。用于测试目的。我在电子表格文件 (F1) 中插入了一个工作表 ( W1 )。我正在尝试将所有数据从 w1 复制到文件 (F2) 中的工作表 (W2) 中。请帮忙。

这是我的代码:

SpreadsheetService service = ServiceFactory.getService();
    metafeedUrl = new URL(Constants.SPREADSHEET_URL);
    /*
     * Get files first
     */
    SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
    List<SpreadsheetEntry> myFiles = feed.getEntries();
    /*
     * loop through the files and get your required file
     */
    SpreadsheetEntry originalFile = null;
    SpreadsheetEntry newFile = null;
    WorksheetEntry workSheet = null;

    for(SpreadsheetEntry file : myFiles){
        if(file.getTitle().getPlainText().equalsIgnoreCase(originalSpreadSheet)){
            originalFile = file;

            List<WorksheetEntry> worksheets = file.getWorksheets();

            for(WorksheetEntry worksheet : worksheets){
                    if(worksheet.getTitle().getPlainText().equals(worksheetName)){
                        workSheet = worksheet;
                    }
            }
        }

        if(file.getTitle().getPlainText().equalsIgnoreCase(newSpreadSheet)){
            newFile = file;
        }


    }

    if(!(originalFile == null || newFile == null || workSheet == null)){

        /*
         * Insert a new work sheet in new file.
         */
        WorksheetEntry newEntry = service.insert(newFile.getWorksheetFeedUrl(), workSheet);
        /*
         * Get all the rows from old work sheet
         */
        URL listFeedUrl = workSheet.getListFeedUrl();
        ListQuery query = new ListQuery(listFeedUrl);
        query.setReverse(false);
        ListFeed listFeed = service.query(query, ListFeed.class);
        List<ListEntry> entries = listFeed.getEntries();
        /*
         * Batch update in the new work sheet
         */
        ListFeed batchRequest = new ListFeed();
        for(ListEntry entry : entries){
            BatchUtils.setBatchId(entry, entry.getId());
            BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);
            batchRequest.getEntries().add(entry);
            System.err.println("Adding entry " + entry.toString());
        }

        FeedURLFactory urlFactory = FeedURLFactory.getDefault();
        URL list_FeedUrl = urlFactory.getListFeedUrl(newFile.getKey(), newEntry.getId(), "private", "full");
        System.err.println("URL " + list_FeedUrl);
        ListFeed list_Feed = service.getFeed(list_FeedUrl, ListFeed.class);
        // Error at above line
        Link batchLink = list_Feed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
        System.err.println("Link " + batchLink);

        ListFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);

         boolean isSuccess = true;
         for (ListEntry entry : batchResponse.getEntries()) {
           String batchId = BatchUtils.getBatchId(entry);
           if (!BatchUtils.isSuccess(entry)) {
             isSuccess = false;
             BatchStatus status = BatchUtils.getBatchStatus(entry);
             System.out.printf("%s failed (%s) %s", batchId, status.getReason(), status.getContent());
           }
         }
         System.err.println("ALL OK");

}

我收到以下异常:

    com.google.gdata.util.InvalidEntryException: Bad Request
Invalid query parameter value for grid-id.

    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:594)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
    at com.google.gdata.client.Service.getFeed(Service.java:1135)
    at com.google.gdata.client.Service.getFeed(Service.java:998)
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:631)
    at com.google.gdata.client.Service.getFeed(Service.java:1017)
4

1 回答 1

2

讨厌给出坏消息,但不可能ListFeed只使用 ,来进行批量请求CellFeed。查看文档以了解如何对CellFeed.

于 2012-06-11T13:18:01.077 回答