Demo Readme Changelog Buy table module

Summary (version 3.2.2)

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.

Demo

Input Output
Data Code
{
  "html": {
    "fixedColumns": [
      null,
      "Example 1",
      null,
      "Example 2"
    ],
    "widths": [
      100,
      150,
      320,
      100
    ],
    "header": [
      "Source",
      "Hazard",
      "Handling",
      "Protection"
    ],
    "subheader": [
      "The source",
      "The Hazard",
      "The Handling",
      "The Protection"
    ],
    "chunkSize": 6,
    "data": [
      [
        "A1",
        "Lorem ipsum, dolor sit amet..."
      ],
      [
        "B1",
        "Lorem ipsum, dolor sit amet..."
      ],
      [
        "C1",
        "Lorem ipsum, dolor sit amet..."
      ],
      [
        "A1",
        "Lorem ipsum, dolor sit amet..."
      ],
      [
        "B1",
        "Lorem ipsum, dolor sit amet..."
      ],
      ...
    ]
  }
}
const doc = new Docxtemplater();
const inputZip = new JSZip(docX);
doc.loadZip(inputZip)
	.setData(data);
const tableModule = new TableModule.Full({});
doc.attachModule(tableModule);
const output = doc.render().getZip();

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.

CHANGELOG

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.