Iterative Calculation

The following example shows how to use the GemBox.Spreadsheet calculation engine to calculate cell formulas with circular references.

The example demonstrates how to enable and use an iterative calculation in GemBox.Spreadsheet. By default, circular references are not allowed, so we first enable them by setting ExcelFile.CalculationOptions.EnableIterativeCalculation.

Column A contains a simple iterative calculation example where the calculation is limited by CalculationOptions.MaximumIterations. On calling the Calculate method, the formula will be calculated CalculationOptions.MaximumIterations times before the calculation is terminated and the last calculation result is set as the cell value.

Although the calculation of all circular references is limited by CalculationOptions.MaximumIterations, some iterative calculations may be terminated before the limit is reached if the difference between the previous and current calculation is less than CalculationOptions.MaximumChange value. The formulas in column B demonstrate such an iterative calculation process.

Screenshot of Excel formula with circular reference calculated with GemBox.Spreadsheet
Excel formula with circular reference calculated with GemBox.Spreadsheet
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();

        // Set calculation options.
        workbook.CalculationOptions.MaximumIterations = 10;
        workbook.CalculationOptions.MaximumChange = 0.05;
        workbook.CalculationOptions.EnableIterativeCalculation = true;

        // Add new worksheet
        var worksheet = workbook.Worksheets.Add("Iterative Calculation");

        // Some column formatting.
        worksheet.Columns[0].SetWidth(50, LengthUnit.Pixel);
        worksheet.Columns[1].SetWidth(100, LengthUnit.Pixel);

        // Simple example of circular reference limited by MaximumIterations in column A.
        worksheet.Cells["A1"].Formula = "=A2";
        worksheet.Cells["A2"].Formula = "=A1 + 1";

        // Simple example of circular reference limited by MaximumChange in column B.
        worksheet.Cells["B1"].Value = 100000.0;
        worksheet.Cells["B2"].Formula = "=B3 * 0.03";
        worksheet.Cells["B3"].Formula = "=B1 + B2";

        // Calculate all cells.
        worksheet.Calculate();

        workbook.Save("Iterative Calculation.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = New ExcelFile

        ' Set calculation options.
        workbook.CalculationOptions.MaximumIterations = 10
        workbook.CalculationOptions.MaximumChange = 0.05
        workbook.CalculationOptions.EnableIterativeCalculation = True

        ' Add new worksheet.
        Dim worksheet = workbook.Worksheets.Add("Iterative Calculation")

        ' Some column formatting.
        worksheet.Columns(0).SetWidth(50, LengthUnit.Pixel)
        worksheet.Columns(1).SetWidth(100, LengthUnit.Pixel)

        ' Simple example of circular reference limited by MaximumIterations in column A.
        worksheet.Cells("A1").Formula = "=A2"
        worksheet.Cells("A2").Formula = "=A1 + 1"

        ' Simple example of circular reference limited by MaximumChange in column B.
        worksheet.Cells("B1").Value = 100000.0
        worksheet.Cells("B2").Formula = "=B3 * 0.03"
        worksheet.Cells("B3").Formula = "=B1 + B2"

        ' Calculate all cells.
        worksheet.Calculate()

        workbook.Save("Iterative Calculation.%OutputFileType%")
    End Sub
End Module

Check next example or download examples from GitHub.