使用 ICustomFunction 功能
Contents
[
Hide
]
介绍
本文介绍了如何使用 ICustomFunction 功能通过 Aspose.Cells API 实现自定义函数。
ICustomFunction 接口允许您添加自定义公式计算函数以扩展 Aspose.Cells 核心计算引擎以满足某些需求。此功能对于在模板文件或代码中定义自定义(用户定义)函数很有用,在该代码中自定义函数可以像任何其他默认 Microsoft Excel 函数一样使用 Aspose.Cells API 实现和评估。
使用 ICustomFunction 功能
以下示例代码实现了 ICustomFunction 接口,该接口计算并返回两个自定义函数的值,即 MySampleFunc() 和 YourSampleFunc()。这些自定义函数分别位于单元格 A1 和 A2 中。然后它调用 IWorkbook.CalculateFormula(false, ICustomFunction) 方法来调用 ICustomFunction.CalculateCustomFunction() 方法的实现。然后,它在控制台上打印 A1 和 A2 的值,这些值实际上是 ICustomFunction.CalculateCustomFunction() 返回的值。请参阅下面示例代码的控制台输出以获得更多帮助。
示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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.