xlsx-template-expressions

0.0.5 • Public • Published

A hack of optilude's xlsx-template module adding support for Angular expressions to handle logic statements within XLSX templates. See for a complete reference of all possibilities of angularjs parsing: http://teropa.info/blog/2014/03/23/angularjs-expressions-cheatsheet.html.

Example (XLSX) template (double pipes define cells)

Date || {{extractDate}}
People ||
{{table:people.name | upper}} || {{table:people.age}}

Example usage

var XlsxTemplate = require('xlsx-template-expressions');
var expressions = require("angular-expressions");
var fs = require('fs');
 
expressions.filters.upper = function(input) {
    // This condition should be used to make sure that if your input is undefined, your output will be undefined as well and will not throw an error
    if(!input) return input;
    return input.toUpperCase();
}
 
// Load an XLSX file into memory
fs.readFile('./templates/template1.xlsx', function(err, data) {
 
    // Create a template
    var template = new XlsxTemplate(data);
 
    // Replacements take place on first sheet
    var sheetNumber = 1;
 
    // Set up some placeholder values matching the placeholders in the template
    var values = {
        extractDate: new Date(),
        people: [
            {name: "John Smith", age: 20},
            {name: "Bob Johnson", age: 22}
        ]
    };
    
    // Use the angular expressions parser
    template.parser = function(values) {
        return {
            get: function(tag) {
                if (!tag || !values) return false;
                return ((expressions.compile(tag)(values) == undefined) ? false : expressions.compile(tag)(values));
            }
        };
    };
 
    // Perform substitution
    template.substitute(sheetNumber, values);
 
    // Get binary data
    var data = template.generate({type: 'nodebuffer'});
 
    var readStream = new stream.PassThrough();
    readStream.end(data);
    res.set('Content-disposition', 'attachment; filename=output.xlsx');
    res.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    readStream.pipe(res);
 
});

Readme

Keywords

Package Sidebar

Install

npm i xlsx-template-expressions

Weekly Downloads

102

Version

0.0.5

License

MIT

Unpacked Size

48.2 kB

Total Files

3

Last publish

Collaborators

  • thinklaw