This module makes it easy to create a table from a configuration object (header, subheader, ...)
This module is available as part of the docxtemplater ENTREPRISE plan.
This module exposes multiple tags to create or transform tables.
Currently, you can
{:table table1}
syntax,{:vt#users} {:vt/users}
syntaxYou will need docxtemplater v3: npm install docxtemplater
Install this module with npm install --save "$url"
To have the vertical-loop-table, write :
const TableModule = require("docxtemplater-table-module");
doc.attachModule(new TableModule.Vertical());
To create a vertical loop table, you just have to use the {:vt#users} {:vt/users} tags, for example
Table | {:vt#users} |
---|---|
Name | {name} |
Age | {age} |
Address | {address}{:vt/users} |
This will create a column for each user in the users
array.
It will completely remove the column if users
is false or empty.
Your docx should contain the text: {:table table1}
. You can find a working sample at ./sample.js
To have the vertical-loop-table, write :
const TableModule = require("docxtemplater-table-module");
doc.attachModule(new TableModule.Full());
When creating the table, you have multiple options :
fixedColumns
: Example : [null, "Example 1", null, "Example 2"] : This option defines the columns that should take the whole height of the tablewidths
: Example [100, 150, 320, 100] : This option defines the width of each columindent
: Example 150 : This option defines the indenttion of the table, ie the indentation to be added before the leading edge of the tabletotalWidth
: This option cannot be used together with widths
. if you use totalWidth, the width of each cell will be the totalWidth divided by the number of columns. For example, you can set totalWidth to 700
and have 2 columns of each width 350. It is also possible to set a width as a percentage with a string (for example : totalWidth: "80%"
. When using percentages, the current page layout will be taken into account, to have the table width depend on the page layout.header
: Example ["Source", "Hazard", "Handling", "Protection"] : This option defines the header of the table (in bold)subheader
: Example ["The source", "The Hazard", "The Handling", "The Protection"] : This option defines the subheader of the table (in italic)chunkSize
: Example : 6 : This option defines the number of lines after which to break the table into multiple tablesfinalPageBreak
: (true or false), default false. Whether to insert a finalPageBreak after the generated table/tablesYou can also have dynamic chunkSize, for example:
chunkSize: {
type: "dynamic",
size: { min: 7000, max: 8100},
},
This means that the height of the table will never exceed 8100 (the max). If the table exceeds the min, the next line will be in a new table. The unit is an internal word unit.
data
: Example :
data: [
["A1", "lorem"],
["B1", "lorem"],
["C1", "lorem"],
]
This option defines the data of the table.
border
: Example : { type: "all", size: 5, color: "000000"} : This option defines the borders that are around each cell.
It is possible to have global options for all tables created in the document, for example, to have all tables center-aligned :
const options = { align: "center" };
doc.attachModule(new TableModule.Full(options));
If options is an object, it will be merge with the default options.
It is also possible to pass a function here which gets the default options as an argument, and returns the used options.
For example, to remove the default spacingAfter of 200, you can write
const options = function(defaultOptions) {
return {};
};
doc.attachModule(new TableModule.Full(options));
It is possible to create a grid layout like this :
Grid template
Which will be rendered like this :
Grid template
In your code, do the following
const TableModule = require("docxtemplater-table-module");
doc.attachModule(new TableModule.Grid())
doc.setData({
user: {
size: [2, 4], // means 2 lines, 4 columns
widths: [175, 175, 175, 175], // The widths of each column
height: 200, // The height of each column
data: [
{
index: 1,
name: "John",
title: "Foobar",
description: "3374 Olen Thomas Drive Frisco Texas 75034",
},
{
index: 2,
name: "Mary",
title: "Foobaz",
description: "352 Illinois Avenue Yamhill Oregon(OR) 97148",
},
...
]
}
});
If you also have access to the slides module version 3.2.0 or above, you can also have PPTX Grid Layout support.
You can generate multiple "slides" in a grid layout with following template :
companies is an array of objects representing each company.
It will generate output similar to this :
In your code, do the following
const TableModule = require("docxtemplater-table-module");
doc.attachModule(new TableModule.GridPptx());
doc.attachModule(new SlidesModule());
doc.setData({
companies: [
{
name: "first company",
id: "first id",
logo: "2",
region: "france"
},
{
name: "second company",
id: "second",
region: "germany"
},
{
name: "third",
id: "third id",
logo: "4",
region: "russia"
},
{
name: "fourth",
id: "fourth id",
region: "italy"
}
]
});
It is also possible to use the Grid Layout on some named data, for example :
will render :
with following data :
{
"people": [
{
"personName": "Mary foo",
"awards": [
{
"name": "Swim 15km",
"image": "disc.png"
},
{
"name": "Jump 2m",
"image": "disc.png"
},
{
"name": "Drink 2L",
"image": "disc.png"
}
]
}
]
}
With vertical-table loop module, it is now possible to have nested horizontal loops + simple vertical loops
With vertical-table loop module, when you have row loops, you can also have a column that you hide/show based on a condition
In grid slide module for pptx, add possibility to loop over a given value.
Bugfix Cannot read property 'map' of undefined
when having data: [] without any fixedColumns.
Bugfix for Cannot read property 'getAttribute' of undefined
when having styles without names.
For Vertical Table module, the generated document was corrupt when having tables inside tables. This is no more the case
Do not add <w:spacing>
tag if it is already present (fixes setting the spacing on the paragraph to change spacing of each paragraphs in the table).
Add support for finalPageBreak
(default false), and stop inserting page breaks every time.
Add support for newlines (with \n
) in data, header, subheader.
Add support for indent
parameter to offset the table
Add support for table grid functionality when using this module together with the slides module.
Allow to have data: []
to show only a header.
Keep same font-size of text in {:table table1}
Update browser build to use XMLSerializer instead of xmldom
Use requiredAPIVersion
Add option border : { type: "all"}
to force to have borders.
Fix bug totalWidth float
Add possibility to specify totalWidth, for example totalWidth: 700
or totalWidth: '100%'
.
Add table grid module using : {:#grid user}
and {:/grid}
Explanation : On some versions of npm (notably 5.8.0), when having a package containing docxtemplater-table-module, the installation will generate a tree of node_modules that puts the module on a level where it has no access to docxtemplater. By explicitly asking it as a dependency, this issue is avoided.
Make it possible to not have a subheader (header without subheader)
Explanation : Recently, the scopemananger API (internal API) has changed, this new version of the footnotes module makes the module work with both versions newer than 3.6 and older than 3.6 of docxtemplater.
Add meta context argument to custom parser with information about the tag for each types of tags
For Full Table module, add possibility to add function in constructor to be able to customize the default spacingAfter
property (eg for example, remove any default options).
For Full Table module, use style and properties of current paragraph for each cells in the paragraphs.
For Full Table module, Add possibility to :
set the alignment with the align
option
set options global to each table in the constructor
For Full Table module, make all parameters except data
optional.
Handle vertical tables that have different amount of columns. For example :
Table | {:vt#users} | |
name | (The name of the user) | {name} |
address | (The address of the user) | {address}{:vt/users} |
Make generated docx not contain unnecessary commas
Add support for vertical table
.forEach
when last table is empty.The headerSize is now calculated and taken into account when chunking.
Fix .forEach
bug : If the dataset contains a cell that would not fit in the table because it is too high, the module will still put it in there since it cannot be splitted
Add support for dynamic chunkSize.
You can write :
const opts = {
chunkSize: {
type: "dynamic",
size: { min: 7000, max: 8100 }
}
};
Initial release