繁体   English   中英

Rails Axlsx 渲染条件行背景色

[英]Rails Axlsx render conditional row background color

我正在使用axlsx gem 在 Ruby on Rails 中生成 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

如果客户状态说 =“延迟付款”,我如何有条件地更改行背景颜色

我还没有测试过,但这应该可以完成工作。

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

暂无
暂无

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

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