Ways to Calculate Formulas

Introduction

Aspose.Cells has an embedded formula calculation engine. It can not only re-calculate formulas imported from designer templates but also supports calculating the results of formulas added at runtime.

Adding Formulas & Calculating Results

Aspose.Cells supports most of the formulas or functions that are the part of Microsoft Excel. they can be used through the API or using designer spreadsheets. Aspose.Cells supports a huge set of mathematical, string, boolean, date/time, statistical, lookup and reference formulas.

Use the Cell.SetFormula method to add a formula to a cell. When applying a formula to a cell, always begin the string with an equal sign (=) as you do when creating a formula in Microsoft Excel. Use a comma (,) to delimit function parameters.

To calculate the results of formulas, call the Workbook.CalculateFormula() method which processes all the formulas embedded in an Excel file. Please see the following sample code that adds the formula and calculates its results. Please check the output excel file generated with this code.

Sample Code

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Output directory path
U16String outPath(u"..\\Data\\Output\\");
//Path of output excel file
U16String outputAddingFormulasAndCalculatingResults = outPath + u"outputAddingFormulasAndCalculatingResults.xlsx";
//Create workbook
Workbook wb;
//Access first worksheet in the workbook
Worksheet ws = wb.GetWorksheets().Get(0);
//Adding integer values to cells A1, A2 and A3
ws.GetCells().Get(u"A1").PutValue(10);
ws.GetCells().Get(u"A2").PutValue(20);
ws.GetCells().Get(u"A3").PutValue(70);
//Adding a SUM formula to "A4" cell
ws.GetCells().Get(u"A4").SetFormula(u"=SUM(A1:A3)");
//Calculating the results of formulas
wb.CalculateFormula();
//Get the calculated value of the cell
U16String strVal = ws.GetCells().Get(u"A4").GetStringValue();
//Print the calculated value on console
std::cout << "Calculated Result: " << strVal.ToUtf8() <<std::endl;
//Saving the workbook
wb.Save(outputAddingFormulasAndCalculatingResults);
Aspose::Cells::Cleanup();

Calculating Formulas Once Only

When Workbook.CalculateFormula() is called to calculate the values of formulas in a workbook template, Aspose.Cells creates a calculating chain. It increases performance when formulas are calculated for the second or third time.

However, if the template contains lots of formulas, the first time the formula is calculated can consume a lot of CPU processing time and memory.

Aspose.Cells allows you to turn off creating a calculating chain which is useful when you want to calculate formulas only once.

Please call Workbook.GetISettings().SetCreateCalcChain() with false parameter. You can use the provided excel file to test this code.

Sample Code

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
U16String dirPath(u"..\\Data\\Formulas\\");
//Path of input excel file
U16String sampleCalculatingFormulasOnceOnly = dirPath + u"sampleCalculatingFormulasOnceOnly.xlsx";
//Create workbook
Workbook wb(sampleCalculatingFormulasOnceOnly);
//Set the CreateCalcChain as false
wb.GetSettings().GetFormulaSettings().SetEnableCalculationChain(false);
//Get the time before formula calculationint
auto startTime = std::chrono::system_clock::now();
//Calculate the workbook formulas
wb.CalculateFormula();
//Get the time after formula calculation
auto interval = std::chrono::system_clock::now() - startTime;
long long time = std::chrono::duration_cast<std::chrono::milliseconds>(interval).count();
std::cout << "Workbook Formula Calculation Elapsed Time in Milliseconds: " << time << std::endl;
Aspose::Cells::Cleanup();