如何在电子表格中使用自定义颜色?
我需要使用自定义color
和pattern_fg_color( HEX :0x00adb1, RGB :0,173,177)。 我从这里得到的build议,但它没有为我工作(我在另一个基于电子表格gem的库中使用它):
Spreadsheet::Excel::Internals::SEDOC_ROLOC.update(enterprise: 0x00adb1) Spreadsheet::Column.singleton_class::COLORS << :enterprise
testing例子:
Spreadsheet::Format.new(pattern_fg_color: :enterprise)
我得到以下错误:
未知颜色“企业”
任何build议,将不胜感激。
看来现有的颜色映射到另一个hex/ rgb代码比添加一个新的更容易,所以我的解决scheme意味着一个内置的:xls_color_41
被改变。
第二种方法
其实,我没有猴子修补,使用gem的本地方法Spreadsheet::Workbook#set_custom_color
:
document = Spreadsheet::Workbook.new document.set_custom_color(41, 0x00, 0xad, 0xb1)
第一种方法:
我已经结束了与猴子修补Spreadsheet::Excel::Writer::Workbook
类:而不是默认的Excel 97调色板,这是由default_palette
方法返回,我已经定义了一个方法,改变返callback色板为:xls_color_41
从[0x33, 0xcc, 0xcc]
[0x00, 0xad, 0xb1]
[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