Search in sources :

Example 1 with RowData

use of com.google.api.services.sheets.v4.model.RowData in project OpenRefine by OpenRefine.

the class SpreadsheetSerializer method sendBatch.

private void sendBatch(List<RowData> rows) {
    List<Request> requests = prepareBatch(rows);
    // FIXME: We have a 10MB cap on the request size, but I'm not sure we've got a good
    // way to quickly tell how big our request is. Just reduce row count for now.
    BatchUpdateSpreadsheetRequest requestBody = new BatchUpdateSpreadsheetRequest();
    requestBody.setIncludeSpreadsheetInResponse(false);
    requestBody.setRequests(requests);
    Sheets.Spreadsheets.BatchUpdate request;
    try {
        logger.debug("spreadsheetId: " + spreadsheetId);
        // logger.debug("requestBody:" + requestBody.toString());
        request = service.spreadsheets().batchUpdate(spreadsheetId, requestBody);
        BatchUpdateSpreadsheetResponse response = request.execute();
        logger.debug("response:" + response.toPrettyString());
    } catch (IOException e) {
        exceptions.add(e);
    } finally {
        requestBody.clear();
        requests.clear();
        rows.clear();
    }
}
Also used : BatchUpdateSpreadsheetRequest(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest) BatchUpdateSpreadsheetRequest(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest) Request(com.google.api.services.sheets.v4.model.Request) AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) AppendCellsRequest(com.google.api.services.sheets.v4.model.AppendCellsRequest) IOException(java.io.IOException) BatchUpdateSpreadsheetResponse(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse)

Example 2 with RowData

use of com.google.api.services.sheets.v4.model.RowData in project OpenRefine by OpenRefine.

the class SpreadsheetSerializerTests method testDataTypes.

@Test
public void testDataTypes() {
    // options is null, but unused
    SUT.startFile(options);
    List<CellData> row = new ArrayList<>();
    row.add(new CellData("null value", null, "null value", null));
    row.add(new CellData("string value", "a string", "a string as string", null));
    row.add(new CellData("integer value", 42, "42", null));
    row.add(new CellData("double value", new Double(42), "42.0", null));
    row.add(new CellData("boolean value", true, "true", null));
    OffsetDateTime now = OffsetDateTime.now(ZoneId.of("Z"));
    row.add(new CellData("datetime value", now, now.toString(), null));
    SUT.addRow(row, false);
    List<Request> requests = SUT.prepareBatch(SUT.getRows());
    assertEquals(requests.size(), 1);
    List<RowData> rows = requests.get(0).getAppendCells().getRows();
    assertEquals(rows.size(), 1);
    List<com.google.api.services.sheets.v4.model.CellData> values = rows.get(0).getValues();
    assertEquals(values.size(), 6);
    ExtendedValue value = values.get(0).getUserEnteredValue();
    assertEquals(value.getStringValue(), "");
    value = values.get(1).getUserEnteredValue();
    assertEquals(value.getStringValue(), "a string");
    value = values.get(2).getUserEnteredValue();
    assertEquals(value.getNumberValue(), new Double(42));
    value = values.get(3).getUserEnteredValue();
    assertEquals(value.getNumberValue(), new Double(42));
    value = values.get(4).getUserEnteredValue();
    assertEquals(value.getBoolValue(), Boolean.TRUE);
    value = values.get(5).getUserEnteredValue();
    assertEquals(value.getStringValue(), now.toString());
}
Also used : ArrayList(java.util.ArrayList) Request(com.google.api.services.sheets.v4.model.Request) AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) ExtendedValue(com.google.api.services.sheets.v4.model.ExtendedValue) CellData(com.google.refine.exporters.TabularSerializer.CellData) RowData(com.google.api.services.sheets.v4.model.RowData) OffsetDateTime(java.time.OffsetDateTime) Test(org.testng.annotations.Test)

Example 3 with RowData

use of com.google.api.services.sheets.v4.model.RowData in project OpenRefine by OpenRefine.

the class SpreadsheetSerializer method addRow.

@Override
public void addRow(List<CellData> cells, boolean isHeader) {
    List<com.google.api.services.sheets.v4.model.CellData> cellDatas = new ArrayList<>();
    RowData rowData = new RowData();
    for (int c = 0; c < cells.size(); c++) {
        CellData cellData = cells.get(c);
        cellDatas.add(cellData2sheetCellData(cellData));
    }
    rowData.setValues(cellDatas);
    rows.add(rowData);
    if (rows.size() >= BATCH_SIZE) {
        sendBatch(rows);
        if (exceptions.size() > 0) {
            throw new RuntimeException(exceptions.get(0));
        }
    }
}
Also used : RowData(com.google.api.services.sheets.v4.model.RowData) ArrayList(java.util.ArrayList)

Example 4 with RowData

use of com.google.api.services.sheets.v4.model.RowData in project OpenRefine by OpenRefine.

the class SpreadsheetSerializer method prepareBatch.

protected List<Request> prepareBatch(List<RowData> rows) {
    List<Request> requests = new ArrayList<>();
    // If this row is wider than our sheet, add columns to the sheet
    int columns = rows.get(0).getValues().size();
    if (columns > maxColumns) {
        AppendDimensionRequest adr = new AppendDimensionRequest();
        adr.setDimension("COLUMNS");
        adr.setLength(columns - maxColumns);
        maxColumns = columns;
        Request req = new Request();
        req.setAppendDimension(adr);
        requests.add(req);
    }
    AppendCellsRequest acr = new AppendCellsRequest();
    acr.setFields("*");
    acr.setSheetId(0);
    acr.setRows(rows);
    Request request = new Request();
    request.setAppendCells(acr);
    requests.add(request);
    return requests;
}
Also used : AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) AppendCellsRequest(com.google.api.services.sheets.v4.model.AppendCellsRequest) BatchUpdateSpreadsheetRequest(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest) Request(com.google.api.services.sheets.v4.model.Request) AppendDimensionRequest(com.google.api.services.sheets.v4.model.AppendDimensionRequest) AppendCellsRequest(com.google.api.services.sheets.v4.model.AppendCellsRequest) ArrayList(java.util.ArrayList)

Aggregations

AppendDimensionRequest (com.google.api.services.sheets.v4.model.AppendDimensionRequest)3 Request (com.google.api.services.sheets.v4.model.Request)3 ArrayList (java.util.ArrayList)3 AppendCellsRequest (com.google.api.services.sheets.v4.model.AppendCellsRequest)2 BatchUpdateSpreadsheetRequest (com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest)2 RowData (com.google.api.services.sheets.v4.model.RowData)2 BatchUpdateSpreadsheetResponse (com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse)1 ExtendedValue (com.google.api.services.sheets.v4.model.ExtendedValue)1 CellData (com.google.refine.exporters.TabularSerializer.CellData)1 IOException (java.io.IOException)1 OffsetDateTime (java.time.OffsetDateTime)1 Test (org.testng.annotations.Test)1