aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.ExcelExtensions/Columns.cs
blob: 014b755603ba8f36811dbbc81533946a49f654be (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
using System.Collections;

namespace RhSolutions.ExcelExtensions;

public class Columns : IEnumerable<Column>
{
    public Table ParentTable { get; }
    public int Length
    {
        get => _range.Columns.Count;
    }
    private Column[] _columns;
    private Range _range;

    public Columns(Table parentTable)
    {
        ParentTable = parentTable;
        _range = parentTable.Range;
        _columns = new Column[Length];
    }

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

            if (_columns[index] == null)
            {
                _columns[index] = new Column(_range.Columns[index + 1], ParentTable);
                return _columns[index];
            }
            else
            {
                return _columns[index];
            }
        }
    }

    public IEnumerator<Column> GetEnumerator()
    {
        return new ColumnsEnumerator(this);
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}