Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
This topic explains how you can directly calculate your custom functions without first writing them in a worksheet. Please use the Worksheet::CalculateFormula(System::String formula, CalculationOptions opts) method for this purpose.
Please see the following sample code that illustrates the usage of this method. We have used a custom function named MyCompany::CustomFunction() and we calculate its value as “Aspose.Cells.” ourselves; this value is then automatically concatenated with the value of cell A1, which is “Welcome to ”, by the calculation engine. The final calculated value is “Welcome to Aspose.Cells.” As you can see in the code, we have not written our custom function anywhere in a worksheet, and it is calculated directly by our own custom logic.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
class ICustomEngine : public AbstractCalculationEngine
{
public:
void Calculate(CalculationData& data) override
{
// Check the formula name and calculate it yourself
if (data.GetFunctionName() == u"MyCompany.CustomFunction")
{
// This is our calculated value
data.SetCalculatedValue(Aspose::Cells::Object(u"Aspose.Cells."));
}
}
};
class ImplementDirectCalculationOfCustomFunction
{
public:
static void Run()
{
Aspose::Cells::Startup();
// Create a workbook
Workbook wb;
// Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
// Add some text in cell A1
ws.GetCells().Get(u"A1").PutValue(u"Welcome to ");
// Create calculation options with custom engine
CalculationOptions opts;
opts.SetCustomEngine(new ICustomEngine());
// This line shows how you can call your own custom function without
// needing to write it in any worksheet cell
// After the execution of this line, it will return
// Welcome to Aspose.Cells.
Aspose::Cells::Object ret = ws.CalculateFormula(u"=A1 & MyCompany.CustomFunction()", opts);
// Print the calculated value to the console
std::cout << "Calculated Value: " << ret.ToString().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();
}
};
int main()
{
ImplementDirectCalculationOfCustomFunction::Run();
return 0;
}
Below is the console output of the above sample code.
Calculated Value: Welcome to Aspose.Cells.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.