autofit excel column width using spreadsheet gem

ruby-on-rails

Solution

Not that I know of. I've had to do it manually by keeping track of the lengths of the strings in each column and then taking the max length and adding a couple more units to it, then setting the width of the column to that calculated value.

account_name_lengths = []
# generate each row
accounts.each do |account|
  account_name_lengths << account.name.length

  # add to sheet here
end

sheet.column(0).width = account_name_lengths.max + 5

Problem

I am using the Ruby Spreadsheet gem to export data from a rails app to excel, is there a way to get the size of a columns adjust to the size of the content automatically?

Original source