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
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
/* ====================================================================
   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.PTG
{
    using System;
    using System.Text;
 
    using HH.WMS.Utils.NPOI.SS.Formula.Function;
    using HH.WMS.Utils.NPOI.SS.Formula;
 
 
    /**
     * This class provides the base functionality for Excel sheet functions
     * There are two kinds of function Ptgs - tFunc and tFuncVar
     * Therefore, this class will have ONLY two subclasses
     * @author  Avik Sengupta
     * @author Andrew C. Oliver (acoliver at apache dot org)
     */
    [Serializable]
    public abstract class AbstractFunctionPtg : OperationPtg
    {
 
        /**
         * The name of the IF function (i.e. "IF").  Extracted as a constant for clarity.
         */
        public static String FUNCTION_NAME_IF = "IF";
        /** All external functions have function index 255 */
        private static short FUNCTION_INDEX_EXTERNAL = 255;
 
        protected byte returnClass;
        protected byte[] paramClass;
 
        protected byte _numberOfArgs;
        protected short _functionIndex;
 
        protected AbstractFunctionPtg(int functionIndex, int pReturnClass, byte[] paramTypes, int nParams)
        {
            _numberOfArgs = (byte)nParams;
            _functionIndex = (short)functionIndex;
            returnClass = (byte)pReturnClass;
            paramClass = paramTypes;
        }
 
        public override bool IsBaseToken
        {
            get { return false; }
        }
 
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder(64);
            sb.Append(GetType().Name).Append(" [");
            sb.Append(LookupName(_functionIndex));
            sb.Append(" nArgs=").Append(_numberOfArgs);
            sb.Append("]");
            return sb.ToString();
        }
 
        public short GetFunctionIndex()
        {
            return _functionIndex;
        }
        public override int NumberOfOperands
        {
            get
            {
                return _numberOfArgs;
            }
        }
 
        public String Name
        {
            get { return LookupName(_functionIndex); }
        }
        /**
         * external functions Get some special Processing
         * @return <c>true</c> if this is an external function
         */
        public bool IsExternalFunction
        {
            get { return _functionIndex == FUNCTION_INDEX_EXTERNAL; }
        }
 
        public override String ToFormulaString()
        {
            return Name;
        }
 
        public override String ToFormulaString(String[] operands)
        {
            StringBuilder buf = new StringBuilder();
 
            if (IsExternalFunction)
            {
                buf.Append(operands[0]); // first operand is actually the function name
                AppendArgs(buf, 1, operands);
            }
            else
            {
                buf.Append(Name);
                AppendArgs(buf, 0, operands);
            }
            return buf.ToString();
        }
 
        private static void AppendArgs(StringBuilder buf, int firstArgIx, String[] operands)
        {
            buf.Append('(');
            for (int i = firstArgIx; i < operands.Length; i++)
            {
                if (i > firstArgIx)
                {
                    buf.Append(',');
                }
                buf.Append(operands[i]);
            }
            buf.Append(")");
        }
 
        /**
         * Used to detect whether a function name found in a formula is one of the standard excel functions
         * 
         * The name matching is case insensitive.
         * @return <c>true</c> if the name specifies a standard worksheet function,
         *  <c>false</c> if the name should be assumed to be an external function.
         */
        public static bool IsBuiltInFunctionName(String name)
        {
            short ix = FunctionMetadataRegistry.LookupIndexByName(name.ToUpper());
            return ix >= 0;
        }
        protected String LookupName(short index)
        {
            if (index == FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL)
            {
                return "#external#";
            }
            FunctionMetadata fm = FunctionMetadataRegistry.GetFunctionByIndex(index);
            if (fm == null)
            {
                throw new Exception("bad function index (" + index + ")");
            }
            return fm.Name;
        }
 
        /**
         * Resolves internal function names into function indexes.
         * 
         * The name matching is case insensitive.
         * @return the standard worksheet function index if found, otherwise <c>FUNCTION_INDEX_EXTERNAL</c>
         */
        protected static short LookupIndex(String name)
        {
            short ix = FunctionMetadataRegistry.LookupIndexByName(name.ToUpper());
            if (ix < 0)
            {
                return FUNCTION_INDEX_EXTERNAL;
            }
            return ix;
        }
 
        public override byte DefaultOperandClass
        {
            get { return returnClass; }
        }
 
        public byte GetParameterClass(int index)
        {
            if (index >= paramClass.Length)
            {
                // For var-arg (and other?) functions, the metadata does not list all the parameter
                // operand classes.  In these cases, all extra parameters are assumed to have the 
                // same operand class as the last one specified.
                return paramClass[paramClass.Length - 1];
            }
            return paramClass[index];
        }
    }
}