xlsx-populator
v2.3.7
Published
Tool for populating xlsx templates
Downloads
18
Readme
xlsx-populator
Library for populating xlsx templates with json data. Array values will be automatically cloned
Usage
add this module as dependency for you project
Use it, where you need
const { xlsxPopulator } = require('xlsx-populator');
Call
xlsxPopulator
with next arguments:inputFilePath
- relative, or absolute path to .docx fileoutputFilePath
- path, where populated .docx should appeardataObj
- JSON object with data
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);
});
});
};