2.2.2 Formulas

A formula is sequence of values, cell references, names, functions, or operators in a cell that together produce a new value. Formulas are stored in a tokenized representation known as "parsed expressions." In this section, formula is a synonym for parsed expression. A parsed expression is converted into a textual formula at runtime for display and user editing. Cell formulas are specified by the Formula record (section 2.4.127). Array formulas are specified by the Array record (section 2.4.4). Shared formulas are specified by the ShrFmla record (section 2.4.260).

Formulas that are part of a revision as specified in the Shared Workbooks overview (section 2.2.11) are specified by the pe.rgce field or the peOld.rgce field of the RRDDefName record (section 2.4.225), or by the xpe.rgce field or the xpeOld.rgce field of the RRDChgCell record (section 2.4.223).

A parsed expression contains a sequence of parse tokens, each of which is either an operand token (section 2.2.2.2), an operator token (section 2.2.2.1), a control token (section 2.2.2.3), a display token (section 2.2.2.4), or a mem token (section 2.2.2.5). All tokens are stored as Parse Things (Ptg (section 2.5.198.25)).

With the exception of control tokens (section 2.2.2.3), display tokens (section 2.2.2.4), and mem tokens (section 2.2.2.5) that are described in subsequent sections, parsed expressions are stored in Rgce (section 2.5.198.104) using Reverse-Polish notation. Reverse-Polish notation is a logical system for the specification of mathematical formulas in which operands are followed by operators. Inside an Rgce, the operands and operators are represented by an array of Ptg structures (section 2.5.198.25) of variable lengths. The first one or two bytes of a Ptg structure (section 2.5.198.25) contain the token type that determines which specific Ptg type (section 2.5.198.25) the Ptg is, as specified in the Ptg structure The remainder of the structure varies according to the token type.

Evaluation of a formula specified in Reverse-Polish notation is usually based around an evaluation stack. The expression is parsed from beginning to end, and operands are pushed onto the stack as they are encountered. When operators are encountered, the required number of operands is popped from the stack and the result of the operation is pushed back onto the stack. Evaluation begins with an empty stack, and when the evaluation is finished, there will be exactly one value left on the stack. The value is the result of the evaluation. Subsequent subsections refer to a stack as described by this model.