Search in sources :

Example 1 with ExtendedValue

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

the class SpreadsheetSerializer method cellData2sheetCellData.

private com.google.api.services.sheets.v4.model.CellData cellData2sheetCellData(CellData cellData) {
    com.google.api.services.sheets.v4.model.CellData sheetCellData = new com.google.api.services.sheets.v4.model.CellData();
    ExtendedValue ev = new ExtendedValue();
    if (cellData != null) {
        if (cellData.value instanceof String) {
            ev.setStringValue((String) cellData.value);
        } else if (cellData.value instanceof Integer) {
            ev.setNumberValue(new Double((Integer) cellData.value));
        } else if (cellData.value instanceof Double) {
            ev.setNumberValue((Double) cellData.value);
        } else if (cellData.value instanceof OffsetDateTime) {
            // supposedly started internally as a double, but not sure how to transform correctly
            // ev.setNumberValue((Double) cellData.value);
            ev.setStringValue(cellData.value.toString());
        } else if (cellData.value instanceof Boolean) {
            ev.setBoolValue((Boolean) cellData.value);
        } else if (cellData.value == null) {
            ev.setStringValue("");
        } else {
            ev.setStringValue(cellData.value.toString());
        }
    } else {
        ev.setStringValue("");
    }
    sheetCellData.setUserEnteredValue(ev);
    return sheetCellData;
}
Also used : ExtendedValue(com.google.api.services.sheets.v4.model.ExtendedValue) OffsetDateTime(java.time.OffsetDateTime)

Example 2 with ExtendedValue

use of com.google.api.services.sheets.v4.model.ExtendedValue 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)

Aggregations

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