Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.12/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/Excel/Functions/RefAndLookup/Match.cs @ 13398

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 4.5 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.Linq;
28using System.Text;
29using OfficeOpenXml.FormulaParsing.ExpressionGraph;
30using OfficeOpenXml.FormulaParsing.ExcelUtilities;
31
32namespace OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup
33{
34    public class Match : LookupFunction
35    {
36        private enum MatchType
37        {
38            ClosestAbove = -1,
39            ExactMatch = 0,
40            ClosestBelow = 1
41        }
42
43        public Match()
44            : base(new WildCardValueMatcher(), new CompileResultFactory())
45        {
46
47        }
48
49        public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
50        {
51            ValidateArguments(arguments, 2);
52
53            var searchedValue = arguments.ElementAt(0).Value;
54            var address = ArgToString(arguments, 1);
55            var rangeAddressFactory = new RangeAddressFactory(context.ExcelDataProvider);
56            var rangeAddress = rangeAddressFactory.Create(address);
57            var matchType = GetMatchType(arguments);
58            var args = new LookupArguments(searchedValue, address, 0, 0, false);
59            var lookupDirection = GetLookupDirection(rangeAddress);
60            var navigator = LookupNavigatorFactory.Create(lookupDirection, args, context);
61            int? lastMatchResult = default(int?);
62            do
63            {
64                var matchResult = IsMatch(navigator.CurrentValue, searchedValue);
65                if (matchType == MatchType.ClosestBelow && matchResult >= 0)
66                {
67                    if (!lastMatchResult.HasValue && matchResult > 0)
68                    {
69                        // TODO: error handling. This happens only if the first item is
70                        // below the searched value.
71                    }
72                    var index = matchResult == 0 ? navigator.Index + 1 : navigator.Index;
73                    return CreateResult(index, DataType.Integer);
74                }
75                if (matchType == MatchType.ClosestAbove && matchResult <= 0)
76                {
77                    if (!lastMatchResult.HasValue && matchResult < 0)
78                    {
79                        // TODO: error handling. This happens only if the first item is
80                        // above the searched value
81                    }
82                    var index = matchResult == 0 ? navigator.Index + 1 : navigator.Index;
83                    return CreateResult(index, DataType.Integer);
84                }
85                if (matchType == MatchType.ExactMatch && matchResult == 0)
86                {
87                    return CreateResult(navigator.Index + 1, DataType.Integer);
88                }
89                lastMatchResult = matchResult;
90            }
91            while (navigator.MoveNext());
92            return CreateResult(null, DataType.Integer);
93        }
94
95        private MatchType GetMatchType(IEnumerable<FunctionArgument> arguments)
96        {
97            var matchType = MatchType.ClosestBelow;
98            if (arguments.Count() > 2)
99            {
100                matchType = (MatchType)ArgToInt(arguments, 2);
101            }
102            return matchType;
103        }
104    }
105}
Note: See TracBrowser for help on using the repository browser.