@Slanek,
Here is an example, we hope it is useful.
class CellsCppCustomFunction : public ICustomFunction
{
public:
ObjectPtr CalculateCustomFunction(StringPtr functionName, ArrayListPtr paramsList, ArrayListPtr contextObjects)
{
double total = 0;
intrusive_ptr<IReferredArea firstParam = dynamic_pointer_cast<IReferredArea(paramsList-Get(0));
double firstParamVal = Convert::ToDouble(firstParam-GetValues());
intrusive_ptr<IReferredArea secondParam = dynamic_pointer_cast<IReferredArea(paramsList-Get(1));
double valC1 = Convert::ToDouble(secondParam-GetValue(0, 0));
double valC2 = Convert::ToDouble(secondParam-GetValue(1, 0));
double valC3 = Convert::ToDouble(secondParam-GetValue(2, 0));
double valC4 = Convert::ToDouble(secondParam-GetValue(3, 0));
double valC5 = Convert::ToDouble(secondParam-GetValue(4, 0));
total = (valC1 + valC2 + valC3 + valC4 + valC5) / firstParamVal;
return new Primitive<double(total);
}
};
int main(int argc, char** argv)
{
intrusive_ptr<IWorkbook workbook = Factory::CreateIWorkbook();
//Obtaining the reference of the first worksheet
intrusive_ptr<IWorksheet worksheet = workbook-GetIWorksheets()-GetObjectByIndex(0);
//Adding a sample value to "B1" cell
worksheet-GetICells()-GetObjectByIndex(new String("B1"))-PutValue(5);
//Adding a sample value to "C1" cell
worksheet-GetICells()-GetObjectByIndex(new String("C1"))-PutValue(100);
//Adding a sample value to "C2" cell
worksheet-GetICells()-GetObjectByIndex(new String("C2"))-PutValue(150);
//Adding a sample value to "C3" cell
worksheet-GetICells()-GetObjectByIndex(new String("C3"))-PutValue(60);
//Adding a sample value to "C4" cell
worksheet-GetICells()-GetObjectByIndex(new String("C4"))-PutValue(32);
//Adding a sample value to "C5" cell
worksheet-GetICells()-GetObjectByIndex(new String("C5"))-PutValue(62);
//Adding custom formula to intrusive_ptr<ICell A1
workbook-GetIWorksheets()-GetObjectByIndex(0)-GetICells()-GetObjectByIndex(new String("A1"))-SetFormula(new String("=MyFunc(B1,C1:C5)"));
//Calcualting Formulas
workbook-CalculateFormula(false, new CellsCppCustomFunction());
//self-check
//double ret = Aspose::Cells::Systems::ObjectCast<Aspose::Cells::Systems::Double( workbook-GetIWorksheets()-GetObjectByIndex(0)-GetICells()-GetObjectByIndex(new String("A1"))-GetValue());
//EXPECT_DOUBLE_EQ(80.8, ret);
//Assign resultant value to intrusive_ptr<ICell A1
workbook-GetIWorksheets()-GetObjectByIndex(0)-GetICells()-GetObjectByIndex(new String("A1"))-PutValue(workbook-GetIWorksheets()-GetObjectByIndex(0)-GetICells()-GetObjectByIndex(new String("A1"))-GetValue());
workbook-Save(new String("D:\\DemoICustomFunction.xlsx"));
}
Let us know your feedback.