[12074] | 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 | *******************************************************************************/
|
---|
| 31 | using System;
|
---|
| 32 | using System.Collections.Generic;
|
---|
| 33 | using System.Linq;
|
---|
| 34 | using System.Text;
|
---|
| 35 | using OfficeOpenXml.FormulaParsing.Excel.Functions;
|
---|
| 36 | using OfficeOpenXml.FormulaParsing.Utilities;
|
---|
| 37 | namespace OfficeOpenXml.FormulaParsing
|
---|
| 38 | {
|
---|
| 39 | /// <summary>
|
---|
| 40 | /// Provides access to various functionality regarding
|
---|
| 41 | /// excel formula evaluation.
|
---|
| 42 | /// </summary>
|
---|
| 43 | public class FormulaParserManager
|
---|
| 44 | {
|
---|
| 45 | private readonly FormulaParser _parser;
|
---|
| 46 |
|
---|
| 47 | internal FormulaParserManager(FormulaParser parser)
|
---|
| 48 | {
|
---|
| 49 | Require.That(parser).Named("parser").IsNotNull();
|
---|
| 50 | _parser = parser;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// Loads a module containing custom functions to the formula parser. By using
|
---|
| 55 | /// this method you can add your own implementations of Excel functions, by
|
---|
| 56 | /// implementing a <see cref="IFunctionModule"/>.
|
---|
| 57 | /// </summary>
|
---|
| 58 | /// <param name="module">A <see cref="IFunctionModule"/> containing <see cref="ExcelFunction"/>s.</param>
|
---|
| 59 | public void LoadFunctionModule(IFunctionModule module)
|
---|
| 60 | {
|
---|
| 61 | _parser.Configure(x => x.FunctionRepository.LoadModule(module));
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /// <summary>
|
---|
| 65 | /// If the supplied <paramref name="functionName"/> does not exist, the supplied
|
---|
| 66 | /// <paramref name="functionImpl"/> implementation will be added to the formula parser.
|
---|
| 67 | /// If it exists, the existing function will be replaced by the supplied <paramref name="functionImpl">function implementation</paramref>
|
---|
| 68 | /// </summary>
|
---|
| 69 | /// <param name="functionName"></param>
|
---|
| 70 | /// <param name="functionImpl"></param>
|
---|
| 71 | public void AddOrReplaceFunction(string functionName, ExcelFunction functionImpl)
|
---|
| 72 | {
|
---|
| 73 | _parser.Configure(x => x.FunctionRepository.AddOrReplaceFunction(functionName, functionImpl));
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /// <summary>
|
---|
| 77 | /// Returns an enumeration of all functions implemented, both the built in functions
|
---|
| 78 | /// and functions added using the LoadFunctionModule method of this class.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <returns>Function names in lower case</returns>
|
---|
| 81 | public IEnumerable<string> GetImplementedFunctionNames()
|
---|
| 82 | {
|
---|
| 83 | var fnList = _parser.FunctionNames.ToList();
|
---|
| 84 | fnList.Sort((x, y) => String.Compare(x, y, System.StringComparison.Ordinal));
|
---|
| 85 | return fnList;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /// <summary>
|
---|
| 89 | /// Parses the supplied <paramref name="formula"/> and returns the result.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <param name="formula"></param>
|
---|
| 92 | /// <returns></returns>
|
---|
| 93 | public object Parse(string formula)
|
---|
| 94 | {
|
---|
| 95 | return _parser.Parse(formula);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|