Table of Contents

XlsFile.RecalcCell Method

This method will recalculate a single cell and all of it's dependencies, but not the whole workbook.

USE THIS METHOD WITH CARE! You will normally want to simply call ExcelFile.Recalc or just save the file and let FlexCel calculate the workbook for you. This method is for rare situations where you are making thousands of recalculations and the speed of Recalc is not enough, and you have a big part of the spreadsheet that you know that didn't change.

Note: If you are recalculating many cells without changing data, you can speed up calculations by calling ExcelFile.StartBatchRecalcCells and ExcelFile.EndBatchRecalcCells

Syntax

Namespace: FlexCel.XlsAdapter

public override Object RecalcCell(Int32 sheet, Int32 row, Int32 col, Boolean forced)

Parameters

<-> Parameter Type Description
sheet Int32 Sheet for the cell we want to recalculate. Use ExcelFile.ActiveSheet here to refer to the active sheet.
row Int32 Row for the cell we want to recalculate. (1 based)
col Int32 Column for the cell we want to recalculate. (1 based)
forced Boolean When true this method will always perform a recalc. When false, only if there has been a change on the spreadsheet.
While for performance reasons you will normally want to keep this false, you might need to set it to true if the formulas refer to functions like "=NOW()" or "=RANDOM()" that change every time you recalculate.

Returns

The result of the formula at the cell, or null if there is no formula.

Examples

The following code will recalculate the value of cells A1 and A2, but not C7:

    XlsFile xls = new XlsFile(1, TExcelFileFormat.v2021, true);

    xls.SetCellValue(1, 1, new TFormula("=A2 + 5"));
    xls.SetCellValue(2, 1, new TFormula("=A3 * 2"));
    xls.SetCellValue(3, 1, 7);

    xls.SetCellValue(7, 3, new TFormula("=A3 * 2"));

    object ResultValue = xls.RecalcCell(1, 1, 1, true);

    Debug.Assert((double)ResultValue == 19, "RecalcCell returns the value at the cell.");
    Debug.Assert((double)(xls.GetCellValue(1, 1) as TFormula).Result == 19, "Cell A1 was recalculated because we called RecalcCell on it.");
    Debug.Assert((double)(xls.GetCellValue(2, 1) as TFormula).Result == 14, "Cell A2 was recalculated because A1 depends on A2.");
    Debug.Assert((double)(xls.GetCellValue(7, 3) as TFormula).Result == 0, "Cell C7 was NOT recalculated because A1 doesn't have a dependency with it. Call xlsFile.Recalc() to recalc all cells.");

See also