Bug 59393 - IllegalArgumentException when set comment for Multiple cell included cell A1
Summary: IllegalArgumentException when set comment for Multiple cell included cell A1
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.14-FINAL
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-28 16:03 UTC by stone
Modified: 2018-10-26 06:22 UTC (History)
0 users



Attachments
Code to reappear the bug. (3.89 KB, text/plain)
2016-05-05 04:33 UTC, stone
Details

Note You need to log in before you can comment on or make changes to this bug.
Description stone 2016-04-28 16:03:05 UTC
when set comment for Multiple cell included cell "A1", always got a Exception:
java.lang.IllegalArgumentException: 
Multiple cell comments in one cell are not allowed, cell: A1

if don't set comment for cell "A1", Exception will not happen.

if only set comment for cell "A1", Exception will not happen.
Comment 1 Dominik Stadler 2016-04-28 19:35:24 UTC
Can you share some of your code, ideally as self-sufficient unit-test that allows others to reproduce the problem? This will make it much easier for other to take a look.

If you are able we also gladly accept patch-submissions if you can figure out where it fails in Apache POI.
Comment 2 stone 2016-05-05 04:33:08 UTC
Created attachment 33825 [details]
Code to reappear the bug.
Comment 3 stone 2016-05-05 05:11:27 UTC
(In reply to Dominik Stadler from comment #1)
> Can you share some of your code, ideally as self-sufficient unit-test that
> allows others to reproduce the problem? This will make it much easier for
> other to take a look.
> 
> If you are able we also gladly accept patch-submissions if you can figure
> out where it fails in Apache POI.

I made a test cases as (attachment 33825 [details] ) to reproduce the problem.
Comment 4 Javen O'Neal 2016-05-09 03:04:10 UTC
This would require some restructuring of the way comments are added to a cell.

Right now the order to add a comment is: [1]
1. Create an anchor for the comment (that is, the box that displays the comment with a yellow-background).
2. Create a comment on the sheet drawing patriarch and set the anchor from step 1
3. Add author and comment string to comment object
4. Assign the comment to a cell in the sheet (prior to this the comment is parentless).

The problem occurs in step 2, since in order to create the comment, the comment cell address is needed. The cell address is temporarily set as CellAddress(anchor.getRow1(), anchor.getCol1()) until it's set again by step 4 above. [2]:
> return XSSFComment(comments, comments.newComment(ref), vmlShape);

The "Multiple cell comments in one cell are not allowed" error that you saw was POI proactively avoiding a corrupt workbook. Though adding a cell comment to the drawing without adding it to a cell also produces a corrupt workbook. Any rewrite will need to address these issues, while considering backwards compatibility, and hopefully make adding a cell comment easier (such as Sheet.addCellComment(CellAddress address, String comment[, String author[, ClientAnchor anchor]])). I'm open to suggestions; patches greatly appreciated.

In the mean time, to fix your problem, you should make row1 and col1 of the anchor match the address of the cell that will hold the comment before you create the comment:

    anchor.setRow1(0);
    anchor.setCol1(0);
    Comment comment1 = drawing.createCellComment(anchor);
    RichTextString richTextString1 = helper.createRichTextString("comment1");
    comment1.setString(richTextString1);
    cell1.setCellComment(comment1);

    anchor.setRow1(0);
    anchor.setCol1(1);
    Comment comment2 = drawing.createCellComment(anchor);
    RichTextString richTextString2 = helper.createRichTextString("comment2");
    comment2.setString(richTextString2);
    cell2.setCellComment(comment2);

    anchor.setRow1(0);
    anchor.setCol1(2);
    Comment comment3 = drawing.createCellComment(anchor);
    RichTextString richTextString3 = helper.createRichTextString("comment3");
    comment3.setString(richTextString3);
    cell3.setCellComment(comment3);

[1] https://poi.apache.org/spreadsheet/quick-guide.html#CellComments
[2] https://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java?revision=1731980&view=markup#l302
Comment 5 Javen O'Neal 2016-05-09 03:49:40 UTC
Added disabled unit test in r1742868.