Utilizzo della funzione ICustomFunction
introduzione
Questo articolo fornisce informazioni su come usare la funzionalità ICustomFunction per implementare funzioni personalizzate con le API Aspose.Cells.
L’interfaccia ICustomFunction consente di aggiungere funzioni di calcolo delle formule personalizzate per estendere il motore di calcolo principale Aspose.Cells al fine di soddisfare determinati requisiti. Questa funzionalità è utile per definire funzioni personalizzate (definite dall’utente) in un file modello o in un codice in cui la funzione personalizzata può essere implementata e valutata utilizzando le API Aspose.Cells come qualsiasi altra funzione Excel predefinita Microsoft.
Utilizzo della funzione ICustomFunction
Il seguente codice di esempio implementa l’interfaccia ICustomFunction che valuta e restituisce i valori delle due funzioni personalizzate, ad esempio MySampleFunc() e YourSampleFunc(). Queste funzioni personalizzate si trovano rispettivamente all’interno delle celle A1 e A2. Quindi chiama il metodo IWorkbook.CalculateFormula(false, ICustomFunction) per richiamare l’implementazione del metodo ICustomFunction.CalculateCustomFunction(). Quindi, stampa i valori di A1 e A2 sulla console che sono effettivamente i valori restituiti da ICustomFunction.CalculateCustomFunction(). Consulta l’output della console del codice di esempio riportato di seguito per ulteriore assistenza.
Codice d’esempio
//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)); | |
} |
Uscita console
Value of A1: MY sample function was called successfully.
Value of A2: YOUR sample function was called successfully.