コード例 #1
0
        private object EvaluateFunctionOneArg(CalcFunction func, object arg, CalcEvaluatorContext context, bool acceptsArray, bool acceptsReference)
        {
            if (func.AcceptsReference(0) || CalcHelper.TryExtractToSingleValue(arg, out arg))
            {
                return(this.EvaluateFunction(func, new object[] { arg }, context, acceptsArray, acceptsReference, 0, 0));
            }
            CalcArray array = arg as CalcArray;

            object[,] values = new object[array.RowCount, array.ColumnCount];
            for (int i = 0; i < array.RowCount; i++)
            {
                for (int j = 0; j < array.ColumnCount; j++)
                {
                    values[i, j] = this.EvaluateFunction(func, new object[] { array.GetValue(i, j) }, context, acceptsArray, acceptsReference, i, j);
                }
            }
            return(new ConcreteArray <object>(values));
        }
コード例 #2
0
        private bool PrepareFunctionArguments(CalcFunctionExpression expr, CalcEvaluatorContext context, out CalcFunction func, out object[] args, out List <int> expandArrayArgIndex, out object error)
        {
            if (!this.TryGetFunction(expr, context, out func, out error))
            {
                args = null;
                expandArrayArgIndex = null;
                return(false);
            }
            expandArrayArgIndex = new List <int>();
            int argCount = expr.ArgCount;

            args  = new object[argCount];
            error = null;
            if (func.IsBranch)
            {
                int i = func.FindTestArgument();
                if ((i >= 0) && (i < argCount))
                {
                    object obj2;
                    if (!this.EvaluateFunctionArgument(expr.GetArg(i), context, func.AcceptsArray(i), func.AcceptsReference(i), func.AcceptsError(i), func.AcceptsMissingArgument(i), out obj2))
                    {
                        error = obj2;
                        expandArrayArgIndex = null;
                        return(false);
                    }
                    if ((context != null) && !context.ArrayFormulaMode)
                    {
                        if (obj2 is CalcReference)
                        {
                            obj2 = this.ExtractValueFromReference(obj2 as CalcReference, context.Row, context.Column);
                            if (!func.AcceptsError(i) && (obj2 is CalcError))
                            {
                                error = obj2;
                                expandArrayArgIndex = null;
                                return(false);
                            }
                            if (!func.AcceptsMissingArgument(i) && (obj2 is CalcMissingArgument))
                            {
                                obj2 = null;
                            }
                        }
                        else if ((obj2 is CalcArray) && !func.AcceptsArray(i))
                        {
                            if (!context.ExpandArrayToMultiCall)
                            {
                                obj2 = CalcHelper.GetArrayValue(obj2 as CalcArray, 0, 0);
                                if (!func.AcceptsError(i) && (obj2 is CalcError))
                                {
                                    error = obj2;
                                    expandArrayArgIndex = null;
                                    return(false);
                                }
                                if (!func.AcceptsMissingArgument(i) && (obj2 is CalcMissingArgument))
                                {
                                    obj2 = null;
                                }
                            }
                            else
                            {
                                expandArrayArgIndex.Add(i);
                            }
                        }
                    }
                    args[i] = obj2;
                }
                List <int> list  = new List <int>();
                CalcArray  array = CalcConvert.ToArray(args[i]);
                for (int j = 0; j < array.RowCount; j++)
                {
                    for (int k = 0; k < array.ColumnCount; k++)
                    {
                        int item = -1;
                        try
                        {
                            item = func.FindBranchArgument(array.GetValue(j, k));
                        }
                        catch (InvalidCastException)
                        {
                        }
                        if (item != -1)
                        {
                            if (!list.Contains(item))
                            {
                                list.Add(item);
                            }
                            if (list.Count >= (func.MaxArgs - 1))
                            {
                                break;
                            }
                        }
                    }
                }
                if (list.Count == 0)
                {
                    error = CalcErrors.Value;
                    expandArrayArgIndex = null;
                    return(false);
                }
                foreach (int num6 in list)
                {
                    if ((num6 >= 0) && (num6 < argCount))
                    {
                        object obj3;
                        if (!this.EvaluateFunctionArgument(expr.GetArg(num6), context, func.AcceptsArray(num6), func.AcceptsReference(num6), func.AcceptsError(num6), func.AcceptsMissingArgument(num6), out obj3))
                        {
                            error = obj3;
                            expandArrayArgIndex = null;
                            return(false);
                        }
                        if (((context != null) && !context.ArrayFormulaMode) && ((obj3 is CalcArray) && !func.AcceptsArray(num6)))
                        {
                            if (!context.ExpandArrayToMultiCall)
                            {
                                obj3 = CalcHelper.GetArrayValue(obj3 as CalcArray, 0, 0);
                                if (!func.AcceptsError(num6) && (obj3 is CalcError))
                                {
                                    error = obj3;
                                    expandArrayArgIndex = null;
                                    return(false);
                                }
                                if (!func.AcceptsMissingArgument(num6) && (obj3 is CalcMissingArgument))
                                {
                                    obj3 = null;
                                }
                            }
                            else
                            {
                                expandArrayArgIndex.Add(num6);
                            }
                        }
                        args[num6] = obj3;
                    }
                }
                if (expandArrayArgIndex.Count > 0)
                {
                    expandArrayArgIndex.Sort();
                }
            }
            else
            {
                for (int m = 0; m < argCount; m++)
                {
                    object obj4;
                    if (!this.EvaluateFunctionArgument(expr.GetArg(m), context, func.AcceptsArray(m), func.AcceptsReference(m), func.AcceptsError(m), func.AcceptsMissingArgument(m), out obj4))
                    {
                        error = obj4;
                        expandArrayArgIndex = null;
                        return(false);
                    }
                    if (((context != null) && !context.ArrayFormulaMode) && ((obj4 is CalcArray) && !func.AcceptsArray(m)))
                    {
                        if (!context.ExpandArrayToMultiCall)
                        {
                            obj4 = CalcHelper.GetArrayValue(obj4 as CalcArray, 0, 0);
                            if (!func.AcceptsError(m) && (obj4 is CalcError))
                            {
                                error = obj4;
                                expandArrayArgIndex = null;
                                return(false);
                            }
                            if (!func.AcceptsMissingArgument(m) && (obj4 is CalcMissingArgument))
                            {
                                obj4 = null;
                            }
                        }
                        else
                        {
                            expandArrayArgIndex.Add(m);
                        }
                    }
                    args[m] = obj4;
                }
            }
            return(true);
        }
