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
This commit is contained in:
PJ Fanning 2018-07-30 18:44:58 +00:00
parent 6e0a009b8f
commit bfc79d5169
5 changed files with 36 additions and 9 deletions

View File

@ -18,6 +18,7 @@ package org.apache.poi.xssf.eventusermodel;
import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML; import static org.apache.poi.xssf.usermodel.XSSFRelation.NS_SPREADSHEETML;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
@ -115,7 +116,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
*/ */
public XSSFSheetXMLHandler( public XSSFSheetXMLHandler(
Styles styles, Styles styles,
CommentsTable comments, Comments comments,
SharedStrings strings, SharedStrings strings,
SheetContentsHandler sheetContentsHandler, SheetContentsHandler sheetContentsHandler,
DataFormatter dataFormatter, DataFormatter dataFormatter,
@ -159,11 +160,11 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults); this(styles, strings, sheetContentsHandler, new DataFormatter(), formulasNotResults);
} }
private void init(CommentsTable commentsTable) { private void init(Comments commentsTable) {
if (commentsTable != null) { if (commentsTable != null) {
commentCellRefs = new LinkedList<>(); commentCellRefs = new LinkedList<>();
for (CellAddress cellAddress : commentsTable.getCellComments().keySet()) { for (Iterator<CellAddress> iter = commentsTable.getCellAddresses(); iter.hasNext(); ) {
commentCellRefs.add(cellAddress); commentCellRefs.add(iter.next());
} }
} }
} }

View File

@ -230,7 +230,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
public void processSheet( public void processSheet(
SheetContentsHandler sheetContentsExtractor, SheetContentsHandler sheetContentsExtractor,
Styles styles, Styles styles,
CommentsTable comments, Comments comments,
SharedStrings strings, SharedStrings strings,
InputStream sheetInputStream) InputStream sheetInputStream)
throws IOException, SAXException { throws IOException, SAXException {
@ -277,7 +277,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor
text.append(iter.getSheetName()); text.append(iter.getSheetName());
text.append('\n'); text.append('\n');
} }
CommentsTable comments = includeCellComments ? iter.getSheetComments() : null; Comments comments = includeCellComments ? iter.getSheetComments() : null;
processSheet(sheetExtractor, styles, comments, strings, stream); processSheet(sheetExtractor, styles, comments, strings, stream);
if (includeHeadersFooters) { if (includeHeadersFooters) {
sheetExtractor.appendHeaderText(text); sheetExtractor.appendHeaderText(text);

View File

@ -19,6 +19,8 @@ package org.apache.poi.xssf.model;
import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFComment;
import java.util.Iterator;
/** /**
* An interface exposing useful functions for dealing with Excel Workbook Comments. * 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 * 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 * @return returns true if a comment was removed
*/ */
boolean removeComment(CellAddress cellRef); 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();
} }

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; 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.openxml4j.opc.PackagePart;
import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
@ -158,16 +160,30 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
return commentRefs.get(cellRef); 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. * Returns all cell comments on this sheet.
* @return A map of each Comment in this sheet, keyed on the cell address where * @return A map of each Comment in this sheet, keyed on the cell address where
* the comment is located. * the comment is located.
* @deprecated use <code>getCellAddresses</code> instead
*/ */
@Removal(version = "4.2")
@Deprecated
public Map<CellAddress, XSSFComment> getCellComments() { public Map<CellAddress, XSSFComment> getCellComments() {
prepareCTCommentCache(); prepareCTCommentCache();
final TreeMap<CellAddress, XSSFComment> map = new TreeMap<>(); 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)); map.put(e.getKey(), new XSSFComment(this, e.getValue(), null));
} }

View 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 // the cell comments in sheetComments.getCellComments() do not have the client anchors set
Map<CellAddress, XSSFComment> map = new HashMap<>(); 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)); map.put(address, getCellComment(address));
} }
return map; return map;