HotXLS Docs

Calculate method

Calculates the result of specified expression
The built-in evaluator supports arithmetic, cell and range references, defined-name ranges, and these function groups: text functions (TRIM, PROPER, CLEAN, CHAR, CODE, T, EXACT, REPT, FIND, SEARCH, SUBSTITUTE, REPLACE); date functions (EDATE, EOMONTH, DATEDIF, YEARFRAC, DAYS, WEEKNUM, ISOWEEKNUM); workday functions (WORKDAY, NETWORKDAYS); engineering/base-conversion functions (DEC2BIN, DEC2HEX, DEC2OCT, BIN2DEC, BIN2HEX, BIN2OCT, OCT2DEC, OCT2BIN, OCT2HEX, HEX2DEC, HEX2BIN, HEX2OCT, BASE, DECIMAL, ARABIC); bitwise functions (BITAND, BITOR, BITXOR, BITLSHIFT, BITRSHIFT); statistical functions (VAR, VARP, STDEV, STDEVP, MODE, VARA, VARPA, STDEVA, STDEVPA); aggregate functions (PRODUCT, SUMSQ); math functions (GCD, LCM, MROUND, QUOTIENT, MULTINOMIAL) Custom or unsupported worksheet functions can still be resolved through OnUserFunction / OnUserFunctionEx

Syntax

function Calculate(Expression: WideString):Variant;
Expression WideString. A string that specifies an expression which should be calculated

Example

This example calculates the simple expression

Val := Workbook.Calculate('100/2*3');
This example calculates a text expression

Val := Workbook.Calculate('SUBSTITUTE("a-b-c";"-";"+")');
This example determines the smallest value in a range A1:F10 on active Worksheet

Val := Workbook.Calculate('MIN(A1:F10)');
This example is the same to previous

Val := Workbook.ActiveSheet.Calculate('MIN(A1:F10)');

Cached-result verification

TXLSWorkbook and TXLSXWorkbook can compare stored formula results with freshly evaluated results without replacing the stored cache
function CalculateAndVerify: Integer; overload;
function CalculateAndVerify(out Mismatches: TArray<TXLSVerifyMismatch>): Integer; overload;
The return value is the number of mismatches. Each record contains SheetIndex, SheetName, one-based Row and Col, Expected, Actual, and Formula. Volatile formulas, data-table cells, formulas without a stored result, and formulas that cannot be evaluated are skipped
MismatchCount := Workbook.CalculateAndVerify(Mismatches);
for I := 0 to MismatchCount - 1 do
  Log(Mismatches[I].SheetName, Mismatches[I].Row,
    Mismatches[I].Col, Mismatches[I].Expected, Mismatches[I].Actual);

See also

OnUserFunction callback
TXLSWorksheet.Calculate method