From bfc79d51697a72714586f1408c3f4c7d91fbcb3d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 30 Jul 2018 18:44:58 +0000 Subject: [PATCH] add new public method to expose cell addresses that have comments git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1837082 13f79535-47bb-0310-9956-ffa450edef68 --- .../eventusermodel/XSSFSheetXMLHandler.java | 9 +++++---- .../XSSFEventBasedExcelExtractor.java | 4 ++-- .../org/apache/poi/xssf/model/Comments.java | 9 +++++++++ .../apache/poi/xssf/model/CommentsTable.java | 20 +++++++++++++++++-- .../apache/poi/xssf/usermodel/XSSFSheet.java | 3 ++- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java index ffeee4046..af6aedfbd 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java @@ -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 iter = commentsTable.getCellAddresses(); iter.hasNext(); ) { + commentCellRefs.add(iter.next()); } } } diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java index 1a84a6911..526812183 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java @@ -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); diff --git a/src/ooxml/java/org/apache/poi/xssf/model/Comments.java b/src/ooxml/java/org/apache/poi/xssf/model/Comments.java index 50ce87ca9..1341de656 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/Comments.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/Comments.java @@ -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 getCellAddresses(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java index 895ec559e..61a98e3ca 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java @@ -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 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 getCellAddresses instead */ + @Removal(version = "4.2") + @Deprecated public Map getCellComments() { prepareCTCommentCache(); final TreeMap map = new TreeMap<>(); - for (final Entry e: commentRefs.entrySet()) { + for (final Entry e : commentRefs.entrySet()) { map.put(e.getKey(), new XSSFComment(this, e.getValue(), null)); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 68e3b29f4..a26e58e54 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -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 map = new HashMap<>(); - for(CellAddress address : sheetComments.getCellComments().keySet()) { + for(Iterator iter = sheetComments.getCellAddresses(); iter.hasNext(); ) { + CellAddress address = iter.next(); map.put(address, getCellComment(address)); } return map;