Table of Contents

TExcelFile.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 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 StartBatchRecalcCells and EndBatchRecalcCells

Syntax

Unit: FlexCel.Core

function TExcelFile.RecalcCell(const sheet: Integer; const row: Integer; const col: Integer; const forced: Boolean): TCellValue; virtual; abstract;

Parameters

<-> Parameter Type Description
const sheet Integer Sheet for the cell we want to recalculate. Use ActiveSheet here to refer to the active sheet.
const row Integer Row for the cell we want to recalculate. (1 based)
const col Integer Column for the cell we want to recalculate. (1 based)
const 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:

var
  ResultValue: TCellValue;
...
  xls.SetCellValue(1, 1, TFormula.Create('=A2 + 5'));
  xls.SetCellValue(2, 1, TFormula.Create('=A3 * 2'));
  xls.SetCellValue(3, 1, 7);
  xls.SetCellValue(7, 3, TFormula.Create('=A3 * 2'));
  ResultValue := xls.RecalcCell(1, 1, 1, true);
  Assert(ResultValue.AsNumber = 19, 'RecalcCell returns the value at the cell.');
  Assert(xls.GetCellValue(1, 1).AsFormula.FormulaResult = 19, 'Cell A1 was recalculated because we called RecalcCell on it.');
  Assert(xls.GetCellValue(2, 1).AsFormula.FormulaResult = 14, 'Cell A2 was recalculated because A1 depends on A2.');
  Assert(xls.GetCellValue(7, 3).AsFormula.FormulaResult = 0, 'Cell C7 was NOT recalculated because A1 doesn''t have a dependency with it. Call xlsFile.Recalc() to recalc all cells.');

See also