npm.io
4.0.3 • Published 10 months ago

xlsx-populator

Licence
MIT
Version
4.0.3
Deps
8
Size
18.2 MB
Vulns
0
Weekly
0

xlsx-populator

Library for populating xlsx templates with json data. Array values will be automatically cloned

Usage

  1. add this module as dependency for you project

  2. Use it, where you need

    const { xlsxPopulator } = require('xlsx-populator');

  3. Call xlsxPopulator with next arguments:

    • inputFilePath - relative, or absolute path to .docx file
    • outputFilePath - path, where populated .docx should appear
    • dataObj - JSON object with data
  4. Promise will be returned with path to generated file

Usage example:

const handleDocx = (data, cb) => {
  const operationId = new Date().getTime();

  console.log('operationId', operationId);

  const dataObj = data.data;
  const docsPath = '/docs/' + operationId;
  const dirPath = 'public' + docsPath;
  fs.mkdir(dirPath, () => {

    const filePath = dirPath + '/input.docx';
    const file = fs.createWriteStream(filePath);
    const getFunction = data.template.match(/^https/) ? https.get : http.get;
    const request = getFunction(data.template, function(response) {
      response.pipe(file);
      file.on('finish', function() {
        file.close(() => {

          return xlsxPopulator(
            filePath,
            path.join(__dirname,'../',dirPath , '/outputdocx.docx'),
            dataObj)
            .resolve(cb);
        });

      });
    }).on('error', function(err) {
      console.log('! fail to load file', err);

      if (cb) cb(err.message);
    });

  });
};

Keywords