Class: Axlsx::Styles
- Inherits:
-
Object
- Object
- Axlsx::Styles
- Defined in:
- lib/axlsx/stylesheet/styles.rb
Overview
The recommended way to manage styles is with add_style
The Styles class manages worksheet styles In addition to creating the require style objects for a valid xlsx package, this class provides the key mechanism for adding styles to your workbook, and safely applying them to the cells of your worksheet. All portions of the stylesheet are implemented here exception colors, which specify legacy and modified pallete colors, and exLst, whic is used as a future feature data storage area.
Instance Attribute Summary collapse
-
#borders ⇒ SimpleTypedList
readonly
The collection of borders used in this workbook Axlsx predefines THIN_BORDER which can be used to put a border around all of your cells.
-
#cellStyles ⇒ SimpleTypedList
readonly
The collection of named styles, referencing cellStyleXfs items in the workbook.
-
#cellStyleXfs ⇒ SimpleTypedList
readonly
The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook.
-
#cellXfs ⇒ SimpleTypedList
readonly
The collection of master formatting records.
-
#dxfs ⇒ SimpleTypedList
readonly
The collection of non-cell formatting records used in the worksheet.
-
#fills ⇒ SimpleTypedList
readonly
The collection of fills used in this workbook.
-
#fonts ⇒ SimpleTypedList
readonly
The collection of fonts used in this workbook.
-
#numFmts ⇒ SimpleTypedList
readonly
numFmts for your styles.
-
#tableStyles ⇒ SimpleTypedList
readonly
The collection of table styles that will be available to the user in the excel UI.
Instance Method Summary collapse
-
#add_style(options = {}) ⇒ Integer
Drastically simplifies style creation and management.
-
#initialize ⇒ Styles
constructor
Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
-
#parse_alignment_options(options = {}) ⇒ CellAlignment
parses add_style options for alignment noop if options hash does not include :alignment key.
- #parse_border_options(options = {}) ⇒ Border|Integer
-
#parse_fill_options(options = {}) ⇒ Fill|Integer
parses add_style options for fills.
-
#parse_font_options(options = {}) ⇒ Font|Integer
parses add_style options for fonts.
-
#parse_num_fmt_options(options = {}) ⇒ NumFmt|Integer
Parses Style#add_style options for number formatting.
- #parse_protection_options(options = {}) ⇒ CellProtection
-
#to_xml_string(str = '') ⇒ String
Serializes the object.
Constructor Details
#initialize ⇒ Styles
Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
119 120 121 |
# File 'lib/axlsx/stylesheet/styles.rb', line 119 def initialize() load_default_styles end |
Instance Attribute Details
#borders ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of borders used in this workbook Axlsx predefines THIN_BORDER which can be used to put a border around all of your cells.
86 87 88 |
# File 'lib/axlsx/stylesheet/styles.rb', line 86 def borders @borders end |
#cellStyles ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of named styles, referencing cellStyleXfs items in the workbook.
98 99 100 |
# File 'lib/axlsx/stylesheet/styles.rb', line 98 def cellStyles @cellStyles end |
#cellStyleXfs ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook
92 93 94 |
# File 'lib/axlsx/stylesheet/styles.rb', line 92 def cellStyleXfs @cellStyleXfs end |
#cellXfs ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of master formatting records. This is the list that you will actually use in styling a workbook.
104 105 106 |
# File 'lib/axlsx/stylesheet/styles.rb', line 104 def cellXfs @cellXfs end |
#dxfs ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of non-cell formatting records used in the worksheet.
110 111 112 |
# File 'lib/axlsx/stylesheet/styles.rb', line 110 def dxfs @dxfs end |
#fills ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of fills used in this workbook
79 80 81 |
# File 'lib/axlsx/stylesheet/styles.rb', line 79 def fills @fills end |
#fonts ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of fonts used in this workbook
73 74 75 |
# File 'lib/axlsx/stylesheet/styles.rb', line 73 def fonts @fonts end |
#numFmts ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
numFmts for your styles. The default styles, which change based on the system local, are as follows. id formatCode 0 General 1 0 2 0.00 3 #,##0 4 #,##0.00 9 0% 10 0.00% 11 0.00E+00 12 # ?/? 13 # ??/?? 14 mm-dd-yy 15 d-mmm-yy 16 d-mmm 17 mmm-yy 18 h:mm AM/PM 19 h:mm:ss AM/PM 20 h:mm 21 h:mm:ss 22 m/d/yy h:mm 37 #,##0 ;(#,##0) 38 #,##0 ;Red 39 #,##0.00;(#,##0.00) 40 #,##0.00;Red 45 mm:ss 46 [h]:mm:ss 47 mmss.0 48 ##0.0E+0 49 @ Axlsx also defines the following constants which you can use in add_style. NUM_FMT_PERCENT formats to "0%" NUM_FMT_YYYYMMDD formats to "yyyy/mm/dd" NUM_FMT_YYYYMMDDHHMMSS formats to "yyyy/mm/dd hh:mm:ss"
67 68 69 |
# File 'lib/axlsx/stylesheet/styles.rb', line 67 def numFmts @numFmts end |
#tableStyles ⇒ SimpleTypedList (readonly)
The recommended way to manage styles is with add_style
The collection of table styles that will be available to the user in the excel UI
116 117 118 |
# File 'lib/axlsx/stylesheet/styles.rb', line 116 def tableStyles @tableStyles end |
Instance Method Details
#add_style(options = {}) ⇒ Integer
Drastically simplifies style creation and management.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/axlsx/stylesheet/styles.rb', line 222 def add_style(={}) # Default to :xf [:type] ||= :xf raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?([:type] ) fill = font = numFmt = border = alignment = protection = case [:type] when :dxf style = Dxf.new :fill => fill, :font => font, :numFmt => numFmt, :border => border, :alignment => alignment, :protection => protection else style = Xf.new :fillId=>fill || 0, :fontId=>font || 0, :numFmtId=>numFmt || 0, :borderId=>border || 0, :alignment => alignment, :protection => protection, :applyFill=>!fill.nil?, :applyFont=>!font.nil?, :applyNumberFormat =>!numFmt.nil?, :applyBorder=>!border.nil?, :applyAlignment => !alignment.nil?, :applyProtection => !protection.nil? end [:type] == :xf ? cellXfs << style : dxfs << style end |
#parse_alignment_options(options = {}) ⇒ CellAlignment
parses add_style options for alignment noop if options hash does not include :alignment key
260 261 262 263 |
# File 'lib/axlsx/stylesheet/styles.rb', line 260 def (={}) return unless [:alignment] CellAlignment.new [:alignment] end |
#parse_border_options(options = {}) ⇒ Border|Integer
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/axlsx/stylesheet/styles.rb', line 319 def (={}) return unless [:border] b_opts = [:border] if b_opts.is_a?(Hash) raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.keys.include?(:style) && b_opts.keys.include?(:color) border = Border.new b_opts (b_opts[:edges] || [:left, :right, :top, :bottom]).each do |edge| = ["border_#{edge}".to_sym] || {} border_edge = b_opts.merge() = { :name => edge, :style => border_edge[:style], :color => Color.new(:rgb => border_edge[:color]) } border.prs << BorderPr.new() end [:type] == :dxf ? border : borders << border elsif b_opts.is_a? Integer raise ArgumentError, (ERR_INVALID_BORDER_ID % b_opts) unless b_opts < borders.size if [:type] == :dxf borders[b_opts].clone else border = b_opts end end end |
#parse_fill_options(options = {}) ⇒ Fill|Integer
noop if :bg_color is not specified in options
parses add_style options for fills. If the options hash contains :type => :dxf we return a Fill object. If not, we return the index of the fill after being added to the fills collection.
296 297 298 299 300 301 302 303 304 |
# File 'lib/axlsx/stylesheet/styles.rb', line 296 def (={}) return unless [:bg_color] color = Color.new(:rgb=>[:bg_color]) dxf = [:type] == :dxf color_key = dxf ? :bgColor : :fgColor pattern = PatternFill.new(:patternType =>:solid, color_key=>color) fill = Fill.new(pattern) dxf ? fill : fills << fill end |
#parse_font_options(options = {}) ⇒ Font|Integer
noop if none of the options described here are set on the options parameter.
parses add_style options for fonts. If the options hash contains :type => :dxf we return a new Font object. if not, we return the index of the newly created font object in the styles.fonts collection.
280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/axlsx/stylesheet/styles.rb', line 280 def (={}) return if (.keys & [:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name]).empty? fonts.first.instance_values.each do |key, value| # Thanks for that 1.8.7 - cant do a simple merge... [key.to_sym] = value unless .keys.include?(key.to_sym) end font = Font.new() font.color = Color.new(:rgb => [:fg_color]) if [:fg_color] font.name = [:font_name] if [:font_name] [:type] == :dxf ? font : fonts << font end |
#parse_num_fmt_options(options = {}) ⇒ NumFmt|Integer
Parses Style#add_style options for number formatting. noop if neither :format_code or :num_format options are set.
346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/axlsx/stylesheet/styles.rb', line 346 def (={}) return if (.keys & [:format_code, :num_fmt]).empty? #When the user provides format_code - we always need to create a new numFmt object #When the type is :dxf we always need to create a new numFmt object if [:format_code] || [:type] == :dxf #If this is a standard xf we pull from numFmts the highest current and increment for num_fmt [:num_fmt] ||= (@numFmts.map{ |num_fmt| num_fmt.numFmtId }.max + 1) if [:type] != :dxf numFmt = NumFmt.new(:numFmtId => [:num_fmt] || 0, :formatCode=> [:format_code].to_s) [:type] == :dxf ? numFmt : (numFmts << numFmt; numFmt.numFmtId) else [:num_fmt] end end |
#parse_protection_options(options = {}) ⇒ CellProtection
250 251 252 253 |
# File 'lib/axlsx/stylesheet/styles.rb', line 250 def (={}) return if (.keys & [:hidden, :locked]).empty? CellProtection.new() end |
#to_xml_string(str = '') ⇒ String
Serializes the object
364 365 366 367 368 369 370 |
# File 'lib/axlsx/stylesheet/styles.rb', line 364 def to_xml_string(str = '') str << '<styleSheet xmlns="' << XML_NS << '">' [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| self.instance_values[key.to_s].to_xml_string(str) unless self.instance_values[key.to_s].nil? end str << '</styleSheet>' end |