示例#1
0
 protected void BuildFunctionArguments(CompileResult compileResult, DataType dataType, List <FunctionArgument> args)
 {
     if (compileResult.Result is IEnumerable <object> && !(compileResult.Result is ExcelDataProvider.IRangeInfo))
     {
         var compileResultFactory = new CompileResultFactory();
         var argList = new List <FunctionArgument>();
         var objects = compileResult.Result as IEnumerable <object>;
         foreach (var arg in objects)
         {
             var cr = compileResultFactory.Create(arg);
             BuildFunctionArguments(cr, dataType, argList);
         }
         args.Add(new FunctionArgument(argList));
     }
     else
     {
         var funcArg = new FunctionArgument(compileResult.Result, dataType);
         funcArg.ExcelAddressReferenceId = compileResult.ExcelAddressReferenceId;
         if (compileResult.IsHiddenCell)
         {
             funcArg.SetExcelStateFlag(Excel.ExcelCellState.HiddenCell);
         }
         args.Add(funcArg);
     }
 }
示例#2
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 2);
            var items = new List <object>();

            for (int x = 0; x < arguments.Count(); x++)
            {
                items.Add(arguments.ElementAt(x).ValueFirst);
            }

            var chooseIndices = arguments.ElementAt(0).ValueFirst as IEnumerable <FunctionArgument>;

            if (chooseIndices != null && chooseIndices.Count() > 1)
            {
                IntArgumentParser intParser = new IntArgumentParser();
                object[]          values    = chooseIndices.Select(chosenIndex => items[(int)intParser.Parse(chosenIndex.ValueFirst)]).ToArray();
                return(CreateResult(values, DataType.Enumerable));
            }
            else
            {
                var index        = ArgToInt(arguments, 0);
                var choosedValue = arguments.ElementAt(index).Value;
                if (choosedValue is IRangeInfo)
                {
                    return(CreateResult(choosedValue, DataType.Enumerable));
                }
                var factory = new CompileResultFactory();
                return(factory.Create(choosedValue));
            }
        }
示例#3
0
        public void CreateIntResult()
        {
            int           value         = 1437;
            CompileResult compileResult = new CompileResultFactory().Create(value);

            Assert.AreEqual(value, compileResult.Result);
            Assert.AreEqual(DataType.Integer, compileResult.DataType);
        }
示例#4
0
        public void CreateDecimalResult()
        {
            decimal       value         = 1437.756m;
            CompileResult compileResult = new CompileResultFactory().Create(value);

            Assert.AreEqual(value, compileResult.Result);
            Assert.AreEqual(DataType.Decimal, compileResult.DataType);
        }
示例#5
0
        public void CreateResultByteConvertsToInt()
        {
            byte          value         = 7;
            CompileResult compileResult = new CompileResultFactory().Create(value);

            Assert.AreEqual(value, compileResult.Result);
            Assert.AreEqual(DataType.Integer, compileResult.DataType);
        }
示例#6
0
        public void CreateStringResult()
        {
            string        value         = "SomeStringTextHere";
            CompileResult compileResult = new CompileResultFactory().Create(value);

            Assert.AreEqual(value, compileResult.Result);
            Assert.AreEqual(DataType.String, compileResult.DataType);
        }
示例#7
0
        public void CreateNameErrorStringResult()
        {
            string        value         = "#NAME?";
            CompileResult compileResult = new CompileResultFactory().Create(value);

            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Name), compileResult.Result);
            Assert.AreEqual(DataType.ExcelError, compileResult.DataType);
        }
示例#8
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 3);
            var condition       = ArgToBool(arguments, 0);
            var firstStatement  = arguments.ElementAt(1).Value;
            var secondStatement = arguments.ElementAt(2).Value;
            var factory         = new CompileResultFactory();

            return(condition ? factory.Create(firstStatement) : factory.Create(secondStatement));
        }
