Använder ICustomFunction-funktionen

Introduktion

Den här artikeln ger en förståelse för hur du använder ICustomFunction-funktionen för att implementera anpassade funktioner med Aspose.Cells API:er.

ICustomFunction-gränssnittet låter dig lägga till anpassade formelberäkningsfunktioner för att utöka Aspose.Cells kärnberäkningsmotor för att uppfylla vissa krav. Den här funktionen är användbar för att definiera anpassade (användardefinierade) funktioner i en mallfil eller i en kod där den anpassade funktionen kan implementeras och utvärderas med Aspose.Cells API:er som vilken annan standard Microsoft Excel-funktion som helst.

Använder ICustomFunction-funktionen

Följande exempelkod implementerar ICustomFunction-gränssnittet som utvärderar och returnerar värdena för de två anpassade funktionerna, dvs. MySampleFunc() och YourSampleFunc(). Dessa anpassade funktioner finns inuti cellerna A1 respektive A2. Sedan anropar den metoden IWorkbook.CalculateFormula(false, ICustomFunction) för att anropa implementeringen av metoden ICustomFunction.CalculateCustomFunction(). Sedan skriver den ut värdena för A1 och A2 på konsolen som faktiskt är de värden som returneras av ICustomFunction.CalculateCustomFunction(). Se konsolutgången för exempelkoden nedan för mer hjälp.

Exempelkod

//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));
}

Konsolutgång

 Value of A1: MY sample function was called successfully.

Value of A2: YOUR sample function was called successfully.