FormulaParsing
FormulaParserManagerTests.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OfficeOpenXml;
using OfficeOpenXml.FormulaParsing;
using OfficeOpenXml.FormulaParsing.Excel.Functions;
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using OfficeOpenXml.FormulaParsing.ExpressionGraph.FunctionCompilers;
namespace EPPlusTest.FormulaParsing
{
[TestClast]
public clast FormulaParserManagerTests
{
#region test clastes
private clast MyFunction : ExcelFunction
{
public override CompileResult Execute(IEnumerable arguments, ParsingContext context)
{
throw new NotImplementedException();
}
}
private clast MyModule : IFunctionModule
{
public MyModule()
{
Functions = new Dictionary();
Functions.Add("MyFunction", new MyFunction());
CustomCompilers = new Dictionary();
}
public IDictionary Functions { get; }
public IDictionary CustomCompilers { get; }
}
#endregion
[TestMethod]
public void FunctionsShouldBeCopied()
{
using (var package1 = new ExcelPackage())
{
package1.Workbook.FormulaParserManager.LoadFunctionModule(new MyModule());
using (var package2 = new ExcelPackage())
{
var origNumberOfFuncs = package2.Workbook.FormulaParserManager.GetImplementedFunctionNames().Count();
// replace functions including the custom functions from package 1
package2.Workbook.FormulaParserManager.CopyFunctionsFrom(package1.Workbook);
// astertions: number of functions are increased with 1, and the list of function names contains the custom function.
astert.AreEqual(origNumberOfFuncs + 1, package2.Workbook.FormulaParserManager.GetImplementedFunctionNames().Count());
astert.IsTrue(package2.Workbook.FormulaParserManager.GetImplementedFunctionNames().Contains("myfunction"));
}
}
}
}
}