import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellAddress;
+import org.apache.poi.util.Internal;
import org.apache.poi.xssf.usermodel.XSSFComment;
import java.util.Iterator;
*/
public interface Comments {
+ /**
+ * This method is for internal POI use only. POI uses it to link the sheet and comments table.
+ * This method will not move comments from one sheet to another (if a user tries to use this method for that purpose).
+ * @param sheet the sheet that this comments table is associated with
+ * @since POI 5.2.0
+ */
+ @Internal
+ void setSheet(Sheet sheet);
+
int getNumberOfComments();
int getNumberOfAuthors();
*/
XSSFComment findCellComment(CellAddress cellAddress);
- /**
- * Finds the cell comment at cellAddress, if one exists
- *
- * @param sheet the sheet to check for comments (used to find drawing/shape data for comments) - set to null
- * if you don't need the drawing/shape data
- * @param cellAddress the address of the cell to find a comment
- * @return cell comment if one exists, otherwise returns null
- * @see #findCellComment(CellAddress)
- * @since POI 5.2.0
- */
- public XSSFComment findCellComment(Sheet sheet, CellAddress cellAddress);
-
/**
* Remove the comment at cellRef location, if one exists
*
/**
* Create a new comment and add to the CommentTable.
- * @param sheet sheet to add comment to
* @param clientAnchor the anchor for this comment
* @return new XSSFComment
* @since POI 5.2.0
*/
- XSSFComment createNewComment(Sheet sheet, ClientAnchor clientAnchor);
+ XSSFComment createNewComment(ClientAnchor clientAnchor);
/**
* Called after the reference is updated, so that
public static final String DEFAULT_AUTHOR = "";
public static final int DEFAULT_AUTHOR_ID = 0;
+ private Sheet sheet;
+
/**
* Underlying XML Beans CTComment list.
*/
doc.save(out, DEFAULT_XML_OPTIONS);
}
+ @Override
+ @Internal
+ public void setSheet(Sheet sheet) {
+ this.sheet = sheet;
+ }
+
@Override
protected void commit() throws IOException {
PackagePart part = getPackagePart();
*
* @param cellAddress the address of the cell to find a comment
* @return cell comment if one exists, otherwise returns null
- * @see #findCellComment(Sheet, CellAddress)
*/
@Override
public XSSFComment findCellComment(CellAddress cellAddress) {
- CTComment ct = getCTComment(cellAddress);
- return ct == null ? null : new XSSFComment(this, ct, null);
- }
-
- /**
- * Finds the cell comment at cellAddress, if one exists
- *
- * @param sheet the sheet to check for comments (used to find drawing/shape data for comments) - set to null
- * if you don't need the drawing/shape data
- * @param cellAddress the address of the cell to find a comment
- * @return cell comment if one exists, otherwise returns null
- * @see #findCellComment(CellAddress)
- * @since POI 5.2.0
- */
- @Override
- public XSSFComment findCellComment(Sheet sheet, CellAddress cellAddress) {
CTComment ctComment = getCTComment(cellAddress);
if(ctComment == null) {
return null;
return new XSSFComment(this, ctComment,
vml == null ? null : vml.findCommentShape(cellAddress.getRow(), cellAddress.getColumn()));
}
-
+
/**
* Get the underlying CTComment xmlbean for a comment located at cellRef, if it exists
*
/**
* Create a new comment and add to the CommentTable.
- * @param sheet sheet to add comment to
* @param clientAnchor the anchor for this comment
* @return new XSSFComment
* @since POI 5.2.0
*/
@Override
- public XSSFComment createNewComment(Sheet sheet, ClientAnchor clientAnchor) {
+ public XSSFComment createNewComment(ClientAnchor clientAnchor) {
XSSFVMLDrawing vml = getVMLDrawing(sheet, true);
CTShape vmlShape = vml == null ? null : vml.newCommentShape();
if (vmlShape != null && clientAnchor instanceof XSSFClientAnchor && ((XSSFClientAnchor)clientAnchor).isSet()) {
*/
@Override
public XSSFComment createCellComment(ClientAnchor anchor) {
- XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
XSSFSheet sheet = getSheet();
Comments comments = sheet.getCommentsTable(true);
- return comments.createNewComment(getSheet(), ca);
+ return comments.createNewComment(anchor);
}
/**
POIXMLDocumentPart p = rp.getDocumentPart();
if(p instanceof Comments) {
sheetComments = (Comments)p;
+ sheetComments.setSheet(this);
}
if(p instanceof XSSFTable) {
tables.put( rp.getRelationship().getId(), (XSSFTable)p );
* Return cell comment at row, column, if one exists. Otherwise returns null.
*
* @param address the location of the cell comment
- * @return the cell comment, if one exists. Otherwise return null.
+ * @return the cell comment, if one exists. Otherwise, return null.
*/
@Override
public XSSFComment getCellComment(CellAddress address) {
return null;
}
- return sheetComments.findCellComment(this, address);
+ return sheetComments.findCellComment(address);
}
/**
// is this comment part of the current row?
if(cellAddress.getRow() == rownum) {
- XSSFComment oldComment = sheetComments.findCellComment(this, cellAddress);
+ XSSFComment oldComment = sheetComments.findCellComment(cellAddress);
if (oldComment != null) {
XSSFComment xssfComment = new XSSFComment(sheetComments, oldComment.getCTComment(),
oldComment.getCTShape());
int columnIndex = oldCommentAddress.getColumn();
int newColumnIndex = shiftedRowNum(startColumnIndex, endColumnIndex, n, columnIndex);
if(newColumnIndex != columnIndex) {
- XSSFComment oldComment = sheetComments.findCellComment(this, oldCommentAddress);
+ XSSFComment oldComment = sheetComments.findCellComment(oldCommentAddress);
if (oldComment != null) {
XSSFComment xssfComment = new XSSFComment(sheetComments, oldComment.getCTComment(),
oldComment.getCTShape());
sheetComments = (Comments)createRelationship(
XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1);
}
+ sheetComments.setSheet(this);
}
return sheetComments;
}
try (SXSSFWorkbook workbook = new SXSSFWorkbook()) {
CreationHelper factory = workbook.getCreationHelper();
SXSSFSheet sheet = workbook.createSheet();
+ commentsTable.setSheet(sheet);
SXSSFRow row = sheet.createRow(0);
SXSSFCell cell = row.createCell(0);
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol2(1);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum());
- XSSFComment comment = commentsTable.createNewComment(sheet, anchor);
+ XSSFComment comment = commentsTable.createNewComment(anchor);
String uniqueText = UUID.randomUUID().toString();
comment.setString(uniqueText);
comment.setAuthor("author" + uniqueText);
try (SXSSFWorkbook workbook = new SXSSFWorkbook()) {
CreationHelper factory = workbook.getCreationHelper();
SXSSFSheet sheet = workbook.createSheet();
+ commentsTable.setSheet(sheet);
SXSSFRow row = sheet.createRow(0);
SXSSFCell cell = row.createCell(0);
ClientAnchor anchor = factory.createClientAnchor();
anchor.setCol2(1);
anchor.setRow1(row.getRowNum());
anchor.setRow2(row.getRowNum());
- XSSFComment comment = commentsTable.createNewComment(sheet, anchor);
+ XSSFComment comment = commentsTable.createNewComment(anchor);
String uniqueText = UUID.randomUUID().toString();
comment.setString(uniqueText);
comment.setAuthor("author" + uniqueText);