将自定义颜色与Spreadsheet gem一起使用

pot*_*hin 9 ruby excel ruby-on-rails ruby-on-rails-4 spreadsheet-gem

我需要使用自定义colorpattern_fg_color(HEX:0x00adb1,RGB:0,173,177).我从这里得到了建议,但它对我来说没有用(我在另一个基于Spreadsheet gem的库中使用它):

Spreadsheet::Excel::Internals::SEDOC_ROLOC.update(enterprise: 0x00adb1)
Spreadsheet::Column.singleton_class::COLORS << :enterprise
Run Code Online (Sandbox Code Playgroud)

测试示例:

Spreadsheet::Format.new(pattern_fg_color: :enterprise)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

未知的颜色'企业'

任何建议将非常感激.

pot*_*hin 6

似乎将现有颜色映射到另一个hex/rgb代码比添加新颜色要容易得多,因此我的解决方案意味着:xls_color_41更改了内置颜色.

第二种方法

实际上,我使用gem的原生方法在没有猴子修补的情况下获得了相同的结果Spreadsheet::Workbook#set_custom_color:

document = Spreadsheet::Workbook.new
document.set_custom_color(41, 0x00, 0xad, 0xb1) 
Run Code Online (Sandbox Code Playgroud)

第一种方法:

我已经结束了与猴子补丁Spreadsheet::Excel::Writer::Workbook类:不是,是由返回默认的Excel '97调色板default_palette的方法,我已经定义了一个方法,即改变了返回调色板:xls_color_41[0x33, 0xcc, 0xcc][0x00, 0xad, 0xb1].结果如下:

module Spreadsheet
  module Excel
    module Writer
      class Workbook  < Spreadsheet::Writer

        alias_method :excel_palette, :default_palette

        def palette_modifier
          {
            41 => [0x00, 0xad, 0xb1]
          }
        end

       def default_palette
         excel_palette.map.with_index{|rgb, code| [code, rgb]}.to_h.update( palette_modifier ).values
       end

      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)