Index: src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java =================================================================== --- src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (revision 1752837) +++ src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (working copy) @@ -1509,7 +1509,8 @@ * 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).
+ * rows (ie. merged 2 cells on a row to be shifted). All merged regions are completely + * overlaid by shifting will be deleted.
* * TODO Might want to add bounds checking here * Index: src/java/org/apache/poi/ss/usermodel/Sheet.java =================================================================== --- src/java/org/apache/poi/ss/usermodel/Sheet.java (revision 1752837) +++ src/java/org/apache/poi/ss/usermodel/Sheet.java (working copy) @@ -696,7 +696,8 @@ * *
* Additionally shifts merged regions that are completely defined in these - * rows (ie. merged 2 cells on a row to be shifted). + * rows (ie. merged 2 cells on a row to be shifted). All merged regions are completely + * overlaid by shifting will be deleted. *
* @param startRow the row to start shifting
* @param endRow the row to end shifting
Index: src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java
===================================================================
--- src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java (revision 1752837)
+++ src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java (working copy)
@@ -44,12 +44,13 @@
}
/**
- * Shifts, grows, or shrinks the merged regions due to a row shift
+ * Shifts, grows, or shrinks the merged regions due to a row shift.
+ * Merged regions are completely overlaid by shifting will be deleted.
*
* @param startRow the row to start shifting
* @param endRow the row to end shifting
* @param n the number of rows to shift
- * @return an array of affected merged regions
+ * @return an array of affected merged regions, doesn't contain deleted ones
*/
public List
* Additionally shifts merged regions that are completely defined in these
- * rows (ie. merged 2 cells on a row to be shifted).
+ * rows (ie. merged 2 cells on a row to be shifted). All merged regions are completely
+ * overlaid by shifting will be deleted.
*
* @param startRow the row to start shifting
* @param endRow the row to end shifting
Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
===================================================================
--- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (revision 1752837)
+++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (working copy)
@@ -2913,7 +2913,8 @@
*
*
* Additionally shifts merged regions that are completely defined in these
- * rows (ie. merged 2 cells on a row to be shifted).
+ * rows (ie. merged 2 cells on a row to be shifted). All merged regions are completely
+ * overlaid by shifting will be deleted.
*
* @param startRow the row to start shifting
* @param endRow the row to end shifting
Index: src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java
===================================================================
--- src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java (revision 1752837)
+++ src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java (working copy)
@@ -634,7 +634,67 @@
wb.close();
}
-
+
+ @Test
+ public void shiftMergedRowsToMergedRowsUp() throws IOException {
+ Workbook wb = _testDataProvider.createWorkbook();
+ Sheet sheet = wb.createSheet("test");
+
+ // populate sheet cells
+ for (int i = 0; i < 2; i++) {
+ Row row = sheet.createRow(i);
+ for (int j = 0; j < 5; j++) {
+ Cell cell = row.createCell(j);
+ cell.setCellValue(i + "x" + j);
+ }
+ }
+
+ CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
+ CellRangeAddress A2_C2 = new CellRangeAddress(1, 1, 0, 2);
+
+ sheet.addMergedRegion(A1_E1);
+ sheet.addMergedRegion(A2_C2);
+
+ // A1:E1 should be removed
+ // A2:C2 will be A1:C1
+ sheet.shiftRows(1, sheet.getLastRowNum(), -1);
+
+ assertEquals(1, sheet.getNumMergedRegions());
+ assertEquals(CellRangeAddress.valueOf("A1:C1"), sheet.getMergedRegion(0));
+
+ wb.close();
+ }
+
+ @Test
+ public void shiftMergedRowsToMergedRowsDown() throws IOException {
+ Workbook wb = _testDataProvider.createWorkbook();
+ Sheet sheet = wb.createSheet("test");
+
+ // populate sheet cells
+ for (int i = 0; i < 2; i++) {
+ Row row = sheet.createRow(i);
+ for (int j = 0; j < 5; j++) {
+ Cell cell = row.createCell(j);
+ cell.setCellValue(i + "x" + j);
+ }
+ }
+
+ CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
+ CellRangeAddress A2_C2 = new CellRangeAddress(1, 1, 0, 2);
+
+ sheet.addMergedRegion(A1_E1);
+ sheet.addMergedRegion(A2_C2);
+
+ // A1:E1 should be moved to A2:E2
+ // A2:C2 will be removed
+ sheet.shiftRows(0, 0, 1);
+
+ assertEquals(1, sheet.getNumMergedRegions());
+ assertEquals(CellRangeAddress.valueOf("A2:E2"), sheet.getMergedRegion(0));
+
+ wb.close();
+ }
+
private void createHyperlink(CreationHelper helper, Cell cell, int linkType, String ref) {
cell.setCellValue(ref);
Hyperlink link = helper.createHyperlink(linkType);