nodejs Xlsx cell width code example
Example: set auto width sheetjs
let objectMaxLength = [];
for (let i = 0; i < json.length; i++) {
let value = <any>Object.values(json[i]);
for (let j = 0; j < value.length; j++) {
if (typeof value[j] == "number") {
objectMaxLength[j] = 10;
} else {
objectMaxLength[j] =
objectMaxLength[j] >= value[j].length
? objectMaxLength[j]
: value[j].length;
}
}
}
console.log(objectMaxLength);
var wscols = [
{ width: objectMaxLength[0] },
{ width: objectMaxLength[1] },
{ width: objectMaxLength[2] },
{ width: objectMaxLength[3] },
{ width: objectMaxLength[4] },
{ width: objectMaxLength[5] },
{ width: objectMaxLength[6] },
{ width: objectMaxLength[7] },
{ width: objectMaxLength[8] },
{ width: objectMaxLength[9] }
];
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
worksheet["!cols"] = wscols;