1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using OfficeOpenXml.FormulaParsing.Exceptions;
|
---|
6 | using OfficeOpenXml.FormulaParsing.Utilities;
|
---|
7 |
|
---|
8 | namespace OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup
|
---|
9 | {
|
---|
10 | public class ArrayLookupNavigator : LookupNavigator
|
---|
11 | {
|
---|
12 | private readonly FunctionArgument[] _arrayData;
|
---|
13 | private int _index = 0;
|
---|
14 | private object _currentValue;
|
---|
15 |
|
---|
16 | public ArrayLookupNavigator(LookupDirection direction, LookupArguments arguments, ParsingContext parsingContext)
|
---|
17 | : base(direction, arguments, parsingContext)
|
---|
18 | {
|
---|
19 | Require.That(arguments).Named("arguments").IsNotNull();
|
---|
20 | Require.That(arguments.DataArray).Named("arguments.DataArray").IsNotNull();
|
---|
21 | _arrayData = arguments.DataArray.ToArray();
|
---|
22 | Initialize();
|
---|
23 | }
|
---|
24 |
|
---|
25 | private void Initialize()
|
---|
26 | {
|
---|
27 | if (Arguments.LookupIndex >= _arrayData.Length)
|
---|
28 | {
|
---|
29 | throw new ExcelErrorValueException(eErrorType.Ref);
|
---|
30 | }
|
---|
31 | SetCurrentValue();
|
---|
32 |
|
---|
33 | }
|
---|
34 |
|
---|
35 | public override int Index
|
---|
36 | {
|
---|
37 | get { return _index; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | private void SetCurrentValue()
|
---|
41 | {
|
---|
42 | _currentValue = _arrayData[_index];
|
---|
43 | }
|
---|
44 |
|
---|
45 | private bool HasNext()
|
---|
46 | {
|
---|
47 | if (Direction == LookupDirection.Vertical)
|
---|
48 | {
|
---|
49 | return _index < (_arrayData.Length - 1);
|
---|
50 | }
|
---|
51 | else
|
---|
52 | {
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public override bool MoveNext()
|
---|
58 | {
|
---|
59 | if (!HasNext()) return false;
|
---|
60 | if (Direction == LookupDirection.Vertical)
|
---|
61 | {
|
---|
62 | _index++;
|
---|
63 | }
|
---|
64 | SetCurrentValue();
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override object CurrentValue
|
---|
69 | {
|
---|
70 | get { return _arrayData[_index].Value; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public override object GetLookupValue()
|
---|
74 | {
|
---|
75 | return _arrayData[_index].Value;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|