From 9f03e0bc08a3e7d05f19cb187ac1326ef54b2c1a Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sat, 4 Nov 2017 07:29:53 +0000 Subject: [PATCH] bug 61474, github #81: fix ColumnShifter#removalNeeded to use columns instead of rows git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814257 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/usermodel/helpers/ColumnShifter.java | 10 ++++++++-- .../apache/poi/ss/usermodel/helpers/RowShifter.java | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java b/src/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java index 7769f30c58..fdd064a3d7 100644 --- a/src/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java +++ b/src/java/org/apache/poi/ss/usermodel/helpers/ColumnShifter.java @@ -49,6 +49,7 @@ public abstract class ColumnShifter extends BaseRowColShifter { * @param n the number of columns to shift * @return an array of affected merged regions, doesn't contain deleted ones */ + // Keep this code in sync with {@link RowShifter#shiftMergedRegions} public List shiftMergedRegions(int startColumn, int endColumn, int n) { List shiftedRegions = new ArrayList<>(); Set removedIndices = new HashSet<>(); @@ -93,6 +94,7 @@ public abstract class ColumnShifter extends BaseRowColShifter { return shiftedRegions; } + // Keep in sync with {@link RowShifter#removalNeeded} private boolean removalNeeded(CellRangeAddress merged, int startColumn, int endColumn, int n) { final int movedColumns = endColumn - startColumn + 1; @@ -101,10 +103,14 @@ public abstract class ColumnShifter extends BaseRowColShifter { final CellRangeAddress overwrite; if(n > 0) { // area is moved down => overwritten area is [endColumn + n - movedColumns, endColumn + n] - overwrite = new CellRangeAddress(Math.max(endColumn + 1, endColumn + n - movedColumns), endColumn + n, 0, 0); + final int firstCol = Math.max(endColumn + 1, endColumn + n - movedColumns); + final int lastCol = endColumn + n; + overwrite = new CellRangeAddress(0, 0, firstCol, lastCol); } else { // area is moved up => overwritten area is [startColumn + n, startColumn + n + movedColumns] - overwrite = new CellRangeAddress(startColumn + n, Math.min(startColumn - 1, startColumn + n + movedColumns), 0, 0); + final int firstCol = startColumn + n; + final int lastCol = Math.min(startColumn - 1, startColumn + n + movedColumns); + overwrite = new CellRangeAddress(0, 0, firstCol, lastCol); } // if the merged-region and the overwritten area intersect, we need to remove it diff --git a/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java b/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java index 09566e4dcb..965fa97ecf 100644 --- a/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java +++ b/src/java/org/apache/poi/ss/usermodel/helpers/RowShifter.java @@ -51,6 +51,7 @@ public abstract class RowShifter extends BaseRowColShifter { * @param n the number of rows to shift * @return an array of affected merged regions, doesn't contain deleted ones */ + // Keep this code in sync with {@link ColumnShifter#shiftMergedRegions} public List shiftMergedRegions(int startRow, int endRow, int n) { List shiftedRegions = new ArrayList<>(); Set removedIndices = new HashSet<>(); @@ -95,6 +96,7 @@ public abstract class RowShifter extends BaseRowColShifter { return shiftedRegions; } + // Keep in sync with {@link ColumnShifter#removalNeeded} private boolean removalNeeded(CellRangeAddress merged, int startRow, int endRow, int n) { final int movedRows = endRow - startRow + 1; @@ -103,10 +105,14 @@ public abstract class RowShifter extends BaseRowColShifter { final CellRangeAddress overwrite; if(n > 0) { // area is moved down => overwritten area is [endRow + n - movedRows, endRow + n] - overwrite = new CellRangeAddress(Math.max(endRow + 1, endRow + n - movedRows), endRow + n, 0, 0); + final int firstRow = Math.max(endRow + 1, endRow + n - movedRows); + final int lastRow = endRow + n; + overwrite = new CellRangeAddress(firstRow, lastRow, 0, 0); } else { // area is moved up => overwritten area is [startRow + n, startRow + n + movedRows] - overwrite = new CellRangeAddress(startRow + n, Math.min(startRow - 1, startRow + n + movedRows), 0, 0); + final int firstRow = startRow + n; + final int lastRow = Math.min(startRow - 1, startRow + n + movedRows); + overwrite = new CellRangeAddress(firstRow, lastRow, 0, 0); } // if the merged-region and the overwritten area intersect, we need to remove it