Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle formulas #18

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ jobs:
with:
# needed for license-plugin to check last modified date of each file
fetch-depth: 0
- name: Set up JDK
uses: oracle-actions/setup-java@v1
- uses: actions/setup-java@v3
with:
website: jdk.java.net
release: 19
distribution: 'oracle'
java-version: '20'
- name: Cache local Maven repository
uses: actions/cache@v3
with:
Expand Down
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.