Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExpressionGraph/FunctionCompilers/IfFunctionCompiler.cs @ 13401

Last change on this file since 13401 was 12074, checked in by sraggl, 9 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 4.6 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Mats Alm                       Added                   2014-01-27
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using OfficeOpenXml.FormulaParsing.Excel.Functions;
36using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
37using OfficeOpenXml.FormulaParsing.Exceptions;
38using OfficeOpenXml.FormulaParsing.Utilities;
39
40namespace OfficeOpenXml.FormulaParsing.ExpressionGraph.FunctionCompilers
41{
42    /// <summary>
43    /// Why do the If function require a compiler of its own you might ask;)
44    ///
45    /// It is because it only needs to evaluate one of the two last expressions. This
46    /// compiler handles this - it ignores the irrelevant expression.
47    /// </summary>
48    public class IfFunctionCompiler : FunctionCompiler
49    {
50        public IfFunctionCompiler(ExcelFunction function)
51            : base(function)
52        {
53            Require.That(function).Named("function").IsNotNull();
54            if (!(function is If)) throw new ArgumentException("function must be of type If");
55        }
56
57        public override CompileResult Compile(IEnumerable<Expression> children, ParsingContext context)
58        {
59            if(children.Count() < 3) throw new ExcelErrorValueException(eErrorType.Value);
60            var args = new List<FunctionArgument>();
61            Function.BeforeInvoke(context);
62            var firstChild = children.ElementAt(0);
63            var v = firstChild.Compile().Result;
64
65            /****  Handle names and ranges ****/
66            if (v is ExcelDataProvider.INameInfo)
67            {
68                v = ((ExcelDataProvider.INameInfo)v).Value;
69            }
70           
71            if (v is ExcelDataProvider.IRangeInfo)
72            {
73                var r=((ExcelDataProvider.IRangeInfo)v);
74                if(r.GetNCells()>1)
75                {
76                    throw(new ArgumentException("Logical can't be more than one cell"));
77                }
78                v = r.GetOffset(0, 0);
79            }
80            bool boolVal;
81            if(v is bool)
82            {
83                boolVal = (bool)v;
84            }
85            else
86            {
87                if(OfficeOpenXml.Utils.ConvertUtil.IsNumeric(v))
88                {
89                    boolVal = OfficeOpenXml.Utils.ConvertUtil.GetValueDouble(v)!=0;
90                }
91                else
92                {
93                    throw (new ArgumentException("Invalid logical test"));
94                }
95            }
96            /****  End Handle names and ranges ****/
97           
98            args.Add(new FunctionArgument(boolVal));
99            if (boolVal)
100            {
101                var val = children.ElementAt(1).Compile().Result;
102                args.Add(new FunctionArgument(val));
103                args.Add(new FunctionArgument(null));
104            }
105            else
106            {
107                var val = children.ElementAt(2).Compile().Result;
108                args.Add(new FunctionArgument(null));
109                args.Add(new FunctionArgument(val));
110            }
111            return Function.Execute(args, context);
112        }
113    }
114}
Note: See TracBrowser for help on using the repository browser.