Class: OOXML::Excel::Styles

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_xfsObject

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

#fillsObject

Returns the value of attribute fills.



4
5
6
# File 'lib/ooxml_excel/styles.rb', line 4

def fills
  @fills
end

#fontsObject

Returns the value of attribute fonts.



4
5
6
# File 'lib/ooxml_excel/styles.rb', line 4

def fonts
  @fonts
end

#number_formatsObject

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