Skip to content

Commit 91fe429

Browse files
author
Fahad Adeel
committed
Implemented new features and enhancements in the Range class
- Added ClearRange and MergeCells methods to the Range class, enabling the clearing of cell contents and merging of cells within specified ranges. - Implemented ColumnCount and RowCount properties in the Range class to provide easy access to the number of columns and rows in a given range. - Developed the AddDropdownListValidation method in the Worksheet class for adding dropdown list data validation to cells. - Introduced ColumnWidth and RowHeight methods to retrieve the width of a column and the height of a row, enhancing cell dimension handling capabilities. These enhancements aim to improve the flexibility and utility of the Range class, offering more advanced functionalities for manipulating and validating spreadsheet data.
1 parent 695b1b0 commit 91fe429

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

FileFormat.Cells/Range.cs

+55
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ public class Range
1515
public uint EndRowIndex { get; }
1616
public uint EndColumnIndex { get; }
1717

18+
// Returns the count of columns in the range
19+
public uint ColumnCount
20+
{
21+
get { return EndColumnIndex - StartColumnIndex + 1; }
22+
}
23+
24+
// Returns the count of rows in the range
25+
public uint RowCount
26+
{
27+
get { return EndRowIndex - StartRowIndex + 1; }
28+
}
29+
30+
1831
public Range(Worksheet worksheet, uint startRowIndex, uint startColumnIndex, uint endRowIndex, uint endColumnIndex)
1932
{
2033
_worksheet = worksheet ?? throw new ArgumentNullException(nameof(worksheet));
@@ -37,6 +50,48 @@ public void SetValue(string value)
3750
}
3851
}
3952

53+
public void ClearCells()
54+
{
55+
for (uint row = StartRowIndex; row <= EndRowIndex; row++)
56+
{
57+
for (uint column = StartColumnIndex; column <= EndColumnIndex; column++)
58+
{
59+
var cellReference = $"{ColumnIndexToLetter(column)}{row}";
60+
var cell = _worksheet.GetCell(cellReference);
61+
if (cell != null)
62+
{
63+
cell.PutValue(string.Empty); // Clearing the value
64+
cell.ApplyStyle(0); // Resetting the style if needed
65+
}
66+
}
67+
}
68+
}
69+
70+
public void MergeCells()
71+
{
72+
string startCellReference = $"{ColumnIndexToLetter(StartColumnIndex)}{StartRowIndex}";
73+
string endCellReference = $"{ColumnIndexToLetter(EndColumnIndex)}{EndRowIndex}";
74+
75+
_worksheet.MergeCells(startCellReference, endCellReference);
76+
}
77+
78+
79+
public void AddDropdownListValidation(string[] options)
80+
{
81+
82+
83+
for (uint row = StartRowIndex; row <= EndRowIndex; row++)
84+
{
85+
for (uint column = StartColumnIndex; column <= EndColumnIndex; column++)
86+
{
87+
var cellReference = $"{ColumnIndexToLetter(column)}{row}";
88+
_worksheet.AddDropdownListValidation(cellReference, options);
89+
}
90+
}
91+
}
92+
93+
94+
4095
private static string ColumnIndexToLetter(uint columnIndex)
4196
{
4297
string columnLetter = string.Empty;

FileFormat.Cells/Worksheet.cs

+79
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using DocumentFormat.OpenXml.Packaging;
66
using DocumentFormat.OpenXml.Spreadsheet;
77

8+
89
namespace 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>
@@ -395,6 +445,35 @@ public Range GetRange(string startCellReference, string endCellReference)
395445
return GetRange(startCellParts.row, startCellParts.column, endCellParts.row, endCellParts.column);
396446
}
397447

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+
398477
private (uint row, uint column) ParseCellReference(string cellReference)
399478
{
400479
var match = Regex.Match(cellReference, @"([A-Z]+)(\d+)");

0 commit comments

Comments
 (0)