description and source-codeparams => {
if (!(params instanceof Array)) throw 'buildExport expects an array'
let sheets = []
params.forEach(function (sheet, index) {
let specification = sheet.specification
let dataset = sheet.data
let sheet_name = sheet.name || 'Sheet' + index + 1
let data = []
let merges = sheet.merges
let config = {
cols: []
}
if (!specification || !dataset) throw 'missing specification or dataset.'
if (sheet.heading) {
sheet.heading.forEach(function (row) {
data.push(row)
})
}
//build the header row
let header = []
for (let col in specification) {
header.push({
value: specification[col].displayName,
style: specification[col].headerStyle || ''
})
if (specification[col].width) {
if (Number.isInteger(specification[col].width)) {
config.cols.push({ wpx: specification[col].width })
} else if (Number.isInteger(parseInt(specification[col].width))) {
config.cols.push({ wch: specification[col].width })
} else {
throw 'Provide column width as a number'
}
} else {
config.cols.push({})
}
}
data.push(header) //Inject the header at 0
dataset.forEach(record => {
let row = []
for (let col in specification) {
let cell_value = record[col]
if (specification[col].cellFormat && typeof specification[col].cellFormat == 'function') {
cell_value = specification[col].cellFormat(record[col], record)
}
if (specification[col].cellStyle && typeof specification[col].cellStyle == 'function') {
cell_value = {
value: cell_value,
style: specification[col].cellStyle(record[col], record)
}
} else if (specification[col].cellStyle) {
cell_value = {
value: cell_value,
style: specification[col].cellStyle
}
}
// TODO: REMOVE
// if (specification[col].mergeGroup) {
// cell_value.merge = specification[col].mergeGroup
// }
row.push(cell_value) // Push new cell to the row
}
data.push(row) // Push new row to the sheet
})
sheets.push({
name: sheet_name,
data: data,
merge: merges,
config: config
})
})
return excel.build(sheets)
}
example usage...
{ start: { row: 1, column: 1 }, end: { row: 1, column: 10 } },
{ start: { row: 2, column: 1 }, end: { row: 2, column: 5 } },
{ start: { row: 2, column: 6 }, end: { row: 2, column: 10 } }
]
// Create the excel report.
// This function will return Buffer
const report = excel.buildExport(
[ // <- Notice that this is an array. Pass multiple sheets to create multi sheet report
{
name: 'Report', // <- Specify sheet name (optional)
heading: heading, // <- Raw heading array (optional)
merges: merges, // <- Merge cell ranges
specification: specification, // <- Report specification
data: dataset // <-- Report data
...