npoi icon indicating copy to clipboard operation
npoi copied to clipboard

CreateCellComment() on HSSF: 'An item with the same key has already been added.'

Open SergiyStoyan opened this issue 1 year ago • 2 comments

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

var drawingPatriarch = sheet.CreateDrawingPatriarch();
IClientAnchor anchor = drawingPatriarch.CreateAnchor(0,0,0,0,67,70,11,15);
drawingPatriarch.CreateCellComment(anchor);

the core throws the exception:

at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at NPOI.HSSF.Record.EscherAggregate.AddTailRecord(NoteRecord note)
   at NPOI.HSSF.UserModel.HSSFPatriarch.OnCreate(HSSFShape shape)
   at NPOI.HSSF.UserModel.HSSFPatriarch.CreateComment(HSSFAnchor anchor)

Actually it is caused by the following method in npoi-master\main\HSSF\Record\EscherAggregate.cs

        internal void AddTailRecord(NoteRecord note)
        {
            if (tailRec.ContainsKey(note.ShapeId))
                tailRec.Add(note.ShapeId, note);
            else
                tailRec[note.ShapeId] = note;
        }

Where

if (tailRec.ContainsKey(note.ShapeId))
       tailRec.Add(note.ShapeId, note);

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.

SergiyStoyan avatar Dec 14 '23 11:12 SergiyStoyan

Please share the xls file.

tonyqus avatar Dec 14 '23 18:12 tonyqus

test_FailAddingComment.xls

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.

SergiyStoyan avatar Dec 14 '23 23:12 SergiyStoyan