可以将字体颜色应用于单个单元格,而无需创建样式:
Axlsx::Package.new do |p|
p.workbook.add_worksheet(:name => "test") do |ws|
ws.add_row ["a", "b", "c"]
ws.add_row ["d", "e", "f"]
ws.add_row ["g", "h", "i"]
ws.rows.each do |r|
r.cells.each do |c|
c.color = "009900"
if ['a', 'e', 'i'].include?(c.value)
c.color = "009900" // how can I do the same for background color?
end
end
end
end
p.serialize('test.xlsx')
end
可以将样式应用于单个单元格:
Axlsx::Package.new do |p|
p.workbook.add_worksheet(:name => "test") do |ws|
style1 = ws.styles.add_style( :bg_color => "FFFFFF00")
ws.add_row ["a", "b", "c"]
ws.add_row ["d", "e", "f"]
ws.add_row ["g", "h", "i"]
ws.rows.each do |r|
r.cells.each do |c|
if ['a', 'e', 'i'].include?(c.value)
c.style = style1
end
end
end
end
p.serialize('test.xlsx')
end
但在我的情况下,这是不切实际的,因为我想让单元格背景成为单元格值的函数,并且希望避免构建数百或数千种样式。
有什么建议么?