Ruby on rails 将自定义颜色用于电子表格gem
我需要使用自定义的Ruby on rails 将自定义颜色用于电子表格gem,ruby-on-rails,ruby,excel,ruby-on-rails-4,spreadsheet-gem,Ruby On Rails,Ruby,Excel,Ruby On Rails 4,Spreadsheet Gem,我需要使用自定义的color和pattern\u fg\u color(十六进制:0x00adb1,RGB:0173177)。我遵循了来自的建议,但没有成功(我正在另一个基于电子表格gem的库中使用它): 我得到以下错误: 未知颜色“企业” 任何建议都将不胜感激。似乎将现有颜色映射到另一个十六进制/rgb代码要比添加新代码容易得多,因此我的解决方案意味着内置的:xls\u color\u 41已更改 第二种方法 实际上,我使用gem的原生方法电子表格::工作簿#set_custom_color,
color
和pattern\u fg\u color
(十六进制:0x00adb1,RGB:0173177)。我遵循了来自的建议,但没有成功(我正在另一个基于电子表格gem的库中使用它):
我得到以下错误:
未知颜色“企业”
任何建议都将不胜感激。似乎将现有颜色映射到另一个十六进制/rgb代码要比添加新代码容易得多,因此我的解决方案意味着内置的
:xls\u color\u 41
已更改
第二种方法
实际上,我使用gem的原生方法电子表格::工作簿#set_custom_color
,在不使用猴子补丁的情况下也获得了相同的结果:
document = Spreadsheet::Workbook.new
document.set_custom_color(41, 0x00, 0xad, 0xb1)
第一种方法:
我最终使用了monkey patching电子表格::Excel::Writer::Workbook
类:我定义了一个方法,将默认调色板
返回的调色板从[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
模块电子表格
模块Excel
模块编写器
类工作簿<电子表格::编写器
别名\u方法:excel\u调色板,:默认\u调色板
def调色板_修改器
{
41=>[0x00,0xad,0xb1]
}
结束
def默认调色板
excel|palette.map.with_index{rgb,code{code,rgb]}.to_h.update(palette_修饰符)。值
结束
结束
结束
结束
结束
document = Spreadsheet::Workbook.new
document.set_custom_color(41, 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