简体   繁体   English

如何在最后一行之后插入值?

[英]How to insert row after the last row with value?

I am inserting data into a spreadsheet with the new Google Sheets API v4, the code works perfect and the data it is inserted well in the sheet. 我正在使用新的Google表格API v4将数据插入电子表格中,代码非常完美,而且数据也很好地插入到工作表中。

But how to find out the last row with data to add the data after this ? 但是如何在数据之后找出最后一行添加数据呢?

List<List<Object>> arrData = getData();

ValueRange oRange = new ValueRange();
oRange.setRange("Pedidos!AXXXXXXX"); // I NEED THE NUMBER OF THE LAST ROW
oRange.setValues(arrData);

List<ValueRange> oList = new ArrayList<>();
oList.add(oRange);

BatchUpdateValuesRequest oRequest = new BatchUpdateValuesRequest();
oRequest.setValueInputOption("RAW");
oRequest.setData(oList);

BatchUpdateValuesResponse oResp1 = mService.spreadsheets().values().batchUpdate("ID_SPREADSHEET", oRequest).execute();

Is there some trick in the A1 notation for this? 这个A1表示法中有一些技巧吗?

I need an equivalent to .getLastRow from Google Apps Script. 我需要与Google Apps脚本等效的.getLastRow

If you use the append feature and set the range to the entire sheet, the API will find the last row and append the new data after it. 如果您使用追加功能并将范围设置为整个工作表,API将找到最后一行并在其后附加新数据。

This web page explains it. 这个网页解释了它。

https://developers.google.com/sheets/api/guides/values#appending_values https://developers.google.com/sheets/api/guides/values#appending_values

Here is some sample code: 以下是一些示例代码:

String range="Sheet1";
insertData.setValues(rows);
AppendValuesResponse response=service.spreadsheets().values()
.append(spreadsheetId,range,insertData).setValueInputOption("USER_ENTERED")
            .execute();

Note that the response will tell you where it was inserted. 请注意,响应将告诉您插入的位置。

You can use AppendCellsRequest to append a row. 您可以使用AppendCellsRequest附加一行。 The below methods should get you going. 以下方法应该让你去。 I haven't included the getRowDataListForCellStrings method as it is rather application specific. 我没有包含getRowDataListForCellStrings方法,因为它是特定于应用程序的。

First create a Request object containing a AppendCellsRequest: 首先创建一个包含AppendCellsRequest的Request对象:

public BatchUpdateSpreadsheetResponse appendWorksheet(String cellValues) throws SpreadsheetException {
    AppendCellsRequest appendRequest = new AppendCellsRequest();
    appendRequest.setSheetId( mSheet.getProperties().getSheetId() );
    appendRequest.setRows( getRowDataListForCellStrings(cellValues) );
    appendRequest.setFields("userEnteredValue");

    Request req = new Request();
    req.setAppendCells( appendRequest );

    return executeBatchRequest(req);
}

Then call batchUpdate on the spreadsheets() interface: 然后在spreadsheets()界面上调用batchUpdate:

BatchUpdateSpreadsheetResponse executeBatchRequest(Request request) throws SpreadsheetException {
    List<Request> requests = new ArrayList<>();
    requests.add( request );

    BatchUpdateSpreadsheetRequest batchRequest = new BatchUpdateSpreadsheetRequest();
    batchRequest.setRequests( requests );

    try {
          return  mService.spreadsheets().batchUpdate(mSpreadsheet.getSpreadsheetId(), batchRequest).execute();
    } catch (IOException e) {
        throw new SpreadsheetException(e);
    }
}

Unfortunately there doesn't seem to be a way to know which rows were updated. 不幸的是,似乎没有办法知道哪些行已更新。 Not does is seem possible to set valueInputOption when appending in this way. 以这种方式追加时,似乎无法设置valueInputOption。

The v4 API has no way to ask "what is the last row with data", as it's a different style of API than the Apps Script API. v4 API无法询问“数据的最后一行是什么”,因为它是一种与Apps Script API不同的API样式。 You can infer the last row yourself by requesting the data and counting the offset from your first requested row to the last returned row. 您可以通过请求数据并计算从第一个请求的行到最后一个返回的行的偏移​​量来自己推断最后一行。

There is actually a way to ask the API. 实际上有一种方法可以询问API。 I am using this on my node.js client (pretty sure it's very similar) 我在我的node.js客户端上使用它(非常确定它非常相似)

    var sheets = google.sheets('v4');
  sheets.spreadsheets.get({
    auth: auth,
    spreadsheetId: 'spreadsheet_id',
    includeGridData: true
  }, function (err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
    } else {

      var last_row = response.sheets[0].data[0].rowData.length;

    }
  });

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

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