Skip to content

Commit

Permalink
Merge pull request #1136 from Bykiev/FixBug1000
Browse files Browse the repository at this point in the history
Fix removing cell comments in SXSSF
  • Loading branch information
tonyqus authored Jul 29, 2023
2 parents cc5238b + ccf4f49 commit 3665a76
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ooxml/XSSF/Streaming/SXSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ public ICell CopyCellTo(int targetIndex)

public void RemoveCellComment()
{
IComment comment = this.CellComment;
if (comment != null)
{
CellAddress ref1 = new CellAddress(RowIndex, ColumnIndex);
XSSFSheet sh = ((SXSSFSheet)Sheet)._sh;
sh.GetCommentsTable(false).RemoveComment(ref1);
sh.GetVMLDrawing(false).RemoveCommentShape(RowIndex, ColumnIndex);
}

RemoveProperty(Property.COMMENT);
}

Expand Down
103 changes: 103 additions & 0 deletions testcases/ooxml/XSSF/UserModel/TestXSSFComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,109 @@ public void Bug57838DeleteRowsWthCommentsBug()
wb.Close();
}

[Test]
public void TestRemoveXSSFCellComment()
{
IWorkbook wb = new XSSFWorkbook();
try
{
ISheet sheet = wb.CreateSheet();
IRow row = sheet.CreateRow(1);
ICell cell = row.CreateCell(0);
cell.SetCellValue("test");

IDrawing drawing = sheet.CreateDrawingPatriarch();
ICreationHelper factory = wb.GetCreationHelper();
// When the comment box is visible, have it show in a 1x3 space
IClientAnchor anchor = factory.CreateClientAnchor();
anchor.Col1 = cell.ColumnIndex;
anchor.Col2 = cell.ColumnIndex + 1;
anchor.Row1 = row.RowNum;
anchor.Row2 = row.RowNum + 3;
// Create the comment and set the text+author
IComment comment = drawing.CreateCellComment(anchor);
IRichTextString str = factory.CreateRichTextString("Hello, World!");
comment.String = str;
comment.Author = "Apache POI";

cell.CellComment = comment;

var exCellComment = sheet.GetCellComment(new CellAddress(1, 0));
Assert.IsNotNull(exCellComment);
Assert.IsTrue(exCellComment.String.String.Equals("Hello, World!"));
Assert.IsTrue(exCellComment.Author.Equals("Apache POI"));

cell.RemoveCellComment();
exCellComment = sheet.GetCellComment(new CellAddress(1, 0));
Assert.IsNull(exCellComment);

IComment newComment = drawing.CreateCellComment(anchor);
newComment.String = str;
newComment.Author = "Apache POI";
cell.CellComment = newComment;

exCellComment = sheet.GetCellComment(new CellAddress(1, 0));
Assert.NotNull(exCellComment);
Assert.IsTrue(exCellComment.String.String.Equals("Hello, World!"));
Assert.IsTrue(exCellComment.Author.Equals("Apache POI"));
}
finally
{
wb.Close();
}
}

[Test]
public void TestRemoveSXSSFCellComment()
{
IWorkbook wb = new SXSSFWorkbook();
try
{
ISheet sheet = wb.CreateSheet();
IRow row = sheet.CreateRow(1);
ICell cell = row.CreateCell(0);
cell.SetCellValue("test");

IDrawing drawing = sheet.CreateDrawingPatriarch();
ICreationHelper factory = wb.GetCreationHelper();
// When the comment box is visible, have it show in a 1x3 space
IClientAnchor anchor = factory.CreateClientAnchor();
anchor.Col1 = cell.ColumnIndex;
anchor.Col2 = cell.ColumnIndex + 1;
anchor.Row1 = row.RowNum;
anchor.Row2 = row.RowNum + 3;
// Create the comment and set the text+author
IComment comment = drawing.CreateCellComment(anchor);
IRichTextString str = factory.CreateRichTextString("Hello, World!");
comment.String = str;
comment.Author = "Apache POI";

cell.CellComment = comment;

var exCellComment = sheet.GetCellComment(new CellAddress(1, 0));
Assert.IsNotNull(exCellComment);
Assert.IsTrue(exCellComment.String.String.Equals("Hello, World!"));
Assert.IsTrue(exCellComment.Author.Equals("Apache POI"));

cell.RemoveCellComment();
exCellComment = sheet.GetCellComment(new CellAddress(1, 0));
Assert.IsNull(exCellComment);

IComment newComment = drawing.CreateCellComment(anchor);
newComment.String = str;
newComment.Author = "Apache POI";
cell.CellComment = newComment;

exCellComment = sheet.GetCellComment(new CellAddress(1, 0));
Assert.NotNull(exCellComment);
Assert.IsTrue(exCellComment.String.String.Equals("Hello, World!"));
Assert.IsTrue(exCellComment.Author.Equals("Apache POI"));
}
finally
{
wb.Close();
}
}
}

}

0 comments on commit 3665a76

Please sign in to comment.