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());
/**
* Represents a cell comment - a sticky note associated with a cell.
*
- * @author Yegor Kolzlov
+ * @author Yegor Kozlov
*/
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.
*
author = "";
}
+ protected HSSFComment( NoteRecord note, TextObjectRecord txo )
+ {
+ this( (HSSFShape)null, (HSSFAnchor)null );
+ this.txo = txo;
+ this.note = note;
+ }
/**
* Returns whether this comment is visible.
* @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;
}
* @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;
}
* @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;
}
* @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;
}
*
* @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);
}
}
}
}
+ /**
+ * 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());
+ }
+
+ }
}