Spreads...Formula(3)
okay so bestie, welcome to Spreads...Formula(3) from the User Contributed Perl Documentation โ it's the Formula class and it's literally here to generate Excel formulas. ๐
โจ
โจ NAME โจ
Formula โ a class for generating Excel formulas. that's the whole vibe, no cap. ๐
๐ฆ SYNOPSIS ๐ฆ
See the documentation for Spreadsheet::WriteExcel. fr fr that's the synopsis, go read those docs. ๐ซถ
๐ธ DESCRIPTION ๐ธ
This module is used by Spreadsheet::WriteExcel. You do not need to use it directly. like, it's a behind-the-scenes icon, not the main character you call yourself. ๐
๐ NOTES ๐
okay so these notes are here to help developers and maintainers understand the sequence of operation. They're also a pro-memoria for the author. ;-) no cap. ๐ง
Spreadsheet::WriteExcel::Formula converts a textual representation of a formula into the pre-parsed binary format that Excel uses to store formulas. For example 1+2*3 is stored as follows: 1E 01 00 1E 02 00 1E 03 00 05 03.
This string is comprised of operators and operands arranged in a reverse-Polish format. The meaning of the tokens in the above example is shown in the following table:
- token
1Eโ nameptgIntโ value0001(stored as01 00) - token
1Eโ nameptgIntโ value0002(stored as02 00) - token
1Eโ nameptgIntโ value0003(stored as03 00) - token
05โ nameptgMul - token
03โ nameptgAdd
The tokens and token names are defined in the "Excel Developer's Kit" from Microsoft Press. ptg stands for Parse ThinG (as in "That lexer can't grok it, it's a parse thang.") ๐ญ iconic.
In general the tokens fall into two categories: operators such as ptgMul and operands such as ptgInt. When the formula is evaluated by Excel the operand tokens push values onto a stack. The operator tokens then pop the required number of operands off of the stack, perform an operation and push the resulting value back onto the stack. This methodology is similar to the basic operation of a reverse-Polish (RPN) calculator. ๐งฎโจ
Spreadsheet::WriteExcel::Formula parses a formula using a Parse::RecDescent parser (at a later stage it may use a Parse::Yapp parser or Parse::FastDescent). ๐
The parser converts the textual representation of a formula into a parse tree. Thus, 1+2*3 is converted into something like the following, e stands for expression:
e
/ | \
1 + e
/ | \
2 * 3
The function _reverse_tree() recurses down through this structure swapping the order of operators followed by operands to produce a reverse-Polish tree. In other words the formula is converted from in-fix notation to post-fix. Following the above example the resulting tree would look like this:
e
/ | \
1 e +
/ | \
2 3 *
The result of the recursion is a single array of tokens. In our example the simplified form would look like the following:
(1, 2, 3, *, +)
The actual return value contains some additional information to help in the secondary parsing stage:
(_num, 1, _num, 2, _num, 3, ptgMul, ptgAdd, _arg, 1)
The additional tokens are:
_numโ the next token is a number_strโ the next token is a string_ref2dโ the next token is a 2d cell reference_ref3dโ the next token is a 3d cell reference_range2dโ the next token is a 2d range_range3dโ the next token is a 3d range_funcVโ the next token is a function_argโ the next token is the number of args for a function_classโ the next token is a function name_volโ the formula contains a voltile function
The _arg token is generated for all lists but is only used for functions that take a variable number of arguments. ๐ซถ
The _class token indicates the start of the arguments to a function. This allows the post-processor to decide the "class" of the ref and range arguments that the function takes. The class can be reference, value or array. Since function calls can be nested, the class variable is stored on a stack in the @class array. The class of the ref or range is then read as the top element of the stack $class[-1]. When a _funcV is read it pops the class value. ๐
Certain Excel functions such as RAND() and NOW() are designated as volatile and must be recalculated by Excel every time that a cell is updated. Any formulas that contain one of these functions has a specially formatted ptgAttr tag prepended to it to indicate that it is volatile. ๐ฅ
A secondary parsing stage is carried out by parse_tokens() which converts these tokens into a binary string. For the 1+2*3 example this would give:
1E 01 00 1E 02 00 1E 03 00 05 03
This two-pass method could probably have been reduced to a single pass through the Parse::RecDescent parser. However, it was easier to develop and debug this way. ๐
The token values and formula values are stored in the %ptg and %functions hashes. These hashes and the parser object $parser are exposed as global data. This breaks the OO encapsulation, but means that they can be shared by several instances of Spreadsheet::WriteExcel called from the same program. ๐
Non-English function names can be added to the %functions hash using the function_locale.pl program in the examples directory of the distro. The supported languages are: German, French, Spanish, Portuguese, Dutch, Finnish, Italian and Swedish. These languages are not added by default because there are conflicts between functions names in different languages. ๐
The parser is initialised by _init_parser(). The initialisation is delayed until the first formula is parsed. This eliminates the overhead of generating the parser in programs that are not processing formulas. (The parser should really be pre-compiled, this is to-do when the grammar stabilises). ๐
๐ AUTHOR ๐
John McNamara jmcnamara@cpan.org โ the icon behind this. ๐
โญ COPYRIGHT โญ
Copyright MM-MMX, John McNamara.
All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. slay. ๐
mwah ๐ this Spreads...Formula(3) page came from perl v5.42.0 and is dated 2023-10-05. bye bestie, go parse some formulas! ๐ซถโจ
๐ฆ where this page comes from
- opensuse-leap 16.0: perl-Spreadsheet-WriteExcel 2.40-bp160.1.11 ยท Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Spreadsheet-WriteExcel 2.40-bp160.1.11 ยท Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Spreadsheet-WriteExcel 2.40-bp160.1.11 ยท Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Spreadsheet-WriteExcel 2.40-bp160.1.11 ยท Artistic-1.0 OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms