Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/Excel/Functions/FunctionRepository.cs

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 4.8 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.Excel.Functions;
31using OfficeOpenXml.FormulaParsing.Utilities;
32using OfficeOpenXml.FormulaParsing.Exceptions;
33
34namespace OfficeOpenXml.FormulaParsing.Excel.Functions
35{
36    /// <summary>
37    /// This class provides methods for accessing/modifying VBA Functions.
38    /// </summary>
39    public class FunctionRepository : IFunctionNameProvider
40    {
41        private Dictionary<string, ExcelFunction> _functions = new Dictionary<string, ExcelFunction>(StringComparer.InvariantCulture);
42
43        private FunctionRepository()
44        {
45
46        }
47
48        public static FunctionRepository Create()
49        {
50            var repo = new FunctionRepository();
51            repo.LoadModule(new BuiltInFunctions());
52            return repo;
53        }
54
55        /// <summary>
56        /// Loads a module of <see cref="ExcelFunction"/>s to the function repository.
57        /// </summary>
58        /// <param name="module">A <see cref="IFunctionModule"/> that can be used for adding functions</param>
59        public virtual void LoadModule(IFunctionModule module)
60        {
61            foreach (var key in module.Functions.Keys)
62            {
63                var lowerKey = key.ToLower(CultureInfo.InvariantCulture);
64                _functions[lowerKey] = module.Functions[key];
65            }
66        }
67
68        public virtual ExcelFunction GetFunction(string name)
69        {
70            if(!_functions.ContainsKey(name.ToLower(CultureInfo.InvariantCulture)))
71            {
72                //throw new InvalidOperationException("Non supported function: " + name);
73                //throw new ExcelErrorValueException("Non supported function: " + name, ExcelErrorValue.Create(eErrorType.Name));
74                return null;
75            }
76            return _functions[name.ToLower(CultureInfo.InvariantCulture)];
77        }
78
79        /// <summary>
80        /// Removes all functions from the repository
81        /// </summary>
82        public virtual void Clear()
83        {
84            _functions.Clear();
85        }
86
87        /// <summary>
88        /// Returns true if the the supplied <paramref name="name"/> exists in the repository.
89        /// </summary>
90        /// <param name="name"></param>
91        /// <returns></returns>
92        public bool IsFunctionName(string name)
93        {
94            return _functions.ContainsKey(name.ToLower(CultureInfo.InvariantCulture));
95        }
96
97        /// <summary>
98        /// Returns the names of all implemented functions.
99        /// </summary>
100        public IEnumerable<string> FunctionNames
101        {
102            get { return _functions.Keys; }
103        }
104
105        /// <summary>
106        /// Adds or replaces a function.
107        /// </summary>
108        /// <param name="functionName"> Case-insensitive name of the function that should be added or replaced.</param>
109        /// <param name="functionImpl">An implementation of an <see cref="ExcelFunction"/>.</param>
110        public void AddOrReplaceFunction(string functionName, ExcelFunction functionImpl)
111        {
112            Require.That(functionName).Named("functionName").IsNotNullOrEmpty();
113            Require.That(functionImpl).Named("functionImpl").IsNotNull();
114            var fName = functionName.ToLower(CultureInfo.InvariantCulture);
115            if (_functions.ContainsKey(fName))
116            {
117                _functions.Remove(fName);
118            }
119            _functions[fName] = functionImpl;
120        }
121    }
122}
Note: See TracBrowser for help on using the repository browser.