]> source.dussan.org Git - poi.git/commitdiff
Add reproducer for bug 60355
authorDominik Stadler <centic@apache.org>
Sun, 13 Jan 2019 17:13:57 +0000 (17:13 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 13 Jan 2019 17:13:57 +0000 (17:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1851208 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestUnfixedBugs.java
test-data/spreadsheet/HsGetVal.xlsx [new file with mode: 0644]

index eb392080c28db361b84f1563ff246afd21dc2233..cf8fa3a3fc3965af6c224111bc273a0e5b2333ae 100644 (file)
@@ -27,22 +27,30 @@ import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.Calendar;
 import java.util.Date;
-
-import org.apache.poi.hssf.HSSFTestDataSamples;
+import java.util.Hashtable;
+
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.formula.functions.FreeRefFunction;
+import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
+import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
+import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.BorderStyle;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellUtil;
 import org.apache.poi.ss.util.RegionUtil;
 import org.apache.poi.util.LocaleUtil;
 import org.apache.poi.xssf.SXSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.junit.Test;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
 
@@ -334,4 +342,52 @@ public final class TestUnfixedBugs {
         }
     }
 
+    @Test
+    public void testBug60355() throws IOException {
+        try (Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("HsGetVal.xlsx")){
+
+            Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
+            System.out.println("cell_4_1 formula:" + sheet.getRow(4).getCell(1).getCellFormula());
+            System.out.println("cell_4_2 formula:" + sheet.getRow(4).getCell(2).getCellFormula());
+
+            // hard code HsGetValue test values for formulas on the sheet
+            Hashtable<CellAddress, String> cellToValueTable = new Hashtable<>();
+            CellAddress cell4_1 = new CellAddress(4, 1);
+            cellToValueTable.put(cell4_1, "678.0");
+            CellAddress cell4_2 = new CellAddress(4, 2);
+            cellToValueTable.put(cell4_2, "123.0");
+
+            String[] functionNames = {HsGetValue.name};
+            FreeRefFunction[] functionImpls = {new HsGetValue(cellToValueTable)};
+            UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
+            UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
+            workbook.addToolPack(udfToolpack);
+
+            FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
+            formulaEvaluator.setIgnoreMissingWorkbooks(true);
+            formulaEvaluator.evaluateAll();
+        }
+    }
+
+    public class HsGetValue implements FreeRefFunction {
+        public static final String name = "HsGetValue";
+
+        private Hashtable<CellAddress, String> cellValues;
+
+        public HsGetValue(Hashtable<CellAddress, String> cellValues) {
+            super();
+            this.cellValues = cellValues;
+        }
+
+        @Override
+        public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext evaluationContext) {
+            int row = evaluationContext.getRowIndex();
+            int column = evaluationContext.getColumnIndex();
+            CellAddress cell = new CellAddress(row, column);
+
+            String value = cellValues.get(cell);
+            return new NumberEval( Double.parseDouble(value) );
+        }
+
+    }
 }
diff --git a/test-data/spreadsheet/HsGetVal.xlsx b/test-data/spreadsheet/HsGetVal.xlsx
new file mode 100644 (file)
index 0000000..a32d120
Binary files /dev/null and b/test-data/spreadsheet/HsGetVal.xlsx differ