Skip to content

Commit

Permalink
handle formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
jam01 committed Nov 20, 2023
1 parent 34019c7 commit 957b6a4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.github.jam01.xtrasonnet.document.MediaTypes;
import io.github.jam01.xtrasonnet.spi.BasePlugin;
import io.github.jam01.xtrasonnet.spi.PluginException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
Expand All @@ -20,6 +21,7 @@
import sjsonnet.EvalScope;
import sjsonnet.Position;
import sjsonnet.Val;
import upickle.core.Visitor;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -70,22 +72,7 @@ public Val.Literal read(Document<?> doc, Position pos) throws PluginException {
var cVisitor = rVisitor.subVisitor();

var type = cell.getCellType();
Val.Literal val = null;
if (CellType.BOOLEAN == type) {
if (cell.getBooleanCellValue()) val = (Val.Literal) cVisitor.visitTrue(-1);
else cVisitor.visitFalse(-1);
} else if (CellType.NUMERIC == type) {
if (DateUtil.isCellDateFormatted(cell)) {
val = (Val.Literal) cVisitor.visitString(cell.getLocalDateTimeCellValue().toString(), -1);
} else {
val = (Val.Literal) cVisitor.visitFloat64(cell.getNumericCellValue(), -1);
}
} else if (CellType.STRING == type || CellType.FORMULA == type || CellType.BLANK == type) {
val = (Val.Literal) cVisitor.visitString(cell.getStringCellValue(), -1);
} else {
throw new IllegalArgumentException("Cannot represent type: " + type.toString() + " as a jsonnet element");
}

Val.Literal val = literalOf(type, cell, cVisitor);
rVisitor.visitValue(val, -1);
}
sVisitor.visitValue(rVisitor.visitEnd(-1), -1);
Expand All @@ -99,6 +86,25 @@ public Val.Literal read(Document<?> doc, Position pos) throws PluginException {
return bVisitor.visitEnd(-1);
}

private static Val.Literal literalOf(CellType type, Cell cell, Visitor<?, ?> cVisitor) {
if (CellType.BOOLEAN == type) {
if (cell.getBooleanCellValue()) return (Val.Literal) cVisitor.visitTrue(-1);
else return (Val.Literal) cVisitor.visitFalse(-1);
} else if (CellType.NUMERIC == type) {
if (DateUtil.isCellDateFormatted(cell)) {
return (Val.Literal) cVisitor.visitString(cell.getLocalDateTimeCellValue().toString(), -1);
} else {
return (Val.Literal) cVisitor.visitFloat64(cell.getNumericCellValue(), -1);
}
} else if (CellType.STRING == type || CellType.BLANK == type) {
return (Val.Literal) cVisitor.visitString(cell.getStringCellValue(), -1);
} else if (CellType.FORMULA == type) {
return literalOf(cell.getCachedFormulaResultType(), cell, cVisitor);
} else {
throw new IllegalArgumentException("Cannot represent type: " + type.toString() + " as a jsonnet element");
}
}

@Override
public <T> Document<T> write(Val input, MediaType mediaType, Class<T> targetType, EvalScope ev) throws PluginException {
throw new UnsupportedOperationException("Writing excel files is unsupported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.util.Collections;

public class ExcelPluginTest {
private final String simple_xlsx_json = """
{
Expand Down Expand Up @@ -107,4 +109,16 @@ public void read_xlsx_dates() throws JSONException {
JSONAssert.assertEquals(dates_xlsx_json, doc.getContent(), true);
Assertions.assertEquals(MediaTypes.APPLICATION_JSON, doc.getMediaType());
}

public String formula_xlsx_json = """
{"Sheet3":[{"A":"","B":"","C":"","D":"","E":""},{"A":"","B":60,"C":60,"D":3600,"E":""}]}""";

@Test
public void read_xlsx_formula() throws JSONException {
var doc = new Transformer("payload")
.transform(Document.of(TestUtils.resourceAsFile("formula.xlsx"), MediaTypes.APPLICATION_EXCEL));

JSONAssert.assertEquals(formula_xlsx_json, doc.getContent(), true);
Assertions.assertEquals(MediaTypes.APPLICATION_JSON, doc.getMediaType());
}
}
Binary file added xtrasonnet/src/test/resources/formula.xlsx
Binary file not shown.

0 comments on commit 957b6a4

Please sign in to comment.