I'm using axlsx_rails gem to write in .xlsx file. I have multiple columns in spreadsheet based on form fields. It can vary.
I would like to set the column width for all the columns based on data available.
I have used:
col_widths= [10,20,30,40,50]
p = Axlsx::Package.new
p.use_autowidth = true
wb = p.workbook
wb.add_worksheet(:name => 'try') do |sheet|
sheet.add_row ["hi","hello","how","are","you"]
sheet.column_widths col_widths ##this column widths method doesn't take an array.
Is is possible to pass an array to column_widths method or any other ways to convert col_widths array values to so that we can pass it to column_widths method?
Thank you.
The method takes a list of column widths, not an array.
You can use the splat operator to convert your array into a list. Simply add a *
character:
col_widths= [10,20,30,40,50]
p = Axlsx::Package.new
p.use_autowidth = true
wb = p.workbook
wb.add_worksheet(:name => 'try') do |sheet|
sheet.add_row ["hi","hello","how","are","you"]
sheet.column_widths *col_widths ## Here I have used the splat operator
end