--- src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (revision 609594) +++ src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (working copy) @@ -1202,6 +1202,12 @@ 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 @@ * @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; } } --- src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java (revision 609594) +++ src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java (working copy) @@ -24,9 +24,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.TempFile; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.*; /** * Tests row shifting capabilities. @@ -170,5 +168,70 @@ 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); + } }