axlsx_rails gem:列宽问题

时间:2016-07-14 07:03:19

标签: ruby-on-rails ruby axlsx

我正在使用axlsx_rails gem来编写.xlsx文件。我在电子表格中有多个基于表单字段的列。它可以变化。

我想根据可用数据为所有列设置列宽。

我用过:

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.

是否可以将数组传递给column_widths方法或将col_widths数组值转换为任何其他方式,以便我们可以将其传递给column_widths方法?

谢谢。

1 个答案:

答案 0 :(得分:3)

The method获取列宽的列表,而不是数组

您可以使用splat operator将数组转换为列表。只需添加*字符:

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