aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.ExcelExtensions/Column.cs
blob: 341897ccc2940ae1bb93f09bff4671a89ce049cc (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
55
56
57
58
59
60
61
62
63
64
65
namespace RhSolutions.ExcelExtensions;

public sealed class Column
{
    public Table ParentTable { get; }
    public string Header
    {
        get => _range.Cells[1, 1].Value2.ToString() ?? String.Empty;
    }
    public int Index
    {
        get => _range.Column - ParentTable.Range.Column;
    }
    public int Length
    {
        get => _range.Rows.Count;
    }
    private Cell[] _cells;
    private readonly Range _range;

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

    public Cell this[int index]
    {
        get
        {
            if (_cells[index] == null)
            {
                _cells[index] = new Cell(_range.Cells[index + 1, 1], ParentTable);
                return _cells[index];
            }
            else
            {
                return _cells[index];
            }
        }
        set
        {
            if (_cells[index] == null)
            {
                _cells[index] = new Cell(_range.Cells[index + 1, 1], ParentTable);
                _cells[index].Value = value;
            }
            else
            {
                _cells[index].Value = value;
            }
        }
    }

    public Column AddLeft()
    {
        _range.EntireColumn
            .Insert(XlInsertShiftDirection.xlShiftToRight,
                XlInsertFormatOrigin.xlFormatFromRightOrBelow);

        return ParentTable.Columns[this.Index - 1];
    }
}