37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
module.exports = function (RED) {
|
|
"use strict";
|
|
var json2xls = require('json2xls');
|
|
var fs = require('fs');
|
|
|
|
function ConvertJSONToExcel(config) {
|
|
RED.nodes.createNode(this, config);
|
|
this.file = config.file;
|
|
this.on('input', function(msg){
|
|
var node = this;
|
|
var file = msg.filepath;
|
|
if(file === undefined || file === null){
|
|
if(node.file === undefined || node.file === null ){
|
|
console.log("File path cannot be null");
|
|
node.error("filepath cannot be null");
|
|
}else{
|
|
file = node.file;
|
|
}
|
|
}
|
|
if(msg.payload === undefined || msg.payload === null){
|
|
console.log("Payload cannot be blank");
|
|
node.error("payload cannot be null");
|
|
}else{
|
|
var xls = json2xls(msg.payload);
|
|
fs.writeFileSync(file, xls, 'binary');
|
|
msg.attachment=file;
|
|
msg.filename=file;
|
|
msg.file=file;
|
|
node.send(msg);
|
|
}
|
|
});
|
|
|
|
};
|
|
|
|
RED.nodes.registerType("excel", ConvertJSONToExcel);
|
|
}
|