Calculate Custom Functions in GridWeb
Possible Usage Scenarios
Aspose.Cells.GridWeb supports the calculation of custom functions with the GridWeb.CustomCalculationEngine property. This property takes the instance of GridAbstractCalculationEngine interface. Please implement GridAbstractCalculationEngine interface and calculate your custom functions with your own logic.
Calculate Custom Functions in GridWeb
The following sample code adds a custom function named MYTESTFUNC() in cell B3. Then we calculate the value of this function by implementing the GridAbstractCalculationEngine interface. We calculate MYTESTFUNC() in such a way that it multiplies its parameter with 2 and returns the result. So if its parameter is 9, it will return 2*9 = 18.
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
private class GridWebCustomCalculationEngine : GridAbstractCalculationEngine | |
{ | |
public override void Calculate(GridCalculationData data) | |
{ | |
// Calculate MYTESTFUNC() with your own logic. i.e Multiply MYTESTFUNC() parameter with 2 so MYTESTFUNC(3.0) = 6 | |
if ("MYTESTFUNC".Equals(data.FunctionName.ToUpper())) | |
{ | |
data.CalculatedValue = (decimal)(2.0 * (double)data.GetParamValue(0)); | |
} | |
} | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
if (Page.IsPostBack == false && GridWeb1.IsPostBack == false) | |
{ | |
// Assign your own custom calculation engine to GridWeb | |
GridWeb1.CustomCalculationEngine = new GridWebCustomCalculationEngine(); | |
// Access the active worksheet and add your custom function in cell B3 | |
GridWorksheet sheet = GridWeb1.ActiveSheet; | |
GridCell cell = sheet.Cells["B3"]; | |
cell.Formula = "=MYTESTFUNC(9.0)"; | |
// Calculate the GridWeb formula | |
GridWeb1.CalculateFormula(); | |
} | |
} |