From: Javen O'Neal Date: Sun, 11 Sep 2016 04:05:42 +0000 (+0000) Subject: bug 52425: Error adding Comments into cloned Sheets; patch from Daniel X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=abe46f40ed865255bf4cce97d82f02b9ba0f0935;p=poi.git bug 52425: Error adding Comments into cloned Sheets; patch from Daniel git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760221 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 5f1da12ecc..98711c9dda 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -591,20 +591,21 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } } else { //search the referenced drawing in the list of the sheet's relations + final String id = ctDrawing.getId(); for (RelationPart rp : getRelationParts()){ POIXMLDocumentPart p = rp.getDocumentPart(); if(p instanceof XSSFVMLDrawing) { XSSFVMLDrawing dr = (XSSFVMLDrawing)p; String drId = rp.getRelationship().getId(); - if(drId.equals(ctDrawing.getId())){ + if (drId.equals(id)) { drawing = dr; break; } - break; + // do not break here since drawing has not been found yet (see bug 52425) } } if(drawing == null){ - logger.log(POILogger.ERROR, "Can't find VML drawing with id=" + ctDrawing.getId() + " in the list of the sheet's relationships"); + logger.log(POILogger.ERROR, "Can't find VML drawing with id=" + id + " in the list of the sheet's relationships"); } } return drawing; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 689e999bcb..3f7be066d1 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -47,6 +47,9 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellCopyPolicy; import org.apache.poi.ss.usermodel.CellType; 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.Drawing; import org.apache.poi.ss.usermodel.FormulaError; import org.apache.poi.ss.usermodel.IgnoredErrorType; import org.apache.poi.ss.usermodel.IndexedColors; @@ -1975,4 +1978,46 @@ public final class TestXSSFSheet extends BaseTestXSheet { wb.close(); } } + + /** + * See bug #52425 + */ + @Test + public void testInsertCommentsToClonedSheet() { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("52425.xlsx"); + CreationHelper helper = wb.getCreationHelper(); + Sheet sheet2 = wb.createSheet("Sheet 2"); + Sheet sheet3 = wb.cloneSheet(0); + wb.setSheetName(2, "Sheet 3"); + + // Adding Comment to new created Sheet 2 + addComments(helper, sheet2); + // Adding Comment to cloned Sheet 3 + addComments(helper, sheet3); + } + + private void addComments(CreationHelper helper, Sheet sheet) { + Drawing drawing = sheet.createDrawingPatriarch(); + + for (int i = 0; i < 2; i++) { + ClientAnchor anchor = helper.createClientAnchor(); + anchor.setCol1(0); + anchor.setRow1(0 + i); + anchor.setCol2(2); + anchor.setRow2(3 + i); + + Comment comment = drawing.createCellComment(anchor); + comment.setString(helper.createRichTextString("BugTesting")); + + Row row = sheet.getRow(0 + i); + if (row == null) + row = sheet.createRow(0 + i); + Cell cell = row.getCell(0); + if (cell == null) + cell = row.createCell(0); + + cell.setCellComment(comment); + } + + } } diff --git a/test-data/spreadsheet/52425.xlsx b/test-data/spreadsheet/52425.xlsx new file mode 100644 index 0000000000..0659822f64 Binary files /dev/null and b/test-data/spreadsheet/52425.xlsx differ