Skip to content

Commit 695b1b0

Browse files
author
Fahad Adeel
committed
Range Class implemented.
1 parent d1fd5a6 commit 695b1b0

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

FileFormat.Cells/Range.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace FileFormat.Cells
8+
{
9+
public class Range
10+
{
11+
private readonly Worksheet _worksheet;
12+
13+
public uint StartRowIndex { get; }
14+
public uint StartColumnIndex { get; }
15+
public uint EndRowIndex { get; }
16+
public uint EndColumnIndex { get; }
17+
18+
public Range(Worksheet worksheet, uint startRowIndex, uint startColumnIndex, uint endRowIndex, uint endColumnIndex)
19+
{
20+
_worksheet = worksheet ?? throw new ArgumentNullException(nameof(worksheet));
21+
StartRowIndex = startRowIndex;
22+
StartColumnIndex = startColumnIndex;
23+
EndRowIndex = endRowIndex;
24+
EndColumnIndex = endColumnIndex;
25+
}
26+
27+
public void SetValue(string value)
28+
{
29+
for (uint row = StartRowIndex; row <= EndRowIndex; row++)
30+
{
31+
for (uint column = StartColumnIndex; column <= EndColumnIndex; column++)
32+
{
33+
var cellReference = $"{ColumnIndexToLetter(column)}{row}";
34+
var cell = _worksheet.GetCell(cellReference);
35+
cell.PutValue(value);
36+
}
37+
}
38+
}
39+
40+
private static string ColumnIndexToLetter(uint columnIndex)
41+
{
42+
string columnLetter = string.Empty;
43+
while (columnIndex > 0)
44+
{
45+
columnIndex--;
46+
columnLetter = (char)('A' + columnIndex % 26) + columnLetter;
47+
columnIndex /= 26;
48+
}
49+
return columnLetter;
50+
}
51+
}
52+
53+
54+
}

FileFormat.Cells/Worksheet.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,30 @@ public int GetSheetIndex()
382382
// If you specifically need the index, you may need to implement a different approach.
383383
return int.Parse(sheet.SheetId);
384384
}
385+
386+
public Range GetRange(uint startRowIndex, uint startColumnIndex, uint endRowIndex, uint endColumnIndex)
387+
{
388+
return new Range(this, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex);
389+
}
390+
391+
public Range GetRange(string startCellReference, string endCellReference)
392+
{
393+
var startCellParts = ParseCellReference(startCellReference);
394+
var endCellParts = ParseCellReference(endCellReference);
395+
return GetRange(startCellParts.row, startCellParts.column, endCellParts.row, endCellParts.column);
396+
}
397+
398+
private (uint row, uint column) ParseCellReference(string cellReference)
399+
{
400+
var match = Regex.Match(cellReference, @"([A-Z]+)(\d+)");
401+
if (!match.Success)
402+
throw new FormatException("Invalid cell reference format.");
403+
404+
uint row = uint.Parse(match.Groups[2].Value);
405+
uint column = (uint)ColumnLetterToIndex(match.Groups[1].Value);
406+
407+
return (row, column);
408+
}
385409
}
386410

387411
public class CellIndexer

0 commit comments

Comments
 (0)