zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/* ====================================================================
   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
{
    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.Util;
    using HH.WMS.Utils.NPOI.SS.Formula.Functions;
    using HH.WMS.Utils.NPOI.SS.Formula.Udf;
    using System.Collections.Generic;
    using HH.WMS.Utils.NPOI.SS.UserModel;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    using HH.WMS.Utils.NPOI.Util;
 
    /**
     * Evaluates formula cells.<p/>
     *
     * For performance reasons, this class keeps a cache of all previously calculated intermediate
     * cell values.  Be sure To call {@link #ClearCache()} if any workbook cells are Changed between
     * calls To evaluate~ methods on this class.<br/>
     *
     * For POI internal use only
     *
     * @author Josh Micich
     */
    public class WorkbookEvaluator
    {
 
        private IEvaluationWorkbook _workbook;
        private EvaluationCache _cache;
        private int _workbookIx;
 
        private IEvaluationListener _evaluationListener;
        private Hashtable _sheetIndexesBySheet;
        private Dictionary<String, int> _sheetIndexesByName;
        private CollaboratingWorkbooksEnvironment _collaboratingWorkbookEnvironment;
        private IStabilityClassifier _stabilityClassifier;
        private UDFFinder _udfFinder;
 
        public WorkbookEvaluator(IEvaluationWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)
            : this(workbook, null, stabilityClassifier, udfFinder)
        {
 
        }
 
        public WorkbookEvaluator(IEvaluationWorkbook workbook, IEvaluationListener evaluationListener, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)
        {
            _workbook = workbook;
            _evaluationListener = evaluationListener;
            _cache = new EvaluationCache(evaluationListener);
            _sheetIndexesBySheet = new Hashtable();
            _sheetIndexesByName = new Dictionary<string, int>();
            _collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;
            _workbookIx = 0;
            _stabilityClassifier = stabilityClassifier;
 
            AggregatingUDFFinder defaultToolkit = // workbook can be null in unit tests
                workbook == null ? null : (AggregatingUDFFinder)workbook.GetUDFFinder();
            if (defaultToolkit != null && udfFinder != null)
            {
                defaultToolkit.Add(udfFinder);
            }
            _udfFinder = defaultToolkit;
        }
 
        /**
         * also for debug use. Used in ToString methods
         */
        /* package */
        public String GetSheetName(int sheetIndex)
        {
            return _workbook.GetSheetName(sheetIndex);
        }
 
        public WorkbookEvaluator GetOtherWorkbookEvaluator(String workbookName)
        {
            return _collaboratingWorkbookEnvironment.GetWorkbookEvaluator(workbookName);
        }
 
        internal IEvaluationWorkbook Workbook
        {
            get
            {
                return _workbook;
            }
        }
        internal IEvaluationSheet GetSheet(int sheetIndex)
        {
            return _workbook.GetSheet(sheetIndex);
        }
        /* package */
        internal IEvaluationName GetName(String name, int sheetIndex)
        {
            NamePtg namePtg = _workbook.GetName(name, sheetIndex).CreatePtg();
 
            if (namePtg == null)
            {
                return null;
            }
            else
            {
                return _workbook.GetName(namePtg);
            }
        }
        private static bool IsDebugLogEnabled()
        {
            return false;
        }
        private static void LogDebug(String s)
        {
            if (IsDebugLogEnabled())
            {
                Console.WriteLine(s);
            }
        }
        /* package */
        public void AttachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx)
        {
            _collaboratingWorkbookEnvironment = collaboratingWorkbooksEnvironment;
            _cache = cache;
            _workbookIx = workbookIx;
        }
        /* package */
        public CollaboratingWorkbooksEnvironment GetEnvironment()
        {
            return _collaboratingWorkbookEnvironment;
        }
 
        /* package */
        public void DetachFromEnvironment()
        {
            _collaboratingWorkbookEnvironment = CollaboratingWorkbooksEnvironment.EMPTY;
            _cache = new EvaluationCache(_evaluationListener);
            _workbookIx = 0;
        }
        /* package */
        public IEvaluationListener GetEvaluationListener()
        {
            return _evaluationListener;
        }
 
        /**
         * Should be called whenever there are Changes To input cells in the evaluated workbook.
         * Failure To call this method after changing cell values will cause incorrect behaviour
         * of the evaluate~ methods of this class
         */
        public void ClearAllCachedResultValues()
        {
            _cache.Clear();
            _sheetIndexesBySheet.Clear();
        }
 
        /**
         * Should be called To tell the cell value cache that the specified (value or formula) cell 
         * Has Changed.
         */
        public void NotifyUpdateCell(IEvaluationCell cell)
        {
            int sheetIndex = GetSheetIndex(cell.Sheet);
            _cache.NotifyUpdateCell(_workbookIx, sheetIndex, cell);
        }
        /**
         * Should be called To tell the cell value cache that the specified cell Has just been
         * deleted. 
         */
        public void NotifyDeleteCell(IEvaluationCell cell)
        {
            int sheetIndex = GetSheetIndex(cell.Sheet);
            _cache.NotifyDeleteCell(_workbookIx, sheetIndex, cell);
        }
 
        public int GetSheetIndex(IEvaluationSheet sheet)
        {
            object result = _sheetIndexesBySheet[sheet];
            if (result == null)
            {
                int sheetIndex = _workbook.GetSheetIndex(sheet);
                if (sheetIndex < 0)
                {
                    throw new Exception("Specified sheet from a different book");
                }
                result = sheetIndex;
                _sheetIndexesBySheet[sheet] = result;
            }
            return (int)result;
        }
        /* package */
        internal int GetSheetIndexByExternIndex(int externSheetIndex)
        {
            return _workbook.ConvertFromExternSheetIndex(externSheetIndex);
        }
        /**
         * Case-insensitive.
         * @return -1 if sheet with specified name does not exist
         */
        /* package */
        public int GetSheetIndex(String sheetName)
        {
            int result;
            if (_sheetIndexesByName.ContainsKey(sheetName))
            {
                result = _sheetIndexesByName[sheetName];
            }
            else
            {
                int sheetIndex = _workbook.GetSheetIndex(sheetName);
                if (sheetIndex < 0)
                {
                    return -1;
                }
                result = sheetIndex;
                _sheetIndexesByName[sheetName] = result;
            }
            return result;
        }
 
        public ValueEval Evaluate(IEvaluationCell srcCell)
        {
            int sheetIndex = GetSheetIndex(srcCell.Sheet);
            return EvaluateAny(srcCell, sheetIndex, srcCell.RowIndex, srcCell.ColumnIndex, new EvaluationTracker(_cache));
        }
 
 
        /**
         * @return never <c>null</c>, never {@link BlankEval}
         */
        private ValueEval EvaluateAny(IEvaluationCell srcCell, int sheetIndex,
                    int rowIndex, int columnIndex, EvaluationTracker tracker)
        {
            bool shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true
                    : !_stabilityClassifier.IsCellFinal(sheetIndex, rowIndex, columnIndex);
            ValueEval result;
            if (srcCell == null || srcCell.CellType != CellType.FORMULA)
            {
                result = GetValueFromNonFormulaCell(srcCell);
                if (shouldCellDependencyBeRecorded)
                {
                    tracker.AcceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result);
                }
                return result;
            }
 
            FormulaCellCacheEntry cce = _cache.GetOrCreateFormulaCellEntry(srcCell);
            if (shouldCellDependencyBeRecorded || cce.IsInputSensitive)
            {
                tracker.AcceptFormulaDependency(cce);
            }
            IEvaluationListener evalListener = _evaluationListener;
            if (cce.GetValue() == null)
            {
                if (!tracker.StartEvaluate(cce))
                {
                    return ErrorEval.CIRCULAR_REF_ERROR;
                }
                OperationEvaluationContext ec = new OperationEvaluationContext(this, _workbook, sheetIndex, rowIndex, columnIndex, tracker);
 
                try
                {
                    Ptg[] ptgs = _workbook.GetFormulaTokens(srcCell);
                    if (evalListener == null)
                    {
                        result = EvaluateFormula(ec, ptgs);
                    }
                    else
                    {
                        evalListener.OnStartEvaluate(srcCell, cce);
                        result = EvaluateFormula(ec, ptgs);
                        evalListener.OnEndEvaluate(cce, result);
                    }
 
                    tracker.UpdateCacheResult(result);
                }
                catch (NotImplementedException e)
                {
                    throw AddExceptionInfo(e, sheetIndex, rowIndex, columnIndex);
                }
                finally
                {
                    tracker.EndEvaluate(cce);
                }
            }
            else
            {
                if (evalListener != null)
                {
                    evalListener.OnCacheHit(sheetIndex, rowIndex, columnIndex, cce.GetValue());
                }
                return cce.GetValue();
            }
            if (IsDebugLogEnabled())
            {
                String sheetName = GetSheetName(sheetIndex);
                CellReference cr = new CellReference(rowIndex, columnIndex);
                LogDebug("Evaluated " + sheetName + "!" + cr.FormatAsString() + " To " + cce.GetValue().ToString());
            }
            // Usually (result === cce.getValue())
            // But sometimes: (result==ErrorEval.CIRCULAR_REF_ERROR, cce.getValue()==null)
            // When circular references are detected, the cache entry is only updated for
            // the top evaluation frame
            //return cce.GetValue();
            return result;
        }
        /**
 * Adds the current cell reference to the exception for easier debugging.
 * Would be nice to get the formula text as well, but that seems to require
 * too much digging around and casting to get the FormulaRenderingWorkbook.
 */
        private NotImplementedException AddExceptionInfo(NotImplementedException inner, int sheetIndex, int rowIndex, int columnIndex)
        {
            try
            {
                String sheetName = _workbook.GetSheetName(sheetIndex);
                CellReference cr = new CellReference(sheetName, rowIndex, columnIndex, false, false);
                String msg = "Error evaluating cell " + cr.FormatAsString();
                return new NotImplementedException(msg, inner);
            }
            catch (Exception)
            {
                // avoid bombing out during exception handling
                //e.printStackTrace();
                return inner; // preserve original exception
            }
        }
        /**
         * Gets the value from a non-formula cell.
         * @param cell may be <c>null</c>
         * @return {@link BlankEval} if cell is <c>null</c> or blank, never <c>null</c>
         */
        /* package */
        internal static ValueEval GetValueFromNonFormulaCell(IEvaluationCell cell)
        {
            if (cell == null)
            {
                return BlankEval.instance;
            }
            CellType cellType = cell.CellType;
            switch (cellType)
            {
                case CellType.NUMERIC:
                    return new NumberEval(cell.NumericCellValue);
                case CellType.STRING:
                    return new StringEval(cell.StringCellValue);
                case CellType.BOOLEAN:
                    return BoolEval.ValueOf(cell.BooleanCellValue);
                case CellType.BLANK:
                    return BlankEval.instance;
                case CellType.ERROR:
                    return ErrorEval.ValueOf(cell.ErrorCellValue);
            }
            throw new Exception("Unexpected cell type (" + cellType + ")");
        }
        // visibility raised for testing
        /* package */
        public ValueEval EvaluateFormula(OperationEvaluationContext ec, Ptg[] ptgs)
        {
 
            Stack<ValueEval> stack = new Stack<ValueEval>();
            for (int i = 0, iSize = ptgs.Length; i < iSize; i++)
            {
 
                // since we don't know how To handle these yet :(
                Ptg ptg = ptgs[i];
                if (ptg is AttrPtg)
                {
                    AttrPtg attrPtg = (AttrPtg)ptg;
                    if (attrPtg.IsSum)
                    {
                        // Excel prefers To encode 'SUM()' as a tAttr Token, but this evaluator
                        // expects the equivalent function Token
                        //byte nArgs = 1;  // tAttrSum always Has 1 parameter
                        ptg = FuncVarPtg.SUM;//.Create("SUM", nArgs);
                    }
                    if (attrPtg.IsOptimizedChoose)
                    {
                        ValueEval arg0 = stack.Pop();
                        int[] jumpTable = attrPtg.JumpTable;
                        int dist;
                        int nChoices = jumpTable.Length;
                        try
                        {
                            int switchIndex = Choose.EvaluateFirstArg(arg0, ec.RowIndex, ec.ColumnIndex);
                            if (switchIndex < 1 || switchIndex > nChoices)
                            {
                                stack.Push(ErrorEval.VALUE_INVALID);
                                dist = attrPtg.ChooseFuncOffset + 4; // +4 for tFuncFar(CHOOSE)
                            }
                            else
                            {
                                dist = jumpTable[switchIndex - 1];
                            }
                        }
                        catch (EvaluationException e)
                        {
                            stack.Push(e.GetErrorEval());
                            dist = attrPtg.ChooseFuncOffset + 4; // +4 for tFuncFar(CHOOSE)
                        }
                        // Encoded dist for tAttrChoose includes size of jump table, but
                        // countTokensToBeSkipped() does not (it counts whole tokens).
                        dist -= nChoices * 2 + 2; // subtract jump table size
                        i += CountTokensToBeSkipped(ptgs, i, dist);
                        continue;
                    }
                    if (attrPtg.IsOptimizedIf)
                    {
                        ValueEval arg0 = stack.Pop();
                        bool evaluatedPredicate;
                        try
                        {
                            evaluatedPredicate = If.EvaluateFirstArg(arg0, ec.RowIndex, ec.ColumnIndex);
                        }
                        catch (EvaluationException e)
                        {
                            stack.Push(e.GetErrorEval());
                            int dist = attrPtg.Data;
                            i += CountTokensToBeSkipped(ptgs, i, dist);
                            attrPtg = (AttrPtg)ptgs[i];
                            dist = attrPtg.Data + 1;
                            i += CountTokensToBeSkipped(ptgs, i, dist);
                            continue;
                        }
                        if (evaluatedPredicate)
                        {
                            // nothing to skip - true param folows
                        }
                        else
                        {
                            int dist = attrPtg.Data;
                            i += CountTokensToBeSkipped(ptgs, i, dist);
                            Ptg nextPtg = ptgs[i + 1];
                            if (ptgs[i] is AttrPtg && nextPtg is FuncVarPtg)
                            {
                                // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
                                i++;
                                stack.Push(BoolEval.FALSE);
                            }
                        }
                        continue;
                    }
                    if (attrPtg.IsSkip)
                    {
                        int dist = attrPtg.Data + 1;
                        i += CountTokensToBeSkipped(ptgs, i, dist);
                        if (stack.Peek() == MissingArgEval.instance)
                        {
                            stack.Pop();
                            stack.Push(BlankEval.instance);
                        }
                        continue;
                    }
                }
                if (ptg is ControlPtg)
                {
                    // skip Parentheses, Attr, etc
                    continue;
                }
                if (ptg is MemFuncPtg)
                {
                    // can ignore, rest of Tokens for this expression are in OK RPN order
                    continue;
                }
                if (ptg is MemErrPtg) { continue; }
 
                ValueEval opResult;
                if (ptg is OperationPtg)
                {
                    OperationPtg optg = (OperationPtg)ptg;
 
                    if (optg is UnionPtg) { continue; }
 
                    int numops = optg.NumberOfOperands;
                    ValueEval[] ops = new ValueEval[numops];
 
                    // storing the ops in reverse order since they are popping
                    for (int j = numops - 1; j >= 0; j--)
                    {
                        ValueEval p = (ValueEval)stack.Pop();
                        ops[j] = p;
                    }
                    //                logDebug("Invoke " + operation + " (nAgs=" + numops + ")");
                    opResult = OperationEvaluatorFactory.Evaluate(optg, ops, ec);
                }
                else
                {
                    opResult = GetEvalForPtg(ptg, ec);
                }
                if (opResult == null)
                {
                    throw new Exception("Evaluation result must not be null");
                }
                //            logDebug("push " + opResult);
                stack.Push(opResult);
            }
 
            ValueEval value = ((ValueEval)stack.Pop());
            if (stack.Count != 0)
            {
                throw new InvalidOperationException("evaluation stack not empty");
            }
            return DereferenceResult(value, ec.RowIndex, ec.ColumnIndex);
 
        }
        /**
 * Calculates the number of tokens that the evaluator should skip upon reaching a tAttrSkip.
 *
 * @return the number of tokens (starting from <c>startIndex+1</c>) that need to be skipped
 * to achieve the specified <c>distInBytes</c> skip distance.
 */
        private static int CountTokensToBeSkipped(Ptg[] ptgs, int startIndex, int distInBytes)
        {
            int remBytes = distInBytes;
            int index = startIndex;
            while (remBytes != 0)
            {
                index++;
                remBytes -= ptgs[index].Size;
                if (remBytes < 0)
                {
                    throw new Exception("Bad skip distance (wrong token size calculation).");
                }
                if (index >= ptgs.Length)
                {
                    throw new Exception("Skip distance too far (ran out of formula tokens).");
                }
            }
            return index - startIndex;
        }
        /**
         * Dereferences a single value from any AreaEval or RefEval evaluation result.
         * If the supplied evaluationResult is just a plain value, it is returned as-is.
         * @return a <c>NumberEval</c>, <c>StringEval</c>, <c>BoolEval</c>,
         *  <c>BlankEval</c> or <c>ErrorEval</c>. Never <c>null</c>.
         */
        public static ValueEval DereferenceResult(ValueEval evaluationResult, int srcRowNum, int srcColNum)
        {
            ValueEval value;
            try
            {
                value = OperandResolver.GetSingleValue(evaluationResult, srcRowNum, srcColNum);
            }
            catch (EvaluationException e)
            {
                return e.GetErrorEval();
            }
            if (value == BlankEval.instance)
            {
                // Note Excel behaviour here. A blank value is converted To zero.
                return NumberEval.ZERO;
                // Formulas _never_ evaluate To blank.  If a formula appears To have evaluated To
                // blank, the actual value is empty string. This can be verified with ISBLANK().
            }
            return value;
        }
        /**
         * returns an appropriate Eval impl instance for the Ptg. The Ptg must be
         * one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
         * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
         * passed here!
         */
        private ValueEval GetEvalForPtg(Ptg ptg, OperationEvaluationContext ec)
        {
            //  consider converting all these (ptg is XxxPtg) expressions To (ptg.GetType() == XxxPtg.class)
 
            if (ptg is NamePtg)
            {
                // named ranges, macro functions
                NamePtg namePtg = (NamePtg)ptg;
                IEvaluationName nameRecord = _workbook.GetName(namePtg);
                if (nameRecord.IsFunctionName)
                {
                    return new NameEval(nameRecord.NameText);
                }
                if (nameRecord.HasFormula)
                {
                    return EvaluateNameFormula(nameRecord.NameDefinition, ec);
                }
 
                throw new Exception("Don't now how To evalate name '" + nameRecord.NameText + "'");
            }
            if (ptg is NameXPtg)
            {
                return ec.GetNameXEval(((NameXPtg)ptg));
            }
 
            if (ptg is IntPtg)
            {
                return new NumberEval(((IntPtg)ptg).Value);
            }
            if (ptg is NumberPtg)
            {
                return new NumberEval(((NumberPtg)ptg).Value);
            }
            if (ptg is StringPtg)
            {
                return new StringEval(((StringPtg)ptg).Value);
            }
            if (ptg is BoolPtg)
            {
                return BoolEval.ValueOf(((BoolPtg)ptg).Value);
            }
            if (ptg is ErrPtg)
            {
                return ErrorEval.ValueOf(((ErrPtg)ptg).ErrorCode);
            }
            if (ptg is MissingArgPtg)
            {
                return MissingArgEval.instance;
            }
            if (ptg is AreaErrPtg || ptg is RefErrorPtg
                    || ptg is DeletedArea3DPtg || ptg is DeletedRef3DPtg)
            {
                return ErrorEval.REF_INVALID;
            }
            if (ptg is Ref3DPtg)
            {
                Ref3DPtg rptg = (Ref3DPtg)ptg;
                return ec.GetRef3DEval(rptg.Row, rptg.Column, rptg.ExternSheetIndex);
            }
            if (ptg is Area3DPtg)
            {
                Area3DPtg aptg = (Area3DPtg)ptg;
                return ec.GetArea3DEval(aptg.FirstRow, aptg.FirstColumn, aptg.LastRow, aptg.LastColumn, aptg.ExternSheetIndex);
            }
            if (ptg is RefPtg)
            {
                RefPtg rptg = (RefPtg)ptg;
                return ec.GetRefEval(rptg.Row, rptg.Column);
            }
            if (ptg is AreaPtg)
            {
                AreaPtg aptg = (AreaPtg)ptg;
                return ec.GetAreaEval(aptg.FirstRow, aptg.FirstColumn, aptg.LastRow, aptg.LastColumn);
            }
 
            if (ptg is UnknownPtg)
            {
                // POI uses UnknownPtg when the encoded Ptg array seems To be corrupted.
                // This seems To occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790)
                // In any case, formulas are re-parsed before execution, so UnknownPtg should not Get here
                throw new RuntimeException("UnknownPtg not allowed");
            }
            if (ptg is ExpPtg)
            {
                // ExpPtg is used for array formulas and shared formulas.
                // it is currently unsupported, and may not even get implemented here
                throw new RuntimeException("ExpPtg currently not supported");
            }
            throw new RuntimeException("Unexpected ptg class (" + ptg.GetType().Name + ")");
        }
        internal ValueEval EvaluateNameFormula(Ptg[] ptgs, OperationEvaluationContext ec)
        {
            if (ptgs.Length > 1)
            {
                throw new Exception("Complex name formulas not supported yet");
            }
            return GetEvalForPtg(ptgs[0], ec);
        }
 
        /**
         * Used by the lazy ref evals whenever they need To Get the value of a contained cell.
         */
        /* package */
        public ValueEval EvaluateReference(IEvaluationSheet sheet, int sheetIndex, int rowIndex,
            int columnIndex, EvaluationTracker tracker)
        {
 
            IEvaluationCell cell = sheet.GetCell(rowIndex, columnIndex);
            return EvaluateAny(cell, sheetIndex, rowIndex, columnIndex, tracker);
        }
 
        public FreeRefFunction FindUserDefinedFunction(String functionName)
        {
            return _udfFinder.FindFunction(functionName);
        }
    }
}