With 4D v18 R2, 4D Write Pro is able to manage formula objects inside documents. To do so, four new commands have been created: WP Get formulas, WP Insert formula, WP Compute formulas, and WP Freeze formulas. They all can be used with intuitive targets like document, body, headers, etc. And as you may have guessed from the command names, formulas are no longer text expressions but powerful formula objects!
All at once
The first command, WP Get formulas, enables getting all of the formulas within a specific target. Of course the first target that comes to mind is the full document itself, but it could also be a more specific part of the document such as the body, the second section, the left header, just name it! This command returns a collection of objects containing the formula, the range (when the expression is inline), or the anchoredID when the expression is attached to an anchored picture.
Note: The formula object now has a readable attribute called source. This attribute is very useful for finding and replacing actions. (see below)
The WP Compute formulas and WP Freeze formulas commands can be used in a very similar way (i.e., using a target). You can re-evaluate all of the document’s expressions, or you could decide to freeze only the expressions belonging to the footers. Simple and logical.
// find all expressions of the document
C_COLLECTION($_expressions)
$_expressions:=WP Get formulas(WParea)
one by one
The fourth command, WP Insert formula, enables inserting an expression (as a formula object) within any given range. It can also be used to replace existing expressions (once found by the WP Get formulas command).
Here’s a simple replacement example: your document contains Current date expressions and you’d like to replace all of them with a formatted string such as “Thursday 14 November 2019“.
// define what to find
$find:=Formula(Current date)
// define the replacement
$formula:=Formula(String(Current date;System date long))
// find all expressions of the document
$_expressions:=WP Get formulas(WParea)
// query the collection (could have been done on previous line, but easier to read this way!)
$_expressions:=$_expressions.query("formula.source = :1";$find.source)
// then replace each expression with the new one
For each ($expression;$_expressions)
WP INSERT FORMULA($expression.range;$formula;wk replace)
End for each
More improvements
Some existing expressions have also been improved and simplified. Reserved local variables like $wp_pageCount or $wp_author have now been declared obsolete even if they’re still supported. They’ve been replaced by using the more readable and modern This.property. Here’s the complete list of available properties:
$wp_subject -> | This.subject |
$wp_author -> | This.author |
$wp_company -> | This.company |
$wp_notes -> | This.notes |
$wp_dateCreation -> | This.dateCreation |
$wp_dateModified -> | This.dateModified |
$wp_pageNumber -> | This.pageNumber |
$wp_pageCount -> | This.pageCount |
$wp_title -> | This.title |
More powerful, more simple
To conclude, we’re sure that the use of these native commands will really simplify the way you manage formulas inside 4D Write Pro documents. Feel free to read the full documentation and have a look at the HDI database for more information!