I am using node-excel-export
module in node.js to export my data from DB into excel sheet.
The problem here is that, i want the excel sheet to be shipped with formulae applied on specific cells.
The documentation in the site does not demonstrate the usage of formulae clearly.
This is what i have tried:
var XLSX = require('xlsx');
function getSheet(data, opts) {
var ws = {};
var range = {
s: {
c:0,
r:0
},
e: {
c:3,
r:3
}
};
for(var R = 0; R < 4; R++) {
for(var C = 0; C < 3; C++) {
var cell = {
v: data[R][C] ,
t: 'n'
};
var cell_ref = XLSX.utils.encode_cell({
c:C,
r:R
});
if(C == 2) {
cell_ref.f = "A"+R+"B"+R;
}
ws[cell_ref] = cell;
}
}
ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}
var data = [
[1, 2],
[3, 4],
[5, 6],
[7, 8]
];
function Workbook() {
if(!(this instanceof Workbook)) {
return new Workbook();
}
this.SheetNames = [];
this.Sheets = {};
}
var wb = new Workbook();
var wsName = "SheetJS";
wb.SheetNames.push(wsName);
wb.Sheets[wsName] = getSheet(data);
module.exports = {
dataset: [],
getExcelFile: function (resultset, res) {
/* write file */
XLSX.writeFile(wb, 'test.xlsx');
res.download('test.xlsx');
}
};
`
please help.
OK solution found.
I did two mistakes
1.Instead of cell_ref.f
use cell.f
.
2.Formula correction.
Updated code if it helps others:
var XLSX = require('xlsx');
function getSheet(data, opts) {
var ws = {};
var range = {
s: {
c:0,
r:0
},
e: {
c:3,
r:3
}
};
for(var R = 0; R < 4; R++) {
for(var C = 0; C < 3; C++) {
var cell = {
v: data[R][C],
t: 'n'
};
var cell_ref = XLSX.utils.encode_cell({
c:C,
r:R
});
if(C == 2) {
cell.f = "A"+ (R+1) +"+B" + (R+1);
}
ws[cell_ref] = cell;
}
}
ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}
var data = [
[1, 2],
[3, 4],
[5, 6],
[7, 8]
];
function Workbook() {
if(!(this instanceof Workbook)) {
return new Workbook();
}
this.SheetNames = [];
this.Sheets = {};
}
var wb = new Workbook();
var wsName = "SheetJS";
wb.SheetNames.push(wsName);
wb.Sheets[wsName] = getSheet(data);
module.exports = {
dataset: [],
getExcelFile: function (resultset, res) {
/* write file */
XLSX.writeFile(wb, 'test.xlsx');
res.download('test.xlsx');
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.