Skip to content

Commit

Permalink
Merge branch 'jmorgan-iad-master' into 4.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Sep 24, 2023
2 parents 4c4688e + b92a5c5 commit 121357a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
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 @@ -284,7 +284,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 @@ -298,6 +298,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.

0 comments on commit 121357a

Please sign in to comment.