Class: OOXL::Styles

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxl/xl_objects/styles.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Styles

Returns a new instance of Styles.



8
9
10
# File 'lib/ooxl/xl_objects/styles.rb', line 8

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.



7
8
9
# File 'lib/ooxl/xl_objects/styles.rb', line 7

def cell_style_xfs
  @cell_style_xfs
end

#fillsObject

Returns the value of attribute fills.



7
8
9
# File 'lib/ooxl/xl_objects/styles.rb', line 7

def fills
  @fills
end

#fontsObject

Returns the value of attribute fonts.



7
8
9
# File 'lib/ooxl/xl_objects/styles.rb', line 7

def fonts
  @fonts
end

#number_formatsObject

Returns the value of attribute number_formats.



7
8
9
# File 'lib/ooxl/xl_objects/styles.rb', line 7

def number_formats
  @number_formats
end

Class Method Details

.load_from_stream(xml_stream) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ooxl/xl_objects/styles.rb', line 33

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| Font.load_from_node(font_node)},
    fills: fills.map { |fill_node| Fill.load_from_node(fill_node) if fill_node.to_s.include?('patternFill')},
    number_formats: number_formats.map { |num_fmt_node| NumberFormatting.load_from_node(num_fmt_node) },
    cell_style_xfs: cell_style_xfs.map { |cell_style_xfs_node| CellStyleReference.load_from_node(cell_style_xfs_node)}
  )
end

Instance Method Details

#by_id(id) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ooxl/xl_objects/styles.rb', line 12

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



25
26
27
# File 'lib/ooxl/xl_objects/styles.rb', line 25

def fills_by_index(fill_index)
  @fills[fill_index] unless fill_index.blank?
end

#fonts_by_index(font_index) ⇒ Object



21
22
23
# File 'lib/ooxl/xl_objects/styles.rb', line 21

def fonts_by_index(font_index)
  @fonts[font_index] unless font_index.blank?
end

#number_formats_by_index(number_format_index) ⇒ Object



29
30
31
# File 'lib/ooxl/xl_objects/styles.rb', line 29

def number_formats_by_index(number_format_index)
  @number_formats.find { |number_format| number_format.id == number_format_index.to_s}.try(:code)
end