zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
/* ====================================================================
   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;
 
 
    public class SimpleValueVector : ValueVector
    {
        private ValueEval[] _values;
 
        public SimpleValueVector(ValueEval[] values)
        {
            _values = values;
        }
        public ValueEval GetItem(int index)
        {
            return _values[index];
        }
        public int Size
        {
            get
            {
                return _values.Length;
            }
        }
    }
    /**
     * Implementation of Excel function LOOKUP.<p/>
     * 
     * LOOKUP Finds an index  row in a lookup table by the first column value and returns the value from another column.
     * 
     * <b>Syntax</b>:<br/>
     * <b>VLOOKUP</b>(<b>lookup_value</b>, <b>lookup_vector</b>, result_vector)<p/>
     * 
     * <b>lookup_value</b>  The value to be found in the lookup vector.<br/>
     * <b>lookup_vector</b> An area reference for the lookup data. <br/>
     * <b>result_vector</b> Single row or single column area reference from which the result value Is chosen.<br/>
     * 
     * @author Josh Micich
     */
    public class Lookup : Function
    {
 
 
        public ValueEval Evaluate(ValueEval[] args, int srcCellRow, int srcCellCol)
        {
            switch (args.Length)
            {
                case 3:
                    break;
                case 2:
                    // complex rules to choose lookupVector and resultVector from the single area ref
                    throw new Exception("Two arg version of LOOKUP not supported yet");
                default:
                    return ErrorEval.VALUE_INVALID;
            }
 
 
            try
            {
                ValueEval lookupValue = OperandResolver.GetSingleValue(args[0], srcCellRow, srcCellCol);
                AreaEval aeLookupVector = LookupUtils.ResolveTableArrayArg(args[1]);
                AreaEval aeResultVector = LookupUtils.ResolveTableArrayArg(args[2]);
 
                ValueVector lookupVector = CreateVector(aeLookupVector);
                ValueVector resultVector = CreateVector(aeResultVector);
                if (lookupVector.Size > resultVector.Size)
                {
                    // Excel seems to handle this by accessing past the end of the result vector.
                    throw new Exception("Lookup vector and result vector of differing sizes not supported yet");
                }
                int index = LookupUtils.LookupIndexOfValue(lookupValue, lookupVector, true);
 
                return resultVector.GetItem(index);
            }
            catch (EvaluationException e)
            {
                return e.GetErrorEval();
            }
        }
 
        private static ValueVector CreateVector(AreaEval ae)
        {
            ValueVector result = LookupUtils.CreateVector(ae);
            if (result != null)
            {
                return result;
            }
            // extra complexity required to emulate the way LOOKUP can handles these abnormal cases.
            throw new InvalidOperationException("non-vector lookup or result areas not supported yet");
        }
    }
}