Class: Excel::StyleCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/ru_excel/style.rb

Constant Summary collapse

Std_num_fmt_list =
[
        'general',
        '0',
        '0.00',
        '#,##0',
        '#,##0.00',
        '"$"#,##0_);("$"#,##',
        '"$"#,##0_);[Red]("$"#,##',
        '"$"#,##0.00_);("$"#,##',
        '"$"#,##0.00_);[Red]("$"#,##',
        '0%',
        '0.00%',
        '0.00E+00',
        '# ?/?',
        '# ??/??',
        'M/D/YY',
        'D-MMM-YY',
        'D-MMM',
        'MMM-YY',
        'h:mm AM/PM',
        'h:mm:ss AM/PM',
        'h:mm',
        'h:mm:ss',
        'M/D/YY h:mm',
        '_(#,##0_);(#,##0)',
        '_(#,##0_);[Red](#,##0)',
        '_(#,##0.00_);(#,##0.00)',
        '_(#,##0.00_);[Red](#,##0.00)',
        '_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)',
        '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)',
        '_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)',
        '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)',
        'mm:ss',
        '[h]:mm:ss',
        'mm:ss.0',
        '##0.0E+0',
        '@'   
]
NumFormats =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyleCollection

Returns a new instance of StyleCollection.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ru_excel/style.rb', line 74

def initialize
    @_fonts = {}
    @_fonts[Font.new()] = 0
    @_fonts[Font.new()] = 1
    @_fonts[Font.new()] = 2
    @_fonts[Font.new()] = 3
    # The font with index 4 is omitted in all BIFF versions
    @_fonts[Font.new()] = 5
    @_num_formats = NumFormats.dup

    @_xf = {}
    @default_style = XFStyle.new()
    @_default_xf = _add_style(@default_style)[0]
end

Instance Attribute Details

#default_styleObject

Returns the value of attribute default_style.



88
89
90
# File 'lib/ru_excel/style.rb', line 88

def default_style
  @default_style
end

Instance Method Details

#_add_style(style) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/ru_excel/style.rb', line 94

def _add_style(style)
    num_format_str = style.num_format_str
    num_format_idx = @_num_formats[num_format_str] ||=
        164 + @_num_formats.length - Std_num_fmt_list.length
    font = style.font
    font_idx = @_fonts[font] ||= @_fonts.length + 1
    xf = [font_idx, num_format_idx, style.alignment, style.borders, style.pattern, style.protection]
    xf_index = @_xf[xf] ||= 0x10 + @_xf.length
    [xf, xf_index]
end

#_all_cell_stylesObject



126
127
128
129
# File 'lib/ru_excel/style.rb', line 126

def _all_cell_styles
    result = BiffRecord.xfRecord(@_default_xf, 'style') * 16
    result << @_xf.map{|k,v| [v,k]}.sort!.collect!{|_,xf| BiffRecord.xfRecord(xf)}.join('')
end

#_all_fontsObject



114
115
116
117
118
# File 'lib/ru_excel/style.rb', line 114

def _all_fonts
    fonts = @_fonts.map{|k,v| [v,k]}.sort!
    fonts.collect!{|_,font| font.get_biff_record}
    fonts.join('')
end

#_all_num_formatsObject



120
121
122
123
124
# File 'lib/ru_excel/style.rb', line 120

def _all_num_formats
    formats = @_num_formats.to_a.select{|k, v| v>163}
    formats = formats.map{|fmtstr, fmtidx| BiffRecord.numberFormatRecord(fmtidx, fmtstr)}
    formats.join('')
end

#_all_stylesObject



131
132
133
# File 'lib/ru_excel/style.rb', line 131

def _all_styles
    BiffRecord.styleRecord()
end

#add(style) ⇒ Object



90
91
92
# File 'lib/ru_excel/style.rb', line 90

def add(style)
    style == nil ? 0x10 : _add_style(style)[1]
end

#get_biff_dataObject



105
106
107
108
109
110
111
112
# File 'lib/ru_excel/style.rb', line 105

def get_biff_data
    result = ''
    result << _all_fonts()
    result << _all_num_formats()
    result << _all_cell_styles()
    result << _all_styles()
    result
end