0.0.1 • Published 1 year ago

xltpl-js v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

xltpl-js ( node-xlsx-template )

A node module to generate .xlsx files from a .xlsx template.

How it works

When xltpl-js reads a xlsx file, it creates a tree for each worksheet.
And, each tree is translated to a nunjucks template with custom tags.
When the template is rendered, nunjucks extensions of cumtom tags call corresponding tree nodes to write the xlsx file.

How to install

npm install xltpl-js

How to use

  • To use xltpl-js, you need to be familiar with the syntax of nunjucks template.
  • Get a pre-written xls/x file as the template.
  • Insert variables in the cells, such as :
{{name}}
  • Insert control statements in the cells :
{%- for row in rows %}
{% set outer_loop = loop %}{% for row in rows %}
Cell text
{{outer_loop.index}}{{loop.index}}
{%+ endfor%}{%+ endfor%}
  • Run the code
const BookWriter = require('xltpl-js');
async function run() {
    const writer = new BookWriter();
    await writer.readFile('template.xlsx');
    const payloads = await getPayloads();
    writer.renderSheets(payloads);
    await writer.save('result.xlsx');
}
run();

Supported

  • MergedCell
  • Non-string value for a cell (use {{variable}} with no leading or trailing spaces or {%xv variable%} to specify a variable)
  • Image (use {%img variable%})
  • DataValidation
  • AutoFilter

Related