diff --git a/src/java/org/apache/poi/ss/formula/FormulaShifter.java b/src/java/org/apache/poi/ss/formula/FormulaShifter.java index 32da2b17a..74804a884 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaShifter.java +++ b/src/java/org/apache/poi/ss/formula/FormulaShifter.java @@ -85,6 +85,7 @@ public final class FormulaShifter { return new FormulaShifter(srcSheetIndex, dstSheetIndex); } + @Override public String toString() { StringBuffer sb = new StringBuffer(); @@ -322,7 +323,7 @@ public final class FormulaShifter { aptg.setFirstRow(destLastRowIndex+1); return aptg; } - if (destFirstRowIndex < aLastRow && aLastRow <= destLastRowIndex) { + if (destFirstRowIndex <= aLastRow && aLastRow < destLastRowIndex) { // dest rows overlap bottom of area // - truncate the bottom aptg.setLastRow(destFirstRowIndex-1); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java index a8e7374c9..aa2ae2280 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java @@ -17,8 +17,13 @@ package org.apache.poi.xssf.usermodel; +import java.io.IOException; + import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.util.CellUtil; import org.apache.poi.xssf.XSSFITestDataProvider; +import org.apache.poi.xssf.XSSFTestDataSamples; /** * @author Yegor Kozlov @@ -36,4 +41,17 @@ public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows { public void testShiftWithComments() { // disabled test from superclass // TODO - support shifting of comments. } + + public void testBug54524() throws IOException { + XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54524.xlsx"); + XSSFSheet sheet = workbook.getSheetAt(0); + sheet.shiftRows(3, 5, -1); + + Cell cell = CellUtil.getCell(sheet.getRow(1), 0); + assertEquals(1.0, cell.getNumericCellValue()); + cell = CellUtil.getCell(sheet.getRow(2), 0); + assertEquals("SUM(A2:A2)", cell.getCellFormula()); + cell = CellUtil.getCell(sheet.getRow(3), 0); + assertEquals("X", cell.getStringCellValue()); + } } diff --git a/src/testcases/org/apache/poi/ss/formula/TestFormulaShifter.java b/src/testcases/org/apache/poi/ss/formula/TestFormulaShifter.java index 79e42c2c5..e3e955850 100644 --- a/src/testcases/org/apache/poi/ss/formula/TestFormulaShifter.java +++ b/src/testcases/org/apache/poi/ss/formula/TestFormulaShifter.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.formula; import junit.framework.TestCase; + import org.apache.poi.ss.formula.ptg.AreaErrPtg; import org.apache.poi.ss.formula.ptg.AreaPtg; import org.apache.poi.ss.formula.ptg.Ptg; diff --git a/test-data/spreadsheet/54524.xlsx b/test-data/spreadsheet/54524.xlsx new file mode 100644 index 000000000..5a16447e0 Binary files /dev/null and b/test-data/spreadsheet/54524.xlsx differ