ExcelJS wrapText in Table columns

0 votes

Is there a way to get wrapText working for columns in a table? I'm trying the following and it's not registering in excel.

const columnsStyle = { font: { name: 'Arial Black', size: 12 } };
const columns = [
            { name: 'PO#', style: columnsStyle, alignment: { wrapText:true } }
];
 ws.addTable({
                name: 'name',
                ref: `A${lastRowNum}`,
                columns: columns,
                rows: rows,
            });

I've also attempted to include the word "alignment" in the "style" object, however, doing so causes an error when opened in Excel. (Excel claims there was a creation issue because the table looks different and lacks wrap text.) 

const columnsStyle = { font: { name: 'Arial Black', size: 12 }, alignment: { wrapText:true } }; const columns = [ { name: 'PO#', style: columnsStyle, } ];

I also want to get all cells in these columns of the table wrapped too. Does anybody have any idea on how to do this? I looked through the documentation several times and couldn't find anything concrete.

Note: I know I can do ws.getCell(ref).alignment = {wrapText:true} and so I tried looking at the table object to see if I could get a reference of all the cells in it, loop through them, and set the alignment. But I was not able to get cells in the table.

Sep 29, 2022 in Others by Kithuzzz
• 38,010 points
2,433 views

1 answer to this question.

0 votes

I implemented a workaround by detecting what rows the table was contained in and then looping through every row, and then every row's cell. Then finally setting the wrap text on every cell.

//Record where the table starts
const firstTableRowNum = SOME_NUM;

//Create the table
ws.addTable({
    name: 'someTable',
    ref: `A${firstTableRowNum}`,
    columns: columns,
    rows: rows,
});

//Record table's last row
let lastRowNum = ws.lastRow.number;
const lastTableRowNum = lastRowNum;

//Loop through all table's row
for (let i=firstTableRowNum; i<= lastTableRowNum; i++) {
    const row = ws.getRow(i);

    //Now loop through every row's cell and finally set alignment
    row.eachCell({includeEmpty: true}, (cell => {
        cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true };
    }));
}
answered Sep 30, 2022 by narikkadan
• 63,180 points

Related Questions In Others

0 votes
1 answer

Want to compare two columns in excel

Hello To compare two columns in excel ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,220 points
732 views
0 votes
0 answers

Convert Rows to Columns with values in Excel using custom format

1 I having a Excel sheet with 1 ...READ MORE

Feb 17, 2022 in Others by Edureka
• 13,670 points
569 views
0 votes
1 answer
0 votes
1 answer

Convert Rows to columns using 'Pivot' in SQL Server

If you are using SQL Server 2005+, ...READ MORE

answered Jun 20, 2022 in Others by nisha
• 2,210 points
3,645 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,533 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,141 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,700 points
3,867 views
0 votes
1 answer
0 votes
1 answer

Subtotal in result row returns 0 in excel table

Syntax SUBTOTAL(function_num,ref1,[ref2],...) The SUBTOTAL function syntax has the following ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 63,180 points
849 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP