Class: StyleCollection

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

Constant Summary collapse

FIRST_USER_DEFINED_NUM_FORMAT_INDEX =
164
STANDARD_NUMBER_FORMATS =
[
  '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',
  '@'   
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyleCollection

Returns a new instance of StyleCollection.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/surpass/style.rb', line 78

def initialize
  # Populate default font list.
  @fonts = {}
  # Initialize blank fonts into slots 0,1,2,3,5 in order to skip slot 4.
  [0,1,2,3,5].each do |i|
    @fonts[i] = Font.new
  end
  
  # Populate default number format list.
  @number_formats = {}
  STANDARD_NUMBER_FORMATS.each_with_index do |s, i|
    index = (i <= 23) ? i : i + 14
    @number_formats[index] = s
  end
  
  @styles = {}
  @default_style = StyleFormat.new
  
  # Store the 6 parameters of the default_style
  @default_format = add_style(@default_style)[0]
end

Instance Attribute Details

#default_formatObject

Returns the value of attribute default_format.



35
36
37
# File 'lib/surpass/style.rb', line 35

def default_format
  @default_format
end

#default_styleObject

Returns the value of attribute default_style.



34
35
36
# File 'lib/surpass/style.rb', line 34

def default_style
  @default_style
end

#fontsObject

Returns the value of attribute fonts.



31
32
33
# File 'lib/surpass/style.rb', line 31

def fonts
  @fonts
end

#number_formatsObject

Returns the value of attribute number_formats.



32
33
34
# File 'lib/surpass/style.rb', line 32

def number_formats
  @number_formats
end

#stylesObject

Returns the value of attribute styles.



33
34
35
# File 'lib/surpass/style.rb', line 33

def styles
  @styles
end

Instance Method Details

#add(style) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/surpass/style.rb', line 114

def add(style)
  if style.nil?
    0x10 # Return the index of the default style.
  else
    # TODO find way to freeze style so if someone modifies a StyleFormat instance it won't affect previously formatted cells.
    add_style(style)[1] # Return the index of the style just stored.
  end
end

#cell_styles_biffObject



184
185
186
187
188
189
190
191
192
193
# File 'lib/surpass/style.rb', line 184

def cell_styles_biff
  result = ''
  0.upto(15) do |i|
    result += XFRecord.new(@default_format, 'style').to_biff
  end
  @styles.sort.each do |i, f|
    result += XFRecord.new(f).to_biff
  end
  result
end

#default_date_styleObject



101
102
103
# File 'lib/surpass/style.rb', line 101

def default_date_style
  @default_date_style ||= StyleFormat.new(:number_format_string => 'dd-mmm-yyyy')
end

#default_datetime_styleObject



105
106
107
# File 'lib/surpass/style.rb', line 105

def default_datetime_style
  @default_datetime_style ||= StyleFormat.new(:number_format_string => 'dd-mmm-yyyy hh:mm:ss')
end

#default_float_styleObject



109
110
111
# File 'lib/surpass/style.rb', line 109

def default_float_style
  @default_float_style ||= StyleFormat.new(:number_format_string => '#,##0.00')
end

#font_index(font) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/surpass/style.rb', line 133

def font_index(font)
  index = @fonts.index(font)
  if index.nil?
    index = @fonts.length + 1
    @fonts[index] = font
  end
  index
end

#fonts_biffObject

TODO use inject here?



167
168
169
170
171
172
173
# File 'lib/surpass/style.rb', line 167

def fonts_biff
  result = ''
  @fonts.sort.each do |i, f|
    result += f.to_biff
  end
  result
end

#format_index(format) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/surpass/style.rb', line 142

def format_index(format)
  index = @styles.index(format)
  if index.nil?
    index = 0x10 + @styles.length
    @styles[index] = format
  end
  index
end

#number_format_index(number_format_string) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/surpass/style.rb', line 123

def number_format_index(number_format_string)
  index = @number_formats.index(number_format_string)
  if index.nil?
    # TODO implement regex to check if valid string
    index = FIRST_USER_DEFINED_NUM_FORMAT_INDEX + @number_formats.length - STANDARD_NUMBER_FORMATS.length
    @number_formats[index] = number_format_string
  end
  index
end

#number_formats_biffObject



175
176
177
178
179
180
181
182
# File 'lib/surpass/style.rb', line 175

def number_formats_biff
  result = ''
  @number_formats.sort.each do |i, f|
    next if i < FIRST_USER_DEFINED_NUM_FORMAT_INDEX
    result += NumberFormatRecord.new(i, f).to_biff
  end
  result
end

#to_biffObject



162
163
164
# File 'lib/surpass/style.rb', line 162

def to_biff
  fonts_biff + number_formats_biff + cell_styles_biff + StyleRecord.new.to_biff
end