]> source.dussan.org Git - poi.git/commitdiff
[bug-65268] issue reading shared formulas
authorPJ Fanning <fanningpj@apache.org>
Sat, 9 Oct 2021 12:26:21 +0000 (12:26 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 9 Oct 2021 12:26:21 +0000 (12:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894064 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
test-data/spreadsheet/testSharedFormulasSetBlank.xlsx [new file with mode: 0644]

index b383413e70d062321425fd350d0516ab47801409..e953378deeb2b43f4e2c0d61a6539535d0734455 100644 (file)
@@ -4666,25 +4666,21 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
                             XSSFCell nextCell = row.getCell(j);
                             if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA) {
                                 CTCellFormula nextF = nextCell.getCTCell().getF();
-                                nextF.setStringValue(nextCell.getCellFormula(evalWb));
-                                //https://bz.apache.org/bugzilla/show_bug.cgi?id=65464
-                                nextF.setT(STCellFormulaType.SHARED);
-                                if (!nextF.isSetSi()) {
-                                    nextF.setSi(f.getSi());
+                                if (nextF.getT() == STCellFormulaType.SHARED && nextF.getSi() == f.getSi()) {
+                                    nextF.setStringValue(nextCell.getCellFormula(evalWb));
+                                    CellRangeAddress nextRef = new CellRangeAddress(
+                                            nextCell.getRowIndex(), ref.getLastRow(),
+                                            nextCell.getColumnIndex(), ref.getLastColumn());
+                                    nextF.setRef(nextRef.formatAsString());
+
+                                    sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF);
+                                    break DONE;
                                 }
-                                CellRangeAddress nextRef = new CellRangeAddress(
-                                        nextCell.getRowIndex(), ref.getLastRow(),
-                                        nextCell.getColumnIndex(), ref.getLastColumn());
-                                nextF.setRef(nextRef.formatAsString());
-
-                                sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF);
-                                break DONE;
                             }
                         }
                     }
                 }
             }
-
         }
     }
 
index 29bbde1239e98427e29fd25558ef6e6b046f2195..156e89cf256827954a342b2c312fa6dd377bcf86 100644 (file)
@@ -35,15 +35,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.time.Instant;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TimeZone;
-import java.util.TreeMap;
+import java.util.*;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipFile;
@@ -3560,18 +3552,50 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             XSSFSheet sheet = wb.getSheet("SheetWithSharedFormula");
             XSSFCell v15 = sheet.getRow(14).getCell(21);
             XSSFCell v16 = sheet.getRow(15).getCell(21);
+            XSSFCell v17 = sheet.getRow(16).getCell(21);
             assertEquals("U15/R15", v15.getCellFormula());
             assertEquals(STCellFormulaType.SHARED, v15.getCTCell().getF().getT());
             assertEquals("U16/R16", v16.getCellFormula());
             assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT()); //anomaly in original file
+            assertEquals("U17/R17", v17.getCellFormula());
+            assertEquals(STCellFormulaType.SHARED, v17.getCTCell().getF().getT());
             int calcChainSize = wb.getCalculationChain().getCTCalcChain().sizeOfCArray();
 
             v15.removeFormula();
             assertEquals(CellType.NUMERIC, v15.getCellType(), "V15 is no longer a function");
             assertNull(v15.getCTCell().getF(), "V15 xmlbeans function removed");
             assertEquals("U16/R16", v16.getCellFormula());
-            assertEquals(STCellFormulaType.SHARED, v16.getCTCell().getF().getT());
+            assertEquals(STCellFormulaType.NORMAL, v16.getCTCell().getF().getT());
+            assertEquals("U17/R17", v17.getCellFormula());
+            assertEquals(STCellFormulaType.SHARED, v17.getCTCell().getF().getT());
             assertEquals(calcChainSize - 1, wb.getCalculationChain().getCTCalcChain().sizeOfCArray());
         }
     }
+
+    @Test
+    void testSetBlankOnNestedSharedFormulas() throws IOException {
+        try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("testSharedFormulasSetBlank.xlsx")) {
+            XSSFSheet s1 = wb1.getSheetAt(0);
+            assertNotNull(s1);
+            Iterator<Row> rowIterator = s1.rowIterator();
+            int count = 0;
+            StringBuilder sb = new StringBuilder();
+            while (rowIterator.hasNext()) {
+                Row row = rowIterator.next();
+                Iterator<Cell> cellIterator = row.cellIterator();
+                while (cellIterator.hasNext()) {
+                    Cell cell = cellIterator.next();
+
+                    // the toString is needed to exhibit the broken state
+                    sb.append(cell.toString()).append(",");
+                    count++;
+
+                    // breaks the sheet state
+                    cell.setBlank();
+                }
+            }
+            assertEquals(10, count);
+            assertEquals("2-1,2-1,1+2,2-1,2-1,3+3,3+3,3+3,2-1,2-1,", sb.toString());
+        }
+    }
 }
diff --git a/test-data/spreadsheet/testSharedFormulasSetBlank.xlsx b/test-data/spreadsheet/testSharedFormulasSetBlank.xlsx
new file mode 100644 (file)
index 0000000..e5aa087
Binary files /dev/null and b/test-data/spreadsheet/testSharedFormulasSetBlank.xlsx differ