set sheet on comments table

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895230 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-11-21 13:46:42 +00:00
parent 5267b055eb
commit 1f50363e47
5 changed files with 32 additions and 42 deletions

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.model;
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;
@ -30,6 +31,15 @@ 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();
@ -47,18 +57,6 @@ public interface Comments {
*/
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
*
@ -76,12 +74,11 @@ public interface Comments {
/**
* 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

View File

@ -51,6 +51,8 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
public static final String DEFAULT_AUTHOR = "";
public static final int DEFAULT_AUTHOR_ID = 0;
private Sheet sheet;
/**
* Underlying XML Beans CTComment list.
*/
@ -92,6 +94,12 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
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();
@ -175,26 +183,9 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
*
* @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;
@ -204,7 +195,7 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
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
*
@ -233,13 +224,12 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
/**
* 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()) {

View File

@ -380,11 +380,10 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
*/
@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);
}
/**

View File

@ -165,6 +165,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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 );
@ -755,7 +756,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* 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) {
@ -763,7 +764,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return null;
}
return sheetComments.findCellComment(this, address);
return sheetComments.findCellComment(address);
}
/**
@ -3120,7 +3121,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
// 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());
@ -3208,7 +3209,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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());
@ -3523,6 +3524,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
sheetComments = (Comments)createRelationship(
XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1);
}
sheetComments.setSheet(this);
}
return sheetComments;
}

View File

@ -362,6 +362,7 @@ public final class TestXSSFComment extends BaseTestCellComment {
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();
@ -369,7 +370,7 @@ public final class TestXSSFComment extends BaseTestCellComment {
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);
@ -392,6 +393,7 @@ public final class TestXSSFComment extends BaseTestCellComment {
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();
@ -399,7 +401,7 @@ public final class TestXSSFComment extends BaseTestCellComment {
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);