55using DocumentFormat . OpenXml . Packaging ;
66using DocumentFormat . OpenXml . Spreadsheet ;
77
8+
89namespace FileFormat . Cells
910{
1011 /// <summary>
@@ -15,6 +16,10 @@ public sealed class Worksheet
1516 private WorksheetPart _worksheetPart ;
1617 private SheetData _sheetData ;
1718
19+ public const double DefaultColumnWidth = 8.43 ; // Default width in character units
20+ public const double DefaultRowHeight = 15.0 ; // Default height in points
21+
22+
1823 /// <summary>
1924 /// Gets the indexer for cells within the worksheet.
2025 /// </summary>
@@ -171,6 +176,51 @@ public void SetColumnWidth(string columnName, double width)
171176 }
172177 }
173178
179+ public double GetColumnWidth ( uint columnIndex )
180+ {
181+ // Access the Columns collection
182+ var columns = _worksheetPart . Worksheet . GetFirstChild < DocumentFormat . OpenXml . Spreadsheet . Columns > ( ) ;
183+ if ( columns != null )
184+ {
185+ foreach ( var column in columns . Elements < Column > ( ) )
186+ {
187+
188+ // Explicitly cast Min and Max to uint and check for null
189+ uint min = column . Min . HasValue ? column . Min . Value : uint . MinValue ;
190+ uint max = column . Max . HasValue ? column . Max . Value : uint . MaxValue ;
191+
192+ if ( columnIndex >= min && columnIndex <= max )
193+ {
194+ // Also check if Width is set
195+ return column . Width . HasValue ? column . Width . Value : DefaultColumnWidth ;
196+ }
197+ }
198+ }
199+
200+ return DefaultColumnWidth ;
201+ }
202+
203+ public double GetRowHeight ( uint rowIndex )
204+ {
205+ // Assuming _worksheetPart is the OpenXML WorksheetPart
206+ var rows = _worksheetPart . Worksheet . GetFirstChild < DocumentFormat . OpenXml . Spreadsheet . SheetData > ( ) . Elements < Row > ( ) ;
207+
208+ foreach ( var row in rows )
209+ {
210+ // Check if this is the row we are looking for
211+ if ( row . RowIndex . Value == rowIndex )
212+ {
213+ // If Height is set, return it, otherwise return default height
214+ return row . Height . HasValue ? row . Height . Value : DefaultRowHeight ;
215+ }
216+ }
217+
218+ return DefaultRowHeight ; // Return default height if no specific height is set
219+ }
220+
221+
222+
223+
174224 /// <summary>
175225 /// Protects the worksheet with the specified password.
176226 /// </summary>
@@ -382,6 +432,59 @@ public int GetSheetIndex()
382432 // If you specifically need the index, you may need to implement a different approach.
383433 return int . Parse ( sheet . SheetId ) ;
384434 }
435+
436+ public Range GetRange ( uint startRowIndex , uint startColumnIndex , uint endRowIndex , uint endColumnIndex )
437+ {
438+ return new Range ( this , startRowIndex , startColumnIndex , endRowIndex , endColumnIndex ) ;
439+ }
440+
441+ public Range GetRange ( string startCellReference , string endCellReference )
442+ {
443+ var startCellParts = ParseCellReference ( startCellReference ) ;
444+ var endCellParts = ParseCellReference ( endCellReference ) ;
445+ return GetRange ( startCellParts . row , startCellParts . column , endCellParts . row , endCellParts . column ) ;
446+ }
447+
448+ public void AddDropdownListValidation ( string cellReference , string [ ] options )
449+ {
450+ // Convert options array into a comma-separated string
451+ string formula = string . Join ( "," , options ) ;
452+
453+ // Create the data validation object
454+ DataValidation dataValidation = new DataValidation
455+ {
456+ Type = DataValidationValues . List ,
457+ ShowDropDown = true ,
458+ ShowErrorMessage = true ,
459+ ErrorTitle = "Invalid input" ,
460+ Error = "The value entered is not in the list." ,
461+ Formula1 = new Formula1 ( "\" " + formula + "\" " ) , // The formula is enclosed in quotes
462+ SequenceOfReferences = new ListValue < StringValue > { InnerText = cellReference }
463+ } ;
464+
465+ // Add the data validation to the worksheet
466+ var dataValidations = _worksheetPart . Worksheet . GetFirstChild < DataValidations > ( ) ;
467+ if ( dataValidations == null )
468+ {
469+ dataValidations = new DataValidations ( ) ;
470+ _worksheetPart . Worksheet . AppendChild ( dataValidations ) ;
471+ }
472+
473+ dataValidations . AppendChild ( dataValidation ) ;
474+ }
475+
476+
477+ private ( uint row , uint column ) ParseCellReference ( string cellReference )
478+ {
479+ var match = Regex . Match ( cellReference , @"([A-Z]+)(\d+)" ) ;
480+ if ( ! match . Success )
481+ throw new FormatException ( "Invalid cell reference format." ) ;
482+
483+ uint row = uint . Parse ( match . Groups [ 2 ] . Value ) ;
484+ uint column = ( uint ) ColumnLetterToIndex ( match . Groups [ 1 ] . Value ) ;
485+
486+ return ( row , column ) ;
487+ }
385488 }
386489
387490 public class CellIndexer
0 commit comments