Demo Readme Changelog Buy table module Releases RSS

Summary (version 3.4.0)

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.

README

Table Module

This module exposes multiple tags to create or transform tables.

Currently, you can

  • create a table from scratch with the{:table table1} syntax,
  • Loop over table columns with the {:vt#users} {:vt/users} syntax

Installation:

You will need docxtemplater v3: npm install docxtemplater

Install this module with npm install --save "$url"

Building

You can build the es6 into js by running npm run compile

Testing

You can test the module with npm test

Vertical Loop Table

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.

Full Table

Usage

Your docx should contain the text: {:table table1}. You can find a working sample at ./sample.js

Options

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 table
  • widths : Example [100, 150, 320, 100] : This option defines the width of each colum
  • 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 tables

You 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.

Global Options

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));

CHANGELOG

3.4.0

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).

3.3.1

For Full Table module, use style and properties of current paragraph for each cells in the paragraphs.

3.3.0

For Full Table module, Add possibility to :

  • set the alignment with the align option

  • set options global to each table in the constructor

3.2.3

For Full Table module, make all parameters except data optional.

3.2.2

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}

3.2.1

Make generated docx not contain unnecessary commas

3.2.0

Add support for vertical table

3.1.3

  • Add table style only if needed

3.1.2

  • Fix other .forEach when last table is empty.
  • Fix height calculation, that doesn't take into account the height of the vertical cells

3.1.1

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

3.1.0

Add support for dynamic chunkSize.

You can write :

chunkSize: {
    type: "dynamic",
    size: { min: 7000, max: 8100},
},

3.0.0

Initial release

Edgar Hipp

I'm the creator of docxtemplater. I work on making docxtemplater great since 2013.