@@ -1566,48 +1566,73 @@ private static string IndexToColumnLetter(int index)
1566
1566
}
1567
1567
1568
1568
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
+
1569
1581
public void AddComment ( string cellReference , Comment comment )
1570
1582
{
1571
1583
// Ensure the comments part exists
1572
1584
var commentsPart = _worksheetPart . GetPartsOfType < WorksheetCommentsPart > ( ) . FirstOrDefault ( ) ;
1585
+ CommentList commentList ;
1586
+ Authors authors ;
1587
+
1573
1588
if ( commentsPart == null )
1574
1589
{
1575
1590
commentsPart = _worksheetPart . AddNewPart < WorksheetCommentsPart > ( ) ;
1576
1591
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 ( ) ;
1579
1604
}
1580
1605
1581
1606
// Ensure the author exists
1582
- var authors = commentsPart . Comments . Elements < Authors > ( ) . First ( ) ;
1583
1607
var author = authors . Elements < Author > ( ) . FirstOrDefault ( a => a . Text == comment . Author ) ;
1584
1608
if ( author == null )
1585
1609
{
1586
1610
author = new Author ( ) { Text = comment . Author } ;
1587
- authors . Append ( author ) ;
1611
+ authors . AppendChild ( author ) ; // Use AppendChild to add to the XML structure
1588
1612
}
1589
1613
uint authorId = ( uint ) authors . Elements < Author > ( ) . ToList ( ) . IndexOf ( author ) ;
1590
1614
1591
1615
// Add or update the comment
1592
- var commentList = commentsPart . Comments . Elements < CommentList > ( ) . First ( ) ;
1593
1616
var existingComment = commentList . Elements < DocumentFormat . OpenXml . Spreadsheet . Comment > ( ) . FirstOrDefault ( c => c . Reference == cellReference ) ;
1594
1617
if ( existingComment == null )
1595
1618
{
1596
1619
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
1599
1622
}
1600
1623
else
1601
1624
{
1602
1625
// 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 ) ;
1604
1627
}
1605
1628
1606
1629
// Save the changes
1607
1630
commentsPart . Comments . Save ( ) ;
1608
1631
_worksheetPart . Worksheet . Save ( ) ;
1609
1632
}
1610
1633
1634
+
1635
+
1611
1636
public void CopyRange ( Range sourceRange , string targetStartCellReference )
1612
1637
{
1613
1638
var ( targetStartRow , targetStartColumn ) = ParseCellReference ( targetStartCellReference ) ;
0 commit comments