最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sheetjs `!rows` (and `!cols`) property not existing on newly created sheet? - Stack Overflow

programmeradmin0浏览0评论

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

Share Improve this question asked Feb 22, 2021 at 22:58 paul23paul23 9,45517 gold badges74 silver badges167 bronze badges 1
  • 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
Add a ment  | 

2 Answers 2

Reset to default 6

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

!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

发布评论

评论列表(0)

  1. 暂无评论