[英]How to add column to existing row with axlsx gem?
I am trying to format my xlsx file, but i faced some problems.我正在尝试格式化我的 xlsx 文件,但我遇到了一些问题。 For example i need to add column to existing row.例如,我需要将列添加到现有行。 For example here is my code:例如这里是我的代码:
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
So from the code we can see that I generate lots of tyfcb, ceu and etc variables.所以从代码中我们可以看到我生成了很多 tyfcb、ceu 等变量。 All of them are printed in new row.所有这些都打印在新行中。 I want to to print all tyfcb variables in one row, all ceu variables in one row and etc. How should I do with axlsx gem?我想在一行中打印所有 tyfcb 变量,在一行中打印所有 ceu 变量等等。我应该如何处理 axlsx gem? Thanks for answers.感谢您的回答。
Use add_cell 使用add_cell
add_cell(value = '', options = {}) ⇒ Cell
This adds a single cell to the row based on the data provided and updates the worksheet's autofit data. 这将根据提供的数据将单个单元格添加到行中,并更新工作表的自动拟合数据。
http://www.rubydoc.info/github/randym/axlsx/Axlsx/Row http://www.rubydoc.info/github/randym/axlsx/Axlsx/Row
Just add a empty "" to the first cell of row or wherever you want a blank column.只需在行的第一个单元格或您想要空白列的任何位置添加一个空的“”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.