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

fix rawdata parsing for formula cells #291

Merged
merged 1 commit into from
Sep 25, 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
8 changes: 7 additions & 1 deletion src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
cell.setCellStyle(null);
}
String value;
if (options.isRawData() && cell.getCellType() == CellType.NUMERIC) {
if (options.isRawData() && isCellNumeric(cell)) {
value = NumberToTextConverter.toText(cell.getNumericCellValue());
} else {
value = dataFormatter.formatCellValue(cell, baseFormulaEvaluator);
Expand All @@ -285,6 +285,12 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
}
}

private boolean isCellNumeric(Cell cell) {
return (cell.getCellType() == CellType.NUMERIC ||
(cell.getCellType() == CellType.FORMULA &&
cell.getCachedFormulaResultType() == CellType.NUMERIC));
}

private <T> void setFieldData(T instance, Field field, Object data) {
try {
field.setAccessible(true);
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/poiji/deserialize/RawValueFormulaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.poiji.deserialize;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.RowModelFormula;
import com.poiji.option.PoijiOptions;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* Created by jmorgan on 12.09.2023
*/
@RunWith(Parameterized.class)
public class RawValueFormulaTest {

private String path;

public RawValueFormulaTest(String path) {
this.path = path;
}

@Parameterized.Parameters(name = "{index}: ({0})={1}")
public static Iterable<Object[]> queries() {
return Arrays.asList(new Object[][]{
{"src/test/resources/raw_value_formula.xls"},
{"src/test/resources/raw_value_formula.xlsx"},
});
}

@Test
public void shouldMapCalculations() {
PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().headerCount(0).rawData(true).build();
List<RowModelFormula> models = Poiji.fromExcel(new File(path), RowModelFormula.class, options);

for (RowModelFormula model : models) {
assertThat(model.getCurrencyValue(), is(123.45D));
assertThat(model.getFormulaValue(), is(246.90D));
}
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/poiji/deserialize/model/RowModelFormula.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.poiji.deserialize.model;

import com.poiji.annotation.ExcelCell;

/**
* Created by jmorgan on 12.09.2023
*/
public class RowModelFormula {

@ExcelCell(0)
private double currencyValue;

@ExcelCell(1)
private double formulaValue;

public double getCurrencyValue() {
return currencyValue;
}

public void setCurrencyValue(double currencyValue) {
this.currencyValue = currencyValue;
}

public double getFormulaValue() {
return formulaValue;
}

public void setFormulaValue(double formulaValue) {
this.formulaValue = formulaValue;
}
}
Binary file added src/test/resources/raw_value_formula.xls
Binary file not shown.
Binary file added src/test/resources/raw_value_formula.xlsx
Binary file not shown.
Loading