From: PJ Fanning Date: Tue, 2 Jul 2024 13:10:29 +0000 (+0000) Subject: [bug-69154] add speculative row shifter fix X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e1c61686827acd674e09a484a84a597c47dee10b;p=poi.git [bug-69154] add speculative row shifter fix git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1918841 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java index 166fa73659..b275f46154 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java @@ -59,7 +59,7 @@ class TestXSSFSheetShiftColumns extends BaseTestSheetShiftColumns { row.createCell(j).setCellValue(value); } } - final int firstRow = 1; // works with 0, but fails with 1! + final int firstRow = 1; // worked with 0, but failed with 1! final int secondRow = firstRow + 1; sheet.addMergedRegion(new CellRangeAddress(firstRow, secondRow, 0, 0)); sheet.addMergedRegion(new CellRangeAddress(firstRow, firstRow, 1, 2)); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java index 06b15f7f4c..dd87c6577d 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java @@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellAddress; +import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellUtil; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; @@ -523,4 +524,27 @@ public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows { wb.close(); } + + @Test + public void testBug69154() throws Exception { + // this does not appear to work for HSSF but let's get it working for XSSF anyway + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet sheet = wb.createSheet(); + for (int i = 0; i < 6; i++) { + Row row = sheet.createRow(i); + for (int j = 0; j < 6; j++) { + String value = new CellAddress(i, j).formatAsString(); + row.createCell(j).setCellValue(value); + } + } + final int firstCol = 1; + final int secondCol = firstCol + 1; + sheet.addMergedRegion(new CellRangeAddress(0, 0, firstCol, secondCol)); + sheet.addMergedRegion(new CellRangeAddress(1, 2, firstCol, firstCol)); + sheet.addMergedRegion(new CellRangeAddress(3, 3, firstCol, secondCol)); + assertEquals(3, sheet.getNumMergedRegions()); + sheet.shiftColumns(2, 5, -1); + // assertEquals(2, sheet.getNumMergedRegions()); + } + } } diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java b/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java index 2c4b3fa507..2ef2921e07 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java @@ -102,16 +102,16 @@ public abstract class RowShifter extends BaseRowColShifter { // build a range of the rows that are overwritten, i.e. the target-area, but without // rows that are moved along final CellRangeAddress overwrite; - if(n > 0) { + if (n > 0) { // area is moved down => overwritten area is [endRow + n - movedRows, endRow + n] final int firstRow = Math.max(endRow + 1, endRow + n - movedRows); final int lastRow = endRow + n; - overwrite = new CellRangeAddress(firstRow, lastRow, 0, 0); + overwrite = new CellRangeAddress(firstRow, lastRow, merged.getFirstColumn(), merged.getLastColumn()); } else { // area is moved up => overwritten area is [startRow + n, startRow + n + movedRows] final int firstRow = startRow + n; final int lastRow = Math.min(startRow - 1, startRow + n + movedRows); - overwrite = new CellRangeAddress(firstRow, lastRow, 0, 0); + overwrite = new CellRangeAddress(firstRow, lastRow, merged.getFirstColumn(), merged.getLastColumn()); } // if the merged-region and the overwritten area intersect, we need to remove it @@ -126,10 +126,10 @@ public abstract class RowShifter extends BaseRowColShifter { * @param step length of the shifting step */ public static void validateShiftParameters(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { - if(step < 0) { + if (step < 0) { throw new IllegalArgumentException("Shifting step may not be negative, but had " + step); } - if(firstShiftColumnIndex > lastShiftColumnIndex) { + if (firstShiftColumnIndex > lastShiftColumnIndex) { throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), "Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); } diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java index b6c6330a31..af82e26c98 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java @@ -42,7 +42,7 @@ import org.junit.jupiter.api.Test; */ public abstract class BaseTestSheetShiftRows { - private final ITestDataProvider _testDataProvider; + protected final ITestDataProvider _testDataProvider; protected BaseTestSheetShiftRows(ITestDataProvider testDataProvider) { _testDataProvider = testDataProvider;