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
using System;
using System.Collections.Generic;
using System.Text;
using HH.WMS.Utils.NPOI.SS.Formula.Eval;
using HH.WMS.Utils.NPOI.SS.Formula.Functions;
 
namespace HH.WMS.Utils.NPOI.SS.Formula
{
    public class UserDefinedFunction : FreeRefFunction
    {
 
        public static FreeRefFunction instance = new UserDefinedFunction();
 
        private UserDefinedFunction()
        {
            // enforce Singleton
        }
 
        public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
        {
            int nIncomingArgs = args.Length;
            if (nIncomingArgs < 1)
            {
                throw new Exception("function name argument missing");
            }
 
            ValueEval nameArg = args[0];
            String functionName = string.Empty ;
            if (nameArg is NameEval)
            {
                functionName = ((NameEval)nameArg).FunctionName;
            }
            else if (nameArg is NameXEval)
            {
                functionName = ec.GetWorkbook().ResolveNameXText(((NameXEval)nameArg).Ptg);
            }
            else
            {
                throw new Exception("First argument should be a NameEval, but got ("
                        + nameArg.GetType().Name + ")");
            }
            FreeRefFunction targetFunc = ec.FindUserDefinedFunction(functionName);
            if (targetFunc == null)
            {
                throw new NotImplementedException(functionName);
            }
            int nOutGoingArgs = nIncomingArgs - 1;
            ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
            Array.Copy(args, 1, outGoingArgs, 0, nOutGoingArgs);
            return targetFunc.Evaluate(outGoingArgs, ec);
        }
    }
}