exceljs / exceljs

Excel Workbook Manager

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add style to column causes it to be hidden

AJamesPhillips opened this issue · comments

Version: 0.8.0

const Excel = require('exceljs')

const workbook = new Excel.stream.xlsx.WorkbookWriter({
    filename: 'demo_hidden_columns_bug.xlsx', useStyles: true
})

const worksheet = workbook.addWorksheet('sheetName')
worksheet.columns = [
    { header: 'Header One' },
    { header: 'Header Two', style: { numFmt: '#,##0' } },
    { header: 'Header Three', style: { numFmt: 'General' }, hidden: false },
    { header: 'Header Four', width: 20 }
]
const dataRow = [ 1, 2, 3, 4 ]
worksheet.addRow(dataRow).commit()
worksheet.commit()

workbook.commit()

console.log('completed')

Columns 1 and 4 will be visible but 2 and 3 will be hidden.

No the "fix" I submitted doesn't actually work. If width is set to undefined but the style attribute is still present this still results in the column being hidden when the file is exported. This would make sense being that the function I modified was parseOpen and not the function to export the file.

@guyonroche sorry I don't think I have permission to reopen this issue. I can't figure out what attribute in the xml is causing the column to be hidden (the hidden column attribute is set to false, and width is undefined).

commented

@AJamesPhillips, I tried:
sheet.getColumn('D').numFmt = '# ##0.00';
And column became hidden. Do you know how to fix that?

I can't remember now @qo4on . Looks like I set the width too as I can see in the code I did something like this:

const worksheet = workbook.addWorksheet(`some worksheet name`)
const columnDefinitions = [
  { header: 'field 1' },
  { header: 'field 2', style: { numFmt: '#,##0.00' }, width: 30 },
]
worksheet.columns = columnDefinitions

Does that fix it for you?

commented

Thank you @AJamesPhillips, but I don't know how adapt your code for setting width of each individual column.

Fell on exactly the same issue. I can confirm @AJamesPhillips 's solution. When altering the style, you have to explicitly set the width attribute or else the column will be hidden. This problem affects ONLY microsoft office and not LibreOffice, so i assume that each application interprets "undefined" differently when a style is present.

In files row-xform.js(80) and col-xform.js(52), I have added extra if clause on the code. It works for me.
if (node.attributes.hidden != 'false') { model.hidden = true; }
It seems that the library set the model if there are styles to
{... hidden: 'false' }
thus causing the hidden row and cols issue.
I'm new to this. So, I'm not sure what factors that I'm not considering with adding this to the part of the code. I also don't know how to do a git pull request. Do let me know my current hack is a good or bad solution. Appreciate it, Thanks.

@MohdVara that's great that you find a solution :)

If could I ask you to create pull request with this fixation? 👍