简体   繁体   中英

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. 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.

I'm aware I could append new row after last row without knowing/providing Google with the row number using the following code:

// 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?

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).

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. 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

Note: This answer for C# helped me come with this Java solution: https://stackoverflow.com/a/53587428/7878602

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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