]> source.dussan.org Git - poi.git/commitdiff
add new public method to expose cell addresses that have comments
authorPJ Fanning <fanningpj@apache.org>
Mon, 30 Jul 2018 18:44:58 +0000 (18:44 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 30 Jul 2018 18:44:58 +0000 (18:44 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1837082 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
src/ooxml/java/org/apache/poi/xssf/model/Comments.java
src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java

index ffeee4046753bc82332a5ba2b92e88ac2ccdeb29..af6aedfbd05d4bf44491aea6e19d4262c4629968 100644 (file)
@@ -18,6 +18,7 @@ package org.apache.poi.xssf.eventusermodel;
 
 import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML;
 
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Queue;
 
@@ -115,7 +116,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
     */
    public XSSFSheetXMLHandler(
            Styles styles,
-           CommentsTable comments,
+           Comments comments,
            SharedStrings strings,
            SheetContentsHandler sheetContentsHandler,
            DataFormatter dataFormatter,
@@ -159,11 +160,11 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
        this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults);
    }
    
-   private void init(CommentsTable commentsTable) {
+   private void init(Comments commentsTable) {
        if (commentsTable != null) {
            commentCellRefs = new LinkedList<>();
-           for (CellAddress cellAddress : commentsTable.getCellComments().keySet()) {
-               commentCellRefs.add(cellAddress);
+           for (Iterator<CellAddress> iter = commentsTable.getCellAddresses(); iter.hasNext(); ) {
+               commentCellRefs.add(iter.next());
            }
        }
    }
index 1a84a6911435751f57f46e3edf8907dda501ed4d..526812183df9e371bcae7ba0ccb3f31695b18476 100644 (file)
@@ -230,7 +230,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
     public void processSheet(
             SheetContentsHandler sheetContentsExtractor,
             Styles styles,
-            CommentsTable comments,
+            Comments comments,
             SharedStrings strings,
             InputStream sheetInputStream)
             throws IOException, SAXException {
@@ -277,7 +277,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
                     text.append(iter.getSheetName());
                     text.append('\n');
                 }
-                CommentsTable comments = includeCellComments ? iter.getSheetComments() : null;
+                Comments comments = includeCellComments ? iter.getSheetComments() : null;
                 processSheet(sheetExtractor, styles, comments, strings, stream);
                 if (includeHeadersFooters) {
                     sheetExtractor.appendHeaderText(text);
index 50ce87ca9ce7730181382691eb5a46f09755a7e5..1341de656105f8062977d390e41a5b65058a6129 100644 (file)
@@ -19,6 +19,8 @@ package org.apache.poi.xssf.model;
 import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.xssf.usermodel.XSSFComment;
 
+import java.util.Iterator;
+
 /**
  * An interface exposing useful functions for dealing with Excel Workbook Comments.
  * It is intended that this interface should support low level access and not expose
@@ -49,4 +51,11 @@ public interface Comments {
      * @return returns true if a comment was removed
      */
     boolean removeComment(CellAddress cellRef);
+
+    /**
+     * Returns all cell addresses that have comments.
+     * @return An iterator to traverse all cell addresses that have comments.
+     * @since 4.0.0
+     */
+    Iterator<CellAddress> getCellAddresses();
 }
index 895ec559e4450a3e250a65de079dd663dc91a294..61a98e3ca9bffbd4e85a83d5f896552df791d668 100644 (file)
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.TreeMap;
@@ -30,6 +31,7 @@ import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.util.Internal;
+import org.apache.poi.util.Removal;
 import org.apache.poi.xssf.usermodel.XSSFComment;
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
@@ -157,17 +159,31 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
         // Return the comment, or null if not known
         return commentRefs.get(cellRef);
     }
-    
+
+    /**
+     * Returns all cell addresses that have comments.
+     * @return An iterator to traverse all cell addresses that have comments.
+     * @since 4.0.0
+     */
+    @Override
+    public Iterator<CellAddress> getCellAddresses() {
+        prepareCTCommentCache();
+        return commentRefs.keySet().iterator();
+    }
+
     /**
      * Returns all cell comments on this sheet.
      * @return A map of each Comment in this sheet, keyed on the cell address where
      * the comment is located.
+     * @deprecated use <code>getCellAddresses</code> instead
      */
+    @Removal(version = "4.2")
+    @Deprecated
     public Map<CellAddress, XSSFComment> getCellComments() {
         prepareCTCommentCache();
         final TreeMap<CellAddress, XSSFComment> map = new TreeMap<>();
         
-        for (final Entry<CellAddress, CTComment> e: commentRefs.entrySet()) {
+        for (final Entry<CellAddress, CTComment> e : commentRefs.entrySet()) {
             map.put(e.getKey(), new XSSFComment(this, e.getValue(), null));
         }
         
index 68e3b29f40954d78343ea83e93aa00a8f2ca9024..a26e58e54c07170879c39f31738ba719e2799579 100644 (file)
@@ -845,7 +845,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
         }
         // the cell comments in sheetComments.getCellComments() do not have the client anchors set
         Map<CellAddress, XSSFComment> map = new HashMap<>();
-        for(CellAddress address : sheetComments.getCellComments().keySet()) {
+        for(Iterator<CellAddress> iter = sheetComments.getCellAddresses(); iter.hasNext(); ) {
+            CellAddress address = iter.next();
             map.put(address, getCellComment(address));
         }
         return map;