Skip to content

Instantly share code, notes, and snippets.

@kdridi
Created November 23, 2016 16:19
Show Gist options
  • Save kdridi/3566779ead1fcf0604468becb7d4313e to your computer and use it in GitHub Desktop.
Save kdridi/3566779ead1fcf0604468becb7d4313e to your computer and use it in GitHub Desktop.
/* Import dependencies, declare constants */
var fs = require('fs');
var path = require('path');
var XlsxTemplate = require('xlsx-template');
/**
* Your function call
* @param {Object} params Execution parameters
* Members
* - {Array} args Arguments passed to function
* - {Object} kwargs Keyword arguments (key-value pairs) passed to function
* - {String} remoteAddress The IPv4 or IPv6 address of the caller
*
* @param {Function} callback Execute this to end the function call
* Arguments
* - {Error} error The error to show if function fails
* - {Any} returnValue JSON serializable (or Buffer) return value
*/
module.exports = (params, callback) => {
// Load an XLSX file into memory
const filename = path.join(__dirname, 'templates', 'template.xlsx');
fs.readFile(filename, function(err, data) {
if (err) {
console.error(err);
} else {
// 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(),
dates: [ new Date("2013-06-01"), new Date("2013-06-02"), new Date("2013-06-03") ],
people: [
{name: "John Smith", age: 20},
{name: "Bob Johnson", age: 22}
]
};
// Perform substitution
template.substitute(sheetNumber, values);
// Get binary data
var data = template.generate();
// fs.writeFileSync('test.xlsx', data, 'binary'); // <<<<< it's working on local host
callback(null, data); // <<<<< NOT WORKING
// callback(null, Buffer.from(data)); // <<<<< NOT WORKING
// callback(null, Buffer.from(data, 'utf-8)); // <<<<< NOT WORKING
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment