Autofit excel column width using spreadsheet

I use the Ruby Spreadsheet pearl to export data from a rails application to excel, is there a way to get a column size that automatically adjusts to the size of the content?

+4
source share
1 answer

Not that I knew. I had to do this manually, tracking the lengths of the rows in each column, and then taking the maximum length and adding a couple more units to it, and then setting the column width to this 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 
+7
source

Source: https://habr.com/ru/post/1496839/


All Articles