Your wish has finally come true, you can now insert and fill tables by programming in your 4D Write Pro documents. We are not talking about bar tabs mimicking tables here, this is all about “real” tables! Tables where the text will automatically wrap to fit into the column size you defined, that will not “break” when your data changes.
This feature is the first major improvement exceeding the old 4D Write set of functionalities. You will now be able to generate invoices containing tables, fully by programming and style them exactly as you want with a few line of codes! Another scenario is that you can now provide your users with a button in your application UI that will insert a table with a predefined number of lines and rows, a predefined size for each column then you can let the user edit and style the content by itself.
What is possible for the 4D Developer?
Thanks to a new set of commands, 4D developers can now create a table by programming in a 4D Write Pro document, add rows to this table then style the whole table content, just one or several rows, a cell or even a set of cells! The WP Insert table, WP Table append row, WP Table get rows, WP Table get columns and WP Table get cells new commands are easy to use and manipulates ranges following the 4D Write Pro language concept.
What is possible for the end-user?
Once a table has been created by programming, cell contents can be edited by the users of the 4D Write Pro area. They can click into a cell and select, edit, copy/paste, or delete text or pictures just like in regular paragraphs. The users can also select columns, rows, or cells and style them, for instance change the text to bold/italic, change the text/background colors, the text alignment, etc… using the contextual menu or any customized interface.
Download our example
If you want to see how easy it is to build the below table example with 4D Write Pro, please download our example database:
Insert a table and add rows
WP Insert table creates a table and returns a reference on the table object. This reference can be used hereafter to add a new row and populate cell values using WP Table append row.
WPrange:=WP Get range(WParea;wk start text;wk end text)
WPtable:=WP Insert Table(WPrange;wk append)
$row:=WP Table append row(WPtable;[myTable]picture;[myTable]product;[myTable]date;[myTable]value)
Style the table and its content
Then, most of attributes that could already be applied to the document, paragraph or text can also be applied to the table itself, the rows, the columns and even the individual cells. The column width, the alignment, the font type and size, the colors, the borders… anything you need to customize the style of your table can be set as simply as for any other content type in your document.
To do so, simply use WP Table get columns, WP Table get rows and WP Table get cells to get the ranges you want to manipulate using WP SET ATTRIBUTES and WP GET ATTRIBUTES command.
Set font size to 16 pt for the whole table:
WP SET ATTRIBUTES(WPtable;wk font size;16)
Set width of the second column to 150 pt:
$col:=WP Table get columns(WPtable;2;1)
WP SET ATTRIBUTES($col;wk width;"150pt")
Set font style of the first row to bold:
$row:=WP Table get rows(WPtable;1;1)
WP SET ATTRIBUTES($row;wk font bold;wk true)
Set cell (2;2) font color to blue:
$cell:=WP Table get cells(WPtable;2;2)
WP SET ATTRIBUTES($cell;wk text color;"blue")
For more details, we recommend that you take a look at the 4D v16 R4 documentation.