Bug 59687 - XSSFSheet.RemoveRow doesn't handle row gaps properly when removing row comments (typo)
Summary: XSSFSheet.RemoveRow doesn't handle row gaps properly when removing row commen...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.15-dev
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-06-09 17:30 UTC by Greg Woolsey
Modified: 2016-09-22 07:52 UTC (History)
0 users



Attachments
simple file that causes trouble when removing rows with comments and missing rows above. (9.36 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2016-06-09 17:30 UTC, Greg Woolsey
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Greg Woolsey 2016-06-09 17:30:17 UTC
Created attachment 33936 [details]
simple file that causes trouble when removing rows with comments and missing rows above.

The attached file was created with Excel 2016.  It has only one cell with data, A4, with a comment on that cell.  The XML for the sheet shows only that one row defined.

The following test method fails, because XSSFSheet.removeRow(row) uses the physical offset, not the logical row #, when looking for row comments to remove.  If there are undefined rows (gaps in the physical sequence) these are different, and the comments remain after the row is deleted.

The fix is to store the rowNum from the row being deleted, and use that rather than the physical idx in the comment removal loop in XSSFSheet.removeRow(row).

I'd do this as a patch, but I have another patch pending, and would rather not revert and reapply for so small a change.  If that's what it takes, I'll do it, though.

Add this test to TestXSSFSheetShiftRows, and the attached file to test-data/spreadsheet:


    @Test
    public void testRemoveRowWithCommentAndGapAbove() throws IOException {
        final XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("removeRowWithCommentBug.xlsx");
        final XSSFSheet sheet = wb.getSheetAt(0);

        // comment exists
        CellAddress commentCellAddress = new CellAddress(3, 0);
        assertNotNull(sheet.getCellComment(commentCellAddress));
        
        assertEquals("Wrong starting # of comments",  1, sheet.getCommentsTable(true).getNumberOfComments());
        
        sheet.removeRow(sheet.getRow(commentCellAddress.getRow()));
        
        assertEquals("There should not be any comments left!",  0, sheet.getCommentsTable(true).getNumberOfComments());
    }
Comment 1 Javen O'Neal 2016-09-22 07:52:36 UTC
Thanks for catching this error, writing a unit test, and suggesting a fix!

Applied in r1761860 and r1761861. Will be included in POI 3.16 beta 1.