Author: fanningpj
Date: Tue Jul 2 12:58:37 2024
New Revision: 1918838
URL: http://svn.apache.org/viewvc?rev=1918838&view=rev
Log:
[bug-69154] XSSF: Shifting columns with merged regions generates an error about
overlapping regions
Modified:
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java
Modified:
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java?rev=1918838&r1=1918837&r2=1918838&view=diff
==============================================================================
---
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java
(original)
+++
poi/trunk/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftColumns.java
Tue Jul 2 12:58:37 2024
@@ -21,9 +21,16 @@ package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import org.apache.poi.ss.usermodel.BaseTestSheetShiftColumns;
+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.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
class TestXSSFSheetShiftColumns extends BaseTestSheetShiftColumns {
public TestXSSFSheetShiftColumns() {
@@ -40,4 +47,26 @@ class TestXSSFSheetShiftColumns extends
return XSSFTestDataSamples.writeOutAndReadBack(wb);
}
+ @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 < 4; 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 firstRow = 1; // works with 0, but fails with 1!
+ final int secondRow = firstRow + 1;
+ sheet.addMergedRegion(new CellRangeAddress(firstRow, secondRow, 0,
0));
+ sheet.addMergedRegion(new CellRangeAddress(firstRow, firstRow, 1,
2));
+ sheet.addMergedRegion(new CellRangeAddress(firstRow, secondRow, 3,
3));
+ assertEquals(3, sheet.getNumMergedRegions());
+ sheet.shiftColumns(2, 5, -1);
+ assertEquals(2, sheet.getNumMergedRegions());
+ }
+ }
}
\ No newline at end of file
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java?rev=1918838&r1=1918837&r2=1918838&view=diff
==============================================================================
---
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java
(original)
+++
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java
Tue Jul 2 12:58:37 2024
@@ -105,16 +105,16 @@ public abstract class ColumnShifter exte
// build a range of the columns that are overwritten, i.e. the
target-area, but without
// columns that are moved along
final CellRangeAddress overwrite;
- if(n > 0) {
+ if (n > 0) {
// area is moved down => overwritten area is [endColumn + n -
movedColumns, endColumn + n]
final int firstCol = Math.max(endColumn + 1, endColumn + n -
movedColumns);
final int lastCol = endColumn + n;
- overwrite = new CellRangeAddress(0, 0, firstCol, lastCol);
+ overwrite = new CellRangeAddress(merged.getFirstRow(),
merged.getLastRow(), firstCol, lastCol);
} else {
// area is moved up => overwritten area is [startColumn + n,
startColumn + n + movedColumns]
final int firstCol = startColumn + n;
final int lastCol = Math.min(startColumn - 1, startColumn + n +
movedColumns);
- overwrite = new CellRangeAddress(0, 0, firstCol, lastCol);
+ overwrite = new CellRangeAddress(merged.getFirstRow(),
merged.getLastRow(), firstCol, lastCol);
}
// if the merged-region and the overwritten area intersect, we need to
remove it
@@ -122,12 +122,12 @@ public abstract class ColumnShifter exte
}
public void shiftColumns(int firstShiftColumnIndex, int
lastShiftColumnIndex, int step){
- if(step > 0){
+ if (step > 0) {
for (Row row : sheet)
if(row != null)
row.shiftCellsRight(firstShiftColumnIndex,
lastShiftColumnIndex, step);
}
- else if(step < 0){
+ else if (step < 0) {
for (Row row : sheet)
if(row != null)
row.shiftCellsLeft(firstShiftColumnIndex,
lastShiftColumnIndex, -step);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]