Class ExcelFunction
- Namespace
- OfficeOpenXml.FormulaParsing.Excel.Functions
- Assembly
- EPPlus.dll
Base class for Excel function implementations.
public abstract class ExcelFunction
- Inheritance
-
ExcelFunction
- Derived
- Inherited Members
Constructors
ExcelFunction()
Default constructor
public ExcelFunction()
Fields
NumberOfSignificantFigures
Number of significant figures used in roundings, etc.
protected readonly int NumberOfSignificantFigures
Field Value
Properties
ArgumentMinLength
Returns the minimum arguments for the function. Number of arguments are validated before calling the execute. If lesser arguments are supplied a #VALUE! error will be returned.
public abstract int ArgumentMinLength { get; }
Property Value
ArrayBehaviour
Describes how the function works with input ranges and returning arrays.
public virtual ExcelFunctionArrayBehaviour ArrayBehaviour { get; }
Property Value
HandlesVariables
Indicates whether the function handles variables (eg. LET, LAMBDA).
public virtual bool HandlesVariables { get; }
Property Value
IsAllowedInCalculatedPivotTableField
If the function is allowed in a pivot table calculated field. Default is true, if not overridden.
public virtual bool IsAllowedInCalculatedPivotTableField { get; }
Property Value
IsErrorHandlingFunction
Indicates that the function is an ErrorHandlingFunction.
public virtual bool IsErrorHandlingFunction { get; }
Property Value
IsVolatile
If the function returns a different value with the same parameters.
public virtual bool IsVolatile { get; }
Property Value
NamespacePrefix
Information of individual arguments of the function used internally by the formula parser .
public virtual string NamespacePrefix { get; }
Property Value
- string
Function argument information
ParametersInfo
Provides information about the functions parameters.
public virtual ExcelFunctionParametersInfo ParametersInfo { get; }
Property Value
ReturnsReference
If the function returns a range reference
public virtual bool ReturnsReference { get; }
Property Value
Methods
AreEqual(double, double)
Helper method for comparison of two doubles.
protected bool AreEqual(double d1, double d2)
Parameters
Returns
ArgToAddress(IList<FunctionArgument>, int)
Returns a string representation of an arguments address.
protected string ArgToAddress(IList<FunctionArgument> arguments, int index)
Parameters
arguments
IList<FunctionArgument>index
int
Returns
ArgToBool(IList<FunctionArgument>, int)
If the argument is a boolean value its value will be returned. If the argument is an integer value, true will be returned if its value is not 0, otherwise false.
protected bool ArgToBool(IList<FunctionArgument> arguments, int index)
Parameters
arguments
IList<FunctionArgument>index
int
Returns
ArgToBool(IList<FunctionArgument>, int, bool)
If the argument is a boolean value its value will be returned. If the argument is an integer value, true will be returned if its value is not 0, otherwise false. fallback to ValueIfEmpty if datatype is empty
protected bool ArgToBool(IList<FunctionArgument> arguments, int index, bool valueIfEmpty)
Parameters
arguments
IList<FunctionArgument>index
intvalueIfEmpty
bool
Returns
ArgToDecimal(IList<FunctionArgument>, int, out ExcelErrorValue, PrecisionAndRoundingStrategy)
Returns the value of the argument att the position of the 0-based
index
as a double.
protected double ArgToDecimal(IList<FunctionArgument> arguments, int index, out ExcelErrorValue error, PrecisionAndRoundingStrategy precisionAndRoundingStrategy = PrecisionAndRoundingStrategy.DotNet)
Parameters
arguments
IList<FunctionArgument>index
interror
ExcelErrorValueWill be set if an error occurs during conversion
precisionAndRoundingStrategy
PrecisionAndRoundingStrategystrategy for handling precision and rounding of double values
Returns
- double
Value of the argument as an integer.
Exceptions
ArgToDecimal(IList<FunctionArgument>, int, double, out ExcelErrorValue)
Returns the value of the argument att the position of the 0-based
index
as a double.
If the the value is null, zero will be returned.
protected double ArgToDecimal(IList<FunctionArgument> arguments, int index, double valueIfNull, out ExcelErrorValue error)
Parameters
arguments
IList<FunctionArgument>index
intvalueIfNull
doubleerror
ExcelErrorValueWill be set if an error occurs during conversion
Returns
- double
Value of the argument as an integer.
Exceptions
ArgToDecimal(object, out ExcelErrorValue)
Returns the value of the argument att the position of the 0-based
protected double ArgToDecimal(object obj, out ExcelErrorValue error)
Parameters
obj
objecterror
ExcelErrorValueWill be set if the conversion generated an error
Returns
- double
Value of the argument as a double.
Exceptions
ArgToDecimal(object, PrecisionAndRoundingStrategy, out ExcelErrorValue)
Returns the value of the argument att the position of the 0-based
protected double ArgToDecimal(object obj, PrecisionAndRoundingStrategy precisionAndRoundingStrategy, out ExcelErrorValue error)
Parameters
obj
objectprecisionAndRoundingStrategy
PrecisionAndRoundingStrategystrategy for handling precision and rounding of double values
error
ExcelErrorValueAn error type if the operation returns an error.
Returns
- double
Value of the argument as a double.
Exceptions
ArgToInt(IList<FunctionArgument>, int, out ExcelErrorValue, int)
Returns the value of the argument att the position of the 0-based index
index
as an integer.
protected int ArgToInt(IList<FunctionArgument> arguments, int index, out ExcelErrorValue error, int emptyValue = 0)
Parameters
arguments
IList<FunctionArgument>index
interror
ExcelErrorValueIf an error occurs during the conversion it will be returned via this parameter
emptyValue
intValue returned if datatype is empty
Returns
- int
Value of the argument as an integer.
Exceptions
ArgToInt(IList<FunctionArgument>, int, RoundingMethod)
Returns the value of the argument att the position of the 0-based
index
as an integer.
protected int ArgToInt(IList<FunctionArgument> arguments, int index, RoundingMethod roundingMethod)
Parameters
arguments
IList<FunctionArgument>index
introundingMethod
RoundingMethod
Returns
- int
Value of the argument as an integer.
Exceptions
ArgToInt(IList<FunctionArgument>, int, bool, out ExcelErrorValue)
Returns the value of the argument att the position of the 0-based index
protected int ArgToInt(IList<FunctionArgument> arguments, int index, bool ignoreErrors, out ExcelErrorValue error)
Parameters
arguments
IList<FunctionArgument>index
intignoreErrors
boolIf true an Excel error in the cell will be ignored
error
ExcelErrorValueIf an error occurs during the conversion it will be returned via this parameter
Returns
- int
Value of the argument as an integer.
Exceptions
ArgToRangeInfo(IList<FunctionArgument>, int)
protected IRangeInfo ArgToRangeInfo(IList<FunctionArgument> arguments, int index)
Parameters
arguments
IList<FunctionArgument>index
int
Returns
ArgToString(IList<FunctionArgument>, int)
Returns the value of the argument att the position of the 0-based
index
as a string.
protected string ArgToString(IList<FunctionArgument> arguments, int index)
Parameters
arguments
IList<FunctionArgument>index
int
Returns
- string
Value of the argument as a string.
ArgsToDoubleEnumerable(FunctionArgument, ParsingContext, out ExcelErrorValue)
Will return the arguments as an enumerable of doubles using default parameters
protected virtual IList<double> ArgsToDoubleEnumerable(FunctionArgument argument, ParsingContext context, out ExcelErrorValue error)
Parameters
argument
FunctionArgumentcontext
ParsingContexterror
ExcelErrorValue
Returns
ArgsToDoubleEnumerable(FunctionArgument, ParsingContext, Action<DoubleEnumerableParseOptions>, out ExcelErrorValue)
Will return the arguments as an enumerable of doubles.
protected virtual IList<double> ArgsToDoubleEnumerable(FunctionArgument argument, ParsingContext context, Action<DoubleEnumerableParseOptions> configHandler, out ExcelErrorValue error)
Parameters
argument
FunctionArgumentcontext
ParsingContextconfigHandler
Action<DoubleEnumerableParseOptions>error
ExcelErrorValue
Returns
ArgsToDoubleEnumerable(IEnumerable<FunctionArgument>, ParsingContext, out ExcelErrorValue)
Will return the arguments as an enumerable of doubles.
protected virtual IList<double> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments, ParsingContext context, out ExcelErrorValue error)
Parameters
arguments
IEnumerable<FunctionArgument>context
ParsingContexterror
ExcelErrorValue
Returns
ArgsToDoubleEnumerable(IEnumerable<FunctionArgument>, ParsingContext, Action<DoubleEnumerableParseOptions>, out ExcelErrorValue)
Will return the arguments as an enumerable of doubles.
protected virtual IList<double> ArgsToDoubleEnumerable(IEnumerable<FunctionArgument> arguments, ParsingContext context, Action<DoubleEnumerableParseOptions> configHandler, out ExcelErrorValue error)
Parameters
arguments
IEnumerable<FunctionArgument>context
ParsingContextconfigHandler
Action<DoubleEnumerableParseOptions>error
ExcelErrorValue
Returns
Exceptions
ArgsToObjectEnumerable(bool, bool, bool, IEnumerable<FunctionArgument>, ParsingContext)
Will return the arguments as an enumerable of objects.
protected virtual IEnumerable<object> ArgsToObjectEnumerable(bool ignoreHiddenCells, bool ignoreErrors, bool ignoreNestedSubtotalAggregate, IEnumerable<FunctionArgument> arguments, ParsingContext context)
Parameters
ignoreHiddenCells
boolIf a cell is hidden and this value is true the value of that cell will be ignored
ignoreErrors
boolignoreNestedSubtotalAggregate
boolarguments
IEnumerable<FunctionArgument>context
ParsingContext
Returns
CheckForAndHandleExcelError(FunctionArgument, out ExcelErrorValue)
if the supplied arg
contains an Excel error
an ExcelErrorValueException with that errorcode will be thrown
protected void CheckForAndHandleExcelError(FunctionArgument arg, out ExcelErrorValue err)
Parameters
arg
FunctionArgumenterr
ExcelErrorValueIf the cell contains an error the error will be assigned to this variable
CheckForAndHandleExcelError(ICellInfo, out ExcelErrorValue)
If the supplied cell
contains an Excel error
an ExcelErrorValueException with that errorcode will be thrown
protected void CheckForAndHandleExcelError(ICellInfo cell, out ExcelErrorValue err)
Parameters
cell
ICellInfoerr
ExcelErrorValueIf the cell contains an error the error will be assigned to this variable
ConfigureArrayBehaviour(ArrayBehaviourConfig)
Configures parameters of a function that can be arrays (multi-cell ranges) even if the function itself treats them as single values.
public virtual void ConfigureArrayBehaviour(ArrayBehaviourConfig config)
Parameters
config
ArrayBehaviourConfig
CreateAddressResult(IRangeInfo, DataType)
Use this method to create a result to return from Excel functions.
protected CompileResult CreateAddressResult(IRangeInfo result, DataType dataType)
Parameters
result
IRangeInfodataType
DataType
Returns
CreateDynamicArrayResult(object, DataType)
Use this method to create a result to return from Excel functions.
protected CompileResult CreateDynamicArrayResult(object result, DataType dataType)
Parameters
Returns
CreateDynamicArrayResult(object, DataType, FormulaRangeAddress)
Use this method to create a result to return from Excel functions.
protected CompileResult CreateDynamicArrayResult(object result, DataType dataType, FormulaRangeAddress address)
Parameters
result
objectdataType
DataTypeaddress
FormulaRangeAddress
Returns
CreateResult(eErrorType)
Use this method to create a result to return from Excel functions.
protected CompileResult CreateResult(eErrorType errorType)
Parameters
errorType
eErrorType
Returns
CreateResult(object, DataType)
Use this method to create a result to return from Excel functions.
protected CompileResult CreateResult(object result, DataType dataType)
Parameters
Returns
CreateResult(object, DataType, FormulaRangeAddress)
Use this method to create a result to return from Excel functions.
protected CompileResult CreateResult(object result, DataType dataType, FormulaRangeAddress address)
Parameters
result
objectdataType
DataTypeaddress
FormulaRangeAddress
Returns
Divide(double, double)
Divides two numbers. If right
is zero double.PositiveInfinity will be returned.
protected static double Divide(double left, double right)
Parameters
Returns
Execute(IList<FunctionArgument>, ParsingContext)
public abstract CompileResult Execute(IList<FunctionArgument> arguments, ParsingContext context)
Parameters
arguments
IList<FunctionArgument>Arguments to the function, each argument can contain primitive types, lists or Excel ranges
context
ParsingContextThe ParsingContext contains various data that can be useful in functions.
Returns
- CompileResult
A CompileResult containing the calculated value
GetDecimalSingleArgument(FunctionArgument)
Returns the value as if the
protected double? GetDecimalSingleArgument(FunctionArgument arg)
Parameters
arg
FunctionArgument
Returns
GetFirstValue(IEnumerable<FunctionArgument>)
Used for some Lookupfunctions to indicate that function arguments should not be compiled before the function is called.
protected object GetFirstValue(IEnumerable<FunctionArgument> val)
Parameters
Returns
GetNewParameterAddress(IList<CompileResult>, int, ref Queue<FormulaRangeAddress>)
If overridden, this method will be called before the Execute(IList<FunctionArgument>, ParsingContext) method is called with the arguments for any parameter having ParametersInfo set to AdjustParameterAddress and that argument is a range with an address. It can be used to narrow the dependency check for the function returning a queue with addresses to check dependency before executing.
public virtual void GetNewParameterAddress(IList<CompileResult> args, int index, ref Queue<FormulaRangeAddress> addresses)
Parameters
args
IList<CompileResult>The function arguments that will be supplied to the execute method.
index
intThe index of the argument that should be adjusted.
addresses
Queue<FormulaRangeAddress>A queue of addresses that will be calculated before calling the Execute function.
GetResultByObject(object)
Get result by object
protected CompileResult GetResultByObject(object result)
Parameters
result
object
Returns
IsBool(object)
Is bool
protected bool IsBool(object val)
Parameters
val
object
Returns
IsInteger(object)
Returns true if the parameter n
is an integer, otherwise false.
protected bool IsInteger(object n)
Parameters
n
objectThe value to test
Returns
IsNumeric(object)
Is numeric
protected bool IsNumeric(object val)
Parameters
val
object
Returns
IsNumericString(object)
Returns true if the parameter value
is a numeric string, otherwise false.
protected bool IsNumericString(object value)
Parameters
value
objectThe value to test
Returns
IsString(object, bool)
Is string
protected bool IsString(object val, bool allowNullOrEmpty = true)
Parameters
Returns
ThrowArgumentExceptionIf(Func<bool>, string)
Throws an ArgumentException if condition
evaluates to true.
protected void ThrowArgumentExceptionIf(Func<bool> condition, string message)
Parameters
Exceptions
ThrowArgumentExceptionIf(Func<bool>, string, params object[])
Throws an ArgumentException if condition
evaluates to true.
protected void ThrowArgumentExceptionIf(Func<bool> condition, string message, params object[] formats)
Parameters
ThrowExcelErrorValueException(ExcelErrorValue)
Throws an ExcelErrorValueException with the type of given value
set.
protected void ThrowExcelErrorValueException(ExcelErrorValue value)
Parameters
value
ExcelErrorValue
ThrowExcelErrorValueException(eErrorType)
Throws an ExcelErrorValueException with the given errorType
set.
protected void ThrowExcelErrorValueException(eErrorType errorType)
Parameters
errorType
eErrorType
ThrowExcelErrorValueExceptionIf(Func<bool>, eErrorType)
Throws an ArgumentException if condition
evaluates to true.
protected void ThrowExcelErrorValueExceptionIf(Func<bool> condition, eErrorType errorType)
Parameters
condition
Func<bool>errorType
eErrorType
Exceptions
ValidateArguments(IEnumerable<FunctionArgument>, int)
This functions validates that the supplied arguments
contains at least
(the value of) minLength
elements. If one of the arguments is an
Excel range the number of cells in
that range will be counted as well.
[Obsolete("Don't use this method from EPPlus 7.x and up. Use property ArgumentMinLength instead.")]
protected void ValidateArguments(IEnumerable<FunctionArgument> arguments, int minLength)
Parameters
arguments
IEnumerable<FunctionArgument>minLength
int
Exceptions
ValidateArguments(IEnumerable<FunctionArgument>, int, eErrorType)
This functions validates that the supplied arguments
contains at least
(the value of) minLength
elements. If one of the arguments is an
Excel range the number of cells in
that range will be counted as well.
[Obsolete("Don't use this method from EPPlus 7.x and up. Use property ArgumentMinLength instead.")]
protected void ValidateArguments(IEnumerable<FunctionArgument> arguments, int minLength, eErrorType errorTypeToThrow)
Parameters
arguments
IEnumerable<FunctionArgument>minLength
interrorTypeToThrow
eErrorTypeThe eErrorType of the ExcelErrorValueException that will be thrown if
minLength
is not met.