summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYegor Kozlov <yegor@apache.org>2007-03-12 15:43:14 +0000
committerYegor Kozlov <yegor@apache.org>2007-03-12 15:43:14 +0000
commitbdf4ab73d0f881f7604a437c8224267691534b85 (patch)
tree103bec89274b7db2322a24cc1815b41a2a502704
parentda9a6807d6650f637629bec1f471f5cff313047c (diff)
downloadpoi-bdf4ab73d0f881f7604a437c8224267691534b85.tar.gz
poi-bdf4ab73d0f881f7604a437c8224267691534b85.zip
improved work with cell comments
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@517261 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/documentation/content/xdocs/hssf/quick-guide.xml6
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFCell.java82
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java9
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java3
4 files changed, 63 insertions, 37 deletions
diff --git a/src/documentation/content/xdocs/hssf/quick-guide.xml b/src/documentation/content/xdocs/hssf/quick-guide.xml
index 470dcb4589..0d174b5bb2 100644
--- a/src/documentation/content/xdocs/hssf/quick-guide.xml
+++ b/src/documentation/content/xdocs/hssf/quick-guide.xml
@@ -1123,14 +1123,16 @@
<p>
Reading cell comments
</p>
- <source>
+ <source>
HSSFCell cell = sheet.get(3).getColumn((short)1);
HSSFComment comment = cell.getCellComment();
if (comment != null) {
HSSFRichTextString str = comment.getString();
String author = comment.getAuthor();
}
- </source>
+ // alternatively you can retrieve cell comments by (row, column)
+ comment = sheet.getCellComment(3, 1);
+ </source>
</section>
<anchor id="Autofit"/>
<section><title>Adjust column width to fit the contents</title>
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
index 7d41f6d135..752f598afe 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
@@ -973,47 +973,59 @@ public class HSSFCell
}
/**
- * Returns the comment associated with this cell
+ * Returns comment associated with this cell
*
* @return comment associated with this cell
*/
public HSSFComment getCellComment(){
if (comment == null) {
- HashMap txshapes = new HashMap(); //map shapeId and TextObjectRecord
- for (Iterator it = sheet.getRecords().iterator(); it.hasNext(); ) {
- Record rec = ( Record ) it.next();
- if (rec instanceof NoteRecord){
- 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(note, txo);
- comment.setRow(note.getRow());
- comment.setColumn(note.getColumn());
- comment.setAuthor(note.getAuthor());
- comment.setVisible(note.getFlags() == NoteRecord.NOTE_VISIBLE);
- comment.setString(txo.getStr());
- break;
- }
- } else if (rec instanceof ObjRecord){
- ObjRecord obj = (ObjRecord)rec;
- SubRecord sub = (SubRecord)obj.getSubRecords().get(0);
- if (sub instanceof CommonObjectDataSubRecord){
- CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)sub;
- if (cmo.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT){
- //find the nearest TextObjectRecord which holds comment's text and map it to its shapeId
- while(it.hasNext()) {
- rec = ( Record ) it.next();
- if (rec instanceof TextObjectRecord) {
- txshapes.put(new Integer(cmo.getObjectId()), rec);
- break;
- }
- }
-
- }
- }
- }
- }
+ 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
+ for (Iterator it = sheet.getRecords().iterator(); it.hasNext(); ) {
+ Record rec = ( Record ) it.next();
+ if (rec instanceof NoteRecord){
+ NoteRecord note = (NoteRecord)rec;
+ if (note.getRow() == row && note.getColumn() == column){
+ TextObjectRecord txo = (TextObjectRecord)txshapes.get(new Integer(note.getShapeId()));
+ comment = new HSSFComment(note, txo);
+ comment.setRow(note.getRow());
+ comment.setColumn(note.getColumn());
+ comment.setAuthor(note.getAuthor());
+ comment.setVisible(note.getFlags() == NoteRecord.NOTE_VISIBLE);
+ comment.setString(txo.getStr());
+ break;
+ }
+ } else if (rec instanceof ObjRecord){
+ ObjRecord obj = (ObjRecord)rec;
+ SubRecord sub = (SubRecord)obj.getSubRecords().get(0);
+ if (sub instanceof CommonObjectDataSubRecord){
+ CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)sub;
+ if (cmo.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT){
+ //find the nearest TextObjectRecord which holds comment's text and map it to its shapeId
+ while(it.hasNext()) {
+ rec = ( Record ) it.next();
+ if (rec instanceof TextObjectRecord) {
+ txshapes.put(new Integer(cmo.getObjectId()), rec);
+ break;
+ }
+ }
+
+ }
+ }
+ }
+ }
+ return comment;
+ }
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
index 770511457c..d5b7737e32 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
@@ -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);
+ }
+
}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
index 918d1f8130..f1f1882732 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
@@ -101,6 +101,7 @@ public class TestHSSFComment extends TestCase {
cell = row.getCell((short)0);
comment = cell.getCellComment();
assertNull("Cells in the first column are not commented", comment);
+ assertNull(sheet.getCellComment(rownum, 0));
}
for (int rownum = 0; rownum < 3; rownum++) {
@@ -108,6 +109,8 @@ public class TestHSSFComment extends TestCase {
cell = row.getCell((short)1);
comment = cell.getCellComment();
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("Yegor Kozlov", comment.getAuthor());
assertFalse("cells in the second column have not empyy notes",