Well given the following snippet for sheetjs:
const XLSX = require('xlsx');
const workbook = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([[1, 2, 3], [4,5,6]]);
XLSX.utils.book_append_sheet(workbook, ws, page);
Whenever I try to access a row: const rows = ws['!rows'];
it returns undefined
. Inspecting ws
shows it also doesn't hold that property. How would I access the row properties (and set for example styles for the first row).
Well given the following snippet for sheetjs:
const XLSX = require('xlsx');
const workbook = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([[1, 2, 3], [4,5,6]]);
XLSX.utils.book_append_sheet(workbook, ws, page);
Whenever I try to access a row: const rows = ws['!rows'];
it returns undefined
. Inspecting ws
shows it also doesn't hold that property. How would I access the row properties (and set for example styles for the first row).
- Are you using the munity edition? Because it is the professional edition that apparently allows you to use styles. See: sheetjs./pro – Hernán Alarcón Commented Feb 23, 2021 at 2:20
2 Answers
Reset to default 6you need to declare it first, then you can modify width, height
let wb = XLSX.utils.book_new();
let ws = XLSX.utils.table_to_sheet(document.getElementById(id));
ws['!cols'] = [];
ws['!rows'] = [];
ws['!cols'] = [
{'width' : 30}, // width for col A
{'width' : 30}, // width for col B
{'hidden' : true}, ]; // hidding col C
ws['!rows'] = [
{'hpt' : 30},// height for row 1
{'hpt' : 30}, ]; //height for row 2
XLSX.utils.book_append_sheet(wb, ws, 'sheet1');
XLSX.writeFile(wb, filename+'.xlsx');
!rows and !cols has all the properties you give it. so it's undefined until you set some. The following code sets the first column to be 15 characters wide.
ws['!cols'] = [{wch: 15}];
more over here: https://github./sheetjs/sheetjs#worksheet-object