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 a parsed expression. 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 BrtFmlaBool (section 2.4.677), BrtFmlaError (section 2.4.678), BrtFmlaNum (section 2.4.679) and BrtFmlaString (section 2.4.680) records. Array formulas are specified by the BrtArrFmla (section 2.4.6) record. Shared formulas are specified by the BrtShrFmla (section 2.4.785) record.

Formulas that are part of a revision as specified in the Shared Workbooks (section 2.2.12) overview are specified by the rgce.rgce field or the rgceOld.rgce field of the BrtRRDefName (section 2.4.765) record, or by the sequence of records that conform to the CELL rule (defined in section 2.1.7.44) in the BrtRRChgCell (section 2.4.763) collection.

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 (section 2.5.98.16).

With the exception of control tokens, display tokens and mem tokens that are described in subsequent sections, parsed expressions are stored in an Rgce (section 2.5.98.88) structure 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 (section 2.5.98.16) structures of variable lengths. The first one or two bytes of a Ptg structure contain the token type that determines which specific Ptg type 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.