aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.AddIn/ExcelTable/ExcelTable.cs
blob: 2788036172efa3b16ae3bd29f8d557ea5229ca39 (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
namespace RhSolutions.ExcelTable;

public class ExcelTable
{
    public Range Range { get; protected set; }
    public ExcelTable ParentTable { get; protected set; }
    public ExcelRows Rows { get; }
    public ExcelColumns Columns { get; }

    public ExcelTable(Range range)
    {
        Range = range;
        ParentTable = null;
        Rows = new ExcelRows(Range, this);
        Columns = new ExcelColumns(Range, this);
    }

    public ExcelTable(Range range, ExcelTable table)
    {
        Range = range;
        ParentTable = table;
        Rows = new ExcelRows(Range, this);
        Columns = new ExcelColumns(Range, this);
    }

    public ExcelTableCell this[int row, int column]
    {
        get => new(Range.Cells[row + 1, column + 1], this);
    }

    public IEnumerable<ExcelTableCell> Find(object item)
    {
        Range firstFound = Range.Find(item);
        if (firstFound == null)
        {
            yield break;
        }

        Range nextFound = firstFound;

        while (true)
        {
            yield return new ExcelTableCell(nextFound, ParentTable ?? this);
            nextFound = Range.FindNext(nextFound);

            if (nextFound.Row == firstFound.Row
                && nextFound.Column == firstFound.Column)
            {
                yield break;
            }
        }
    }
}