The example provided by exceljs from @dim answer does not really work as described.
Not only is the example use-case a really bad example of what a real world use-case for this might look like, it simply does not work. What most people want in this situation is to have a table header as a merged and centered cell above the row of column labels where data may then be added by key. I have tried a lot of things to get the sheet.columns
functionality to leave the first row of data alone but it always adds the column headers over whatever else I put there.
I have tried:
adding title values before defining sheet.columns
(as in the example)
adding the column headers as a raw list before defining sheet.columns
(as in the example)
offsetting the merged title cell from the columns used in the table (as in the example) (why would anyone do this anyway?)
adding rows between title and column headers (as in the example) (again why?)
merging more than one row into title cell (as in the example)
None of this gives me anything other than the final column key in my title cell (using Microsoft Excel). It seems that the timing of defining sheet.columns
has no bearing on the scope of the table being defined. I'm sure adding a title cell that falls outside of the columns of the actual table would work, but, as I stated, this is not very useful for most of the standard use-cases.
The workarounds I have found are these:
- make the last column your sheet.columns a use the table title as it's key, do not include anything for it in the headers row, and ensure no table data comes in using that key (the downside being that the title is merged with an extra column outside of the table data, causing it to look a little funny and off center)
- make your title cell on the second row, your headers on the third, and set
sheet.getRow(1).hidden = true
to hide the headers that inevitably appear on row one (the downside being your sheet starts on row two, for no obvious reason)
I started down the path of the first workaround, bookending empty columns on either end of the table to at least balance the title cell out, but it ended up looking too strange so I settled on using the second workaround.