Calcular funciones personalizadas en GridWeb
Escenarios de uso posibles
Aspose.Cells.GridWeb admite el cálculo de funciones personalizadas con la propiedad GridWeb.CustomCalculationEngine. Esta propiedad toma la instancia de la interfaz GridAbstractCalculationEngine. Por favor implementa la interfaz GridAbstractCalculationEngine e calcula tus funciones customizadas con tu propia lógica.
Calcular funciones personalizadas en GridWeb
El siguiente código de muestra agrega una función personalizada llamada MYTESTFUNC() en la celda B3. Luego calculamos el valor de esta función implementando la interfaz GridAbstractCalculationEngine. Calculamos MYTESTFUNC() de tal manera que multiplicamos su parámetro por 2 y devolvemos el resultado. Así que si su parámetro es 9, devolverá 2*9 = 18.
Código de muestra
// 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(); | |
} | |
} |