]> source.dussan.org Git - poi.git/commitdiff
[bug-66039] show use of setCellFormulaValidation=false in a test
authorPJ Fanning <fanningpj@apache.org>
Fri, 29 Apr 2022 10:52:34 +0000 (10:52 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 29 Apr 2022 10:52:34 +0000 (10:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1900375 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestStructuredReferences.java

index f169e7b69d273c00f79e41331ab692ed511418d9..5a77c45c6874468182893812fa0e763a309bf987 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.poi.ss.usermodel.Table;
 import org.apache.poi.ss.util.AreaReference;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFTable;
@@ -105,6 +106,37 @@ class TestStructuredReferences {
         }
     }
 
+    @Test
+    void testCellFormulaValidationFalse() throws Exception {
+        //relates to https://bz.apache.org/bugzilla/show_bug.cgi?id=66039
+        try (XSSFWorkbook workbook = new XSSFWorkbook()) {
+            workbook.setCellFormulaValidation(false);
+            XSSFSheet sheet = workbook.createSheet();
+
+            //set sheet content
+            sheet.createRow(0).createCell(0).setCellValue("Field1");
+            sheet.getRow(0).createCell(1).setCellValue("Field2");
+            sheet.createRow(1).createCell(0).setCellValue(123);
+            sheet.getRow(1).createCell(1);
+
+            //create the table
+            String tableName = "Table1";
+            CellReference topLeft = new CellReference(sheet.getRow(0).getCell(0));
+            CellReference bottomRight = new CellReference(sheet.getRow(1).getCell(1));
+            AreaReference tableArea = workbook.getCreationHelper().createAreaReference(topLeft, bottomRight);
+            XSSFTable dataTable = sheet.createTable(tableArea);
+            dataTable.setName(tableName);
+            dataTable.setDisplayName(tableName);
+
+            //set the formula in sheet - when setCellFormulaValidation=true (the default), the code tries to
+            //tidy up this formula (but incorrectly, in this case) - gets adjusted to Sheet0!A2:A2
+            XSSFCell formulaCell = sheet.getRow(1).getCell(1);
+            formulaCell.setCellFormula(tableName + "[[#This Row],[Field1]]");
+
+            assertEquals("Table1[[#This Row],[Field1]]", formulaCell.getCellFormula());
+        }
+    }
+
     private static void confirm(FormulaEvaluator fe, Cell cell, double expectedResult) {
         fe.clearAllCachedResultValues();
         CellValue cv = fe.evaluate(cell);