zhao
2021-07-07 2fdf959ac739edd6de84aa8053b8b9683dce8e8b
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Licensed to the Apache Software Foundation (ASF) Under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for Additional information regarding copyright ownership.
* The ASF licenses this file to You Under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed Under the License is distributed on an "AS Is" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations Under the License.
*/
 
namespace HH.WMS.Utils.NPOI.SS.Formula.Functions
{
    using System;
    using HH.WMS.Utils.NPOI.SS.Formula.Eval;
    using HH.WMS.Utils.NPOI.SS.Formula.Functions;
    using HH.WMS.Utils.NPOI.SS.Formula;
 
    public class SingleValueVector : ValueVector
    {
 
        private ValueEval _value;
 
        public SingleValueVector(ValueEval value)
        {
            _value = value;
        }
 
        public ValueEval GetItem(int index)
        {
            if (index != 0)
            {
                throw new ArgumentException("Invalid index ("
                        + index + ") only zero is allowed");
            }
            return _value;
        }
 
        public int Size
        {
            get
            {
                return 1;
            }
        }
    }
    /**
     * Implementation for the MATCH() Excel function.<p/>
     * 
     * <b>Syntax:</b><br/>
     * <b>MATCH</b>(<b>lookup_value</b>, <b>lookup_array</b>, match_type)<p/>
     * 
     * Returns a 1-based index specifying at what position in the <b>lookup_array</b> the specified 
     * <b>lookup_value</b> Is found.<p/>
     * 
     * Specific matching behaviour can be modified with the optional <b>match_type</b> parameter.
     * 
     *    <table border="0" cellpAdding="1" cellspacing="0" summary="match_type parameter description">
     *      <tr><th>Value</th><th>Matching Behaviour</th></tr>
     *      <tr><td>1</td><td>(default) Find the largest value that Is less than or equal to lookup_value.
     *        The lookup_array must be in ascending <i>order</i>*.</td></tr>
     *      <tr><td>0</td><td>Find the first value that Is exactly equal to lookup_value.
     *        The lookup_array can be in any order.</td></tr>
     *      <tr><td>-1</td><td>Find the smallest value that Is greater than or equal to lookup_value.
     *        The lookup_array must be in descending <i>order</i>*.</td></tr>
     *    </table>
     * 
     * * Note regarding <i>order</i> - For the <b>match_type</b> cases that require the lookup_array to
     *  be ordered, MATCH() can produce incorrect results if this requirement Is not met.  Observed
     *  behaviour in Excel Is to return the lowest index value for which every item after that index
     *  breaks the match rule.<br/>
     *  The (ascending) sort order expected by MATCH() Is:<br/>
     *  numbers (low to high), strings (A to Z), bool (FALSE to TRUE)<br/>
     *  MATCH() ignores all elements in the lookup_array with a different type to the lookup_value. 
     *  Type conversion of the lookup_array elements Is never performed.
     *  
     *  
     * @author Josh Micich
     */
    public class Match : Function
    {
 
 
        public ValueEval Evaluate(ValueEval[] args, int srcCellRow, int srcCellCol)
        {
 
            double match_type = 1; // default
 
            switch (args.Length)
            {
                case 3:
                    try
                    {
                        match_type = EvaluateMatchTypeArg(args[2], srcCellRow, srcCellCol);
                    }
                    catch (EvaluationException)
                    {
                        // Excel/MATCH() seems to have slightly abnormal handling of errors with
                        // the last parameter.  Errors do not propagate up.  Every error Gets
                        // translated into #REF!
                        return ErrorEval.REF_INVALID;
                    }
                    break;
                case 2:
                    break;
                default:
                    return ErrorEval.VALUE_INVALID;
            }
 
            bool matchExact = match_type == 0;
            // Note - Excel does not strictly require -1 and +1
            bool FindLargestLessThanOrEqual = match_type > 0;
 
 
            try
            {
                ValueEval lookupValue = OperandResolver.GetSingleValue(args[0], srcCellRow, srcCellCol);
                ValueVector lookupRange = EvaluateLookupRange(args[1]);
                int index = FindIndexOfValue(lookupValue, lookupRange, matchExact, FindLargestLessThanOrEqual);
                return new NumberEval(index + 1); // +1 to Convert to 1-based
            }
            catch (EvaluationException e)
            {
                return e.GetErrorEval();
            }
        }
 
