diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index 100b539de..012c3a256 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -127,7 +127,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { out.close(); } - public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, + @Override + public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2) { return new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); } @@ -177,6 +178,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { return shape; } + @Override public XSSFPicture createPicture(ClientAnchor anchor, int pictureIndex){ return createPicture((XSSFClientAnchor)anchor, pictureIndex); } @@ -202,7 +204,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { return chart; } - public XSSFChart createChart(ClientAnchor anchor) { + @Override + public XSSFChart createChart(ClientAnchor anchor) { return createChart((XSSFClientAnchor)anchor); } @@ -212,6 +215,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { * @param pictureIndex the index of the picture in the workbook collection of pictures, * {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} . */ + @SuppressWarnings("resource") protected PackageRelationship addPictureReference(int pictureIndex){ XSSFWorkbook wb = (XSSFWorkbook)getParent().getParent(); XSSFPictureData data = wb.getAllPictures().get(pictureIndex); @@ -285,6 +289,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { * to the sheet. * @return the newly created comment. */ + @Override public XSSFComment createCellComment(ClientAnchor anchor) { XSSFClientAnchor ca = (XSSFClientAnchor)anchor; XSSFSheet sheet = (XSSFSheet)getParent(); @@ -300,8 +305,12 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { vmlShape.getClientDataArray(0).setAnchorArray(0, position); } String ref = new CellReference(ca.getRow1(), ca.getCol1()).formatAsString(); - XSSFComment shape = new XSSFComment(comments, comments.newComment(ref), vmlShape); - return shape; + + if(comments.findCellComment(ref) != null) { + throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref); + } + + return new XSSFComment(comments, comments.newComment(ref), vmlShape); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 79bbc4ea6..658d03e50 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -18,13 +18,7 @@ package org.apache.poi.xssf.usermodel; import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -52,26 +46,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.functions.Function; -import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.CellValue; -import org.apache.poi.ss.usermodel.ClientAnchor; -import org.apache.poi.ss.usermodel.Comment; -import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.DataFormatter; -import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.ss.usermodel.Drawing; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.FormulaError; -import org.apache.poi.ss.usermodel.FormulaEvaluator; -import org.apache.poi.ss.usermodel.Hyperlink; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.Name; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; @@ -1331,6 +1306,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { factory.createRichTextString("I like this cell. It's my favourite.")); comment1.setAuthor("Bob T. Fish"); + anchor = factory.createClientAnchor(); + anchor.setCol1(0); + anchor.setCol2(4); + anchor.setRow1(1); + anchor.setRow2(1); Comment comment2 = drawing.createCellComment(anchor); comment2.setString( factory.createRichTextString("This is much less fun...")); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java index d69a62f48..e0c6d4055 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java @@ -26,6 +26,7 @@ import junit.framework.TestCase; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.FontUnderline; import org.apache.poi.xssf.XSSFTestDataSamples; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties; @@ -37,7 +38,7 @@ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing; * @author Yegor Kozlov */ public class TestXSSFDrawing extends TestCase { - public void testRead(){ + public void testRead() throws IOException{ XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); //the sheet has one relationship and it is XSSFDrawing @@ -719,4 +720,26 @@ public class TestXSSFDrawing extends TestCase { stream.close(); }*/ } + + public void testBug56835CellComment() throws Exception { + XSSFWorkbook wb = new XSSFWorkbook(); + try { + XSSFSheet sheet = wb.createSheet(); + XSSFDrawing drawing = sheet.createDrawingPatriarch(); + + // first comment works + ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4); + XSSFComment comment = drawing.createCellComment(anchor); + assertNotNull(comment); + + try { + drawing.createCellComment(anchor); + fail("Should fail if we try to add the same comment for the same cell"); + } catch (IllegalArgumentException e) { + // expected + } + } finally { + wb.close(); + } + } }