diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java index d40be0f5db..ff8fad93ce 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/Comments.java @@ -46,6 +46,16 @@ public interface Comments { */ XSSFComment findCellComment(CellAddress cellAddress); + /** + * Finds the cell comment at cellAddress, if one exists + * + * @param sheet the sheet that has the comment + * @param cellAddress the address of the cell to find a comment + * @return cell comment if one exists, otherwise returns null + * @since POI 5.2.0 + */ + public XSSFComment findCellComment(XSSFSheet sheet, CellAddress cellAddress); + /** * Remove the comment at cellRef location, if one exists * diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java index a47557013a..f4a5493436 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java @@ -197,6 +197,7 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { * @return cell comment if one exists, otherwise returns null * @since POI 5.2.0 */ + @Override public XSSFComment findCellComment(XSSFSheet sheet, CellAddress cellAddress) { CTComment ctComment = getCTComment(cellAddress); if(ctComment == null) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index 99a0bed281..ce57c96740 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -45,7 +45,7 @@ import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.util.ImageUtils; import org.apache.poi.util.Internal; import org.apache.poi.util.Units; -import org.apache.poi.xssf.model.CommentsTable; +import org.apache.poi.xssf.model.Comments; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; @@ -383,7 +383,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing _rows = new TreeMap<>(); private List hyperlinks; private ColumnHelper columnHelper; - private CommentsTable sheetComments; + private Comments sheetComments; /** * cache of master shared formulas in this sheet. * Master shared formula is the first formula in a group of shared formulas is saved in the f element. @@ -163,8 +164,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // Look for bits we're interested in for(RelationPart rp : getRelationParts()){ POIXMLDocumentPart p = rp.getDocumentPart(); - if(p instanceof CommentsTable) { - sheetComments = (CommentsTable)p; + if(p instanceof Comments) { + sheetComments = (Comments)p; } if(p instanceof XSSFTable) { tables.put( rp.getRelationship().getId(), (XSSFTable)p ); @@ -3506,18 +3507,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * * @param create create a new comments table if it does not exist */ - protected CommentsTable getCommentsTable(boolean create) { + protected Comments getCommentsTable(boolean create) { if(sheetComments == null && create){ // Try to create a comments table with the same number as // the sheet has (i.e. sheet 1 -> comments 1) try { - sheetComments = (CommentsTable)createRelationship( + sheetComments = (Comments)createRelationship( XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), Math.toIntExact(sheet.getSheetId())); } catch(PartAlreadyExistsException e) { // Technically a sheet doesn't need the same number as - // it's comments, and clearly someone has already pinched + // its comments, and clearly someone has already pinched // our number! Go for the next available one instead - sheetComments = (CommentsTable)createRelationship( + sheetComments = (Comments)createRelationship( XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1); } } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java index caea41f0d2..44b663ce68 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFComment.java @@ -44,6 +44,7 @@ import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.model.Comments; import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.xmlbeans.XmlObject; @@ -210,7 +211,7 @@ public final class TestXSSFComment extends BaseTestCellComment { XSSFClientAnchor ca = (XSSFClientAnchor) anchor; // create comments and vmlDrawing parts if they don't exist - CommentsTable comments = wb.getXSSFWorkbook() + Comments comments = wb.getXSSFWorkbook() .getSheetAt(0).getCommentsTable(true); XSSFVMLDrawing vml = wb.getXSSFWorkbook() .getSheetAt(0).getVMLDrawing(true); @@ -223,7 +224,8 @@ public final class TestXSSFComment extends BaseTestCellComment { } // create the comment in two different ways and verify that there is no difference - XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1); + CommentsTable commentsTable = (CommentsTable)comments; + XSSFComment shape1 = new XSSFComment(comments, commentsTable.newComment(CellAddress.A1), vmlShape1); shape1.setColumn(ca.getCol1()); shape1.setRow(ca.getRow1()); @@ -236,7 +238,7 @@ public final class TestXSSFComment extends BaseTestCellComment { } CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1()); - XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2); + XSSFComment shape2 = new XSSFComment(comments, commentsTable.newComment(ref), vmlShape2); assertEquals(shape1.getAuthor(), shape2.getAuthor()); assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor()); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 2b341c3520..fea707e148 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -74,6 +74,7 @@ import org.apache.poi.util.LocaleUtil; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.model.CalculationChain; +import org.apache.poi.xssf.model.Comments; import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.streaming.SXSSFWorkbook; @@ -267,7 +268,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { XSSFComment comment = dg.createCellComment(new XSSFClientAnchor()); Cell cell = sheet.createRow(0).createCell(0); - CommentsTable comments = sheet.getCommentsTable(false); + CommentsTable comments = (CommentsTable) sheet.getCommentsTable(false); CTComments ctComments = comments.getCTComments(); cell.setCellComment(comment); @@ -917,10 +918,10 @@ public final class TestXSSFSheet extends BaseTestXSheet { void commentsTable() throws IOException { try (XSSFWorkbook wb1 = new XSSFWorkbook()) { XSSFSheet sheet1 = wb1.createSheet(); - CommentsTable comment1 = sheet1.getCommentsTable(false); + CommentsTable comment1 = (CommentsTable) sheet1.getCommentsTable(false); assertNull(comment1); - comment1 = sheet1.getCommentsTable(true); + comment1 = (CommentsTable) sheet1.getCommentsTable(true); assertNotNull(comment1); assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName()); @@ -928,10 +929,10 @@ public final class TestXSSFSheet extends BaseTestXSheet { //second sheet XSSFSheet sheet2 = wb1.createSheet(); - CommentsTable comment2 = sheet2.getCommentsTable(false); + CommentsTable comment2 = (CommentsTable) sheet2.getCommentsTable(false); assertNull(comment2); - comment2 = sheet2.getCommentsTable(true); + comment2 = (CommentsTable) sheet2.getCommentsTable(true); assertNotNull(comment2); assertSame(comment2, sheet2.getCommentsTable(true)); @@ -944,7 +945,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { //now test against a workbook containing cell comments try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) { XSSFSheet sheet1 = wb2.getSheetAt(0); - CommentsTable comment1 = sheet1.getCommentsTable(true); + CommentsTable comment1 = (CommentsTable) sheet1.getCommentsTable(true); assertNotNull(comment1); assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName()); assertSame(comment1, sheet1.getCommentsTable(true));