mirror of https://github.com/apache/poi.git
changing attributes of existing cell comments is reflected after save
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@501875 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5fdbf91301
commit
579c07e3bf
|
@ -986,7 +986,7 @@ public class HSSFCell
|
|||
NoteRecord note = (NoteRecord)rec;
|
||||
if (note.getRow() == record.getRow() && note.getColumn() == record.getColumn()){
|
||||
TextObjectRecord txo = (TextObjectRecord)txshapes.get(new Integer(note.getShapeId()));
|
||||
comment = new HSSFComment(null, null);
|
||||
comment = new HSSFComment(note, txo);
|
||||
comment.setRow(note.getRow());
|
||||
comment.setColumn(note.getColumn());
|
||||
comment.setAuthor(note.getAuthor());
|
||||
|
|
|
@ -28,7 +28,7 @@ import java.util.Iterator;
|
|||
/**
|
||||
* Represents a cell comment - a sticky note associated with a cell.
|
||||
*
|
||||
* @author Yegor Kolzlov
|
||||
* @author Yegor Kozlov
|
||||
*/
|
||||
public class HSSFComment extends HSSFTextbox {
|
||||
|
||||
|
@ -36,6 +36,9 @@ public class HSSFComment extends HSSFTextbox {
|
|||
private short col, row;
|
||||
private String author;
|
||||
|
||||
private NoteRecord note = null;
|
||||
private TextObjectRecord txo = null;
|
||||
|
||||
/**
|
||||
* Construct a new comment with the given parent and anchor.
|
||||
*
|
||||
|
@ -56,6 +59,12 @@ public class HSSFComment extends HSSFTextbox {
|
|||
author = "";
|
||||
}
|
||||
|
||||
protected HSSFComment( NoteRecord note, TextObjectRecord txo )
|
||||
{
|
||||
this( (HSSFShape)null, (HSSFAnchor)null );
|
||||
this.txo = txo;
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this comment is visible.
|
||||
|
@ -63,6 +72,7 @@ public class HSSFComment extends HSSFTextbox {
|
|||
* @param visible <code>true</code> if the comment is visible, <code>false</code> otherwise
|
||||
*/
|
||||
public void setVisible(boolean visible){
|
||||
if(note != null) note.setFlags(visible ? NoteRecord.NOTE_VISIBLE : NoteRecord.NOTE_HIDDEN);
|
||||
this.visible = visible;
|
||||
}
|
||||
|
||||
|
@ -90,6 +100,7 @@ public class HSSFComment extends HSSFTextbox {
|
|||
* @param row the 0-based row of the cell that contains the comment
|
||||
*/
|
||||
public void setRow(int row){
|
||||
if(note != null) note.setRow((short)row);
|
||||
this.row = (short)row;
|
||||
}
|
||||
|
||||
|
@ -108,6 +119,7 @@ public class HSSFComment extends HSSFTextbox {
|
|||
* @param col the 0-based column of the cell that contains the comment
|
||||
*/
|
||||
public void setColumn(short col){
|
||||
if(note != null) note.setColumn(col);
|
||||
this.col = col;
|
||||
}
|
||||
|
||||
|
@ -126,6 +138,7 @@ public class HSSFComment extends HSSFTextbox {
|
|||
* @param author the name of the original author of the comment
|
||||
*/
|
||||
public void setAuthor(String author){
|
||||
if(note != null) note.setAuthor(author);
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
|
@ -134,10 +147,16 @@ public class HSSFComment extends HSSFTextbox {
|
|||
*
|
||||
* @param string Sets the rich text string used by this object.
|
||||
*/
|
||||
public void setString( HSSFRichTextString string )
|
||||
{
|
||||
//if font is not set we must set the default one implicitly
|
||||
public void setString( HSSFRichTextString string ) {
|
||||
//if font is not set we must set the default one
|
||||
if (string.numFormattingRuns() == 0) string.applyFont((short)0);
|
||||
|
||||
if (txo != null) {
|
||||
int frLength = ( string.numFormattingRuns() + 1 ) * 8;
|
||||
txo.setFormattingRunLength( (short) frLength );
|
||||
txo.setTextLength( (short) string.length() );
|
||||
txo.setStr( string );
|
||||
}
|
||||
super.setString(string);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -117,4 +117,45 @@ public class TestHSSFComment extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test that we can modify existing cell comments
|
||||
*/
|
||||
public static void testModifyComments() throws Exception {
|
||||
|
||||
String dir = System.getProperty("HSSF.testdata.path");
|
||||
FileInputStream is = new FileInputStream(new File(dir, "SimpleWithComments.xls"));
|
||||
HSSFWorkbook wb = new HSSFWorkbook(is);
|
||||
is.close();
|
||||
|
||||
HSSFSheet sheet = wb.getSheetAt(0);
|
||||
|
||||
HSSFCell cell;
|
||||
HSSFRow row;
|
||||
HSSFComment comment;
|
||||
|
||||
for (int rownum = 0; rownum < 3; rownum++) {
|
||||
row = sheet.getRow(rownum);
|
||||
cell = row.getCell((short)1);
|
||||
comment = cell.getCellComment();
|
||||
comment.setAuthor("Mofified["+rownum+"] by Yegor");
|
||||
comment.setString(new HSSFRichTextString("Modified comment at row " + rownum));
|
||||
}
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
wb.write(out);
|
||||
out.close();
|
||||
|
||||
wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
|
||||
sheet = wb.getSheetAt(0);
|
||||
|
||||
for (int rownum = 0; rownum < 3; rownum++) {
|
||||
row = sheet.getRow(rownum);
|
||||
cell = row.getCell((short)1);
|
||||
comment = cell.getCellComment();
|
||||
|
||||
assertEquals("Mofified["+rownum+"] by Yegor", comment.getAuthor());
|
||||
assertEquals("Modified comment at row " + rownum, comment.getString().getString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue