Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/Excel/Functions/Math/CountIf.cs @ 12074

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using OfficeOpenXml.FormulaParsing.ExcelUtilities;
6using OfficeOpenXml.FormulaParsing.ExpressionGraph;
7using OfficeOpenXml.FormulaParsing.Utilities;
8using OfficeOpenXml.Utils;
9using Require = OfficeOpenXml.FormulaParsing.Utilities.Require;
10
11namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Math
12{
13    public class CountIf : ExcelFunction
14    {
15        private readonly NumericExpressionEvaluator _numericExpressionEvaluator;
16        private readonly WildCardValueMatcher _wildCardValueMatcher;
17
18        public CountIf()
19            : this(new NumericExpressionEvaluator(), new WildCardValueMatcher())
20        {
21
22        }
23
24        public CountIf(NumericExpressionEvaluator evaluator, WildCardValueMatcher wildCardValueMatcher)
25        {
26            Require.That(evaluator).Named("evaluator").IsNotNull();
27            Require.That(wildCardValueMatcher).Named("wildCardValueMatcher").IsNotNull();
28            _numericExpressionEvaluator = evaluator;
29            _wildCardValueMatcher = wildCardValueMatcher;
30        }
31
32        private bool Evaluate(object obj, string expression)
33        {
34            double? candidate = default(double?);
35            if (IsNumeric(obj))
36            {
37                candidate = ConvertUtil.GetValueDouble(obj);
38            }
39            if (candidate.HasValue)
40            {
41                return _numericExpressionEvaluator.Evaluate(candidate.Value, expression);
42            }
43            return _wildCardValueMatcher.IsMatch(expression, obj.ToString()) == 0;
44        }
45
46        public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
47        {
48            var functionArguments = arguments as FunctionArgument[] ?? arguments.ToArray();
49            ValidateArguments(functionArguments, 2);
50            var range = functionArguments.ElementAt(0);
51            var criteria = ArgToString(functionArguments, 1);
52            double result = 0d;
53            if (range.IsExcelRange)
54            {
55                foreach (var cell in range.ValueAsRangeInfo)
56                {
57                    if (Evaluate(cell.Value, criteria))
58                    {
59                        result++;
60                    }
61                }
62            }
63            else if (range.Value is IEnumerable<FunctionArgument>)
64            {
65                foreach (var arg in (IEnumerable<FunctionArgument>) range.Value)
66                {
67                    if(Evaluate(arg.Value, criteria))
68                    {
69                        result++;
70                    }
71                }
72            }
73            else
74            {
75                if (Evaluate(range.Value, criteria))
76                {
77                    result++;
78                }
79            }
80            return CreateResult(result, DataType.Integer);
81        }
82    }
83}
Note: See TracBrowser for help on using the repository browser.