コード例 #3
0
        private object EvaluateFunctionTwoOrMoreArgs(CalcFunction func, object[] args, CalcEvaluatorContext context, bool acceptsArray, bool acceptsReference)
        {
            int length = args.Length;

            bool[] flagArray = new bool[length];
            bool   flag      = true;

            for (int i = 0; i < length; i++)
            {
                object obj2  = null;
                bool   flag2 = func.AcceptsReference(i);
                bool   flag3 = flag2 || CalcHelper.TryExtractToSingleValue(args[i], out obj2);
                flag         = flag && flag3;
                flagArray[i] = flag3;
                if (!flag2)
                {
                    args[i] = obj2;
                }
            }
            if (flag)
            {
                return(this.EvaluateFunction(func, args, context, acceptsArray, acceptsReference, 0, 0));
            }
            int[] numArray  = new int[length];
            int[] numArray2 = new int[length];
            for (int j = 0; j < length; j++)
            {
                CalcArray array = args[j] as CalcArray;
                numArray[j]  = (flagArray[j] || (array == null)) ? -1 : array.RowCount;
                numArray2[j] = (flagArray[j] || (array == null)) ? -1 : array.ColumnCount;
            }
            int num4 = -1;
            int num5 = -1;

            if (!flagArray[0])
            {
                num4 = numArray[0];
                num5 = numArray2[0];
            }
            for (int k = 1; k < length; k++)
            {
                if (!flagArray[k])
                {
                    int num7 = numArray[k];
                    int num8 = numArray2[k];
                    num4 = (num4 == -1) ? num7 : ((num7 > 1) ? ((num4 > 1) ? Math.Min(num7, num4) : num7) : num4);
                    num5 = (num5 == -1) ? num8 : ((num8 > 1) ? ((num5 > 1) ? Math.Min(num8, num5) : num8) : num5);
                }
            }
            object[,] values = new object[num4, num5];
            using (context.EnterExpandArrayToMultiCallMode())
            {
                for (int m = 0; m < num4; m++)
                {
                    for (int n = 0; n < num5; n++)
                    {
                        object[] objArray2 = new object[length];
                        for (int num11 = 0; num11 < length; num11++)
                        {
                            objArray2[num11] = flagArray[num11] ? args[num11] : CalcHelper.GetArrayValue(args[num11] as CalcArray, m, n);
                        }
                        values[m, n] = this.EvaluateFunction(func, objArray2, context, acceptsArray, acceptsReference, m, n);
                    }
                }
                return(new ConcreteArray <object>(values));
            }
        }