It works for Arrays or Arrays([[..][..]]
) using range
or using single cell as well.
From the doc:
xlsx-populate also supports ranges of cells to allow
parsing/manipulation of multiple cells at once.
const r = workbook.sheet(0).range("A1:C3");
// Set all cell values to the same value:
r.value(5);
// Set the values using a 2D array:
r.value([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
// Set the values using a callback function:
r.value((cell, ri, ci, range) => Math.random());
Alternatively, you can set the values in a range with only the
top-left cell in the range:
workbook.sheet(0).cell("A1").value([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
So all you need to do is converting objects to CSV maybe for an example JS object:
Modified(only removed the .join
s) From this answer:
const json = [
{
h1:1,
h2:2
},
{
h1:3,
h2:4
}
];
var fields = Object.keys(json[0])
var replacer = function(key, value) { return value === null ? '' : value }
var csv = json.map(function(row){
return fields.map(function(fieldName){
return JSON.stringify(row[fieldName], replacer)
})
})
csv.unshift(fields) // add header column
console.log(csv) //[ [ 'h1', 'h2' ], [ '1', '2' ], [ '3', '4' ] ]