我正在使用 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采用列宽的列表,而不是数组。
您可以使用 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
关于ruby-on-rails - axlsx_rails gem : Columns width issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38367569/