0

Hi I'm using the Google Sheets API v4 for Java. I want to make a Server List where I register up new Server IP's for my own small project. At the moment I can append a new Entry at a empty row using

AppendCellsRequest appendCellReq = new AppendCellsRequest();
appendCellReq.setSheetId(0);
appendCellReq.setRows(rowData);
appendCellReq.setFields("userEnteredValue");

The Problem is now, that I want delete this row later, so I need to figure out how to find it later. My Idea was to add a UniqueID or to search for the exact added Values or to remember the row number. However a way would it be to find and replace all cells. But I would rather have a way to get the row number of my added data. I'm very happy to hear some advices.

1 Answers1

0

Since long search I finaly found an answer. There are tutorials which indeed made it possible to append a row, but wasnt able to return in which Row they inserted. With this code its now possible. I found it after hours of searching somewhere. It is not the best code, but it works and can be modified.

    String range = "A1"; // TODO: Update placeholder value.

    // How the input data should be interpreted.
    String valueInputOption = "RAW"; // TODO: Update placeholder value.

    // How the input data should be inserted.
    String insertDataOption = "INSERT_ROWS"; // TODO: Update placeholder value.

    // TODO: Assign values to desired fields of `requestBody`:
    ValueRange requestBody = new ValueRange();

    List<Object> data1 = new ArrayList<Object>();
    data1.addAll(Arrays.asList(dataArr));

    List<List<Object>> data2 = new ArrayList<List<Object>>();
    data2.add(data1);

    requestBody.setValues(data2);

    Sheets sheetsService;
    try {
        sheetsService = getSheetsService();
        Sheets.Spreadsheets.Values.Append request = sheetsService.spreadsheets().values().append(spreadSheetId,
                range, requestBody);

        request.setValueInputOption(valueInputOption);
        request.setInsertDataOption(insertDataOption);

        AppendValuesResponse response = request.execute();

        // TODO: Change code below to process the `response` object:

        Logger.println(response.getTableRange());

        String startCell = response.getTableRange().split(":")[1];
        String colString = startCell.replaceAll("\\d", "");
        String row = startCell.replaceAll(colString, "");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }