استخدام ميزة ICustomFunction
مقدمة
تقدم هذه المقالة فهمًا لكيفية استخدام ميزة ICustomFunction لتنفيذ وظائف مخصصة مع واجهات برمجة التطبيقات Aspose.Cells.
تتيح لك واجهة ICustomFunction إضافة وظائف حساب الصيغة المخصصة لتوسيع محرك الحساب الأساسي Aspose.Cells من أجل تلبية متطلبات معينة. هذه الميزة مفيدة لتحديد وظائف مخصصة (محددة من قبل المستخدم) في ملف قالب أو في رمز حيث يمكن تنفيذ الوظيفة المخصصة وتقييمها باستخدام واجهات برمجة تطبيقات Aspose.Cells مثل أي وظيفة إكسل Microsoft افتراضية أخرى.
استخدام ميزة ICustomFunction
يقوم نموذج التعليمات البرمجية التالي بتنفيذ واجهة ICustomFunction التي تقوم بتقييم وإرجاع قيم الوظيفتين المخصصتين مثل MySampleFunc () و YourSampleFunc (). توجد هذه الوظائف المخصصة داخل الخلايا A1 و A2 على التوالي. ثم تقوم باستدعاء طريقة IWorkbook.CalculateFormula (false، ICustomFunction) لاستدعاء تنفيذ طريقة ICustomFunction.CalculateCustomFunction (). بعد ذلك ، يقوم بطباعة قيم A1 و A2 على وحدة التحكم والتي هي في الواقع القيم التي تم إرجاعها بواسطة ICustomFunction.CalculateCustomFunction (). الرجاء مراجعة إخراج وحدة التحكم لنموذج التعليمات البرمجية أدناه للحصول على مزيد من المساعدة.
عينة من الرموز
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Implement ICustomFunction interface | |
class CustomFunction : public ICustomFunction | |
{ | |
public: | |
//Evalaute and return the values of your custom functions | |
intrusive_ptr<Aspose::Cells::System::Object> | |
CalculateCustomFunction( | |
intrusive_ptr<Aspose::Cells::System::String> functionName, | |
intrusive_ptr<Aspose::Cells::System::Collections::ArrayList> paramsList, | |
intrusive_ptr<Aspose::Cells::System::Collections::ArrayList> contextObjects) | |
{ | |
if (functionName->Equals(new String("MySampleFunc"))) | |
{ | |
return new String("MY sample function was called successfully."); | |
} | |
if (functionName->Equals(new String("YourSampleFunc"))) | |
{ | |
return new String("YOUR sample function was called successfully."); | |
} | |
return NULL; | |
} | |
}; | |
//Using ICustomFunction Feature | |
void UsingICustomFunctionFeature() | |
{ | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet in the workbook | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Adding custom formulas to Cell A1 and A2 | |
ws->GetICells()->GetObjectByIndex(new String("A1"))->SetFormula(new String("=MySampleFunc()")); | |
ws->GetICells()->GetObjectByIndex(new String("A2"))->SetFormula(new String("=YourSampleFunc()")); | |
// Calcualting Formulas | |
intrusive_ptr<CustomFunction> custFunc = new CustomFunction(); | |
wb->CalculateFormula(false, custFunc); | |
//Print the value of cell A1 and A2 after the calculation of custom function implemented by us. | |
intrusive_ptr<String> valA1 = ws->GetICells()->GetObjectByIndex(new String("A1"))->GetStringValue(); | |
intrusive_ptr<String> valA2 = ws->GetICells()->GetObjectByIndex(new String("A2"))->GetStringValue(); | |
//Print the values on console | |
StringPtr str1 = new String("Value of A1: "); | |
Console::WriteLine(str1->StringAppend(valA1)); | |
StringPtr str2 = new String("Value of A2: "); | |
Console::WriteLine(str2->StringAppend(valA2)); | |
} |
إخراج وحدة التحكم
Value of A1: MY sample function was called successfully.
Value of A2: YOUR sample function was called successfully.