using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace FastExcel
{
///
/// Represents a range of cells
///
public class CellRange
{
internal string SheetName;
///
/// Column Range Start
///
public string ColumnStart { get; set; }
///
/// Column Range End
///
public string ColumnEnd { get; set; }
///
/// Row Range Start
///
public int RowStart { get; set; }
///
/// Row Range End
///
public int? RowEnd { get; set; }
///
/// Defines a range of cells using a reference string
///
/// Reference string i.e. Sheet1!$A$1
/// Thrown when reference is invalid or not supported
internal CellRange(string reference)
{
if (!Regex.IsMatch(reference, @"^[^\[\]\*\/\\\?\:]{1,31}\!\$[A-z]{1,4}:?\$"))
{
throw new ArgumentException("CellRange reference argument is invalid or not supported.");
}
string[] splitReference = reference.Split('!');
SheetName = splitReference[0];
string range = splitReference[1];
// Default value
RowStart = 1;
if (range.Contains(":"))
{
string[] splitRange = range.Split(':');
ColumnStart = splitRange[0].Split('$')[1];
ColumnEnd = splitRange[1].Split('$')[1];
if (splitRange[0].Count(c => c == '$') > 1)
{
RowStart = Convert.ToInt32(splitRange[0].Split('$')[2]);
RowEnd = Convert.ToInt32(splitRange[1].Split('$')[2]);
}
}
else
{
ColumnStart = ColumnEnd = range.Split('$')[1];
RowEnd = RowStart = Convert.ToInt32(range.Split('$')[2]);
}
}
///
/// Defines a cell range using varibles
///
/// Column Letter start
/// Column Letter end
/// First row number
/// last row number
public CellRange(string columnStart, string columnEnd, int rowStart = 1, int? rowEnd = null)
{
ColumnStart = columnStart;
ColumnEnd = columnEnd;
RowStart = rowStart;
RowEnd = rowEnd;
}
}
}