1 | /* Copyright (C) 2011 Jan Källman
|
---|
2 | *
|
---|
3 | * This library is free software; you can redistribute it and/or
|
---|
4 | * modify it under the terms of the GNU Lesser General Public
|
---|
5 | * License as published by the Free Software Foundation; either
|
---|
6 | * version 2.1 of the License, or (at your option) any later version.
|
---|
7 |
|
---|
8 | * This library is distributed in the hope that it will be useful,
|
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
11 | * See the GNU Lesser General Public License for more details.
|
---|
12 | *
|
---|
13 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
14 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
15 | *
|
---|
16 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
17 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
18 | *
|
---|
19 | * Code change notes:
|
---|
20 | *
|
---|
21 | * Author Change Date
|
---|
22 | *******************************************************************************
|
---|
23 | * Mats Alm Added 2013-12-03
|
---|
24 | *******************************************************************************/
|
---|
25 | using System;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using OfficeOpenXml.FormulaParsing.ExcelUtilities;
|
---|
30 | using OfficeOpenXml.FormulaParsing.ExpressionGraph;
|
---|
31 | using OfficeOpenXml.FormulaParsing.Exceptions;
|
---|
32 |
|
---|
33 | namespace OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup
|
---|
34 | {
|
---|
35 | public abstract class LookupFunction : ExcelFunction
|
---|
36 | {
|
---|
37 | private readonly ValueMatcher _valueMatcher;
|
---|
38 | private readonly CompileResultFactory _compileResultFactory;
|
---|
39 |
|
---|
40 | public LookupFunction()
|
---|
41 | : this(new LookupValueMatcher(), new CompileResultFactory())
|
---|
42 | {
|
---|
43 |
|
---|
44 | }
|
---|
45 |
|
---|
46 | public LookupFunction(ValueMatcher valueMatcher, CompileResultFactory compileResultFactory)
|
---|
47 | {
|
---|
48 | _valueMatcher = valueMatcher;
|
---|
49 | _compileResultFactory = compileResultFactory;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override bool IsLookupFuction
|
---|
53 | {
|
---|
54 | get
|
---|
55 | {
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected int IsMatch(object o1, object o2)
|
---|
61 | {
|
---|
62 | return _valueMatcher.IsMatch(o1, o2);
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected LookupDirection GetLookupDirection(RangeAddress rangeAddress)
|
---|
66 | {
|
---|
67 | var nRows = rangeAddress.ToRow - rangeAddress.FromRow;
|
---|
68 | var nCols = rangeAddress.ToCol - rangeAddress.FromCol;
|
---|
69 | return nCols > nRows ? LookupDirection.Horizontal : LookupDirection.Vertical;
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected CompileResult Lookup(LookupNavigator navigator, LookupArguments lookupArgs)
|
---|
73 | {
|
---|
74 | object lastValue = null;
|
---|
75 | object lastLookupValue = null;
|
---|
76 | int? lastMatchResult = null;
|
---|
77 | if (lookupArgs.SearchedValue == null)
|
---|
78 | {
|
---|
79 | return new CompileResult(ExcelErrorValue.Create(eErrorType.NA), DataType.ExcelError);
|
---|
80 | }
|
---|
81 | do
|
---|
82 | {
|
---|
83 | var matchResult = IsMatch(navigator.CurrentValue, lookupArgs.SearchedValue);
|
---|
84 | if (matchResult != 0)
|
---|
85 | {
|
---|
86 | if (lastValue != null && navigator.CurrentValue == null) break;
|
---|
87 |
|
---|
88 | if (lookupArgs.RangeLookup)
|
---|
89 | {
|
---|
90 | if (lastValue == null && matchResult > 0)
|
---|
91 | {
|
---|
92 | ThrowExcelErrorValueException(eErrorType.NA);
|
---|
93 | }
|
---|
94 | if (lastValue != null && matchResult > 0 && lastMatchResult < 0)
|
---|
95 | {
|
---|
96 | return _compileResultFactory.Create(lastLookupValue);
|
---|
97 | }
|
---|
98 | lastMatchResult = matchResult;
|
---|
99 | lastValue = navigator.CurrentValue;
|
---|
100 | lastLookupValue = navigator.GetLookupValue();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | else
|
---|
104 | {
|
---|
105 | return _compileResultFactory.Create(navigator.GetLookupValue());
|
---|
106 | }
|
---|
107 | }
|
---|
108 | while (navigator.MoveNext());
|
---|
109 |
|
---|
110 | if (lookupArgs.RangeLookup)
|
---|
111 | {
|
---|
112 | return _compileResultFactory.Create(lastLookupValue);
|
---|
113 | }
|
---|
114 | return new CompileResult(ExcelErrorValue.Create(eErrorType.NA), DataType.ExcelError);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|