dtjohnson / xlsx-populate

Excel XLSX parser/generator written in JavaScript with Node.js and browser support, jQuery/d3-style method chaining, encryption, and a focus on keeping existing workbook features and styles in tact.
MIT License
956 stars 183 forks source link

Is there Autofit feature? #293

Open guhyeon opened 3 years ago

guhyeon commented 3 years ago

Hello, thank you for your efforts. There was an issue resizing column widths for content. Excel has a feature called AutoFit. Does xlsx-populate have that feature? If not, do you have any plans for it?

antofa commented 3 years ago

@guhyeon I use below function for autowidth.

function setAutoWidth(workbook) {
    workbook.sheets().map(sheet => {
        const rows = sheet.usedRange().value(),
            widths = rows.reduce(
                (memo, row) => row.map((cell, index) => Math.max(cell?.length ?? 0, memo[index])),
                Array(rows[0].length).fill(0)
            );

        widths.map((width, index) => sheet.column(index + 1).width(width + 2)); // 2 extra points
    });
}
papb commented 3 years ago

Hello, you might be interested in @papb/json-excel, that has this feature.