From ab1a19c3d4471c66b76c5cce7682ae34710356fb Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 20 Nov 2021 14:20:49 +0000 Subject: [PATCH] add shape data to comments in iterator git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895204 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xssf/model/Comments.java | 5 +++-- .../apache/poi/xssf/model/CommentsTable.java | 13 +++++++++--- .../apache/poi/xssf/usermodel/XSSFSheet.java | 20 +++++++++---------- 3 files changed, 22 insertions(+), 16 deletions(-) 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 7e8929e2da..96f867a9ec 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 @@ -72,10 +72,11 @@ public interface Comments { Iterator getCellAddresses(); /** - * @return iterator of comments (without their VML Shapes set) + * @param sheet the sheet to check for comments + * @return iterator of comments * @since POI 5.2.0 */ - Iterator commentIterator(); + Iterator commentIterator(Sheet sheet); /** * Create a new comment and add to the CommentTable. 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 e9fcc043ce..f405eba754 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 @@ -101,11 +101,13 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { } /** - * @return iterator of comments (without their VML Shapes set) + * @param sheet the sheet to check for comments + * @return iterator of comments * @since POI 5.2.0 */ @Override - public Iterator commentIterator() { + public Iterator commentIterator(Sheet sheet) { + XSSFVMLDrawing vml = getVMLDrawing(sheet, false); final CommentsTable table = this; return new Iterator() { private final CTComment[] commentsArray = getCTComments().getCommentList().getCommentArray(); @@ -119,7 +121,12 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { @Override public XSSFComment next() { CTComment ctComment = commentsArray[counter++]; - return new XSSFComment(table, ctComment, null); + CellAddress cellAddress = new CellAddress(ctComment.getRef()); + CTShape shape = null; + if (vml != null) { + shape = vml.findCommentShape(cellAddress.getRow(), cellAddress.getColumn()); + } + return new XSSFComment(table, ctComment, shape); } }; } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 2f92f5ce55..72c23dae6e 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -2956,14 +2956,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { */ @Override public void shiftRows(int startRow, int endRow, final int n, boolean copyRowHeight, boolean resetOriginalRowHeight) { - XSSFVMLDrawing vml = getVMLDrawing(false); - int sheetIndex = getWorkbook().getSheetIndex(this); String sheetName = getWorkbook().getSheetName(sheetIndex); FormulaShifter formulaShifter = FormulaShifter.createForRowShift( sheetIndex, sheetName, startRow, endRow, n, SpreadsheetVersion.EXCEL2007); - removeOverwritten(vml, startRow, endRow, n); - shiftCommentsAndRows(vml, startRow, endRow, n); + removeOverwritten(startRow, endRow, n); + shiftCommentsAndRows(startRow, endRow, n); XSSFRowShifter rowShifter = new XSSFRowShifter(this); rowShifter.shiftMergedRegions(startRow, endRow, n); @@ -3023,7 +3021,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } // remove all rows which will be overwritten - private void removeOverwritten(XSSFVMLDrawing vml, int startRow, int endRow, final int n) { + private void removeOverwritten(int startRow, int endRow, final int n) { + XSSFVMLDrawing vml = getVMLDrawing(false); HashSet rowsToRemoveSet = new HashSet<>(); for (Iterator it = rowIterator() ; it.hasNext() ; ) { XSSFRow row = (XSSFRow)it.next(); @@ -3052,7 +3051,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // also remove any comments associated with this row if (sheetComments != null) { - Iterator commentIterator = sheetComments.commentIterator(); + Iterator commentIterator = sheetComments.commentIterator(this); while (commentIterator.hasNext()) { XSSFComment comment = commentIterator.next(); CellAddress ref = comment.getAddress(); @@ -3078,10 +3077,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } } - } - private void shiftCommentsAndRows(XSSFVMLDrawing vml, int startRow, int endRow, final int n){ + private void shiftCommentsAndRows(int startRow, int endRow, final int n) { // then do the actual moving and also adjust comments/rowHeight // we need to sort it in a way so the shifting does not mess up the structures, // i.e. when shifting down, start from down and go up, when shifting up, vice-versa @@ -3115,7 +3113,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // is there a change necessary for the current row? if(newrownum != rownum) { - Iterator commentIterator = sheetComments.commentIterator(); + Iterator commentIterator = sheetComments.commentIterator(this); while (commentIterator.hasNext()) { XSSFComment oldComment = commentIterator.next(); CellReference ref = new CellReference(oldComment.getRow(), oldComment.getColumn()); @@ -3123,7 +3121,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // is this comment part of the current row? if(ref.getRow() == rownum) { XSSFComment xssfComment = new XSSFComment(sheetComments, oldComment.getCTComment(), - vml == null ? null : vml.findCommentShape(rownum, ref.getCol())); + oldComment.getCTShape()); // we should not perform the shifting right here as we would then find // already shifted comments and would shift them again... @@ -3200,7 +3198,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { if (sheetComments != null) { - Iterator commentIterator = sheetComments.commentIterator(); + Iterator commentIterator = sheetComments.commentIterator(this); while (commentIterator.hasNext()) { XSSFComment oldComment = commentIterator.next(); CellReference ref = new CellReference(oldComment.getRow(), oldComment.getColumn()); -- 2.39.5