axlsx_rails gem: 列宽问题
axlsx_rails gem: Columns width issue
我正在使用 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 方法?
谢谢。
The method 采用 list 列宽,而不是 array.
您可以使用 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
我正在使用 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 方法?
谢谢。
The method 采用 list 列宽,而不是 array.
您可以使用 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