Ruby Axlsx true auto-width for columns?

2.4k views Asked by At

I'm using Axlsx to create an Excel file with one worksheet. I cannot figure out how to get all of the column widths to automatically resize to fit all of the data. Consider the following code where I add rows to a worksheet ws:

  ws.add_row ["this is just a test to see how far it will go because column will not show", "test", "this is also a testtt"]
  ws.add_row [""]

By default, columns seem to use auto-width, but even if not, I tried adding the :widths=>[:auto, :auto, :auto] to my add_row code. It seems like some kind of auto fit is applied to my columns because that first long column is expanding to fit most of the cell. But it does not fit entire cell, just expands enough to fit most but leaves off the last word or two.

Any way to achieve a true autofit functionality?

1

There are 1 answers

0
siruku6 On

How about the next cord ?

wb = Axlsx::Package.new.workbook
wb.use_autowidth = true
wb.add_worksheet(name: 'sheet_name') do |sheet|
  ws.add_row ["this is just a test to see how far it will go because column will not show", "test", "this is also a testtt"]
end