批量listEntry使用java插入谷歌电子表格

时间:2012-06-11 03:22:46

标签: java gdata google-sheets

我正在尝试将批量数据插入到Google电子表格中。用于测试目的。我在电子表格文件(F1)中插入了一个工作表(W1)。我正在尝试将文件(F2)中的所有数据从w1复制到工作表(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)

1 个答案:

答案 0 :(得分:2)

讨厌给出坏消息,但 可以使用ListFeed进行批量请求,只有CellFeed。请查看docs,了解如何为CellFeed批量请求。