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
parses Style#add_style options for borders.
-
#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
- #style_index ⇒ Object
-
#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.
120 121 122 |
# File 'lib/axlsx/stylesheet/styles.rb', line 120 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.
87 88 89 |
# File 'lib/axlsx/stylesheet/styles.rb', line 87 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.
99 100 101 |
# File 'lib/axlsx/stylesheet/styles.rb', line 99 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
93 94 95 |
# File 'lib/axlsx/stylesheet/styles.rb', line 93 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.
105 106 107 |
# File 'lib/axlsx/stylesheet/styles.rb', line 105 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.
111 112 113 |
# File 'lib/axlsx/stylesheet/styles.rb', line 111 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
80 81 82 |
# File 'lib/axlsx/stylesheet/styles.rb', line 80 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
74 75 76 |
# File 'lib/axlsx/stylesheet/styles.rb', line 74 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"
68 69 70 |
# File 'lib/axlsx/stylesheet/styles.rb', line 68 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
117 118 119 |
# File 'lib/axlsx/stylesheet/styles.rb', line 117 def tableStyles @tableStyles end |
Instance Method Details
#add_style(options = {}) ⇒ Integer
Drastically simplifies style creation and management.
An index for cell styles where keys are styles codes as per Axlsx::Style and values are Cell#raw_style
The reason for the backward key/value ordering is that style lookup must be most efficient, while add_style
can be less efficient
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/axlsx/stylesheet/styles.rb', line 229 def add_style( = {}) # Default to :xf [:type] ||= :xf raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?([:type]) if [:border].is_a?(Hash) if [:border][:edges] == :all [:border][:edges] = Axlsx::Border::EDGES elsif [:border][:edges] [:border][:edges] = [:border][:edges].map(&:to_sym) ### normalize for style caching end end if [:type] == :xf # Check to see if style in cache already font_defaults = { name: @fonts.first.name, sz: @fonts.first.sz, family: @fonts.first.family } raw_style = { type: :xf }.merge(font_defaults).merge() if raw_style[:format_code] raw_style.delete(:num_fmt) end xf_index = style_index.key(raw_style) if xf_index return xf_index end end fill = font = numFmt = border = alignment = protection = style = case [:type] when :dxf Dxf.new fill: fill, font: font, numFmt: numFmt, border: border, alignment: alignment, protection: protection else 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 if [:type] == :xf xf_index = (cellXfs << style) # Add styles to style_index cache for re-use style_index[xf_index] = raw_style xf_index else dxfs << style end end |
#parse_alignment_options(options = {}) ⇒ CellAlignment
parses add_style options for alignment noop if options hash does not include :alignment key
304 305 306 307 308 |
# File 'lib/axlsx/stylesheet/styles.rb', line 304 def ( = {}) return unless [:alignment] CellAlignment.new [:alignment] end |
#parse_border_options(options = {}) ⇒ Border|Integer
noop if :border is not specified in options
parses Style#add_style options for borders. Border style definition hashes must include :style and :color key-value entries and may include an :edges entry that references an array of symbols identifying which border edges you wish to apply the style or any other valid Border initializer options. If the :edges entity is not provided the style is applied to all edges of cells that reference this style. Also available :border_top, :border_right, :border_bottom and :border_left options with :style and/or :color key-value entries, which override :border values.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/axlsx/stylesheet/styles.rb', line 403 def ( = {}) if [:border].nil? && Border::EDGES.all? { |x| [:"border_#{x}"].nil? } return nil end if [:border].is_a?(Integer) if [:border] >= borders.size raise ArgumentError, format(ERR_INVALID_BORDER_ID, [:border]) end if [:type] == :dxf return borders[[:border]].clone else return [:border] end end validate_border_hash = lambda { |val| unless val.key?(:style) && val.key?(:color) raise ArgumentError, format(ERR_INVALID_BORDER_OPTIONS, [:border]) end } borders_array = [] if [:border].nil? base_border_opts = {} elsif [:border].is_a?(Array) borders_array += [:border] base_border_opts = {} [:border].each do |b_opts| if b_opts[:edges].nil? base_border_opts = base_border_opts.merge(b_opts) end end else borders_array << [:border] base_border_opts = [:border] validate_border_hash.call(base_border_opts) end Border::EDGES.each do |edge| val = [:"border_#{edge}"] if val borders_array << val.merge(edges: [edge]) end end border = Border.new(base_border_opts) Border::EDGES.each do |edge| edge_b_opts = base_border_opts skip_edge = true borders_array.each do |b_opts| if b_opts[:edges] && b_opts[:edges].include?(edge) edge_b_opts = edge_b_opts.merge(b_opts) skip_edge = false end end if [:"border_#{edge}"] edge_b_opts = edge_b_opts.merge([:"border_#{edge}"]) skip_edge = false end if skip_edge && base_border_opts[:edges] next end unless edge_b_opts.empty? if base_border_opts.empty? validate_border_hash.call(edge_b_opts) end border.prs << BorderPr.new({ name: edge, style: edge_b_opts[:style], color: Color.new(rgb: edge_b_opts[:color]) }) end end if [:type] == :dxf border else borders << border end end |
#parse_fill_options(options = {}) ⇒ Fill|Integer
noop unless at least one of the documented attributes is 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.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/axlsx/stylesheet/styles.rb', line 345 def ( = {}) return unless [:bg_color] || [:pattern_type] || [:pattern_bg_color] || [:pattern_fg_color] pattern_type = [:pattern_type] || :solid dxf = [:type] == :dxf = { patternType: pattern_type } if [:pattern_bg_color] && [:bg_color] warn 'Both `bg_color` and `pattern_bg_color` got defined. To get a solid background without defining it in `patter_type`, use only `bg_color`, otherwise use only `pattern_bg_color` to avoid confusion.' end bg_color = [:pattern_bg_color] || [:bg_color] fg_color = [:pattern_fg_color] # Both bgColor and fgColor happens to configure the background of the cell. # One of them sets the "background" of the cell, while the other one is # responsibe for the "pattern" of the cell. When you pick "solid" pattern for # a normal xf style, then it's a rectangle covering all bgColor with fgColor, # which means we need to to set the given background color to fgColor as well. # For some reason I wasn't able find, it works the opposite for dxf styles # (differential formatting records), so to get the expected color, we need # to put it into bgColor. We only need these cross-assignments when using # "solid" pattern and the user provided only one color to get the least # amount of surprise if bg_color [:bgColor] = Color.new(rgb: bg_color) elsif pattern_type == :solid && fg_color [:bgColor] = Color.new(rgb: fg_color) end if fg_color [:fgColor] = Color.new(rgb: fg_color) elsif pattern_type == :solid && bg_color [:fgColor] = Color.new(rgb: bg_color) end pattern = PatternFill.new() 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.
325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/axlsx/stylesheet/styles.rb', line 325 def ( = {}) return if (.keys & [:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name]).empty? Axlsx.instance_values_for(fonts.first).each do |key, value| # Thanks for that 1.8.7 - cant do a simple merge... [key.to_sym] = value unless .key?(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.
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/axlsx/stylesheet/styles.rb', line 503 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(&:numFmtId).max + 1) if [:type] != :dxf numFmt = NumFmt.new(numFmtId: [:num_fmt] || 0, formatCode: [:format_code].to_s) if [:type] == :dxf numFmt else numFmts << numFmt numFmt.numFmtId end else [:num_fmt] end end |
#parse_protection_options(options = {}) ⇒ CellProtection
293 294 295 296 297 |
# File 'lib/axlsx/stylesheet/styles.rb', line 293 def ( = {}) return if (.keys & [:hidden, :locked]).empty? CellProtection.new() end |
#style_index ⇒ Object
124 125 126 |
# File 'lib/axlsx/stylesheet/styles.rb', line 124 def style_index @style_index ||= {} end |
#to_xml_string(str = +'')) ⇒ String
Serializes the object
526 527 528 529 530 531 532 533 |
# File 'lib/axlsx/stylesheet/styles.rb', line 526 def to_xml_string(str = +'') str << '<styleSheet xmlns="' << XML_NS << '">' instance_vals = Axlsx.instance_values_for(self) [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| instance_vals[key.to_s].to_xml_string(str) unless instance_vals[key.to_s].nil? end str << '</styleSheet>' end |