Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExcelUtilities/ValueMatcher.cs @ 15888

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 4.1 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                   2013-03-01 (Prior file history on https://github.com/swmal/ExcelFormulaParser)
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35
36namespace OfficeOpenXml.FormulaParsing.ExcelUtilities
37{
38    public class ValueMatcher
39    {
40        public const int IncompatibleOperands = -2;
41
42        public virtual int IsMatch(object o1, object o2)
43        {
44            if (o1 != null && o2 == null) return 1;
45            if (o1 == null && o2 != null) return -1;
46            if (o1 == null && o2 == null) return 0;
47            //Handle ranges and defined names
48            o1 = CheckGetRange(o1);
49            o2 = CheckGetRange(o2);
50
51            if (o1 is string && o2 is string)
52            {
53                return CompareStringToString(o1.ToString().ToLower(), o2.ToString().ToLower());
54            }
55            else if( o1.GetType() == typeof(string))
56            {
57                return CompareStringToObject(o1.ToString(), o2);
58            }
59            else if (o2.GetType() == typeof(string))
60            {
61                return CompareObjectToString(o1, o2.ToString());
62            }
63            return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
64        }
65
66        private static object CheckGetRange(object v)
67        {
68            if (v is ExcelDataProvider.IRangeInfo)
69            {
70                var r = ((ExcelDataProvider.IRangeInfo)v);
71                if (r.GetNCells() > 1)
72                {
73                    v = ExcelErrorValue.Create(eErrorType.NA);
74                }
75                v = r.GetOffset(0, 0);
76            }
77            else if (v is ExcelDataProvider.INameInfo)
78            {
79                var n = ((ExcelDataProvider.INameInfo)v);
80                v = CheckGetRange(n);
81            }
82            return v;
83        }
84
85        protected virtual int CompareStringToString(string s1, string s2)
86        {
87            return s1.CompareTo(s2);
88        }
89
90        protected virtual int CompareStringToObject(string o1, object o2)
91        {
92            double d1;
93            if (double.TryParse(o1, out d1))
94            {
95                return d1.CompareTo(Convert.ToDouble(o2));
96            }
97            return IncompatibleOperands;
98        }
99
100        protected virtual int CompareObjectToString(object o1, string o2)
101        {
102            double d2;
103            if (double.TryParse(o2, out d2))
104            {
105                return Convert.ToDouble(o1).CompareTo(d2);
106            }
107            return IncompatibleOperands;
108        }
109    }
110}
Note: See TracBrowser for help on using the repository browser.