zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
/*
* 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;
 
 
    /*
     * Implementation for the Excel function SUMPRODUCT<p/>
     * 
     * Syntax : <br/>
     *  SUMPRODUCT ( array1[, array2[, array3[, ...]]])
     *    <table border="0" cellpAdding="1" cellspacing="0" summary="Parameter descriptions">
     *      <tr><th>array1, ... arrayN </th><td>typically area references, 
     *      possibly cell references or scalar values</td></tr>
     *    </table><br/>
     *    
     * Let A<b>n</b><sub>(<b>i</b>,<b>j</b>)</sub> represent the element in the <b>i</b>th row <b>j</b>th column 
     * of the <b>n</b>th array<br/>   
     * Assuming each array has the same dimensions (W, H), the result Is defined as:<br/>    
     * SUMPRODUCT = &Sigma;<sub><b>i</b>: 1..H</sub>  
     *     (  &Sigma;<sub><b>j</b>: 1..W</sub>  
     *       (  &Pi;<sub><b>n</b>: 1..N</sub> 
     *             A<b>n</b><sub>(<b>i</b>,<b>j</b>)</sub> 
     *    ) 
     *  ) 
     * 
     * @author Josh Micich
     */
    public class Sumproduct : Function
    {
        [Serializable]
        private class EvalEx : Exception
        {
            private ErrorEval _error;
 
            public EvalEx(ErrorEval error)
            {
                _error = error;
            }
            public ErrorEval GetError()
            {
                return _error;
            }
        }
 
        public ValueEval Evaluate(ValueEval[] args, int srcCellRow, int srcCellCol)
        {
 
            int maxN = args.Length;
 
            if (maxN < 1)
            {
                return ErrorEval.VALUE_INVALID;
            }
            ValueEval firstArg = args[0];
            try
            {
                if (firstArg is NumericValueEval)
                {
                    return EvaluateSingleProduct(args);
                }
                if (firstArg is RefEval)
                {
                    return EvaluateSingleProduct(args);
                }
                if (firstArg is TwoDEval)
                {
                    TwoDEval ae = (TwoDEval)firstArg;
                    if (ae.IsRow && ae.IsColumn)
                    {
                        return EvaluateSingleProduct(args);
                    }
                    return EvaluateAreaSumProduct(args);
                }
            }
            catch (EvalEx e)
            {
                return e.GetError();
            }
            throw new Exception("Invalid arg type for SUMPRODUCT: ("
                    + firstArg.GetType().Name + ")");
        }
 
        private ValueEval EvaluateSingleProduct(ValueEval[] evalArgs)
        {
            int maxN = evalArgs.Length;
 
            double term = 1D;
            for (int n = 0; n < maxN; n++)
            {
                double val = GetScalarValue(evalArgs[n]);
                term *= val;
            }
            return new NumberEval(term);
        }
        private static double GetScalarValue(ValueEval arg)
        {
 
            ValueEval eval;
            if (arg is RefEval)
            {
                RefEval re = (RefEval)arg;
                eval = re.InnerValueEval;
            }
            else
            {
                eval = arg;
            }
 
            if (eval == null)
            {
                throw new ArgumentException("parameter may not be null");
            }
            if (eval is AreaEval)
            {
                AreaEval ae = (AreaEval)eval;
                // an area ref can work as a scalar value if it is 1x1
                if (!ae.IsColumn || !ae.IsRow)
                {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                eval = ae.GetRelativeValue(0, 0);
            }
 
            if (!(eval is ValueEval))
            {
                throw new ArgumentException("Unexpected value eval class ("
                        + eval.GetType().Name + ")");
            }
 
            return GetProductTerm((ValueEval)eval, true);
        }
        private ValueEval EvaluateAreaSumProduct(ValueEval[] evalArgs)
        {
            int maxN = evalArgs.Length;
            AreaEval[] args = new AreaEval[maxN];
            try
            {
                Array.Copy(evalArgs, 0, args, 0, maxN);
            }
            catch (Exception)
            {
                // one of the other args was not an AreaRef
                return ErrorEval.VALUE_INVALID;
            }
 
 
            AreaEval firstArg = args[0];
 
            int height = firstArg.LastRow - firstArg.FirstRow + 1;
            int width = firstArg.LastColumn - firstArg.FirstColumn + 1; // TODO - junit
 
            // first check dimensions
            if (!AreasAllSameSize(args, height, width))
            {
                // normally this results in #VALUE!, 
                // but errors in individual cells take precedence
                for (int i = 1; i < args.Length; i++)
                {
                    ThrowFirstError(args[i]);
                }
                return ErrorEval.VALUE_INVALID;
            }
            double acc = 0;
 
            for (int rrIx = 0; rrIx < height; rrIx++)
            {
                for (int rcIx = 0; rcIx < width; rcIx++)
                {
                    double term = 1D;
                    for (int n = 0; n < maxN; n++)
                    {
                        double val = GetProductTerm(args[n].GetRelativeValue(rrIx, rcIx), false);
                        term *= val;
                    }
                    acc += term;
                }
            }
 
            return new NumberEval(acc);
        }
 
        private static void ThrowFirstError(TwoDEval areaEval)
        {
            int height = areaEval.Height;
            int width = areaEval.Width;
            for (int rrIx = 0; rrIx < height; rrIx++)
            {
                for (int rcIx = 0; rcIx < width; rcIx++)
                {
                    ValueEval ve = areaEval.GetValue(rrIx, rcIx);
                    if (ve is ErrorEval)
                    {
                        throw new EvaluationException((ErrorEval)ve);
                    }
                }
            }
        }
        private static bool AreasAllSameSize(TwoDEval[] args, int height, int width)
        {
            for (int i = 0; i < args.Length; i++)
            {
                TwoDEval areaEval = args[i];
                // check that height and width match
                if (areaEval.Height != height)
                {
                    return false;
                }
                if (areaEval.Width != width)
                {
                    return false;
                }
            }
            return true;
        }
        /**
         * Determines a <c>double</c> value for the specified <c>ValueEval</c>. 
         * @param IsScalarProduct <c>false</c> for SUMPRODUCTs over area refs.
         * @throws EvalEx if <c>ve</c> represents an error value.
         * <p/>
         * Note - string values and empty cells are interpreted differently depending on 
         * <c>isScalarProduct</c>.  For scalar products, if any term Is blank or a string, the
         * error (#VALUE!) Is raised.  For area (sum)products, if any term Is blank or a string, the
         * result Is zero.
         */
        private static double GetProductTerm(ValueEval ve, bool IsScalarProduct)
        {
 
            if (ve is BlankEval || ve == null)
            {
                // TODO - shouldn't BlankEval.INSTANCE be used always instead of null?
                // null seems to occur when the blank cell Is part of an area ref (but not reliably)
                if (IsScalarProduct)
                {
                    throw new EvalEx(ErrorEval.VALUE_INVALID);
                }
                return 0;
            }
 
            if (ve is ErrorEval)
            {
                throw new EvalEx((ErrorEval)ve);
            }
            if (ve is StringEval)
            {
                if (IsScalarProduct)
                {
                    throw new EvalEx(ErrorEval.VALUE_INVALID);
                }
                // Note for area SUMPRODUCTs, string values are interpreted as zero
                // even if they would Parse as valid numeric values
                return 0;
            }
            if (ve is NumericValueEval)
            {
                NumericValueEval nve = (NumericValueEval)ve;
                return nve.NumberValue;
            }
            throw new Exception("Unexpected value eval class ("
                    + ve.GetType().Name + ")");
        }
    }
}