示例#9
0
        public void CreateListEnumerableResult()
        {
            List <string> listEnumerableObj = new List <string> {
                "asasd", "wrtdgff", "sdfsfds"
            };
            CompileResult compileResult = new CompileResultFactory().Create(listEnumerableObj);

            Assert.AreEqual(listEnumerableObj, compileResult.Result);
            Assert.AreEqual(DataType.Enumerable, compileResult.DataType);
        }
示例#10
0
        /// <summary>
        /// Compiles the expression.
        /// </summary>
        /// <returns>A <see cref="CompileResult"/> result of the expression compilation.</returns>
        public override CompileResult Compile()
        {
            var c    = this._parsingContext.Scopes.Current;
            var name = _parsingContext.ExcelDataProvider.GetName(c.Address.Worksheet, ExpressionString);

            if (name == null)
            {
                return(new CompileResult(eErrorType.Name));
            }
            if (name.Value == null)
            {
                return(null);
            }
            if (name.Value is ExcelDataProvider.IRangeInfo range)
            {
                if (range.IsMulti)
                {
                    return(new CompileResult(range, DataType.Enumerable));
                }
                else
                {
                    if (range.IsEmpty && range.Address._fromRowFixed == true && range.Address._fromColFixed == true)
                    {
                        return(null);
                    }
                    var address = range.Address;
                    var column  = address.Start.Column;
                    var row     = address.Start.Row;
                    if (address._fromColFixed == false)
                    {
                        column = (column - 1 + c.Address.FromCol) % ExcelPackage.MaxColumns;
                        if (column == 0)
                        {
                            column = ExcelPackage.MaxColumns;
                        }
                    }
                    if (address._fromRowFixed == false)
                    {
                        row = (row - 1 + c.Address.FromRow) % ExcelPackage.MaxRows;
                        if (row == 0)
                        {
                            row = ExcelPackage.MaxRows;
                        }
                    }
                    var factory = new CompileResultFactory();
                    return(factory.Create(range.Worksheet.Cells[row, column].Value));
                }
            }
            else
            {
                var factory = new CompileResultFactory();
                return(factory.Create(name.Value));
            }
        }
示例#11
0
        public void CreateErrorType()
        {
            var factory = new CompileResultFactory();

            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Value), factory.Create("#VALUE!").Result);
            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Name), factory.Create("#NAME?").Result);
            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Div0), factory.Create("#DIV/0!").Result);
            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.NA), factory.Create("#N/A").Result);
            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Null), factory.Create("#NULL!").Result);
            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Num), factory.Create("#NUM!").Result);
            Assert.AreEqual(ExcelErrorValue.Create(eErrorType.Ref), factory.Create("#REF!").Result);
        }
示例#12
0
        private bool EvaluateOperator(object left, object right, IOperator op)
        {
            var compileResultFactory = new CompileResultFactory();
            var leftResult           = compileResultFactory.Create(left);
            var rightResult          = compileResultFactory.Create(right);
            var result = op.Apply(leftResult, rightResult);

            if (result.DataType != DataType.Boolean)
            {
                throw new ArgumentException("Illegal operator in expression");
            }
            return((bool)result.Result);
        }
示例#13
0
文件: If.cs 项目: nxoxn/EPPlus
        /// <summary>
        /// Evaluates the specifed <paramref name="arguments"/> as follows:
        ///  - [0]: the condition to evaluate;
        ///  - [1]: the value to evaluate if true;
        ///  - [2]: the value to evaluate if false.
        /// </summary>
        /// <param name="arguments">The arguments to evaluate.</param>
        /// <param name="context">The context within which to evaluate the function.</param>
        /// <returns>The result of either the second or third argument if the condition evaluates to true or false, respectively.</returns>
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            if (this.ArgumentsAreValid(arguments, 3, out eErrorType argumentError) == false)
            {
                return(new CompileResult(argumentError));
            }
            var condition       = ArgToBool(arguments.ElementAt(0));
            var firstStatement  = arguments.ElementAt(1).Value;
            var secondStatement = arguments.ElementAt(2).Value;
            var factory         = new CompileResultFactory();

            return(condition ? factory.Create(firstStatement) : factory.Create(secondStatement));
        }
