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

65 lines
1.6 KiB
C#

namespace RhSolutions.ExcelExtensions;
public class Table
{
public Range Range { get; protected set; }
public Table ParentTable { get; protected set; }
public Rows Rows { get; }
public Columns Columns { get; }
private Dictionary<string, Column> _columnsByHeader;
public Table(Range range)
{
Range = range;
ParentTable = this;
Rows = new Rows(this);
Columns = new Columns(this);
_columnsByHeader = new();
foreach(var column in Columns)
{
if (_columnsByHeader.ContainsKey(column.Header))
{
throw new ArgumentException($"Заголовок столбца {column.Header} не уникален");
}
else
{
_columnsByHeader.Add(column.Header, column);
}
}
}
public Column ColumnByHeader(string header)
{
return _columnsByHeader[header];
}
public Cell this[int row, int column]
{
get => this.Rows[row][column];
}
public IEnumerable<Cell> Search(object item)
{
Range firstFound = Range.Find(item);
if (firstFound == null)
{
yield break;
}
Range nextFound = firstFound;
while (true)
{
yield return this[nextFound.Row - ParentTable.Range.Row, nextFound.Column - ParentTable.Range.Column];
nextFound = Range.FindNext(nextFound);
if (nextFound.Row == firstFound.Row
&& nextFound.Column == firstFound.Column)
{
yield break;
}
}
}
}