You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by ye...@apache.org on 2007/03/12 16:43:15 UTC

svn commit: r517261 - in /jakarta/poi/trunk/src: documentation/content/xdocs/hssf/ java/org/apache/poi/hssf/usermodel/ testcases/org/apache/poi/hssf/usermodel/

Author: yegor
Date: Mon Mar 12 08:43:14 2007
New Revision: 517261

URL: http://svn.apache.org/viewvc?view=rev&rev=517261
Log:
improved work with cell comments

Modified:
    jakarta/poi/trunk/src/documentation/content/xdocs/hssf/quick-guide.xml
    jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
    jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java

Modified: jakarta/poi/trunk/src/documentation/content/xdocs/hssf/quick-guide.xml
URL: http://svn.apache.org/viewvc/jakarta/poi/trunk/src/documentation/content/xdocs/hssf/quick-guide.xml?view=diff&rev=517261&r1=517260&r2=517261
==============================================================================
--- jakarta/poi/trunk/src/documentation/content/xdocs/hssf/quick-guide.xml (original)
+++ jakarta/poi/trunk/src/documentation/content/xdocs/hssf/quick-guide.xml Mon Mar 12 08:43:14 2007
@@ -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>

Modified: jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?view=diff&rev=517261&r1=517260&r2=517261
==============================================================================
--- jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Mon Mar 12 08:43:14 2007
@@ -973,47 +973,59 @@
     }
 
     /**
-     * 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;
+   }
 }

Modified: jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?view=diff&rev=517261&r1=517260&r2=517261
==============================================================================
--- jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Mon Mar 12 08:43:14 2007
@@ -1478,4 +1478,13 @@
         }
     }
 
+    /**
+     * 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);
+    }
+
 }

Modified: jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
URL: http://svn.apache.org/viewvc/jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java?view=diff&rev=517261&r1=517260&r2=517261
==============================================================================
--- jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java (original)
+++ jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java Mon Mar 12 08:43:14 2007
@@ -101,6 +101,7 @@
              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 @@
              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", 



---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/