]> source.dussan.org Git - poi.git/commitdiff
[bug-66215] add test case (that shows we have issues and need fixes)
authorPJ Fanning <fanningpj@apache.org>
Mon, 15 Aug 2022 18:40:25 +0000 (18:40 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 15 Aug 2022 18:40:25 +0000 (18:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903440 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/test/java/org/apache/poi/ss/tests/formula/TestStructuredReferences.java
test-data/spreadsheet/bug66215.xlsx [new file with mode: 0644]

index 5a77c45c6874468182893812fa0e763a309bf987..64881a2b3b91a92ea88cb5a296433ae5ac62c18d 100644 (file)
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellValue;
@@ -30,11 +31,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;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xssf.usermodel.*;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -137,6 +134,52 @@ class TestStructuredReferences {
         }
     }
 
+    @Test
+    void testBug66215() throws Exception {
+        //relates to https://bz.apache.org/bugzilla/show_bug.cgi?id=66215 (this does not work as it should)
+        try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("bug66215.xlsx")) {
+            XSSFSheet sheet = workbook.getSheetAt(0);
+            XSSFRow row3 = sheet.getRow(3);
+            XSSFRow row5 = sheet.getRow(5);
+            assertEquals("Tabelle1[[#This Row],[Total]]/Tabelle1[[#Totals],[Total]]", row3.getCell(5).getCellFormula());
+            assertEquals("Tabelle1[[#This Row],[Total]]/Tabelle1[[#Totals],[Total]]", row5.getCell(5).getCellFormula());
+            XSSFTable table = sheet.getTables().get(0);
+
+            int lastTableRow = table.getEndCellReference().getRow();
+            int totalsRowCount = table.getTotalsRowCount();
+            int lastTableDataRow = lastTableRow - totalsRowCount;
+
+            // we will add one row in table data
+            lastTableRow++;
+            lastTableDataRow++;
+
+            // new table area plus one row
+            AreaReference newTableArea = new AreaReference(
+                    table.getStartCellReference(),
+                    new CellReference(
+                            lastTableRow,
+                            table.getEndCellReference().getCol()
+                    ),
+                    SpreadsheetVersion.EXCEL2007
+            );
+
+            if (totalsRowCount > 0) {
+                workbook.setCellFormulaValidation(false);
+                sheet.shiftRows(lastTableDataRow, lastTableRow++, 1);
+            }
+
+            // set new table area
+            table.setArea(newTableArea);
+
+            XSSFRow row4 = sheet.getRow(4);
+            //the next formula has been adjusted more than it should but seems to return correct value
+            assertEquals("Tabelle2!E5:E5/Tabelle2!E8:E8", row4.getCell(5).getCellFormula());
+            XSSFRow row7 = sheet.getRow(7);
+            //the next formula is completely wrong (should be the same as the value in the row4 assertion above)
+            assertEquals("SUBTOTAL(109,Tabelle1[Percentage])", row7.getCell(5).getCellFormula());
+        }
+    }
+
     private static void confirm(FormulaEvaluator fe, Cell cell, double expectedResult) {
         fe.clearAllCachedResultValues();
         CellValue cv = fe.evaluate(cell);
diff --git a/test-data/spreadsheet/bug66215.xlsx b/test-data/spreadsheet/bug66215.xlsx
new file mode 100644 (file)
index 0000000..c9bb680
Binary files /dev/null and b/test-data/spreadsheet/bug66215.xlsx differ