I need to use custom color
and pattern_fg_color
(HEX: 0x00adb1, RGB: 0,173,177). I was following advice from here, but it didn't work out for me (I am using it in another library based on the Spreadsheet gem):
Spreadsheet::Excel::Internals::SEDOC_ROLOC.update(enterprise: 0x00adb1)
Spreadsheet::Column.singleton_class::COLORS << :enterprise
Test example:
Spreadsheet::Format.new(pattern_fg_color: :enterprise)
And I get the following error:
unknown color 'enterprise'
Any suggestions would be appreciated greatly.
It seems that mapping an existing color to another hex/rgb code is much easier than adding a new one, so my solution implies that a built-in :xls_color_41
is changed.
Actually, I've achieved the same result without monkey patching, using gem's native method Spreadsheet::Workbook#set_custom_color
:
document = Spreadsheet::Workbook.new
document.set_custom_color(41, 0x00, 0xad, 0xb1)
I've ended up with monkey patching Spreadsheet::Excel::Writer::Workbook
class: instead of default Excel '97 palette that was returned by default_palette
method, I've defined a method, that changes returned palette for :xls_color_41
from [0x33, 0xcc, 0xcc]
to [0x00, 0xad, 0xb1]
. The result is as follows:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With