mirror of https://github.com/apache/poi.git
Fix bug #44070 - patch from Gian Carlo Pace
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@609620 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a176593fb2
commit
2fa190d507
|
@ -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 <code>null</code> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue