4D Write Pro keeps offering more and more programming capabilities! Let’s say you want to programmatically change the style of a table, paragraph, or image in an existing 4D Write Pro document. With 4D v17, it’s possible! Now you can access any element or part of a document, by programming. These parts, called elements, will be returned either as a collection thanks to the WP Get elements function, or as a single element with WP Get element by ID function.
collection of elements
The WP Get elements command returns a collection of elements of any type (paragraphs, tables, images, etc. If a typed range or reference is passed, the command will return a collection containing only elements of the corresponding type (unless specified in the second parameter). Otherwise, the command returns a collection containing all of the available elements, no matter its type.
- If the first parameter is a range of a specific type (a paragraph range for instance), then the returned elements will also be paragraphs.
- If the first parameter is not specific (a full document or a heterogeneous range) then the returned elements can be filtered with a final parameter (optional).
// Returns all elements of the document
$allElements:=WP Get elements( )
// Returns all table elements of the document
$allTables:=WP Get elements( ;wk type table)
// Returns all paragraphs of the range
$paragraphCol:=WP Get elements($paragraphRange)
// Returns all paragraphs of the table range
$paragraphCol:=WP Get elements($tableRange;wk type paragraph)
// Returns all the tables of the given range
$someTableCol:=WP Get elements($customRange;wk type table)
Once created, the collection can then be parsed. Each element is an object that can be used as parameter for the command, WP SET ATTRIBUTES.
The WP Get element by ID command allows access to a single element. In 4D Write Pro documents, some elements have default IDs (like pictures and tables). When they exist, these IDs can be modified, and when they don’t exist, they can be created. Knowing that, the behavior of the command is quite easy to understand, it returns a single element for a given ID!
Once you get the element, you can manipulate it as shown below:
$element:=WP Get element by ID(myDoc;"Table1")
WP SET ATTRIBUTES($element;wk border style;wk solid)
WP SET ATTRIBUTES($element;wk border width;"4px")
WP SET ATTRIBUTES($element;wk border color;"blue")