GridWeb でカスタム関数を計算する

考えられる使用シナリオ

Aspose.Cells.GridWeb は、GridWeb.CustomCalculationEngine プロパティを使用してカスタム関数の計算をサポートします。このプロパティは、GridAbstractCalculationEngine インターフェイスのインスタンスを取ります。 GridAbstractCalculationEngine インターフェイスを実装し、独自のロジックでカスタム関数を計算してください。

GridWeb でカスタム関数を計算する

次のサンプル コードは、セル B3 に MYTESTFUNC() という名前のカスタム関数を追加します。次に、GridAbstractCalculationEngine インターフェイスを実装して、この関数の値を計算します。パラメータに 2 を掛けて結果を返すように MYTESTFUNC() を計算します。したがって、パラメータが 9 の場合、2*9 = 18 が返されます。

サンプルコード

// 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();
}
}