Closed SergiyStoyan closed 6 months ago
Please share the xls file.
On the attached file the code following below throws the exception 'An item with the same key has already been added.'
string file = @"c:\test_FailAddingComment.xls";
using (System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
fs.Position = 0;
var workbook = new HSSFWorkbook(fs);
ISheet sheet = workbook.GetSheetAt(0);
var drawingPatriarch2 = sheet.CreateDrawingPatriarch();
IClientAnchor anchor2 = drawingPatriarch2.CreateAnchor(0, 0, 0, 0, 55, 71, 58, 75);
IComment comment2 = drawingPatriarch2.CreateCellComment(anchor2);
}
The initial file was created in Excel and then edited with NPOI without issues. It was a working file. For uploading here it was cleaned from data which does not affect the issue.
NPOI Version
2.6.2
File Type
XLS
Upload the Excel File
There is no need for a test file because the error is directly seen in the NPOI code.
Issue Description
Sometimes on calling
the core throws the exception:
Actually it is caused by the following method in npoi-master\main\HSSF\Record\EscherAggregate.cs
Where
will ALWAYS throw an exception if the key exists. It looks like a typo because if the goal was to prohibit resetting the key then why was it not done through an explicit exception instead of calling Add() as if the goal is to reset?
For some reason, sometimes, despite the anchor being created new right before calling drawingPatriarch.CreateCellComment(anchor), tailRec happens to contain the ShapeId and thus weirdly tries adding it instead of resetting. But the main bug is probably within HSSFPatriarch.NewShapeId() which assigns an occupied ID to a new Shape.