]> source.dussan.org Git - poi.git/commitdiff
[bug-65306] issue with shift rows that remove rows and how shared formulas are affected
authorPJ Fanning <fanningpj@apache.org>
Sun, 10 Oct 2021 06:39:56 +0000 (06:39 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sun, 10 Oct 2021 06:39:56 +0000 (06:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894089 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/bug65306.xlsx [new file with mode: 0644]

index e953378deeb2b43f4e2c0d61a6539535d0734455..48436bb8263dbe4fbe05ebc04839ebece22a27f6 100644 (file)
@@ -2951,8 +2951,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
      * Calls shiftRows(startRow, endRow, n, false, false);
      *
      * <p>
-     * Additionally shifts merged regions that are completely defined in these
-     * rows (ie. merged 2 cells on a row to be shifted).
+     * Additionally, shifts merged regions that are completely defined in these
+     * rows (i.e. merged 2 cells on a row to be shifted).
      * @param startRow the row to start shifting
      * @param endRow the row to end shifting
      * @param n the number of rows to shift
@@ -2968,8 +2968,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
      * Code ensures that rows don't wrap around
      *
      * <p>
-     * Additionally shifts merged regions that are completely defined in these
-     * rows (ie. merged 2 cells on a row to be shifted). All merged regions that are
+     * Additionally, shifts merged regions that are completely defined in these
+     * rows (i.e. merged 2 cells on a row to be shifted). All merged regions that are
      * completely overlaid by shifting will be deleted.
      *
      * @param startRow the row to start shifting
@@ -3054,6 +3054,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
 
             // check if we should remove this row as it will be overwritten by the data later
             if (shouldRemoveRow(startRow, endRow, n, rownum)) {
+                for (Cell c : row) {
+                    c.setBlank();
+                }
                 // remove row from worksheet.getSheetData row array
                 // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
                 //noinspection UnnecessaryBoxing
index 156e89cf256827954a342b2c312fa6dd377bcf86..ae0770f5555cb91963678b79d0adf79c403ccbc5 100644 (file)
@@ -3598,4 +3598,29 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             assertEquals("2-1,2-1,1+2,2-1,2-1,3+3,3+3,3+3,2-1,2-1,", sb.toString());
         }
     }
+
+    @Test
+    void testBug65306() throws IOException {
+        try (XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("bug65306.xlsx")) {
+            XSSFSheet sheet = wb1.getSheetAt(0);
+            assertNotNull(sheet);
+            XSSFCell a1 = sheet.getRow(0).getCell(0);
+            XSSFCell b1 = sheet.getRow(0).getCell(1);
+            XSSFCell a2 = sheet.getRow(1).getCell(0);
+            XSSFCell b2 = sheet.getRow(1).getCell(1);
+            assertEquals(1.0, a1.getNumericCellValue());
+            assertEquals(2.0, a2.getNumericCellValue());
+            assertEquals("$A$1+3*$A$2", b1.getCellFormula());
+            assertEquals("$A$1+3*$A$2", b2.getCellFormula());
+            sheet.shiftRows(1, 1, -1);
+            assertNull(sheet.getRow(1), "row 2 was removed?");
+            a1 = sheet.getRow(0).getCell(0);
+            b1 = sheet.getRow(0).getCell(1);
+            assertEquals(2.0, a1.getNumericCellValue());
+            assertEquals("#REF!+3*$A$1", b1.getCellFormula());
+            try (FileOutputStream fos = new FileOutputStream("abc.xlsx")) {
+                wb1.write(fos);
+            }
+        }
+    }
 }
diff --git a/test-data/spreadsheet/bug65306.xlsx b/test-data/spreadsheet/bug65306.xlsx
new file mode 100644 (file)
index 0000000..a2af186
Binary files /dev/null and b/test-data/spreadsheet/bug65306.xlsx differ