GridWebでカスタム関数を計算する
可能な使用シナリオ
Aspose.Cells.GridWebは、GridWeb.CustomCalculationEngineプロパティを使用してカスタム関数の計算をサポートしています。このプロパティはGridAbstractCalculationEngineインターフェースのインスタンスを取ります。GridAbstractCalculationEngineインターフェースを実装し、独自のロジックでカスタム関数を計算してください。
GridWebでカスタム関数を計算する
次のサンプルコードは、セル B3 に MYTESTFUNC() というカスタム関数を追加します。次に、GridAbstractCalculationEngine インターフェースを実装して、この関数の値を計算します。MYTESTFUNC()は、そのパラメータを2倍にして結果を返すように計算されます。そのため、パラメータが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(); | |
} | |
} |