【问题标题】:Google Spreadsheet Batch Insert from android来自android的Google电子表格批量插入
【发布时间】:2014-04-07 21:31:11
【问题描述】:

我发现this 的帖子非常有帮助。我也一直尝试使用documentation,但我似乎无法将两者(以及其他来源)结合起来进行批量插入。

问题似乎与BatchUtils.setBatchOperationType(...) 的设置有关。如果我将其设置为UPDATE,批处理将返回 400 错误“”缺少条目 id”,如果我将其设置为 INSERT 或不设置它,我会收到 501“批处理不支持插入。”

插入真的不支持批量操作吗?我认为我做错了什么的可能性更大。

我给你代码:

    String scope = "oauth2:https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/";

    String accountName = "xxxxxxxxxxxxx@gmail.com";
    String accessToken = GoogleAuthUtil.getTokenWithNotification(GDriveActivity.this, accountName, scope, null);

    SpreadsheetService service = new SpreadsheetService("v1");
    service.setHeader("Authorization", "Bearer " + accessToken);
    service.setProtocolVersion(SpreadsheetService.Versions.V3);

    String id = "https://spreadsheets.google.com/feeds/spreadsheets/tnkZhv6iUaHx9-BVsqbWEKg";
    URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);

    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    for (SpreadsheetEntry spreadsheet : spreadsheets) {

        if (spreadsheet.getId().equals(id)) {
            WorksheetFeed worksheetFeed = service.getFeed(spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
            List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
            WorksheetEntry worksheet = worksheets.get(0);

            URL cellFeedUrl = worksheet.getCellFeedUrl();
            CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);

            CellFeed batchRequest = new CellFeed();

            // this will go in a loop, later
            addEntryToBatch(batchRequest, 1, 1, "Some text", "A");
            addEntryToBatch(batchRequest, 1, 2, "More text", "B");

            Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
            service.setHeader("If-Match", "*"); // btw, not sure what this does. Bonus if you can clue me in.
            URL url = new URL(batchLink.getHref());
            CellFeed batchResponse = service.batch(url, batchRequest);
            service.setHeader("If-Match", null); // turns off the thing above.
            for (CellEntry entry : batchResponse.getEntries()) {
                if (!BatchUtils.isSuccess(entry)) {

                    // always end up here :(
                    BatchStatus status = BatchUtils.getBatchStatus(entry);
                    int code = status.getCode(); // 400 or 501
                    String reason = status.getReason(); // "Missing entry id" or "Insert not supported on batch."
                }
            }
            break;
        }
    }


    //-----------

    private void addEntryToBatch(CellFeed batchRequest, int row, int col, String entryText, String id) {

        if (entryText != null) {
            CellEntry entry = new CellEntry(row, col, entryText);
            BatchUtils.setBatchId(entry, id);

            // BatchUtils.setBatchOperationType(entry, BatchOperationType.INSERT); // <-- returns 501
            // BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE); // <-- returns 400
            // Ommiting the OperationType returns 501

            batchRequest.getEntries().add(entry);
        }
    }

还有任何东西关于我的代码的其他奇怪或时髦...请评论和批评。我对此很陌生,这基本上是来自多个来源的一些科学怪人代码。

【问题讨论】:

    标签: android google-sheets


    【解决方案1】:

    回答我自己的问题:

    据我所知,不可能进行批量插入。即使您正在使用只有空单元格的空白工作表,您必须首先检索这些空白单元格,然后更新它们。

    我将再次指出this post,我基本上回收它是为了做我的“插入”(这实际上是对空白单元格的更新)。

    很高兴错了。如果您有更好的解决方案,请发布。

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2021-07-02
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多