Formula Tracing and Auditing
Overview
Debugging formula networks in large Delphi models requires tracing how cell values flow from input variables to summary aggregations; HotXLS provides auditing APIs to trace formula precedents and dependents step-by-step
Formula Tracing API
TXLSFormulaTracer reduces a formula from its innermost expressions and records source spans, substituted expressions, intermediate values, step kinds, and exact operand-stack state before and after each step
TXLSFormulaStepKind = (stPush, stReduce, stFunction);
TXLSFormulaStep = record
Depth: Integer;
Source: WideString;
Expression: WideString;
Value: Variant;
StepKind: TXLSFormulaStepKind;
StackBefore: WideString;
StackAfter: WideString;
TokenStart: Integer;
TokenEnd: Integer;
end;
Source is the original sub-expression text, Expression substitutes already-evaluated operands with their values, and Value is the step result; StackBefore / StackAfter are comma-separated operand-stack snapshots and TokenStart / TokenEnd pin the step to a 1-based character span in the source formula
Trace and TraceReversed return the immutable steps as a TXLSFormulaStepArray, a dynamic array of TXLSFormulaStep records; TraceReversed returns the same data in reverse evaluation order for step-back interfaces
Interactive Debugger
TXLSFormulaDebugger builds a complete parsed-node model and then records actual execution without evaluating unselected lazy branches
TXLSFormulaDebugNodeKind = (fdnNumber, fdnString, fdnBoolean, fdnReference,
fdnRange, fdnParentheses, fdnUnary, fdnPercent, fdnBinary, fdnFunction);
TXLSFormulaDebugState = (fdsPending, fdsEvaluated, fdsSkipped, fdsError);
TXLSFormulaDebugBranch = (fdbNone, fdbIfTrue, fdbIfFalse, fdbIfErrorValue,
fdbIfErrorFallback, fdbChoose, fdbIfs, fdbSwitch);
TXLSFormulaDependencyKind = (fdkCell, fdkRange, fdkDefinedName);
The returned session exposes operands, intermediate values, source spans, static dependencies, evaluation order, branch choices, skipped nodes, standard formula errors, and propagated error origins. Each TXLSFormulaDebugNode reports its tree position (NodeId, ParentNodeId, Depth), Kind and State, source span, operand node ids and values, the taken Branch, and the recorded error triplet (ErrorCode, ErrorText, ErrorOriginNodeId); each TXLSFormulaDependency reports Kind plus the resolved sheet, reference text, bounds, and evaluation state of one precedent
Cursor methods let an application move forward or backward through the session or seek one node without recalculating the formula
Auditing Features
Check the key interfaces for formula auditing below
- HasFormula property — Detection of whether a cell contains a formula
- Formula property — Read or modify formula tokens
- TXLSFormulaDebugger class — Parsed-tree and runtime diagnosis
- Calculation engine overview — Recalculation details