RhSolutions-AddIn/RhSolutions.ExcelExtensions/Column.cs

66 lines
1.5 KiB
C#
Raw Normal View History

2023-04-06 08:29:39 +03:00
namespace RhSolutions.ExcelExtensions;
2023-03-31 15:27:31 +03:00
2023-04-06 08:29:39 +03:00
public sealed class Column
2023-03-31 15:27:31 +03:00
{
2023-04-06 08:29:39 +03:00
public Table ParentTable { get; }
2023-03-31 15:27:31 +03:00
public string Header
{
2023-04-06 08:29:39 +03:00
get => _range.Cells[1, 1].Value2.ToString() ?? String.Empty;
2023-03-31 15:27:31 +03:00
}
public int Index
{
2023-04-06 08:29:39 +03:00
get => _range.Column - ParentTable.Range.Column;
2023-03-31 15:27:31 +03:00
}
public int Length
{
2023-04-06 08:29:39 +03:00
get => _range.Rows.Count;
2023-03-31 15:27:31 +03:00
}
2023-04-06 08:29:39 +03:00
private Cell[] _cells;
private readonly Range _range;
2023-03-31 15:27:31 +03:00
2023-04-06 08:29:39 +03:00
public Column(Range range, Table table)
2023-03-31 15:27:31 +03:00
{
2023-04-06 08:29:39 +03:00
_cells = new Cell[range.Rows.Count];
_range = range;
ParentTable = table ??
throw new ArgumentNullException("table");
2023-03-31 15:27:31 +03:00
}
2023-04-06 08:29:39 +03:00
public Cell this[int index]
2023-03-31 15:27:31 +03:00
{
2023-04-06 08:29:39 +03:00
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;
}
}
2023-03-31 15:27:31 +03:00
}
2023-04-01 15:58:42 +03:00
public Column AddLeft()
2023-03-31 15:27:31 +03:00
{
2023-04-06 08:29:39 +03:00
_range.EntireColumn
2023-03-31 15:27:31 +03:00
.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
return ParentTable.Columns[this.Index - 1];
}
}