Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Implementing missing or new Excel functions

Mats Alm edited this page Sep 6, 2017 · 11 revisions

Epplus has a number of builtin Excel functions, these should cover the most commonly used functions in Excel. For various reasons you might want to add new ones - for example, you could implement missing Excel functions or you could implement functions that corresponds to VBA functions in a workbook. Here is how you do it:

The ExcelFunction class

Every function in Epplus must inherit the abstract OfficeOpenXml.FormulaParsing.Excel.Functions.ExcelFunction. Here is an example of how to implement a function, using the existing Cos function in EPPlus:

    public class Cos : ExcelFunction
    {
        public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 1);
            var arg = ArgToDecimal(arguments, 0);
            return CreateResult(System.Math.Cos(arg), DataType.Decimal);
        }
    }

Epplus will execute this function and send in the arguments to the function via the parameter arguments. The parameter _context _contains some useful information, one example is context.Scopes.Current.Address which gives you the address of the current cell.

ValidateArguments, ArgToDecimal and CreateResult are examples of helper methods that you will find in the ExcelFunction class.

Add a new function to EPPlus code base

New functions should be added to the namespace hierarchy under OfficeOpenXml.FormulaParsing.Excel.Functions. When this is done you need to register the new function in the constructur of the OfficeOpenXml.FormulaParsing.Excel.Functions.BuildInFunctions class.

Functions["cos"] = new Cos();

If you want to commit new functions to the EPPlus project you must also write unit tests for the added function/functions. These tests are located in the _EPPlusTest _project, see namespace EPPlusTest.Excel.Functions. Contributions/Pull requests without test coverage will not be accepted.