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 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());
}
}
}

View 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);

View 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();
}

View 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;
@ -158,11 +160,25 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
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<>();

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
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;