From: PJ Fanning Date: Sun, 10 Oct 2021 06:39:56 +0000 (+0000) Subject: [bug-65306] issue with shift rows that remove rows and how shared formulas are affected X-Git-Tag: REL_5_2_0~414 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7d47c205e9c6878b870f705d27992822098c4d04;p=poi.git [bug-65306] issue with shift rows that remove rows and how shared formulas are affected git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894089 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index e953378dee..48436bb826 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -2951,8 +2951,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * Calls shiftRows(startRow, endRow, n, false, false); * *

- * 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 * *

- * 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 diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 156e89cf25..ae0770f555 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -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 index 0000000000..a2af1869fd Binary files /dev/null and b/test-data/spreadsheet/bug65306.xlsx differ