Calculate Formulas
- 6 minutes to read
The Spreadsheet Document API calculates formulas, array formulas and dynamic array formulas on demand. The API does not calculate formulas when you assign a formula to a cell or modify cells that formulas reference. Call a calculation method before you read formula results.
Calculate Formulas in a Workbook
Call the Workbook.Calculate() method to calculate formulas on all worksheets. The Calculate method calculates formulas with outdated results (for example, new formulas and formulas that reference modified cells) and formulas with volatile functions (for example, RAND).
The following code snippet assigns values to cells A1 and A2, adds formulas to cells A3 and A4, calculates formulas, and displays the results in the console:
using System;
using DevExpress.Docs.Spreadsheet;
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = 15;
worksheet.Cells["A2"].Value = 4;
worksheet.Cells["A3"].Formula = "=A1*A2";
worksheet.Cells["A4"].Formula = "=DATE(2026,12,31)-A1";
workbook.Calculate();
Console.WriteLine(worksheet.Cells["A3"].Value);
Console.WriteLine(worksheet.Cells["A4"].GetDateValue().ToShortDateString());
If a formula result is outdated, the Cell.Value property returns the CellValue.Empty value. In this instance, the Cell.GetDateValue method throws an InvalidOperationException.
Calculate a Worksheet or Cell Range
Call the Workbook.Calculate(sheet) or Workbook.Calculate(range) method to calculate formulas on a single worksheet or in a cell range. Formulas outside the specified worksheet or range keep outdated results.
The following code snippet calculates formulas in the B1:B2 cell range and displays the results in the console:
using System;
using DevExpress.Docs.Spreadsheet;
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = 200;
worksheet.Cells["B1"].Formula = "=A1*0.1";
worksheet.Cells["B2"].Formula = "=A1*0.2";
workbook.Calculate(worksheet["B1:B2"]);
Console.WriteLine(worksheet.Cells["B1"].Value);
Console.WriteLine(worksheet.Cells["B2"].Value);
Note
If you call the Workbook.Calculate(sheet) or Workbook.Calculate(range) method, the API does not calculate referenced formulas outside the specified worksheet or range. If a referenced formula has an outdated result, the API can calculate an incorrect result, and a later call to the Workbook.Calculate() method does not recalculate it. Call the Workbook.CalculateFull() method to recalculate all formulas.
Recalculate All Formulas
A document stores each formula with its last calculated result (a cached value). The Workbook.Calculate() method treats cached values from a loaded document as current formula results.
If a document can contain outdated cached values (for example, a document that another tool generated), call the Workbook.CalculateFull method. The CalculateFull method recalculates all formulas in the workbook.
Calculate Formulas When You Load, Save, or Export a Workbook
Use the following properties to calculate formulas when the API loads, saves, or exports a workbook:
| Property | Description |
|---|---|
| LoadOptions.CalculateOnLoad | Gets or sets whether the API calculates formulas when it loads a workbook. |
| SaveOptions.CalculateBeforeSave | Gets or sets whether the API calculates formulas before it saves a workbook. |
| PdfExportOptions.CalculateBeforeExport | Gets or sets whether to calculate formulas with outdated results before export. |
These properties apply to formulas with outdated results. Cached values from a loaded document stay unchanged. To recalculate cached values, call the Workbook.CalculateFull() method.
The following code snippet calculates formulas and saves a workbook to an XLSX file:
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = 10;
worksheet.Cells["A2"].Formula = "=A1*2";
SaveOptions saveOptions = new SaveOptions() {
DocumentFormat = DocumentFormat.Xlsx,
CalculateBeforeSave = true
};
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream, saveOptions);
If the CalculateBeforeSave property is false and the workbook contains outdated formula results, the API marks an XLSX, XLSM, XLTX, XLTM, or XLSB document as not calculated. Microsoft Excel recalculates formulas when it opens such a document. If the Spreadsheet Document API loads such a document, formula results are empty until you call the Workbook.Calculate() method or load the document with the CalculateOnLoad property set to true. Other formats cannot store this calculation status.
Refer to the following help topics for additional information:
Specify Calculation Settings
The Workbook.CalculationOptions property provides access to the workbook’s formula calculation settings. The settings take effect when the API calculates formulas.
Use the following properties to specify how the API calculates formulas:
| Property | Description |
|---|---|
| CalculationOptions.Iterative | Gets or sets whether the API calculates formulas with circular references iteratively. |
| CalculationOptions.MaxIterationCount | Gets or sets the maximum number of iterations for formulas with circular references. |
| CalculationOptions.MaxChange | Gets or sets the maximum change between the results of two iterations in iterative calculation. |
| CalculationOptions.PrecisionAsDisplayed | Gets or sets whether formulas use formula results rounded to the displayed precision. |
| CalculationOptions.Use1904DateSystem | Gets or sets whether the workbook uses the 1904 date system. |
Important
CalculationOptions.Mode, CalculationOptions.RecalculateBeforeSaving, CalculationOptions.FullCalculationOnLoad, and CalculationOptions.UpdateLinksMode properties are document settings for spreadsheet applications (for example, Microsoft Excel). The Spreadsheet Document API saves these settings to the document and does not use them when it calculates formulas.
Calculate Formulas with Circular References
A formula has a circular reference if the formula refers to its own cell directly or through other formulas. If iterative calculation is disabled, such formulas keep outdated results after calculation.
Set the CalculationOptions.Iterative property to true to calculate formulas with circular references iteratively. The CalculationOptions.MaxIterationCount and CalculationOptions.MaxChange properties specify when iterative calculation stops.
The first calculation after you assign a formula with a circular reference evaluates the formula once. Later calls to the Workbook.Calculate() or Workbook.CalculateFull() method calculate the formula iteratively. Each call starts from the current formula result.
The following code snippet enables iterative calculation for a formula with a circular reference and displays the calculated result in the console:
using System;
using DevExpress.Docs.Spreadsheet;
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Create a formula that refers to its own cell.
worksheet.Cells["A1"].Formula = "=A1/2+1";
// Enable iterative calculation.
CalculationOptions options = workbook.CalculationOptions;
options.Iterative = true;
options.MaxIterationCount = 100;
options.MaxChange = 0.001;
// The first calculation evaluates the new formula once.
// The second calculation calculates the formula iteratively.
workbook.Calculate();
workbook.Calculate();
Console.WriteLine(worksheet.Cells["A1"].Value);