示例#14
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 2);
            var crf     = new CompileResultFactory();
            var maxArgs = arguments.Count() < (127 * 2) ? arguments.Count() : 127 * 2;

            for (var x = 0; x < maxArgs; x += 2)
            {
                if (System.Math.Round(ArgToDecimal(arguments, x), 15) != 0d)
                {
                    return(crf.Create(arguments.ElementAt(x + 1).Value));
                }
            }
            return(CreateResult(ExcelErrorValue.Create(eErrorType.NA), DataType.ExcelError));
        }
示例#15
0
        public void CalculateUsingEuropeanDates()
        {
            var us = new CultureInfo("en-US");

            Thread.CurrentThread.CurrentCulture = us;
            var crf     = new CompileResultFactory();
            var result  = crf.Create("1/15/2014");
            var numeric = result.ResultNumeric;

            Assert.AreEqual(41654, numeric);
            var gb = new CultureInfo("en-GB");

            Thread.CurrentThread.CurrentCulture = gb;
            var euroResult = crf.Create("15/1/2014");
            var eNumeric   = euroResult.ResultNumeric;

            Assert.AreEqual(41654, eNumeric);
        }
示例#16
0
文件: Index.cs 项目: user20112/EPPlus
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 2);
            var arg1 = arguments.ElementAt(0);
            var args = arg1.Value as IEnumerable <FunctionArgument>;
            var crf  = new CompileResultFactory();

            if (args != null)
            {
                var index = ArgToInt(arguments, 1);
                if (index > args.Count())
                {
                    throw new ExcelErrorValueException(eErrorType.Ref);
                }
                var candidate = args.ElementAt(index - 1);
                //Commented JK-Can be any data type
                //if (!IsNumber(candidate.Value))
                //{
                //    throw new ExcelErrorValueException(eErrorType.Value);
                //}
                //return CreateResult(ConvertUtil.GetValueDouble(candidate.Value), DataType.Decimal);
                return(crf.Create(candidate.Value));
            }
            if (arg1.IsExcelRange)
            {
                var row = ArgToInt(arguments, 1);
                var col = arguments.Count() > 2 ? ArgToInt(arguments, 2) : 1;
                var ri  = arg1.ValueAsRangeInfo;
                if (row > ri.Address._toRow - ri.Address._fromRow + 1 ||
                    col > ri.Address._toCol - ri.Address._fromCol + 1)
                {
                    ThrowExcelErrorValueException(eErrorType.Ref);
                }
                var candidate = ri.GetOffset(row - 1, col - 1);
                //Commented JK-Can be any data type
                //if (!IsNumber(candidate.Value))
                //{
                //    throw new ExcelErrorValueException(eErrorType.Value);
                //}
                return(crf.Create(candidate));
            }
            throw new NotImplementedException();
        }
示例#17
0
 public ExpressionEvaluator(WildCardValueMatcher wildCardValueMatcher, CompileResultFactory compileResultFactory)
 {
     _wildCardValueMatcher = wildCardValueMatcher;
     _compileResultFactory = compileResultFactory;
 }
示例#18
0
 public Switch(CompileResultFactory compileResultFactory)
 {
     _compileResultFactory = compileResultFactory;
 }
示例#19
0
文件: Choose.cs 项目: Stenrud/EPPlus
 public Choose()
 {
     _compileResultFactory = new CompileResultFactory();
 }
 public NumericExpressionEvaluator(ValueMatcher valueMatcher, CompileResultFactory compileResultFactory)
 {
     _valueMatcher         = valueMatcher;
     _compileResultFactory = compileResultFactory;
 }
示例#21
0
 public LookupFunction(ValueMatcher valueMatcher, CompileResultFactory compileResultFactory)
 {
     _valueMatcher         = valueMatcher;
     _compileResultFactory = compileResultFactory;
 }
