简体   繁体   English

axlsx_rails gem:列宽问题

[英]axlsx_rails gem: Columns width issue

I'm using axlsx_rails gem to write in .xlsx file. 我正在使用axlsx_rails gem在.xlsx文件中编写。 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? 是否可以将数组传递给column_widths方法或任何其他将col_widths数组值转换为的方法,以便我们可以将其传递给column_widths方法?

Thank you. 谢谢。

The method takes a list of column widths, not an array . 该方法采用列宽列表 ,而不是array

You can use the splat operator to convert your array into a list. 您可以使用splat运算符将数组转换为列表。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM