Class AbstractCalculationEngine

Class AbstractCalculationEngine

Namespace: Aspose.Cells
Assembly: Aspose.Cells.dll (25.2.0)

Represents user’s custom calculation engine to extend the default calculation engine of Aspose.Cells.

public abstract class AbstractCalculationEngine

Inheritance

objectAbstractCalculationEngine

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Examples

csharp
[C#]
Workbook wb = new Workbook("custom_calc.xlsx");
CalculationOptions opts = new CalculationOptions();
opts.CustomEngine = new MyEngine();
wb.CalculateFormula(opts);

class MyEngine : AbstractCalculationEngine
{
    public override void Calculate(CalculationData data)
    {
        string funcName = data.FunctionName.ToUpper();
        if ("MYFUNC".Equals(funcName))
        {
            //do calculation for MYFUNC here
            int count = data.ParamCount;
            object res = null;
            for (int i = 0; i < count; i++)
            {
                object pv = data.GetParamValue(i);
                if (pv is ReferredArea)
                {
                    ReferredArea ra = (ReferredArea)pv;
                    pv = ra.GetValue(0, 0);
                }
                //process the parameter here
                //res = ...;
            }
            data.CalculatedValue = res;
        }
    }
}

Remarks

User should not modify any part of the Workbook directly in this implementation(except the calculated result of the custom function, which can be set by CalculationData.CalculatedValue property). Otherwise unexpected result or Exception may be caused. If user needs to change other data than calculated result in the implementation for some custom functions, for example, change cell’s formula, style, …etc., user should gather those data in this implementation and change them out of the scope of formula calculation.

Constructors

AbstractCalculationEngine()

protected AbstractCalculationEngine()

Properties

IsParamArrayModeRequired

Indicates whether this engine needs the parameter to be calculated in array mode. Default value is false. If Aspose.Cells.CalculationData.GetParamValueInArrayMode(System.Int32,System.Int32,System.Int32) is required when calculating custom functions and user has not updated the definition for them (by Aspose.Cells.Workbook.UpdateCustomFunctionDefinition(Aspose.Cells.CustomFunctionDefinition)), this property needs to be set as true.

public virtual bool IsParamArrayModeRequired { get; }

Property Value

bool

Remarks

If this custom calculation engine needs the parameter to be calculated in array mode, more stacks will be required to cache the tree for parameters and Calculate() method may be called recursively to calculate the parameter’s value. For performance consideration, please keep this property as the default value(false) if there is no special requirement.

IsParamLiteralRequired

Indicates whether this engine needs the literal text of parameter while doing calculation. Default value is false.

public virtual bool IsParamLiteralRequired { get; }

Property Value

bool

Remarks

If this custom calculation engine needs the parameter’s literal text, more stacks will be required to cache the literal text for parameters and Calculate() method may be called recursively to calculate the parameter’s value. Generally the literal text is not needed for calculating formulas and this property should be kept as false for most implementations to get better performance.

ProcessBuiltInFunctions

Whether built-in functions that have been supported by the built-in engine should be checked and processed by this implementation. Default is false.

public virtual bool ProcessBuiltInFunctions { get; }

Property Value

bool

Remarks

If user needs to change the calculation logic of some built-in functions, this property should be set as true. Otherwise please leave this property as false for performance consideration.

Methods

Calculate(CalculationData)

Calculates one function with given data.

public abstract void Calculate(CalculationData data)

Parameters

data CalculationData

the required data to calculate function such as function name, parameters, …etc.

Remarks

User should set the calculated value for given data for all functions(including excel native functions) that he wants to calculate by himself in this implementation.

ForceRecalculate(string)

Whether force given function to be recalculated always when calculating shared formulas.

public virtual bool ForceRecalculate(string functionName)

Parameters

functionName string

name of the function. Generally it is custom function’s name. If Aspose.Cells.AbstractCalculationEngine.ProcessBuiltInFunctions is true, then built-in functions will also be checked here.

Returns

bool

true if the specified function needs to be recalculated always.

Remarks

For shared formulas, multiple cells share the same function. If the function’s parameters keep same for those cells too, then generally this function needs to be calculated only once. So for performance consideration we only calculate such kind of function once too(Aspose.Cells.AbstractCalculationEngine.Calculate(Aspose.Cells.CalculationData) is called only once, instead of being called repeatedly for every cell). However, for user’s custom implementation, maybe the function, especially the custom function, needs to be calculated differently for different cells. If so, user needs to override this method to make it return true for the function. And for Aspose.Cells.AbstractCalculationEngine.Calculate(Aspose.Cells.CalculationData), the given Aspose.Cells.CalculationData.CalculatedValue may have been initialized with the cached value of previous calculation.

SkipCalculation()

protected void SkipCalculation()