11using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
24using System . Text ;
35using System . Text . RegularExpressions ;
46using DocumentFormat . OpenXml ;
@@ -16,11 +18,11 @@ public sealed class Worksheet
1618 {
1719 private WorksheetPart _worksheetPart ;
1820 private SheetData _sheetData ;
19-
21+ private DocumentFormat . OpenXml . Spreadsheet . Cell sourceCell ;
2022 public const double DefaultColumnWidth = 8.43 ; // Default width in character units
2123 public const double DefaultRowHeight = 15.0 ; // Default height in points
2224
23-
25+ private WorkbookPart _workbookPart ;
2426 /// <summary>
2527 /// Gets the CellIndexer for the worksheet. This property provides indexed access to the cells of the worksheet.
2628 /// </summary>
@@ -41,15 +43,17 @@ public sealed class Worksheet
4143 /// <exception cref="InvalidOperationException">
4244 /// Thrown if SheetData is not found in the provided worksheet.
4345 /// </exception>
44- private Worksheet ( WorksheetPart worksheetPart , DocumentFormat . OpenXml . Spreadsheet . Worksheet worksheet )
46+ private Worksheet ( WorksheetPart worksheetPart , DocumentFormat . OpenXml . Spreadsheet . Worksheet worksheet , WorkbookPart workbookPart )
4547 {
4648 _worksheetPart = worksheetPart ?? throw new ArgumentNullException ( nameof ( worksheetPart ) ) ;
4749
4850 _sheetData = worksheet ? . Elements < SheetData > ( ) . FirstOrDefault ( )
4951 ?? throw new InvalidOperationException ( "SheetData not found in the worksheet." ) ;
52+ _workbookPart = workbookPart ?? throw new ArgumentNullException ( nameof ( workbookPart ) ) ;
5053
5154 // Initialize the Cells property
5255 this . Cells = new CellIndexer ( this ) ;
56+
5357 }
5458
5559 /// <summary>
@@ -61,9 +65,9 @@ private Worksheet(WorksheetPart worksheetPart, DocumentFormat.OpenXml.Spreadshee
6165 /// <returns>A new instance of the Worksheet class.</returns>
6266 public class WorksheetFactory
6367 {
64- public static Worksheet CreateInstance ( WorksheetPart worksheetPart , DocumentFormat . OpenXml . Spreadsheet . Worksheet worksheet )
68+ public static Worksheet CreateInstance ( WorksheetPart worksheetPart , DocumentFormat . OpenXml . Spreadsheet . Worksheet worksheet , WorkbookPart workbookPart )
6569 {
66- return new Worksheet ( worksheetPart , worksheet ) ;
70+ return new Worksheet ( worksheetPart , worksheet , workbookPart ) ;
6771 }
6872 }
6973
@@ -123,7 +127,7 @@ public string Name
123127 public Cell GetCell ( string cellReference )
124128 {
125129 // This logic used to be in your indexer
126- return new Cell ( GetOrCreateCell ( cellReference ) , _sheetData ) ;
130+ return new Cell ( GetOrCreateCell ( cellReference ) , _sheetData , _workbookPart ) ;
127131 }
128132
129133 /// <summary>
@@ -1604,8 +1608,36 @@ public void AddComment(string cellReference, Comment comment)
16041608 _worksheetPart . Worksheet . Save ( ) ;
16051609 }
16061610
1611+ public void CopyRange ( Range sourceRange , string targetStartCellReference )
1612+ {
1613+ var ( targetStartRow , targetStartColumn ) = ParseCellReference ( targetStartCellReference ) ;
1614+
1615+ uint rowOffset = targetStartRow - sourceRange . StartRowIndex ;
1616+ uint columnOffset = targetStartColumn - sourceRange . StartColumnIndex ;
1617+
1618+ for ( uint row = sourceRange . StartRowIndex ; row <= sourceRange . EndRowIndex ; row ++ )
1619+ {
1620+ for ( uint column = sourceRange . StartColumnIndex - 1 ; column < sourceRange . EndColumnIndex ; column ++ )
1621+ {
1622+ // Calculate target cell's row and column indices
1623+ uint targetRow = row + rowOffset ;
1624+ uint targetColumn = column + columnOffset ;
1625+
1626+ // Construct source and target cell references
1627+ string sourceCellRef = $ "{ IndexToColumnLetter ( ( int ) column ) } { row } ";
1628+ string targetCellRef = $ "{ IndexToColumnLetter ( ( int ) targetColumn ) } { targetRow } ";
1629+
1630+ this . Cells [ targetCellRef ] . PutValue ( this . Cells [ sourceCellRef ] . GetValue ( ) ) ;
1631+ }
1632+ }
1633+
1634+ // Save the worksheet to apply changes
1635+ _worksheetPart . Worksheet . Save ( ) ;
1636+ }
1637+
16071638
16081639
1640+
16091641 }
16101642
16111643 public class CellIndexer
@@ -1635,6 +1667,7 @@ public Cell this[string cellReference]
16351667 return _worksheet . GetCell ( cellReference ) ;
16361668 }
16371669 }
1670+
16381671 }
16391672}
16401673
0 commit comments