'Sheetjs `!rows` (and `!cols`) property not existing on newly created sheet?

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).



Solution 1:[1]

you 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');

Solution 2:[2]

!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.com/sheetjs/sheetjs#worksheet-object

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jesper