diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index b52fa1e5b2..ba9ff72a7e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -1202,6 +1202,12 @@ public class HSSFSheet row2Replace.createCellFromRecord( cellRecord ); sheet.addValueRecord( rowNum + n, cellRecord ); } + + // move comments if exist (can exist even if cell is null) + HSSFComment comment = getCellComment(rowNum, col); + if (comment != null) { + comment.setRow(rowNum + n); + } } } if ( endRow == lastrow || endRow + n > lastrow ) lastrow = Math.min( endRow + n, 65535 ); @@ -1671,7 +1677,21 @@ public class HSSFSheet * @return cell comment or null if not found */ public HSSFComment getCellComment(int row, int column){ - return HSSFCell.findCellComment(sheet, row, column); + // Don't call findCellComment directly, otherwise + // two calls to this method will result in two + // new HSSFComment instances, which is bad + HSSFRow r = getRow(row); + if(r != null) { + HSSFCell c = r.getCell((short)column); + if(c != null) { + return c.getCellComment(); + } else { + // No cell, so you will get new + // objects every time, sorry... + return HSSFCell.findCellComment(sheet, row, column); + } + } + return null; } } diff --git a/src/testcases/org/apache/poi/hssf/data/comments.xls b/src/testcases/org/apache/poi/hssf/data/comments.xls new file mode 100644 index 0000000000..9eb0329b0e Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/comments.xls differ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java b/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java index d07a3eea5a..91876fff25 100755 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java @@ -25,6 +25,8 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.TempFile; import java.io.File; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -170,5 +172,70 @@ public class TestSheetShiftRows extends TestCase { assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6)); } + + + public void testShiftWithComments() throws Exception { + String filename = System.getProperty( "HSSF.testdata.path" ); + filename = filename + "/comments.xls"; + FileInputStream fin = new FileInputStream( filename ); + HSSFWorkbook wb = new HSSFWorkbook( fin ); + fin.close(); + + HSSFSheet sheet = wb.getSheet("Sheet1"); + assertEquals(3, sheet.getLastRowNum()); + + // Verify comments are in the position expected + assertNotNull(sheet.getCellComment(0,0)); + assertNull(sheet.getCellComment(1,0)); + assertNotNull(sheet.getCellComment(2,0)); + assertNotNull(sheet.getCellComment(3,0)); + + String comment1 = sheet.getCellComment(0,0).getString().getString(); + assertEquals(comment1,"comment top row1 (index0)\n"); + String comment3 = sheet.getCellComment(2,0).getString().getString(); + assertEquals(comment3,"comment top row3 (index2)\n"); + String comment4 = sheet.getCellComment(3,0).getString().getString(); + assertEquals(comment4,"comment top row4 (index3)\n"); + + // Shifting all but first line down to test comments shifting + sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + wb.write(outputStream); + + // Test that comments were shifted as expected + assertEquals(4, sheet.getLastRowNum()); + assertNotNull(sheet.getCellComment(0,0)); + assertNull(sheet.getCellComment(1,0)); + assertNull(sheet.getCellComment(2,0)); + assertNotNull(sheet.getCellComment(3,0)); + assertNotNull(sheet.getCellComment(4,0)); + + String comment1_shifted = sheet.getCellComment(0,0).getString().getString(); + assertEquals(comment1,comment1_shifted); + String comment3_shifted = sheet.getCellComment(3,0).getString().getString(); + assertEquals(comment3,comment3_shifted); + String comment4_shifted = sheet.getCellComment(4,0).getString().getString(); + assertEquals(comment4,comment4_shifted); + + // Write out and read back in again + // Ensure that the changes were persisted + wb = new HSSFWorkbook( new ByteArrayInputStream(outputStream.toByteArray()) ); + sheet = wb.getSheet("Sheet1"); + assertEquals(4, sheet.getLastRowNum()); + + // Verify comments are in the position expected after the shift + assertNotNull(sheet.getCellComment(0,0)); + assertNull(sheet.getCellComment(1,0)); + assertNull(sheet.getCellComment(2,0)); + assertNotNull(sheet.getCellComment(3,0)); + assertNotNull(sheet.getCellComment(4,0)); + + comment1_shifted = sheet.getCellComment(0,0).getString().getString(); + assertEquals(comment1,comment1_shifted); + comment3_shifted = sheet.getCellComment(3,0).getString().getString(); + assertEquals(comment3,comment3_shifted); + comment4_shifted = sheet.getCellComment(4,0).getString().getString(); + assertEquals(comment4,comment4_shifted); + } }