Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introducedin
09.1

Description

Excerpt

The IFunction interface is used to implement custom Excel functions in .NET to be passed in to ExcelWriter Application. You can register your custom logic to a specific Excel Function by using the Workbook.RegisterCustomFunction method.

Signature
C#
C#
public interface IFunction
{
    ComputationResultFunctionValue CustomFunctionCalculate(IList<ComputationResult>IList<FunctionValue> args, Cell currentCell);
}
Signature
vb.net
vb.net
Public Interface IFunction
    Function CustomFunctionCalculate(IList<ComputationResult> args, Cell currentCellargs As IList(Of FunctionValue), currentCell As Cell) As ComputationResultFunctionValue
End Interface
Example
Code Block
csharp
csharp
titleC#
class Concat : IFunction
    {

	//Your custom function public ComputationResult CustomFunction(IList<ComputationResult>should be called Calculate and have the same signature as the sample function below 
        public FunctionValue Calculate(IList<FunctionValue> args, Cell currentCell)
        {

	    //Implement the custom logic you want to calculate your custom/overridden Excel function 
            StringBuilder result = new StringBuilder("");
            foreach (ComputationResultFunctionValue arg in args)
            if ({
                result.Append(',');
                switch (arg.Type)
!= ComputationResultType.RANGE)
                   {
                    case FunctionValueType.BOOLEAN:
                        result.Append((bool)arg.Value ? "True" : "False");
                        break;
                    case FunctionValueType.STRING:
                        result.Append(arg.Value);
                        break;
                    case FunctionValueType.NUMBER:
                        result.Append(((double)arg.Value).ToString("0.#####"));
                        break;
                    case FunctionValueType.NULL:
                        result.Append(' ');
                        break;
                    case FunctionValueType.RANGE:
                        result.Append(arg.Value.ToString());
                        break;
                }
            }

            //Must return a FunctionValue object
            return new ComputationResultFunctionValue(result.ToString());
        }
    }
Code Block
vbnet
vbnet
titlevb.net
Class Concat
	Implements IFunction

	//Your custom function should be called Calculate and have the same signature as the sample function below 
	Public Function CustomFunctionCalculate(args As IList(Of ComputationResultFunctionValue), currentCell As Cell) As ComputationResult
		FunctionValue
	        //Implement the custom logic you want to calculate your custom/overridden Excel function 		
                Dim result As New StringBuilder("")
		For Each arg As ComputationResultFunctionValue In args
			If result.Append(",")

			Select Case arg.Type
<> ComputationResultType.RANGE Then
								Case FunctionValueType.[BOOLEAN]
					result.Append(If(CBool(arg.Value), "True", "False"))
					Exit Select
				Case FunctionValueType.[STRING]
					result.Append(arg.Value)
			End If		Exit Select
				Case FunctionValueType.NUMBER
					result.Append(CDbl(arg.Value).ToString("0.#####"))
					Exit Select
				Case FunctionValueType.NULL
					result.Append(" ")
					Exit Select
				Case FunctionValueType.RANGE
					result.Append(arg.Value.ToString())
					Exit Select
			End Select
		Next

 		//Must return a FunctionValue object
		Return New ComputationResultFunctionValue(result.ToString())
	End Function
End Class

Methods

Name

Description

CustomFunctionCalculate(IList<ComputationResult>IList<FunctionValue>,Cell)

Excerpt Include
IFunction.CustomFunctionCalculate
IFunction.CustomFunctionCalculate
nopaneltrue

Scrollbar