2023-04-01 15:58:42 +03:00
|
|
|
|
namespace RhSolutions.ExcelExtensions;
|
2023-03-31 15:27:31 +03:00
|
|
|
|
|
2023-04-01 15:58:42 +03:00
|
|
|
|
public class Table
|
2023-03-31 15:27:31 +03:00
|
|
|
|
{
|
|
|
|
|
public Range Range { get; protected set; }
|
2023-04-01 15:58:42 +03:00
|
|
|
|
public Table ParentTable { get; protected set; }
|
|
|
|
|
public Rows Rows { get; }
|
|
|
|
|
public Columns Columns { get; }
|
2023-04-06 08:29:39 +03:00
|
|
|
|
private Dictionary<string, Column> _columnsByHeader;
|
2023-03-31 15:27:31 +03:00
|
|
|
|
|
2023-04-01 15:58:42 +03:00
|
|
|
|
public Table(Range range)
|
2023-03-31 15:27:31 +03:00
|
|
|
|
{
|
|
|
|
|
Range = range;
|
2023-04-06 08:29:39 +03:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-31 15:27:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 08:29:39 +03:00
|
|
|
|
public Column ColumnByHeader(string header)
|
2023-03-31 15:27:31 +03:00
|
|
|
|
{
|
2023-04-06 08:29:39 +03:00
|
|
|
|
return _columnsByHeader[header];
|
2023-03-31 15:27:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 08:29:39 +03:00
|
|
|
|
public Cell this[int row, int column]
|
2023-03-31 15:27:31 +03:00
|
|
|
|
{
|
2023-04-06 08:29:39 +03:00
|
|
|
|
get => this.Rows[row][column];
|
2023-03-31 15:27:31 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 08:29:39 +03:00
|
|
|
|
public IEnumerable<Cell> Search(object item)
|
2023-03-31 15:27:31 +03:00
|
|
|
|
{
|
|
|
|
|
Range firstFound = Range.Find(item);
|
|
|
|
|
if (firstFound == null)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Range nextFound = firstFound;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2023-04-06 08:29:39 +03:00
|
|
|
|
yield return this[nextFound.Row - ParentTable.Range.Row, nextFound.Column - ParentTable.Range.Column];
|
2023-03-31 15:27:31 +03:00
|
|
|
|
nextFound = Range.FindNext(nextFound);
|
|
|
|
|
|
|
|
|
|
if (nextFound.Row == firstFound.Row
|
|
|
|
|
&& nextFound.Column == firstFound.Column)
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|