示例#22
0
        /// <summary>
        /// Takes the data and the associated row/column value and returns the value from the cell range
        /// at the given row or column value.
        /// </summary>
        /// <param name="arguments">The cell range, the row value, and the column value.</param>
        /// <param name="context">The context in which the function is called.</param>
        /// <returns>A <see cref="CompileResult"/> result.</returns>
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            if (this.ArgumentsAreValid(arguments, 2, out eErrorType argumentError) == false)
            {
                return(new CompileResult(argumentError));
            }
            var cellRange        = arguments.ElementAt(0);
            var rowDataCandidate = arguments.ElementAt(1);

            var result = new CompileResultFactory();

            if (cellRange.Value is IEnumerable <FunctionArgument> args)
            {
                var index = this.ArgToInt(arguments, 1);
                if (index > args.Count())
                {
                    throw new ExcelErrorValueException(eErrorType.Ref);
                }
                var candidate = args.ElementAt(index - 1);
                return(base.CreateResult(candidate.Value, DataType.Integer));
            }
            if (rowDataCandidate != null && rowDataCandidate.Value is ExcelErrorValue && rowDataCandidate.Value.ToString() == ExcelErrorValue.Values.NA)
            {
                return(base.CreateResult(rowDataCandidate.Value, DataType.Integer));
            }

            // A single cell array ignores the row number and column number arguments, returning the single value in the array.
            if (!cellRange.IsExcelRange)
            {
                return(new CompileResult(cellRange.Value, cellRange.DataType));
            }
            else
            {
                var rowCandidate = arguments.ElementAt(1).DataType;
                if (rowCandidate == DataType.Date)
                {
                    return(new CompileResult(eErrorType.Ref));
                }
                else if (rowCandidate == DataType.Decimal)
                {
                    return(new CompileResult(eErrorType.Ref));
                }
                var row = this.ArgToInt(arguments, 1);

                if (row == 0)
                {
                    return(new CompileResult(eErrorType.Value));
                }
                if (row < 0)
                {
                    return(new CompileResult(eErrorType.Value));
                }

                var column = 1;
                if (arguments.Count() > 2)
                {
                    var colCandidate = arguments.ElementAt(2).DataType;
                    if (colCandidate == DataType.Date)
                    {
                        return(new CompileResult(eErrorType.Ref));
                    }
                    else if (colCandidate == DataType.Decimal)
                    {
                        return(new CompileResult(eErrorType.Ref));
                    }
                    else
                    {
                        column = this.ArgToInt(arguments, 2);
                    }
                }
                else
                if ((arguments.ElementAt(0).ValueAsRangeInfo.Address.Columns > 1) && arguments.ElementAt(0).ValueAsRangeInfo.Address.Rows > 1)
                {
                    return(new CompileResult(eErrorType.Ref));
                }
                if ((column == 0 && row == 0) || column < 0)
                {
                    return(new CompileResult(eErrorType.Value));
                }

                var rangeInfo = cellRange.ValueAsRangeInfo;
                if (rangeInfo.Address.Rows == 1 && arguments.Count() < 3)
                {
                    column = row;
                    row    = 1;
                }
                var numColumns = arguments.ElementAt(0).ValueAsRangeInfo.Address.Columns;
                if ((numColumns > 1 && column == 0))
                {
                    return(new CompileResult(eErrorType.Value));
                }
                if (row > rangeInfo.Address.Rows || column > rangeInfo.Address.Columns)
                {
                    return(new CompileResult(eErrorType.Ref));
                }
                if (row > rangeInfo.Address._toRow - rangeInfo.Address._fromRow + 1 || column > rangeInfo.Address._toCol - rangeInfo.Address._fromCol + 1)
                {
                    return(new CompileResult(eErrorType.Value));
                }
                var candidate = rangeInfo.GetOffset(row - 1, column - 1);
                if (column == 0)
                {
                    candidate = rangeInfo.GetOffset(row - 1, column);
                }
                return(base.CreateResult(candidate, DataType.Integer));
            }
        }