简体   繁体   English

使用 Google Sheets API v4 将新数据插入到 Google 电子表格的顶部

[英]Insert New Data to Top of Google Spreadsheet using Google Sheets API v4

I'm using Google Sheets to keep track of recent orders on my website.我正在使用 Google 表格来跟踪我网站上最近的订单。 I'd like to add details of each new order to a Google spreadsheet and have the most recent order details show up on top of the sheet and the older entries on the bottom.我想将每个新订单的详细信息添加到 Google 电子表格中,并在表格顶部显示最新的订单详细信息,在底部显示旧条目。

I'm aware I could append new row after last row without knowing/providing Google with the row number using the following code:我知道我可以使用以下代码在不知道/向 Google 提供行号的情况下在最后一行之后追加新行:

// INSERTS NEW DATA AFTER LAST ROW
ValueRange body = new ValueRange().setValues(writeData);
AppendValuesResponse appendValuesResponse =
        service.spreadsheets().values().append(spreadsheetId, range, body)
                .setValueInputOption(valueInputOption)
                .execute();

However, this would mean that the newer entries are added to the bottom of the sheet instead of the top.但是,这意味着较新的条目将添加到工作表的底部而不是顶部。 My requirements are exactly the opposite.我的要求正好相反。 I need to add/append newer entries to the top of the sheet and have older entries move to the bottom.我需要将较新的条目添加/追加到工作表的顶部,并将较旧的条目移到底部。

Is there a way to accomplish this natively using the API?有没有办法使用 API 本地完成此操作?

There is no direct method for this.对此没有直接的方法。 Your closest chance to manipulate the order of data in your sheet is to Sort a range with multiple sorting specifications or make it that the data is added to sheet from bottom (like A+ctr to A+ctr by use of a decrementing variable counter 10-1).操作工作表中数据顺序的最接近机会是对具有多个排序规范的范围进行排序,或者使数据从底部添加到工作表中(例如使用递减变量计数器 10 从 A+ctr 到 A+ctr -1).

I had a very similar requirement and I am sharing how I solved it.我有一个非常相似的要求,我正在分享我是如何解决它的。

I know the number of rows I want to insert at the top.我知道要在顶部插入的行数。 I inserted blank rows at the top under the headers.我在标题下方的顶部插入了空白行。 I set startIndex and endIndex based on row count and request a BatchUpdate on the Spreadsheet.我根据行数设置startIndexendIndex并在电子表格上请求 BatchUpdate。 Then updated the range (which are now blank after this call) with values.然后用值更新范围(在这次调用之后现在是空白的)。

    log.trace("Inserting Blank Row At the Top of Sheet");

    DimensionRange dimensionRange = new DimensionRange();
    dimensionRange.setDimension("ROWS");
    dimensionRange.setStartIndex(1); // depends on your requirement
    dimensionRange.setEndIndex(10);  // depends on your requirement

    InsertDimensionRequest insertDimension = new InsertDimensionRequest();
    insertDimension.setRange(dimensionRange);

    Request insertionRequest = new Request();
    insertionRequest.setInsertDimension(insertDimension);

    BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
    batchUpdateSpreadsheetRequest.setRequests(Arrays.asList(insertionRequest));

    try {
        BatchUpdateSpreadsheetResponse execute = sheets.spreadsheets()
                .batchUpdate(spreadsheetId, batchUpdateSpreadsheetRequest)
                .execute();
    } catch (IOException e) {
        log.error("Exception Occurred While Inserting Blank Row - Details: {}", e.getMessage());
        e.printStackTrace();
    }

Google Reference Doc: InsertDimensionRequest - Sheets API Reference Google 参考文档: InsertDimensionRequest - Sheets API 参考

Note: This answer for C# helped me come with this Java solution: https://stackoverflow.com/a/53587428/7878602注意:C# 的这个答案帮助我找到了这个 Java 解决方案: https : //stackoverflow.com/a/53587428/7878602

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM