pjfanning commented on code in PR #1580:
URL: https://github.com/apache/poi/pull/1580#discussion_r4647991760
##########
poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFSheet.java:
##########
@@ -324,6 +324,21 @@ public void removeRow(Row row) {
_firstrow = -1;
_lastrow = -1;
}
+
+ // also remove any comment located in that row
+ removeCommentsInRow(hrow.getRowNum());
+ }
+ }
+
+ private void removeCommentsInRow(int rowNum) {
+ final HSSFPatriarch patriarch = getDrawingPatriarch();
Review Comment:
`getDrawingPatriarch()` aggregates the sheet's drawing records into an
`EscherAggregate` if that hasn't happened yet. So every `removeRow` on a sheet
with any drawing (charts, images) now forces re-serialisation of the drawing,
even when the removed row has no comment. Previously those records were written
back byte for byte, and EscherAggregate can change or drop escher content it
doesn't support. Could we first do a cheap check for a NoteRecord / comment on
this row and only touch the patriarch if there is one?
##########
poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFSheet.java:
##########
@@ -324,6 +324,21 @@ public void removeRow(Row row) {
_firstrow = -1;
_lastrow = -1;
}
+
+ // also remove any comment located in that row
+ removeCommentsInRow(hrow.getRowNum());
Review Comment:
This runs after the row is already removed from `_rows` and the
`InternalSheet`. If aggregating the drawing throws (malformed or unusual
drawing/chart records that `EscherAggregate` can't parse), `removeRow` now
fails with the row gone but its comments left behind, where before it
succeeded. Looking up the patriarch before mutating `_rows`/`_sheet`, or
handling the failure, would avoid the half-applied state.
##########
poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFSheet.java:
##########
@@ -324,6 +324,21 @@ public void removeRow(Row row) {
_firstrow = -1;
_lastrow = -1;
}
+
+ // also remove any comment located in that row
+ removeCommentsInRow(hrow.getRowNum());
+ }
+ }
+
+ private void removeCommentsInRow(int rowNum) {
Review Comment:
The same top-level-only 'find comments by row and remove from the patriarch'
logic already exists in `moveCommentsForRowShift` and
`HSSFCell.removeCellComment`. A shared helper that finds a comment's actual
container (patriarch or group) and removes it there would fix grouped comments
in `removeRow`, `removeCellComment` and `shiftRows` together, rather than
adding a third copy.
##########
poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFSheet.java:
##########
@@ -324,6 +324,21 @@ public void removeRow(Row row) {
_firstrow = -1;
_lastrow = -1;
}
+
+ // also remove any comment located in that row
+ removeCommentsInRow(hrow.getRowNum());
+ }
+ }
+
+ private void removeCommentsInRow(int rowNum) {
+ final HSSFPatriarch patriarch = getDrawingPatriarch();
+ if (patriarch == null) {
+ return;
+ }
+ for (final HSSFShape shape : new ArrayList<>(patriarch.getChildren()))
{
Review Comment:
This only scans the patriarch's top-level children, so comments inside an
`HSSFShapeGroup` (which Excel produces after grouping drawing objects) are
never removed, while `getCellComments()` / `findCellComment()` do search groups
recursively. For such a file the NoteRecord/TXO survive `removeRow` and the
comment is still there after write + reopen, so the leak isn't closed for
grouped comments. This needs to recurse and remove the comment from the group
that holds it (`HSSFShapeGroup.removeShape`).
Minor: it also copies and scans the full child list on every call, so
removing many rows is O(rows x shapes) with an allocation each time. Copying
only when there's a match would avoid that.
##########
poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheet.java:
##########
@@ -1116,6 +1117,30 @@ void bug59135() throws IOException {
}
}
+ @Test
+ void removeRowRemovesComments() throws IOException {
Review Comment:
Could we add cases for:
- a comment inside a shape group (currently survives, see the comment on
`removeCommentsInRow`);
- a sheet with no drawing, to check `removeRow` doesn't create a patriarch /
drawing group;
- the NoteRecord and TXO actually being gone from the written stream, not
just from `getCellComments()`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]