Skip to content

Commit b1572bf

Browse files
author
Fahad Adeel
committed
Add AddComment method to handle cell comments in Worksheets
Implement the AddComment method in the Worksheet class to add or update comments in specific cells. This method ensures compatibility with existing comments, managing the worksheet's comment part and authors list dynamically. It supports adding new comments if none exist or updating existing ones, thereby enhancing the functionality of our Excel handling utility. This update is crucial for features that rely on providing feedback or notes directly within the Excel documents, such as automated report generation or data validation processes. - Ensure the worksheet comments part exists before adding or updating comments. - Manage authors list to avoid duplicates and use existing indices where possible.
1 parent e5ea08c commit b1572bf

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

.DS_Store

4 KB
Binary file not shown.

FileFormat.Cells/Worksheet.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,48 +1566,73 @@ private static string IndexToColumnLetter(int index)
15661566
}
15671567

15681568

1569+
/// <summary>
1570+
/// Adds or updates a comment in a specified cell within the worksheet. If the cell already has a comment,
1571+
/// it updates the existing comment text. If there is no comment, it creates a new one.
1572+
/// </summary>
1573+
/// <param name="cellReference">The cell reference where the comment should be added, e.g., "A1".</param>
1574+
/// <param name="comment">The comment object containing the author and the text of the comment.</param>
1575+
/// <remarks>
1576+
/// This method ensures that the worksheet comments part exists before adding or updating a comment.
1577+
/// It also manages the authors list to ensure that each author is only added once and reuses the existing author index if available.
1578+
/// Usage of this method requires that the workbook and worksheet are properly initialized and that the worksheet part is correctly associated.
1579+
/// </remarks>
1580+
15691581
public void AddComment(string cellReference, Comment comment)
15701582
{
15711583
// Ensure the comments part exists
15721584
var commentsPart = _worksheetPart.GetPartsOfType<WorksheetCommentsPart>().FirstOrDefault();
1585+
CommentList commentList;
1586+
Authors authors;
1587+
15731588
if (commentsPart == null)
15741589
{
15751590
commentsPart = _worksheetPart.AddNewPart<WorksheetCommentsPart>();
15761591
commentsPart.Comments = new Comments();
1577-
commentsPart.Comments.AppendChild(new CommentList());
1578-
commentsPart.Comments.AppendChild(new Authors());
1592+
1593+
// Initialize new CommentList and Authors only if a new comments part is created
1594+
commentList = new CommentList();
1595+
authors = new Authors();
1596+
commentsPart.Comments.AppendChild(commentList);
1597+
commentsPart.Comments.AppendChild(authors);
1598+
}
1599+
else
1600+
{
1601+
// Retrieve existing CommentList and Authors
1602+
commentList = commentsPart.Comments.Elements<CommentList>().First();
1603+
authors = commentsPart.Comments.Elements<Authors>().First();
15791604
}
15801605

15811606
// Ensure the author exists
1582-
var authors = commentsPart.Comments.Elements<Authors>().First();
15831607
var author = authors.Elements<Author>().FirstOrDefault(a => a.Text == comment.Author);
15841608
if (author == null)
15851609
{
15861610
author = new Author() { Text = comment.Author };
1587-
authors.Append(author);
1611+
authors.AppendChild(author); // Use AppendChild to add to the XML structure
15881612
}
15891613
uint authorId = (uint)authors.Elements<Author>().ToList().IndexOf(author);
15901614

15911615
// Add or update the comment
1592-
var commentList = commentsPart.Comments.Elements<CommentList>().First();
15931616
var existingComment = commentList.Elements<DocumentFormat.OpenXml.Spreadsheet.Comment>().FirstOrDefault(c => c.Reference == cellReference);
15941617
if (existingComment == null)
15951618
{
15961619
var newComment = new DocumentFormat.OpenXml.Spreadsheet.Comment() { Reference = cellReference, AuthorId = authorId };
1597-
newComment.Append(new CommentText(new DocumentFormat.OpenXml.Spreadsheet.Text(comment.Text)));
1598-
commentList.Append(newComment);
1620+
newComment.AppendChild(new CommentText(new DocumentFormat.OpenXml.Spreadsheet.Text(comment.Text)));
1621+
commentList.AppendChild(newComment); // Ensure appending to commentList
15991622
}
16001623
else
16011624
{
16021625
// Update the existing comment's text
1603-
existingComment.CommentText = new CommentText(new DocumentFormat.OpenXml.Spreadsheet.Text(comment.Text));
1626+
existingComment.Elements<CommentText>().First().Text = new DocumentFormat.OpenXml.Spreadsheet.Text(comment.Text);
16041627
}
16051628

16061629
// Save the changes
16071630
commentsPart.Comments.Save();
16081631
_worksheetPart.Worksheet.Save();
16091632
}
16101633

1634+
1635+
16111636
public void CopyRange(Range sourceRange, string targetStartCellReference)
16121637
{
16131638
var (targetStartRow, targetStartColumn) = ParseCellReference(targetStartCellReference);

0 commit comments

Comments
 (0)