الحساب المباشر للوظيفة المخصصة دون كتابتها في ورقة عمل
الحساب المباشر للوظيفة المخصصة دون كتابتها في ورقة العمل
تشرح هذه القائمة كيف يمكنك حساب وظائفك المخصصة مباشرة دون كتابتها أولاً في ورق العمل. يُرجى استخدام الطريقة Worksheet.CalculateFormula(string formula, CalculationOptions opts) لهذا الغرض.
يرجى الاطلاع على الرمز العيني التالي الذي يوضح استخدام هذه الطريقة. لقد استخدمنا وظيفة مخصصة باسم MyCompany.CustomFunction() وقمنا بحساب قيمتها كـ ‘Aspose.Cells.’ بأنفسنا ثم يتم دمج هذه القيمة تلقائيًا مع قيمة الخلية A1 التي هي ‘مرحبًا بك في ' بواسطة محرك الحساب وتعيد القيمة المحسوبة النهائية كـ ‘مرحبا بك في Aspose.Cells.’ كما ترون في الرمز البرمجي لم نكتب وظيفتنا المخصصة في أي مكان في ورقة العمل ويتم حسابها مباشرة بواسطة منطقنا المخصص.
نموذج برمجة
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class ICustomEngine : AbstractCalculationEngine | |
{ | |
// Override the Calculate method with custom logic | |
public override void Calculate(CalculationData data) | |
{ | |
// Check the forumla name and calculate it yourself | |
if (data.FunctionName == "MyCompany.CustomFunction") | |
{ | |
// This is our calculated value | |
data.CalculatedValue = "Aspose.Cells."; | |
} | |
} | |
} | |
class ImplementDirectCalculationOfCustomFunction | |
{ | |
public static void Run() | |
{ | |
// Create a workbook | |
Workbook wb = new Workbook(); | |
// Accesss first worksheet | |
Worksheet ws = wb.Worksheets[0]; | |
// Add some text in cell A1 | |
ws.Cells["A1"].PutValue("Welcome to "); | |
// Create a calculation options with custom engine | |
CalculationOptions opts = new CalculationOptions(); | |
opts.CustomEngine = new ICustomEngine(); | |
// This line shows how you can call your own custom function without | |
// a need to write it in any worksheet cell | |
// After the execution of this line, it will return | |
// Welcome to Aspose.Cells. | |
object ret = ws.CalculateFormula("=A1 & MyCompany.CustomFunction()", opts); | |
// Print the calculated value on Console | |
Console.WriteLine("Calculated Value: " + ret); | |
} | |
} |
مخرجات الوحدة
فيما يلي إخراج وحدة التحكم للرمز العيني أعلاه.
Calculated Value: Welcome to Aspose.Cells.