Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create sheet and update data with one request

I want to implement Google sheets api request with one api call.

I managed to implement this code:

List<Request> requests = new ArrayList<>();

        List<CellData> values = new ArrayList<>();

        values.add(new CellData()
            .setUserEnteredValue(new ExtendedValue()
                .setStringValue("Hello World!")));

        requests.add(new Request().setAddSheet(new AddSheetRequest()
            .setProperties(new SheetProperties()
                .setTitle("scstc")))
            .setUpdateCells(new UpdateCellsRequest()
                .setStart(new GridCoordinate()
                    .setSheetId(0)
                    .setRowIndex(0)
                    .setColumnIndex(0))
                .setRows(Arrays.asList(
                    new RowData().setValues(values)))
                .setFields("userEnteredValue,userEnteredFormat.backgroundColor"))
        );

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

        BatchUpdateSpreadsheetResponse response = service.spreadsheets().batchUpdate(spreadsheetId, body).execute();

But I get error:

400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid value at 'requests[0]' (oneof), oneof field 'kind' is already set. Cannot set 'updateCells'",
    "reason" : "badRequest"
  } ],
  "message" : "Invalid value at 'requests[0]' (oneof), oneof field 'kind' is already set. Cannot set 'updateCells'",
  "status" : "INVALID_ARGUMENT"
}
    at com.google.sheet.impl.GoogleSheetBasicTest1_____1.hello(GoogleSheetBasicTest1_____1.java:133)

Do you how how I can fix this issue?

like image 622
Peter Penzov Avatar asked Dec 27 '16 14:12

Peter Penzov


1 Answers

Each Request object is intended to have just a single value set within it. You are setting two values:

requests.add(new Request()
            .setAddSheet(...)
            .setUpdateCells(...));

Instead of doing the above, you need to use two request objects:

requests.add(new Request().setAddSheet(...));
requests.add(new Request().setUpdateCells(...));
like image 181
Sam Berlin Avatar answered Oct 13 '22 12:10

Sam Berlin