From d280b7438854dd9aaa31d723559303358548946a Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 29 Apr 2022 10:52:34 +0000 Subject: [PATCH] [bug-66039] show use of setCellFormulaValidation=false in a test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1900375 13f79535-47bb-0310-9956-ffa450edef68 --- .../formula/TestStructuredReferences.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestStructuredReferences.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestStructuredReferences.java index f169e7b69d..5a77c45c68 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestStructuredReferences.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestStructuredReferences.java @@ -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);