/* Copyright (C) 2011 Jan Källman * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html * * All code and executables are provided "as is" with no warranty either express or implied. * The author accepts no liability for any damage or loss of business that this product may cause. * * Code change notes: * * Author Change Date ******************************************************************************* * Mats Alm Added 2013-12-03 *******************************************************************************/ using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using OfficeOpenXml.FormulaParsing.Excel.Functions; using OfficeOpenXml.FormulaParsing.Utilities; using OfficeOpenXml.FormulaParsing.Exceptions; namespace OfficeOpenXml.FormulaParsing.Excel.Functions { /// /// This class provides methods for accessing/modifying VBA Functions. /// public class FunctionRepository : IFunctionNameProvider { private Dictionary _functions = new Dictionary(StringComparer.InvariantCulture); private FunctionRepository() { } public static FunctionRepository Create() { var repo = new FunctionRepository(); repo.LoadModule(new BuiltInFunctions()); return repo; } /// /// Loads a module of s to the function repository. /// /// A that can be used for adding functions public virtual void LoadModule(IFunctionModule module) { foreach (var key in module.Functions.Keys) { var lowerKey = key.ToLower(CultureInfo.InvariantCulture); _functions[lowerKey] = module.Functions[key]; } } public virtual ExcelFunction GetFunction(string name) { if(!_functions.ContainsKey(name.ToLower(CultureInfo.InvariantCulture))) { //throw new InvalidOperationException("Non supported function: " + name); //throw new ExcelErrorValueException("Non supported function: " + name, ExcelErrorValue.Create(eErrorType.Name)); return null; } return _functions[name.ToLower(CultureInfo.InvariantCulture)]; } /// /// Removes all functions from the repository /// public virtual void Clear() { _functions.Clear(); } /// /// Returns true if the the supplied exists in the repository. /// /// /// public bool IsFunctionName(string name) { return _functions.ContainsKey(name.ToLower(CultureInfo.InvariantCulture)); } /// /// Returns the names of all implemented functions. /// public IEnumerable FunctionNames { get { return _functions.Keys; } } /// /// Adds or replaces a function. /// /// Case-insensitive name of the function that should be added or replaced. /// An implementation of an . public void AddOrReplaceFunction(string functionName, ExcelFunction functionImpl) { Require.That(functionName).Named("functionName").IsNotNullOrEmpty(); Require.That(functionImpl).Named("functionImpl").IsNotNull(); var fName = functionName.ToLower(CultureInfo.InvariantCulture); if (_functions.ContainsKey(fName)) { _functions.Remove(fName); } _functions[fName] = functionImpl; } } }