Class: OpenXml::Xlsx::Parts::Stylesheet

Inherits:
Part
  • Object
show all
Includes:
Elements
Defined in:
lib/openxml/xlsx/parts/stylesheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStylesheet

Returns a new instance of Stylesheet.



9
10
11
12
13
14
15
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 9

def initialize
  @formats = []
  @fonts = [Font.new("Calibri", 12)]
  @fills = [PatternFill.new("none"), PatternFill.new("gray125")]
  @borders = [Border.new]
  @styles = [Style.new]
end

Instance Attribute Details

#bordersObject (readonly)

Returns the value of attribute borders.



7
8
9
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 7

def borders
  @borders
end

#fillsObject (readonly)

Returns the value of attribute fills.



7
8
9
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 7

def fills
  @fills
end

#fontsObject (readonly)

Returns the value of attribute fonts.



7
8
9
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 7

def fonts
  @fonts
end

#formatsObject (readonly)

Returns the value of attribute formats.



7
8
9
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 7

def formats
  @formats
end

#stylesObject (readonly)

Returns the value of attribute styles.



7
8
9
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 7

def styles
  @styles
end

Instance Method Details

#reference_of(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 17

def reference_of(options={})
  case format = options[:format]
  when NumberFormat
    options[:format_id] = Xlsx.index!(formats, format) + NUMBER_FORMAT_START_ID
  when ImpliedNumberFormat
    options[:format_id] = format.id
  end
  options[:font_id] = Xlsx.index!(fonts, options[:font]) if options.key? :font
  options[:fill_id] = Xlsx.index!(fills, options[:fill]) if options.key? :fill
  options[:border_id] = Xlsx.index!(borders, options[:border]) if options.key? :border

  style = Style.new(
    options.fetch(:format_id, 0),
    options.fetch(:font_id, 0),
    options.fetch(:fill_id, 0),
    options.fetch(:border_id, 0),
    options.fetch(:alignment, nil))

  Xlsx.index!(styles, style)
end

#to_xmlObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/openxml/xlsx/parts/stylesheet.rb', line 38

def to_xml
  build_standalone_xml do |xml|
    xml.styleSheet(xmlns: "http://schemas.openxmlformats.org/spreadsheetml/2006/main", "xmlns:mc" => "http://schemas.openxmlformats.org/markup-compatibility/2006") do
      xml.numFmts(count: formats.length) do
        formats.each_with_index do |format, index|
          format.to_xml(index + NUMBER_FORMAT_START_ID, xml)
        end
      end

      xml.fonts(count: fonts.length) do
        fonts.each do |font|
          font.to_xml(xml)
        end
      end

      xml.fills(count: fills.length) do
        fills.each do |fill|
          fill.to_xml(xml)
        end
      end

      xml.borders(count: borders.length) do
        borders.each do |border|
          border.to_xml(xml)
        end
      end

      xml.cellXfs(count: styles.length) do
        styles.each do |style|
          style.to_xml(xml)
        end
      end
    end
  end
end