Skip to content

Commit 8c897eb

Browse files
authored
Merge pull request #14 from fahadadeel/main
Add Intuitive Freeze Panes Helper Methods
2 parents 46c5dcd + 9d0dc0e commit 8c897eb

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System;
2+
3+
namespace Openize.Cells
4+
{
5+
/// <summary>
6+
/// Provides extension methods for easy management of freeze panes in Excel worksheets.
7+
/// </summary>
8+
public static class WorksheetFreezePaneExtensions
9+
{
10+
/// <summary>
11+
/// Freezes the top row of the worksheet.
12+
/// </summary>
13+
/// <param name="worksheet">The worksheet.</param>
14+
/// <returns>The worksheet for method chaining.</returns>
15+
/// <exception cref="ArgumentNullException">Thrown when worksheet is null.</exception>
16+
public static Worksheet FreezeTopRow(this Worksheet worksheet)
17+
{
18+
if (worksheet == null)
19+
throw new ArgumentNullException(nameof(worksheet));
20+
21+
worksheet.FreezePane(1, 0);
22+
return worksheet;
23+
}
24+
25+
/// <summary>
26+
/// Freezes the first column of the worksheet.
27+
/// </summary>
28+
/// <param name="worksheet">The worksheet.</param>
29+
/// <returns>The worksheet for method chaining.</returns>
30+
/// <exception cref="ArgumentNullException">Thrown when worksheet is null.</exception>
31+
public static Worksheet FreezeFirstColumn(this Worksheet worksheet)
32+
{
33+
if (worksheet == null)
34+
throw new ArgumentNullException(nameof(worksheet));
35+
36+
worksheet.FreezePane(0, 1);
37+
return worksheet;
38+
}
39+
40+
/// <summary>
41+
/// Freezes both the top row and first column of the worksheet.
42+
/// </summary>
43+
/// <param name="worksheet">The worksheet.</param>
44+
/// <returns>The worksheet for method chaining.</returns>
45+
/// <exception cref="ArgumentNullException">Thrown when worksheet is null.</exception>
46+
public static Worksheet FreezeTopRowAndFirstColumn(this Worksheet worksheet)
47+
{
48+
if (worksheet == null)
49+
throw new ArgumentNullException(nameof(worksheet));
50+
51+
worksheet.FreezePane(1, 1);
52+
return worksheet;
53+
}
54+
55+
/// <summary>
56+
/// Freezes the specified number of top rows.
57+
/// </summary>
58+
/// <param name="worksheet">The worksheet.</param>
59+
/// <param name="rowCount">The number of rows to freeze.</param>
60+
/// <returns>The worksheet for method chaining.</returns>
61+
/// <exception cref="ArgumentNullException">Thrown when worksheet is null.</exception>
62+
/// <exception cref="ArgumentOutOfRangeException">Thrown when rowCount is negative.</exception>
63+
public static Worksheet FreezeTopRows(this Worksheet worksheet, int rowCount)
64+
{
65+
if (worksheet == null)
66+
throw new ArgumentNullException(nameof(worksheet));
67+
68+
if (rowCount < 0)
69+
throw new ArgumentOutOfRangeException(nameof(rowCount), "Row count cannot be negative.");
70+
71+
worksheet.FreezePane(rowCount, 0);
72+
return worksheet;
73+
}
74+
75+
/// <summary>
76+
/// Freezes the specified number of leftmost columns.
77+
/// </summary>
78+
/// <param name="worksheet">The worksheet.</param>
79+
/// <param name="columnCount">The number of columns to freeze.</param>
80+
/// <returns>The worksheet for method chaining.</returns>
81+
/// <exception cref="ArgumentNullException">Thrown when worksheet is null.</exception>
82+
/// <exception cref="ArgumentOutOfRangeException">Thrown when columnCount is negative.</exception>
83+
public static Worksheet FreezeLeftColumns(this Worksheet worksheet, int columnCount)
84+
{
85+
if (worksheet == null)
86+
throw new ArgumentNullException(nameof(worksheet));
87+
88+
if (columnCount < 0)
89+
throw new ArgumentOutOfRangeException(nameof(columnCount), "Column count cannot be negative.");
90+
91+
worksheet.FreezePane(0, columnCount);
92+
return worksheet;
93+
}
94+
95+
/// <summary>
96+
/// Freezes the panes at a specified cell position, freezing all rows above and all columns to the left of the cell.
97+
/// </summary>
98+
/// <param name="worksheet">The worksheet.</param>
99+
/// <param name="cellReference">The cell reference (e.g., "B3") indicating the position for the freeze pane.</param>
100+
/// <returns>The worksheet for method chaining.</returns>
101+
/// <exception cref="ArgumentNullException">Thrown when worksheet or cellReference is null.</exception>
102+
/// <exception cref="FormatException">Thrown when cellReference format is invalid.</exception>
103+
public static Worksheet FreezePanesAt(this Worksheet worksheet, string cellReference)
104+
{
105+
if (worksheet == null)
106+
throw new ArgumentNullException(nameof(worksheet));
107+
108+
if (string.IsNullOrEmpty(cellReference))
109+
throw new ArgumentNullException(nameof(cellReference));
110+
111+
// Parse cell reference to get row and column
112+
var match = System.Text.RegularExpressions.Regex.Match(cellReference, @"([A-Z]+)(\d+)");
113+
if (!match.Success)
114+
throw new FormatException($"Invalid cell reference format: {cellReference}");
115+
116+
string columnPart = match.Groups[1].Value;
117+
int rowNumber = int.Parse(match.Groups[2].Value);
118+
119+
// Convert column letter to column number
120+
int columnNumber = 0;
121+
foreach (char c in columnPart)
122+
{
123+
columnNumber = columnNumber * 26 + (c - 'A' + 1);
124+
}
125+
126+
// Freeze panes at the specified position
127+
worksheet.FreezePane(rowNumber - 1, columnNumber);
128+
return worksheet;
129+
}
130+
131+
/// <summary>
132+
/// Removes all freeze panes from the worksheet.
133+
/// </summary>
134+
/// <param name="worksheet">The worksheet.</param>
135+
/// <returns>The worksheet for method chaining.</returns>
136+
/// <exception cref="ArgumentNullException">Thrown when worksheet is null.</exception>
137+
public static Worksheet UnfreezePanes(this Worksheet worksheet)
138+
{
139+
if (worksheet == null)
140+
throw new ArgumentNullException(nameof(worksheet));
141+
142+
worksheet.FreezePane(0, 0);
143+
return worksheet;
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)