        private static ValueVector EvaluateLookupRange(ValueEval eval)
        {
            if (eval is RefEval)
            {
                RefEval re = (RefEval)eval;
                return new SingleValueVector(re.InnerValueEval);
            }
            if (eval is TwoDEval)
            {
                ValueVector result = LookupUtils.CreateVector((TwoDEval)eval);
                if (result == null)
                {
                    throw new EvaluationException(ErrorEval.NA);
                }
                return result;
            }
 
            // Error handling for lookup_range arg Is also Unusual
            if (eval is NumericValueEval)
            {
                throw new EvaluationException(ErrorEval.NA);
            }
            if (eval is StringEval)
            {
                StringEval se = (StringEval)eval;
                double d = OperandResolver.ParseDouble(se.StringValue);
                if (double.IsNaN(d))
                {
                    // plain string
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                // else looks like a number
                throw new EvaluationException(ErrorEval.NA);
            }
            throw new Exception("Unexpected eval type (" + eval.GetType().Name + ")");
        }
 
 
 
        private static double EvaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol)
        {
            ValueEval match_type = OperandResolver.GetSingleValue(arg, srcCellRow, srcCellCol);
 
            if (match_type is ErrorEval)
            {
                throw new EvaluationException((ErrorEval)match_type);
            }
            if (match_type is NumericValueEval)
            {
                NumericValueEval ne = (NumericValueEval)match_type;
                return ne.NumberValue;
            }
            if (match_type is StringEval)
            {
                StringEval se = (StringEval)match_type;
                double d = OperandResolver.ParseDouble(se.StringValue);
                if (double.IsNaN(d))
                {
                    // plain string
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                // if the string Parses as a number, it Is OK
                return d;
            }
            throw new Exception("Unexpected match_type type (" + match_type.GetType().Name + ")");
        }
 
        /**
         * @return zero based index
         */
        private static int FindIndexOfValue(ValueEval lookupValue, ValueVector lookupRange,
                bool matchExact, bool FindLargestLessThanOrEqual)
        {
 
            LookupValueComparer lookupComparer = CreateLookupComparer(lookupValue, matchExact);
 
            if (matchExact)
            {
                for (int i = 0; i < lookupRange.Size; i++)
                {
                    if (lookupComparer.CompareTo(lookupRange.GetItem(i)).IsEqual)
                    {
                        return i;
                    }
                }
                throw new EvaluationException(ErrorEval.NA);
            }
 
            if (FindLargestLessThanOrEqual)
            {
                // Note - backward iteration
                for (int i = lookupRange.Size - 1; i >= 0; i--)
                {
                    CompareResult cmp = lookupComparer.CompareTo(lookupRange.GetItem(i));
                    if (cmp.IsTypeMismatch)
                    {
                        continue;
                    }
                    if (!cmp.IsLessThan)
                    {
                        return i;
                    }
                }
                throw new EvaluationException(ErrorEval.NA);
            }
 
            // else - Find smallest greater than or equal to
            // TODO - Is binary search used for (match_type==+1) ?
            for (int i = 0; i < lookupRange.Size; i++)
            {
                CompareResult cmp = lookupComparer.CompareTo(lookupRange.GetItem(i));
                if (cmp.IsEqual)
                {
                    return i;
                }
                if (cmp.IsGreaterThan)
                {
                    if (i < 1)
                    {
                        throw new EvaluationException(ErrorEval.NA);
                    }
                    return i - 1;
                }
            }
 
            throw new EvaluationException(ErrorEval.NA);
        }
 
        private static LookupValueComparer CreateLookupComparer(ValueEval lookupValue, bool matchExact)
        {
            if (matchExact && lookupValue is StringEval)
            {
                String stringValue = ((StringEval)lookupValue).StringValue;
                if (IsLookupValueWild(stringValue))
                {
                    throw new Exception("Wildcard lookup values '" + stringValue + "' not supported yet");
                }
 
            }
            return LookupUtils.CreateLookupComparer(lookupValue);
        }
 
        private static bool IsLookupValueWild(String stringValue)
        {
            if (stringValue.IndexOf('?') >= 0 || stringValue.IndexOf('*') >= 0)
            {
                return true;
            }
            return false;
        }
    }
}