我正在尝试格式化我的xlsx文件,但我遇到了一些问题。例如,我需要将列添加到现有行。例如,这是我的代码:
wb.add_worksheet(name: "Sums") do |sheet|
sheet.add_row ["1.", "Rodiklis"], :style=>[title]
sheet.add_row ["1.1", "Rekomendacijų vertė"]
@departaments.each do |departament|
sheet.add_row ["", departament.name]
end
@all_items.each do |summary|
tyfcb = 0
rgi = 0
rgo = 0
rgirgo = 0
total = 0
rgirgo_per_user = 0
meeting_1_2_1 = 0
meeting_1_2_1_per_user = 0
ceu = 0
ceu_per_user = 0
v = 0
v_per_user = 0
summary.departament.contacts.each do |c|
items = c.items.where(summary_id: summary)
tyfcb += c.item_x(items, 'tyfcb')
rgi += c.item_x(items, 'rgi')
rgo += c.item_x(items, 'rgo')
meeting_1_2_1 += c.item_x(items, '1_2_1')
total += 1
ceu += c.item_x(items, 'ceu')
v += c.item_x(items, 'v')
end
rgirgo = rgi + rgo
rgirgo_per_user = rgirgo.to_f / total.to_f
meeting_1_2_1_per_user = meeting_1_2_1.to_f / total.to_f
ceu_per_user = ceu.to_f / total.to_f
v_per_user = v.to_f / total.to_f
sheet.add_row [summary.departament.name,summary.id, tyfcb]
sheet.add_row [summary.departament.name,summary.id, rgirgo]
sheet.add_row [summary.departament.name,summary.id, sprintf('%.2f', rgirgo_per_user)]
sheet.add_row [summary.departament.name,summary.id, meeting_1_2_1]
sheet.add_row [summary.departament.name,summary.id, sprintf('%.2f', meeting_1_2_1_per_user)]
sheet.add_row [summary.departament.name,summary.id, ceu]
sheet.add_row [summary.departament.name,summary.id, sprintf('%.2f', ceu_per_user)]
sheet.add_row [summary.departament.name, summary.id, v]
sheet.add_row [summary.departament.name,summary.id, sprintf('%.2f', v_per_user)]
end
end
因此从代码中我们可以看到我生成了大量的tyfcb,ceu等变量。所有这些都以新行打印。我想在一行中打印所有tyfcb变量,在一行中打印所有ceu变量等等。我应该如何处理axlsx gem?谢谢你的回答。
答案 0 :(得分:2)
使用add_cell
add_cell(value = '', options = {}) ⇒ Cell
根据提供的数据向行添加单个单元格,并更新工作表的自动调整数据。