Rails Axlsx 渲染条件行背景色

Rails Axlsx render conditional row background color

我正在使用 axlsx gem 在 Rails 上的 Ruby 中生成 Excel 张纸。

   wb = xlsx_package.workbook

   wb.styles do |s|
       title = s.add_style :b => true, :sz => 10,
           :border => { :style => :thin, :color => "00" },
           :alignment => {
              :horizontal => :center,
              :vertical => :center
           }
       row = s.add_style :b => false,
             :sz => 10,
             :border => { :style => :thin, :color => "00" },
             :alignment => {
                :horizontal => :left,
                :vertical => :center
             }


       wb.add_worksheet(name: "Customer") do |sheet|
       sheet.add_row ['Customer Name', 'Status'] :style => title
       @customers.each do |customer|
          sheet.add_row [customer.name, customer.status] :style => row
       end
   end

如果客户状态为 = "Late Payment"

,我如何有条件地更改行背景颜色

我还没有测试过,但这应该可以。

wb = xlsx_package.workbook

wb.styles do |s|
   title = s.add_style :b => true, :sz => 10,
           :border => { :style => :thin, :color => "00" },
           :alignment => {
             :horizontal => :center,
             :vertical => :center
           }
   row = s.add_style :b => false,
         :sz => 10,
         :border => { :style => :thin, :color => "00" },
         :alignment => {
            :horizontal => :left,
            :vertical => :center
         }

   red_cell_row = s.add_style :b => false,
                  :sz => 10,
                  :border => { :style => :thin, :color => "00" },
                  :alignment => {
                    :horizontal => :left,
                    :vertical => :center
                  },
                  :bg_color => "FF0000", 
                  :fg_color => "000000"

   wb.add_worksheet(name: "Customer") do |sheet|
   sheet.add_row ['Customer Name', 'Status'] :style => title
   @customers.each do |customer|
      if customer.status == "Late Payment"
        sheet.add_row [customer.name, customer.status] :style => red_cell_row
      else
        sheet.add_row [customer.name, customer.status] :style => row
      end
   end
end