improved work with cell comments

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@517261 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2007-03-12 15:43:14 +00:00
parent 5868b3f7f1
commit 8a3e83c0df
4 changed files with 63 additions and 37 deletions

View File

@ -1130,6 +1130,8 @@
HSSFRichTextString str = comment.getString(); HSSFRichTextString str = comment.getString();
String author = comment.getAuthor(); String author = comment.getAuthor();
} }
// alternatively you can retrieve cell comments by (row, column)
comment = sheet.getCellComment(3, 1);
</source> </source>
</section> </section>
<anchor id="Autofit"/> <anchor id="Autofit"/>

View File

@ -973,18 +973,31 @@ public class HSSFCell
} }
/** /**
* Returns the comment associated with this cell * Returns comment associated with this cell
* *
* @return comment associated with this cell * @return comment associated with this cell
*/ */
public HSSFComment getCellComment(){ public HSSFComment getCellComment(){
if (comment == null) { if (comment == null) {
comment = findCellComment(sheet, record.getRow(), record.getColumn());
}
return comment;
}
/**
* Cell comment finder.
* Returns cell comment for the specified sheet, row and column.
*
* @return cell comment or <code>null</code> if not found
*/
protected static HSSFComment findCellComment(Sheet sheet, int row, int column){
HSSFComment comment = null;
HashMap txshapes = new HashMap(); //map shapeId and TextObjectRecord HashMap txshapes = new HashMap(); //map shapeId and TextObjectRecord
for (Iterator it = sheet.getRecords().iterator(); it.hasNext(); ) { for (Iterator it = sheet.getRecords().iterator(); it.hasNext(); ) {
Record rec = ( Record ) it.next(); Record rec = ( Record ) it.next();
if (rec instanceof NoteRecord){ if (rec instanceof NoteRecord){
NoteRecord note = (NoteRecord)rec; NoteRecord note = (NoteRecord)rec;
if (note.getRow() == record.getRow() && note.getColumn() == record.getColumn()){ if (note.getRow() == row && note.getColumn() == column){
TextObjectRecord txo = (TextObjectRecord)txshapes.get(new Integer(note.getShapeId())); TextObjectRecord txo = (TextObjectRecord)txshapes.get(new Integer(note.getShapeId()));
comment = new HSSFComment(note, txo); comment = new HSSFComment(note, txo);
comment.setRow(note.getRow()); comment.setRow(note.getRow());
@ -1013,7 +1026,6 @@ public class HSSFCell
} }
} }
} }
}
return comment; return comment;
} }
} }

View File

@ -1478,4 +1478,13 @@ public class HSSFSheet
} }
} }
/**
* Returns cell comment for the specified row and column
*
* @return cell comment or <code>null</code> if not found
*/
public HSSFComment getCellComment(int row, int column){
return HSSFCell.findCellComment(sheet, row, column);
}
} }

View File

@ -101,6 +101,7 @@ public class TestHSSFComment extends TestCase {
cell = row.getCell((short)0); cell = row.getCell((short)0);
comment = cell.getCellComment(); comment = cell.getCellComment();
assertNull("Cells in the first column are not commented", comment); assertNull("Cells in the first column are not commented", comment);
assertNull(sheet.getCellComment(rownum, 0));
} }
for (int rownum = 0; rownum < 3; rownum++) { for (int rownum = 0; rownum < 3; rownum++) {
@ -108,6 +109,8 @@ public class TestHSSFComment extends TestCase {
cell = row.getCell((short)1); cell = row.getCell((short)1);
comment = cell.getCellComment(); comment = cell.getCellComment();
assertNotNull("Cells in the second column have comments", comment); assertNotNull("Cells in the second column have comments", comment);
assertNotNull("Cells in the second column have comments", sheet.getCellComment(rownum, 1));
assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType()); assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType());
assertEquals("Yegor Kozlov", comment.getAuthor()); assertEquals("Yegor Kozlov", comment.getAuthor());
assertFalse("cells in the second column have not empyy notes", assertFalse("cells in the second column have not empyy notes",