Class: OOXML::Excel::Styles
- Inherits:
-
Object
- Object
- OOXML::Excel::Styles
- Defined in:
- lib/ooxml_excel/styles.rb,
lib/ooxml_excel/styles.rb,
lib/ooxml_excel/styles.rb,
lib/ooxml_excel/styles.rb,
lib/ooxml_excel/styles.rb
Defined Under Namespace
Classes: CellStyleXfs, Fill, Font, NumFmt
Instance Attribute Summary collapse
-
#cell_style_xfs ⇒ Object
Returns the value of attribute cell_style_xfs.
-
#fills ⇒ Object
Returns the value of attribute fills.
-
#fonts ⇒ Object
Returns the value of attribute fonts.
-
#number_formats ⇒ Object
Returns the value of attribute number_formats.
Class Method Summary collapse
Instance Method Summary collapse
- #by_id(id) ⇒ Object
- #fills_by_index(fill_index) ⇒ Object
- #fonts_by_index(font_index) ⇒ Object
-
#initialize(**attrs) ⇒ Styles
constructor
A new instance of Styles.
- #number_formats_by_index(number_format_index) ⇒ Object
Constructor Details
#initialize(**attrs) ⇒ Styles
Returns a new instance of Styles.
5 6 7 |
# File 'lib/ooxml_excel/styles.rb', line 5 def initialize(**attrs) attrs.each { |property, value| send("#{property}=", value)} end |
Instance Attribute Details
#cell_style_xfs ⇒ Object
Returns the value of attribute cell_style_xfs.
4 5 6 |
# File 'lib/ooxml_excel/styles.rb', line 4 def cell_style_xfs @cell_style_xfs end |
#fills ⇒ Object
Returns the value of attribute fills.
4 5 6 |
# File 'lib/ooxml_excel/styles.rb', line 4 def fills @fills end |
#fonts ⇒ Object
Returns the value of attribute fonts.
4 5 6 |
# File 'lib/ooxml_excel/styles.rb', line 4 def fonts @fonts end |
#number_formats ⇒ Object
Returns the value of attribute number_formats.
4 5 6 |
# File 'lib/ooxml_excel/styles.rb', line 4 def number_formats @number_formats end |
Class Method Details
.load_from_stream(xml_stream) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ooxml_excel/styles.rb', line 30 def self.load_from_stream(xml_stream) style_doc = Nokogiri.XML(xml_stream).remove_namespaces! fonts = style_doc.xpath('//fonts/font') fills = style_doc.xpath('//fills/fill') number_formats = style_doc.xpath('//numFmts/numFmt') # This element contains the master formatting records (xf) which # define the formatting applied to cells in this workbook. # link: https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cellformats(v=office.14).aspx cell_style_xfs = style_doc.xpath('//cellXfs/xf') self.new( fonts: fonts.map { |font_node| Excel::Styles::Font.load_from_node(font_node)}, fills: fills.map { |fill_node| Excel::Styles::Fill.load_from_node(fill_node)}, number_formats: number_formats.map { |num_fmt_node| Excel::Styles::NumFmt.load_from_node(num_fmt_node) }, cell_style_xfs: cell_style_xfs.map { |cell_style_xfs_node| Excel::Styles::CellStyleXfs.load_from_node(cell_style_xfs_node)} ) end |
Instance Method Details
#by_id(id) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/ooxml_excel/styles.rb', line 9 def by_id(id) cell_style = cell_style_xfs.fetch(id) { font: fonts_by_index(cell_style.font_id), fill: fills_by_index(cell_style.fill_id), number_format: number_formats_by_index(cell_style.number_formatting_id), } end |
#fills_by_index(fill_index) ⇒ Object
22 23 24 |
# File 'lib/ooxml_excel/styles.rb', line 22 def fills_by_index(fill_index) @fills[fill_index] end |
#fonts_by_index(font_index) ⇒ Object
18 19 20 |
# File 'lib/ooxml_excel/styles.rb', line 18 def fonts_by_index(font_index) @fonts[font_index] end |
#number_formats_by_index(number_format_index) ⇒ Object
26 27 28 |
# File 'lib/ooxml_excel/styles.rb', line 26 def number_formats_by_index(number_format_index) @number_formats.find { |number_format| number_format.id == number_format_index.to_s}.try(:code) end |