aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.ExcelExtensions/Row.cs
blob: 01df2e446919e27ac7594c8649f1d466797654b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
namespace RhSolutions.ExcelExtensions;

public sealed class Row
{
    public Table ParentTable { get; }
    public int Index
    {
        get => _range.Row - ParentTable.Range.Row;
    }
    public int Length
    {
        get => _range.Columns.Count;
    }
    private readonly Cell[] _cells;
    private readonly Range _range;

    public Row(Range range, Table table) 
    {
        _cells = new Cell[range.Columns.Count];
        _range = range;
        ParentTable = table ??
            throw new ArgumentNullException("table");
    }

    public Cell this[int index]
    {
        get 
        {
            if (index < 0 || index >= Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (_cells[index] == null)
            {
                _cells[index] = new Cell(_range.Cells[1, index + 1], ParentTable);
                return _cells[index];
            }
            else
            {
                return _cells[index];
            }
        }
    }

    public Cell this[string header]
    {
        get
        {
            int columnIndex = ParentTable.ColumnByHeader(header).Index;
            return this[columnIndex];
        }
    }
}