5
5
using DocumentFormat . OpenXml . Packaging ;
6
6
using DocumentFormat . OpenXml . Spreadsheet ;
7
7
8
+
8
9
namespace FileFormat . Cells
9
10
{
10
11
/// <summary>
@@ -15,6 +16,10 @@ public sealed class Worksheet
15
16
private WorksheetPart _worksheetPart ;
16
17
private SheetData _sheetData ;
17
18
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
+
18
23
/// <summary>
19
24
/// Gets the indexer for cells within the worksheet.
20
25
/// </summary>
@@ -171,6 +176,51 @@ public void SetColumnWidth(string columnName, double width)
171
176
}
172
177
}
173
178
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
+
174
224
/// <summary>
175
225
/// Protects the worksheet with the specified password.
176
226
/// </summary>
@@ -382,6 +432,59 @@ public int GetSheetIndex()
382
432
// If you specifically need the index, you may need to implement a different approach.
383
433
return int . Parse ( sheet . SheetId ) ;
384
434
}
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
+ }
385
488
}
386
489
387
490
public class CellIndexer
0 commit comments