RhSolutions-AddIn/RhSolutions.ExcelExtensions/Column.cs
2023-04-06 08:29:39 +03:00

66 lines
1.5 KiB
C#

namespace RhSolutions.ExcelExtensions;
public sealed class Column
{
public Table ParentTable { get; }
public string Header
{
get => _range.Cells[1, 1].Value2.ToString() ?? String.Empty;
}
public int Index
{
get => _range.Column - ParentTable.Range.Column;
}
public int Length
{
get => _range.Rows.Count;
}
private Cell[] _cells;
private readonly Range _range;
public Column(Range range, Table table)
{
_cells = new Cell[range.Rows.Count];
_range = range;
ParentTable = table ??
throw new ArgumentNullException("table");
}
public Cell this[int index]
{
get
{
if (_cells[index] == null)
{
_cells[index] = new Cell(_range.Cells[index + 1, 1], ParentTable);
return _cells[index];
}
else
{
return _cells[index];
}
}
set
{
if (_cells[index] == null)
{
_cells[index] = new Cell(_range.Cells[index + 1, 1], ParentTable);
_cells[index].Value = value;
}
else
{
_cells[index].Value = value;
}
}
}
public Column AddLeft()
{
_range.EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
return ParentTable.Columns[this.Index - 1];
}
}