حساب وظائف مخصصة في GridWeb

سيناريوهات الاستخدام المحتملة

يدعم Aspose.Cells.GridWeb حساب الوظائف المخصصة باستخدام خاصية GridWeb.CustomCalculationEngine. تأخذ هذه الخاصية مثيلًا لواجهة GridAbstractCalculationEngine. يرجى تنفيذ واجهة GridAbstractCalculationEngine وحساب الوظائف المخصصة الخاصة بك باستخدام منطقك الخاص.

حساب الوظائف المخصصة في GridWeb

يضيف الكود المعاين التالي وظيفة مخصصة بالاسم MYTESTFUNC() في الخلية B3. ثم نحسب قيمة هذه الوظيفة عن طريق تنفيذ واجهة 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();
}
}