As developers, we often create complex and advanced 4D Write Pro documents. We may generate tables that need to be filled from a data source, and we may not be sure of the exact rows and columns. Manually filling the tables with data every time is a laborious task. We heard you, and here is where data context comes as an invaluable option, opening the world of ORDA and object-oriented development to 4D Write Pro documents.
It is now possible to define a “datasource” for the tables of 4D Write Pro. Whether it is an entity selection or a collection, this “datasource” will automatically feed the rows of these tables.
Let’s delve into the details!
A picture is worth 1000 words.
This feature enables you to create documents just like this one:
Once completed, it can be exported (as PDF, as Docx, or any other available export format.), printed, modified, etc. Let’s see, step by step, how to proceed!
Table, header row, “Template” row, Other rows
In a 4D Write Pro document, a table can now contain one or more header rows. The row that immediately follows the header will be considered a template row that will be repeated and filled by the content of the datasource elements. The following rows (if any) will be automatically “pushed” downwards and can contain more information, possibly also based on the table datasource. The original document used to generate the above report looks like this:
The first frame, at the top, is filled with information provided by the data context. If you’re not familiar with this concept, we strongly recommend reading this blog post first. Let’s see how the table itself is defined.
In this case, the table contains three parts (detailed later):
- The first row has been defined as a header.
- The second row is the template row.
- The following rows contain extra information based on the table datasource and calculations.
ASSOCIATION OF A DATA SOURCE TO A TABLE
As its name suggests, a data source provides data to a table. Therefore, this data source should be a formula whose result is either an entity selection or a collection. If it is not the case (after all, a formula can return anything), the result of the formula can still be accessed in the table (see below), but no row will be used as a template.
Two ways to proceed
#1: DEFINE THE DATASOURCE WITH THE User interface
The user interface has been modified so that a formula can be directly associated with a table.
Let’s imagine a “Company” table connected to an “Employee” table via an “employees” link. If a Company entity is accessible via a context in the document, then “employees” will be the datasource of the table.
The formula associated with the table can be This.data.company.employees.
… where This.data (the context) is going to be provided (later on) by programming:
$context:=New object("company";$company) // $company is a company entity WP SET DATA CONTEXT(WParea;$context)
#2: DEFINE THE DATASOURCE BY PROGRAMMING
The datasource can also be set using a single line of code.
Each table can now hold a new attribute which can be defined with the WP SET ATTRIBUTES command.
WP SET ATTRIBUTES($table;wk data source;Formula(This.data.company.employees))
HOW TO DEFINE THE CONTENT OF THE ROWS?
Once the table datasource has been defined, it’s time to fill the rows!
New attributes are available thanks to the This keyword which is based on the datasource items.
In the template row, you can use This.item.xxx where xxx (lastname, address, zipCode…) will be the attributes of the current entity you want to access like This.item.name or This.item.age. You can also access the item’s index in the selection (or collection) with the attribute This.itemIndex …
Once computed (using WP COMPUTE FORMULAS), the calculated rows can be displayed as seen in the introduction.
More table attributes!
Some other attributes can also be used anywhere in the table, usually in the following rows.
This.table.datasource returns the formula associated with the table. Still, it will probably be better to use This.tableData, whose value is the already evaluated result of this formula (usually a collection or a selection entity).
This optimizes the processing, of course, and ensures consistency in the document.
From this last attribute (tableData), it becomes easy to calculate sums, averages, and minimum or maximum values thanks to formulas like these:
Even more attributes?
New attributes have been added that can be reached in any table (even without a defined data source). They will allow having access to:
- The table itself uses This.table,
- The current row with This.row,
- The current row number via This.rowIndex.
PRINT full reports in four lines of code!
Once this kind of document is created, it can be used as a template.
For each ($company; $companies) WP SET DATA CONTEXT($template; New object("company"; $company)) WP PRINT($template) // (before printing, expressions will be recomputed by default) End for each
CUSTOMIZE REPORTS, LISTS, ETC.
You can also use a template whose tables have been filled with values from the datasource. You will be able to highlight rows, change colors and styles, and even remove or add empty rows!
The best way to do this is to freeze the document first so that all the inserted data is now regular rows and text. Freezing the document is not mandatory, but you must be aware all your modifications will be lost if formulas are recomputed.
Finally, if your document has to be saved (not only printed or exported), then you should use a copy of your templates so that you don’t lose the templates themselves.
MyWorkingDocument :=WP New($template))
This new feature allows you to generate documents from templates. Their content can be based on ORDA with a minimum of programming lines.
These documents can be exported as PDF, Docx, eMail. They can also be used as improvements to list forms and Print selection.
Automatic document generation is now greatly facilitated, whether it’s for invoices, catalogs, or inventories.