zhao
2021-07-09 0821715ebc11d3934d0594a1cc2c39686d808906
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* 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.HSSF.UserModel
{
    using System;
    using System.Collections;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.SS.Formula.Eval;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    using HH.WMS.Utils.NPOI.SS.Formula.Udf;
    using HH.WMS.Utils.NPOI.SS.UserModel;
 
    /**
     * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
     * 
     */
    public class HSSFFormulaEvaluator : IFormulaEvaluator
    {
        private WorkbookEvaluator _bookEvaluator;
        // params to lookup the right constructor using reflection
        private static Type[] VALUE_CONTRUCTOR_CLASS_ARRAY = new Type[] { typeof(Ptg) };
 
        private static Type[] AREA3D_CONSTRUCTOR_CLASS_ARRAY = new Type[] { typeof(Ptg), typeof(ValueEval[]) };
 
        private static Type[] REFERENCE_CONSTRUCTOR_CLASS_ARRAY = new Type[] { typeof(Ptg), typeof(ValueEval) };
 
        private static Type[] REF3D_CONSTRUCTOR_CLASS_ARRAY = new Type[] { typeof(Ptg), typeof(ValueEval) };
 
        // Maps for mapping *Eval to *Ptg
        private static Hashtable VALUE_EVALS_MAP = new Hashtable();
 
        /*
         * Following is the mapping between the Ptg tokens returned 
         * by the FormulaParser and the *Eval classes that are used 
         * by the FormulaEvaluator
         */
        static HSSFFormulaEvaluator()
        {
            VALUE_EVALS_MAP[typeof(BoolPtg)] = typeof(BoolEval);
            VALUE_EVALS_MAP[typeof(IntPtg)] = typeof(NumberEval);
            VALUE_EVALS_MAP[typeof(NumberPtg)] = typeof(NumberEval);
            VALUE_EVALS_MAP[typeof(StringPtg)] = typeof(StringEval);
 
        }
 
 
        protected IRow row;
        protected ISheet sheet;
        protected IWorkbook workbook;
        [Obsolete]
        public HSSFFormulaEvaluator(ISheet sheet, IWorkbook workbook)
            : this(workbook)
        {
            this.sheet = sheet;
            this.workbook = workbook;
        }
 
        public HSSFFormulaEvaluator(IWorkbook workbook)
            : this(workbook, null)
        {
            this.workbook = workbook;
        }
        /**
         * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
         * for the (conservative) assumption that any cell may have its definition changed after
         * evaluation begins.
         */
        public HSSFFormulaEvaluator(IWorkbook workbook, IStabilityClassifier stabilityClassifier)
            : this(workbook, stabilityClassifier, null)
        {
 
        }
 
 
 
        /**
         * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
         */
        public HSSFFormulaEvaluator(IWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)
        {
            _bookEvaluator = new WorkbookEvaluator(HSSFEvaluationWorkbook.Create(workbook), stabilityClassifier, udfFinder);
        }
 
        /**
     * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
     * for the (conservative) assumption that any cell may have its definition changed after
     * evaluation begins.
     * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
     */
        public static HSSFFormulaEvaluator Create(IWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)
        {
            return new HSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
        }
 
 
        private static void SetCellType(ICell cell, CellValue cv)
        {
            CellType cellType = cv.CellType;
            switch (cellType)
            {
                case CellType.BOOLEAN:
                case CellType.ERROR:
                case CellType.NUMERIC:
                case CellType.STRING:
                    cell.SetCellType(cellType);
                    return;
                case CellType.BLANK:
                    // never happens - blanks eventually get translated to zero
                    break;
                case CellType.FORMULA:
                    // this will never happen, we have already evaluated the formula
                    break;
            }
            throw new InvalidOperationException("Unexpected cell value type (" + cellType + ")");
        }
        private static void SetCellValue(ICell cell, CellValue cv)
        {
            CellType cellType = cv.CellType;
            switch (cellType)
            {
                case CellType.BOOLEAN:
                    cell.SetCellValue(cv.BooleanValue);
                    break;
                case CellType.ERROR:
                    cell.SetCellErrorValue((byte)cv.ErrorValue);
                    break;
                case CellType.NUMERIC:
                    cell.SetCellValue(cv.NumberValue);
                    break;
                case CellType.STRING:
                    cell.SetCellValue(new HSSFRichTextString(cv.StringValue));
                    break;
                //case CellType.BLANK:
                //// never happens - blanks eventually get translated to zero
                //case CellType.FORMULA:
                //// this will never happen, we have already evaluated the formula
                default:
                    throw new InvalidOperationException("Unexpected cell value type (" + cellType + ")");
            }
        }
        /**
         * Coordinates several formula evaluators together so that formulas that involve external
         * references can be evaluated.
         * @param workbookNames the simple file names used to identify the workbooks in formulas
         * with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")
         * @param evaluators all evaluators for the full set of workbooks required by the formulas. 
         */
        public static void SetupEnvironment(String[] workbookNames, HSSFFormulaEvaluator[] evaluators)
        {
            WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.Length];
            for (int i = 0; i < wbEvals.Length; i++)
            {
                wbEvals[i] = evaluators[i]._bookEvaluator;
            }
            CollaboratingWorkbooksEnvironment.Setup(workbookNames, wbEvals);
        }
 
        /**
         * If cell Contains a formula, the formula is Evaluated and returned,
         * else the CellValue simply copies the appropriate cell value from
         * the cell and also its cell type. This method should be preferred over
         * EvaluateInCell() when the call should not modify the contents of the
         * original cell. 
         * @param cell
         */
        /**
         * If cell contains a formula, the formula is evaluated and returned,
         * else the CellValue simply copies the appropriate cell value from
         * the cell and also its cell type. This method should be preferred over
         * evaluateInCell() when the call should not modify the contents of the
         * original cell.
         * 
         * @param cell may be <c>null</c> signifying that the cell is not present (or blank)
         * @return <c>null</c> if the supplied cell is <c>null</c> or blank
         */
        public CellValue Evaluate(ICell cell)
        {
            if (cell == null)
            {
                return null;
            }
 
            switch (cell.CellType)
            {
                case CellType.BOOLEAN:
                    return CellValue.ValueOf(cell.BooleanCellValue);
                case CellType.ERROR:
                    return CellValue.GetError(cell.ErrorCellValue);
                case CellType.FORMULA:
                    return EvaluateFormulaCellValue(cell);
                case CellType.NUMERIC:
                    return new CellValue(cell.NumericCellValue);
                case CellType.STRING:
                    return new CellValue(cell.RichStringCellValue.String);
                case CellType.BLANK:
                    return null;
            }
            throw new InvalidOperationException("Bad cell type (" + cell.CellType + ")");
        }
 
 
        /**
 * Should be called whenever there are major changes (e.g. moving sheets) to input cells
 * in the evaluated workbook.  If performance is not critical, a single call to this method
 * may be used instead of many specific calls to the notify~ methods.
 *  
 * Failure to call this method after changing cell values will cause incorrect behaviour
 * of the evaluate~ methods of this class
 */
        public void ClearAllCachedResultValues()
        {
            _bookEvaluator.ClearAllCachedResultValues();
        }
        /**
         * Should be called to tell the cell value cache that the specified (value or formula) cell 
         * has changed.
         * Failure to call this method after changing cell values will cause incorrect behaviour
         * of the evaluate~ methods of this class
         */
        public void NotifyUpdateCell(ICell cell)
        {
            _bookEvaluator.NotifyUpdateCell(new HSSFEvaluationCell(cell));
        }
        /**
         * Should be called to tell the cell value cache that the specified cell has just been
         * deleted. 
         * Failure to call this method after changing cell values will cause incorrect behaviour
         * of the evaluate~ methods of this class
         */
        public void NotifyDeleteCell(ICell cell)
        {
            _bookEvaluator.NotifyDeleteCell(new HSSFEvaluationCell(cell));
        }
 
        /**
         * Should be called to tell the cell value cache that the specified (value or formula) cell
         * has changed.
         * Failure to call this method after changing cell values will cause incorrect behaviour
         * of the evaluate~ methods of this class
         */
        public void NotifySetFormula(ICell cell)
        {
            _bookEvaluator.NotifyUpdateCell(new HSSFEvaluationCell(cell));
        }
 
        /**
         * If cell Contains formula, it Evaluates the formula,
         *  and saves the result of the formula. The cell
         *  remains as a formula cell.
         * Else if cell does not contain formula, this method leaves
         *  the cell UnChanged. 
         * Note that the type of the formula result is returned,
         *  so you know what kind of value is also stored with
         *  the formula. 
         * <pre>
         * int EvaluatedCellType = evaluator.EvaluateFormulaCell(cell);
         * </pre>
         * Be aware that your cell will hold both the formula,
         *  and the result. If you want the cell Replaced with
         *  the result of the formula, use {@link #EvaluateInCell(HSSFCell)}
         * @param cell The cell to Evaluate
         * @return The type of the formula result (the cell's type remains as CellType.FORMULA however)
         */
        public CellType EvaluateFormulaCell(ICell cell)
        {
            if (cell == null || cell.CellType != CellType.FORMULA)
            {
                return CellType.Unknown;
            }
            CellValue cv = EvaluateFormulaCellValue(cell);
            // cell remains a formula cell, but the cached value is changed
            SetCellValue(cell, cv);
            return cv.CellType;
        }
        /**
         * Returns a CellValue wrapper around the supplied ValueEval instance.
         * @param eval
         */
        private CellValue EvaluateFormulaCellValue(ICell cell)
        {
            ValueEval eval = _bookEvaluator.Evaluate(new HSSFEvaluationCell((HSSFCell)cell));
            if (eval is NumberEval)
            {
                NumberEval ne = (NumberEval)eval;
                return new CellValue(ne.NumberValue);
            }
            if (eval is BoolEval)
            {
                BoolEval be = (BoolEval)eval;
                return CellValue.ValueOf(be.BooleanValue);
            }
            if (eval is StringEval)
            {
                StringEval ne = (StringEval)eval;
                return new CellValue(ne.StringValue);
            }
            if (eval is ErrorEval)
            {
                return CellValue.GetError(((ErrorEval)eval).ErrorCode);
            }
            throw new InvalidOperationException("Unexpected eval class (" + eval.GetType().Name + ")");
        }
        /**
         * If cell Contains formula, it Evaluates the formula, and
         *  puts the formula result back into the cell, in place
         *  of the old formula.
         * Else if cell does not contain formula, this method leaves
         *  the cell UnChanged. 
         * Note that the same instance of Cell is returned to 
         * allow chained calls like:
         * <pre>
         * int EvaluatedCellType = evaluator.EvaluateInCell(cell).CellType;
         * </pre>
         * Be aware that your cell value will be Changed to hold the
         *  result of the formula. If you simply want the formula
         *  value computed for you, use {@link #EvaluateFormulaCell(HSSFCell)}
         * @param cell
         */
        public ICell EvaluateInCell(ICell cell)
        {
            if (cell == null)
            {
                return null;
            }
            if (cell.CellType == CellType.FORMULA)
            {
                CellValue cv = EvaluateFormulaCellValue(cell);
                SetCellValue(cell, cv);
                SetCellType(cell, cv); // cell will no longer be a formula cell
            }
            return cell;
        }
 
        /**
         * Loops over all cells in all sheets of the supplied
         *  workbook.
         * For cells that contain formulas, their formulas are
         *  Evaluated, and the results are saved. These cells
         *  remain as formula cells.
         * For cells that do not contain formulas, no Changes
         *  are made.
         * This is a helpful wrapper around looping over all 
         *  cells, and calling EvaluateFormulaCell on each one.
         */
        public static void EvaluateAllFormulaCells(HSSFWorkbook wb)
        {
            EvaluateAllFormulaCells(wb, new HSSFFormulaEvaluator(wb));
        }
        /**
           * Loops over all cells in all sheets of the supplied
           *  workbook.
           * For cells that contain formulas, their formulas are
           *  evaluated, and the results are saved. These cells
           *  remain as formula cells.
           * For cells that do not contain formulas, no changes
           *  are made.
           * This is a helpful wrapper around looping over all
           *  cells, and calling evaluateFormulaCell on each one.
           */
        public static void EvaluateAllFormulaCells(IWorkbook wb)
        {
            IFormulaEvaluator evaluator = wb.GetCreationHelper().CreateFormulaEvaluator();
            EvaluateAllFormulaCells(wb, evaluator);
        }
        private static void EvaluateAllFormulaCells(IWorkbook wb, IFormulaEvaluator evaluator)
        {
            for (int i = 0; i < wb.NumberOfSheets; i++)
            {
                ISheet sheet = wb.GetSheetAt(i);
 
                for (IEnumerator it = sheet.GetRowEnumerator(); it.MoveNext(); )
                {
                    IRow r = (IRow)it.Current;
                    foreach (ICell c in r.Cells)
                    {
                        if (c.CellType == CellType.FORMULA)
                        {
                            evaluator.EvaluateFormulaCell(c);
                        }
                    }
                }
            }
        }
 
 
        public void EvaluateAll()
        {
            HSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook, this);
        }
 
    }
}