Example #1
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);
        }