Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/Excel/Functions/Math/Average.cs

Last change on this file was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 4.1 KB
Line 
1/* Copyright (C) 2011  Jan Källman
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
11 * See the GNU Lesser General Public License for more details.
12 *
13 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
14 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
15 *
16 * All code and executables are provided "as is" with no warranty either express or implied.
17 * The author accepts no liability for any damage or loss of business that this product may cause.
18 *
19 * Code change notes:
20 *
21 * Author             Change            Date
22 *******************************************************************************
23 * Mats Alm                       Added                   2013-12-03
24 *******************************************************************************/
25using System;
26using System.Collections.Generic;
27using System.Globalization;
28using System.Linq;
29using System.Text;
30using OfficeOpenXml.FormulaParsing.ExpressionGraph;
31using OfficeOpenXml.Utils;
32
33namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Math
34{
35    public class Average : HiddenValuesHandlingFunction
36    {
37        public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
38        {
39            ValidateArguments(arguments, 1, eErrorType.Div0);
40            double nValues = 0d, result = 0d;
41            foreach (var arg in arguments)
42            {
43                Calculate(arg, context, ref result, ref nValues);
44            }
45            return CreateResult(Divide(result, nValues), DataType.Decimal);
46        }
47
48        private void Calculate(FunctionArgument arg, ParsingContext context, ref double retVal, ref double nValues, bool isInArray = false)
49        {
50            if (ShouldIgnore(arg))
51            {
52                return;
53            }
54            if (arg.Value is IEnumerable<FunctionArgument>)
55            {
56                foreach (var item in (IEnumerable<FunctionArgument>)arg.Value)
57                {
58                    Calculate(item, context, ref retVal, ref nValues, true);
59                }
60            }
61            else if (arg.IsExcelRange)
62            {
63                foreach (var c in arg.ValueAsRangeInfo)
64                {
65                    if (ShouldIgnore(c, context)) continue;
66                    CheckForAndHandleExcelError(c);
67                    if (!IsNumeric(c.Value)) continue;
68                    nValues++;
69                    retVal += c.ValueDouble;
70                }
71            }
72            else
73            {
74                var numericValue = GetNumericValue(arg.Value, isInArray);
75                if (numericValue.HasValue)
76                {
77                    nValues++;
78                    retVal += numericValue.Value;
79                }
80                else if ((arg.Value is string) && !ConvertUtil.IsNumericString(arg.Value))
81                {
82                    if (!isInArray)
83                    {
84                        ThrowExcelErrorValueException(eErrorType.Value);
85                    }
86                }
87            }
88            CheckForAndHandleExcelError(arg);
89        }
90
91        private double? GetNumericValue(object obj, bool isInArray)
92        {
93            if (IsNumeric(obj))
94            {
95                return ConvertUtil.GetValueDouble(obj);
96            }
97            else if ((obj is bool) && !isInArray)
98            {
99                return ConvertUtil.GetValueDouble(obj);
100            }
101            else if (ConvertUtil.IsNumericString(obj))
102            {
103                return double.Parse(obj.ToString(), CultureInfo.InvariantCulture);
104            }
105            return default(double?);
106        }
107    }
108}
Note: See TracBrowser for help on using the repository browser.