class Xlsxrb::Elements::Styles
Represents the workbook stylesheet definitions (fonts, fills, borders, cellXfs, numFmts, etc.). Encapsulates stylesheet collections, item lookups, and precomputed O(1) number format classifications.
Public Class Methods
(?Hash[untyped, untyped]? initial_hash) → void
Source
# File lib/xlsxrb/elements/styles.rb, line 23 def initialize(initial_hash = nil) @data = initial_hash.is_a?(Hash) ? initial_hash.dup : {} invalidate_cache! end
Initializes the styles container, optionally wrapping an existing styles dictionary.
@param initial_hash [Hash, nil] Initial styles dictionary.
Public Instance Methods
(untyped other) → bool
Source
# File lib/xlsxrb/elements/styles.rb, line 451 def ==(other) other.is_a?(Styles) ? @data == other.to_h : false end
Equality check against another Styles.
@param other [Object] @return [Boolean] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 309 def add_border(spec = nil, **kwargs) entry = (spec || kwargs).dup borders_arr = (@data[:borders] ||= []) borders_arr << entry invalidate_cache! borders_arr.size - 1 end
Appends a border entry to the stylesheet and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 381 def add_cell_style(spec = nil, **kwargs) entry = (spec || kwargs).dup cs_arr = (@data[:cell_styles] ||= []) cs_arr << entry invalidate_cache! cs_arr.size - 1 end
Appends a named cell style entry and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 367 def add_cell_style_xf(spec = nil, **kwargs) entry = (spec || kwargs).dup xfs_arr = (@data[:cell_style_xfs] ||= []) xfs_arr << entry invalidate_cache! xfs_arr.size - 1 end
Appends a cell style format (cellStyleXf) entry and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 339 def add_cell_xf(spec = nil, **kwargs) entry = (spec || kwargs).dup xfs_arr = (@data[:cell_xfs] ||= []) xfs_arr << entry invalidate_cache! xfs_arr.size - 1 end
Appends a cell format (cellXf) entry and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 353 def add_dxf(spec = nil, **kwargs) entry = (spec || kwargs).dup dxfs_arr = (@data[:dxfs] ||= []) dxfs_arr << entry invalidate_cache! dxfs_arr.size - 1 end
Appends a differential format (dxf) entry and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 294 def add_fill(spec = nil, **kwargs) entry = (spec || kwargs).dup entry[:pattern_type] ||= entry[:pattern] if entry.key?(:pattern) fills_arr = (@data[:fills] ||= []) fills_arr << entry invalidate_cache! fills_arr.size - 1 end
Appends a fill entry to the stylesheet and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 279 def add_font(spec = nil, **kwargs) entry = (spec || kwargs).dup entry[:size] ||= entry[:sz] if entry.key?(:sz) fonts_arr = (@data[:fonts] ||= []) fonts_arr << entry invalidate_cache! fonts_arr.size - 1 end
Appends a font entry to the stylesheet and returns its 0-based index.
@param spec [Hash, nil] @return [Integer] @api public
(Integer id, String code) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 324 def add_number_format(id, code) raise ArgumentError, "num_fmt_id must be an Integer" unless id.is_a?(Integer) raise ArgumentError, "format_code must be a String" unless code.is_a?(String) (@data[:num_fmts] ||= {})[id] = code invalidate_cache! id end
Sets a custom number format code and returns the format id.
@param id [Integer] Format ID (custom formats typically start at 164). @param code [String] Format code pattern. @return [Integer] @api public
(?Hash[Symbol, untyped]? spec, **untyped kwargs) → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 396 def add_style(spec = nil, **kwargs) opts = (spec || kwargs).dup font_id = opts[:font_id] || (opts[:font] ? add_font(opts[:font]) : 0) fill_id = opts[:fill_id] || (opts[:fill] ? add_fill(opts[:fill]) : 0) border_id = opts[:border_id] || (opts[:border] ? add_border(opts[:border]) : 0) num_fmt_id = opts[:num_fmt_id] || 0 if opts[:number_format] code = opts[:number_format].to_s existing_id = number_formats.key(code) || Ooxml::Utils::BUILTIN_NUM_FMT_CODES.key(code) num_fmt_id = existing_id || add_number_format(next_num_fmt_id, code) end xf_spec = { font_id: font_id, fill_id: fill_id, border_id: border_id, num_fmt_id: num_fmt_id } xf_spec[:alignment] = opts[:alignment] if opts[:alignment] xf_spec[:apply_alignment] = true if opts[:alignment] || opts[:apply_alignment] xf_spec[:apply_fill] = true if opts[:fill] || opts[:apply_fill] xf_spec[:apply_font] = true if opts[:font] || opts[:apply_font] xf_spec[:apply_border] = true if opts[:border] || opts[:apply_border] xf_spec[:apply_number_format] = true if opts[:number_format] || opts[:apply_number_format] xf_spec[:protection] = opts[:protection] if opts[:protection] xf_spec[:apply_protection] = true if opts[:protection] || opts[:apply_protection] add_cell_xf(xf_spec) end
High-level helper to compose and register a cell format (xf), adding subordinate font, fill, border, and number format definitions as needed. Returns the xf_id.
@param spec [Hash, nil] @return [Integer] @api public
(Integer? id) → Elements::Border?
Source
# File lib/xlsxrb/elements/styles.rb, line 207 def border(id) return nil unless id.is_a?(Integer) && id >= 0 borders[id] end
Looks up a border entry by 0-based index.
@param id [Integer, nil] @return [Elements::Border, nil] @api public
() → Array[Elements::Border]
Source
# File lib/xlsxrb/elements/styles.rb, line 67 def borders @borders ||= (@data[:borders] || EMPTY_ARRAY).map { |b| b.is_a?(Elements::Border) ? b : Elements::Border.new(**b) }.freeze end
Returns array of border entries from the styles.
@return [Array<Elements::Border>] @api public
(Integer? id) → Elements::CellStyle?
Source
# File lib/xlsxrb/elements/styles.rb, line 267 def cell_style(id) return nil unless id.is_a?(Integer) && id >= 0 cell_styles[id] end
Looks up a named cell style entry by 0-based index.
@param id [Integer, nil] @return [Elements::CellStyle, nil] @api public
(Integer? id) → Elements::CellXf?
Source
# File lib/xlsxrb/elements/styles.rb, line 255 def cell_style_xf(id) return nil unless id.is_a?(Integer) && id >= 0 cell_style_xfs[id] end
Looks up a cell style format (cellStyleXf) entry by 0-based index.
@param id [Integer, nil] @return [Elements::CellXf, nil] @api public
() → Array[Elements::CellXf]
Source
# File lib/xlsxrb/elements/styles.rb, line 108 def cell_style_xfs @cell_style_xfs ||= (@data[:cell_style_xfs] || EMPTY_ARRAY).map { |x| x.is_a?(Elements::CellXf) ? x : Elements::CellXf.new(**x) }.freeze end
Returns array of cell style format (cellStyleXfs) entries from the styles.
@return [Array<Elements::CellXf>] @api public
() → Array[Elements::CellStyle]
Source
# File lib/xlsxrb/elements/styles.rb, line 117 def cell_styles @cell_styles ||= (@data[:cell_styles] || EMPTY_ARRAY).map { |s| s.is_a?(Elements::CellStyle) ? s : Elements::CellStyle.new(**s) }.freeze end
Returns array of named cell styles from the styles.
@return [Array<Elements::CellStyle>] @api public
(Integer? id) → Elements::CellXf?
Source
# File lib/xlsxrb/elements/styles.rb, line 219 def cell_xf(id) return nil unless id.is_a?(Integer) && id >= 0 cell_xfs[id] end
Looks up a cell format (cellXf) entry by 0-based index.
@param id [Integer, nil] @return [Elements::CellXf, nil] @api public
() → Array[Elements::CellXf]
Source
# File lib/xlsxrb/elements/styles.rb, line 76 def cell_xfs @cell_xfs ||= (@data[:cell_xfs] || EMPTY_ARRAY).map { |x| x.is_a?(Elements::CellXf) ? x : Elements::CellXf.new(**x) }.freeze end
Returns array of cell format (cellXfs) entries from the styles.
@return [Array<Elements::CellXf>] @api public
() → Elements::Colors
Source
# File lib/xlsxrb/elements/styles.rb, line 156 def colors @colors ||= begin raw = @data[:colors] || EMPTY_HASH raw.is_a?(Elements::Colors) ? raw : Elements::Colors.new(**raw) end end
Returns colors dictionary or palette.
@return [Elements::Colors] @api public
(Integer? style_index) → bool
Source
# File lib/xlsxrb/elements/styles.rb, line 497 def date_format?(style_index) return false unless style_index?(style_index) precompute! unless @date_flags @date_flags[style_index] || false end
Returns whether the style index represents a date, datetime, or time pattern.
@param style_index [Integer, nil] @return [Boolean] @api public
(Integer? style_index) → bool
Source
# File lib/xlsxrb/elements/styles.rb, line 536 def date_only_format?(style_index) format_type(style_index) == :date end
Returns whether the style index represents a date-only pattern.
@param style_index [Integer, nil] @return [Boolean] @api public
(Integer? style_index) → bool
Source
# File lib/xlsxrb/elements/styles.rb, line 510 def datetime_format?(style_index) return false unless style_index?(style_index) precompute! unless @datetime_flags @datetime_flags[style_index] || false end
Returns whether the style index represents a datetime pattern.
@param style_index [Integer, nil] @return [Boolean] @api public
(Integer? id) → Elements::Dxf?
Source
# File lib/xlsxrb/elements/styles.rb, line 243 def dxf(id) return nil unless id.is_a?(Integer) && id >= 0 dxfs[id] end
Looks up a differential format (dxf) entry by 0-based index.
@param id [Integer, nil] @return [Elements::Dxf, nil] @api public
() → Array[Elements::Dxf]
Source
# File lib/xlsxrb/elements/styles.rb, line 99 def dxfs @dxfs ||= (@data[:dxfs] || EMPTY_ARRAY).map { |d| d.is_a?(Elements::Dxf) ? d : Elements::Dxf.new(**d) }.freeze end
Returns array of differential format (dxf) entries from the styles.
@return [Array<Elements::Dxf>] @api public
() → bool
Source
# File lib/xlsxrb/elements/styles.rb, line 431 def empty? @data.empty? end
Returns true if the styles container has no registered definitions.
@return [Boolean] @api public
(Integer? id) → Elements::Fill?
Source
# File lib/xlsxrb/elements/styles.rb, line 195 def fill(id) return nil unless id.is_a?(Integer) && id >= 0 fills[id] end
Looks up a fill entry by 0-based index.
@param id [Integer, nil] @return [Elements::Fill, nil] @api public
() → Array[Elements::Fill]
Source
# File lib/xlsxrb/elements/styles.rb, line 50 def fills @fills ||= (@data[:fills] || EMPTY_ARRAY).map do |f| if f.is_a?(Elements::Fill) f else f = f.dup f[:pattern_type] ||= f[:pattern] if f.key?(:pattern) Elements::Fill.new(**f) end end.freeze end
Returns array of fill entries from the styles.
@return [Array<Elements::Fill>] @api public
(Integer? id) → Elements::Font?
Source
# File lib/xlsxrb/elements/styles.rb, line 169 def font(id) return nil unless id.is_a?(Integer) && id >= 0 fonts[id] end
Looks up a font entry by 0-based index.
@param id [Integer, nil] @return [Elements::Font, nil] @api public
(Integer? id) → Numeric?
Source
# File lib/xlsxrb/elements/styles.rb, line 181 def font_size(id) f = font(id) return nil unless f sz = f.size sz if sz.is_a?(Numeric) end
Returns the font size in points for the font entry at index id.
@param id [Integer, nil] @return [Numeric, nil] @api public
() → Array[Elements::Font]
Source
# File lib/xlsxrb/elements/styles.rb, line 33 def fonts @fonts ||= (@data[:fonts] || EMPTY_ARRAY).map do |f| if f.is_a?(Elements::Font) f else f = f.dup f[:size] ||= f[:sz] if f.key?(:sz) Elements::Font.new(**f) end end.freeze end
Returns array of font entries from the styles.
@return [Array<Elements::Font>] @api public
(Integer? style_index) → Symbol?
Source
# File lib/xlsxrb/elements/styles.rb, line 484 def format_type(style_index) return nil unless style_index?(style_index) precompute! unless @format_types @format_types[style_index] end
Returns the classified format category (:date, :datetime, :time, :number, :text, :general) for a style index.
@param style_index [Integer, nil] @return [Symbol, nil] @api public
() → Integer
Source
# File lib/xlsxrb/elements/styles.rb, line 461 def hash @data.hash end
Computes hash code based on underlying data.
@return [Integer] @api public
() → Array[String]
Source
# File lib/xlsxrb/elements/styles.rb, line 138 def indexed_colors @data[:indexed_colors] || EMPTY_ARRAY end
Returns array of indexed color strings (ARGB hex).
@return [Array<String>] @api public
() → Array[Elements::Color]
Source
# File lib/xlsxrb/elements/styles.rb, line 147 def mru_colors @mru_colors ||= (@data[:mru_colors] || EMPTY_ARRAY).map { |c| c.is_a?(Elements::Color) ? c : Elements::Color.new(**c) }.freeze end
Returns array of MRU (most recently used) color dictionaries.
@return [Array<Elements::Color>] @api public
(Integer? style_index) → String?
Source
# File lib/xlsxrb/elements/styles.rb, line 471 def number_format(style_index) return nil unless style_index?(style_index) precompute! unless @format_codes @format_codes[style_index] end
Returns the format code string for a given style index.
@param style_index [Integer, nil] @return [String, nil] @api public
(Integer? id) → String?
Source
# File lib/xlsxrb/elements/styles.rb, line 231 def number_format_code(id) return nil unless id.is_a?(Integer) number_formats[id] end
Looks up a custom format code by numFmtId.
@param id [Integer, nil] @return [String, nil] @api public
() → Hash[Integer, String]
Source
# File lib/xlsxrb/elements/styles.rb, line 85 def number_formats raw = @data[:num_fmts] if raw.is_a?(Array) raw.to_h { |nf| [nf[:num_fmt_id] || nf["num_fmt_id"], nf[:format_code] || nf["format_code"]] } else raw || EMPTY_HASH end end
Returns custom number formats as { numFmtId => formatCode }.
@return [Hash<Integer, String>] @api public
() → self
Source
# File lib/xlsxrb/elements/styles.rb, line 545 def precompute! xfs = cell_xfs size = xfs.is_a?(Array) ? xfs.size : 0 codes = Array.new(size) types = Array.new(size) d_flags = Array.new(size, false) dt_flags = Array.new(size, false) t_flags = Array.new(size, false) if size.positive? cs_xfs = cell_style_xfs n_fmts = number_formats size.times do |i| xf = xfs[i] next unless xf.is_a?(Hash) || xf.is_a?(Elements::CellXf) num_fmt_id = xf.is_a?(Elements::CellXf) ? xf.num_fmt_id : xf[:num_fmt_id] xf_id = xf.is_a?(Elements::CellXf) ? xf.xf_id : xf[:xf_id] if (num_fmt_id.nil? || num_fmt_id.zero?) && xf_id && cs_xfs.is_a?(Array) # An xfId outside the array names no parent. Array#[] would count a negative # one from the end and raise RangeError for one that does not fit in a word. parent_xf = xf_id.is_a?(Integer) && xf_id.between?(0, cs_xfs.size - 1) ? cs_xfs[xf_id] : nil if parent_xf.is_a?(Elements::CellXf) num_fmt_id = parent_xf.num_fmt_id elsif parent_xf.is_a?(Hash) num_fmt_id = parent_xf[:num_fmt_id] end end code = num_fmt_id ? (n_fmts[num_fmt_id] || Ooxml::Utils::BUILTIN_NUM_FMT_CODES[num_fmt_id]) : nil codes[i] = code type = NumberFormatter.format_type(code) types[i] = type d_flags[i] = %i[date datetime time].include?(type) dt_flags[i] = (type == :datetime) t_flags[i] = (type == :time) end end @format_codes = codes @format_types = types @date_flags = d_flags @datetime_flags = dt_flags @time_flags = t_flags self end
Precomputes format code and type classification lookups for all cellXfs.
@return [self] @api private
() → Elements::TableStyles
Source
# File lib/xlsxrb/elements/styles.rb, line 126 def table_styles @table_styles ||= begin raw = @data[:table_styles] || EMPTY_HASH raw.is_a?(Elements::TableStyles) ? raw : Elements::TableStyles.new(**raw) end end
Returns table styles configuration dictionary from the styles.
@return [Elements::TableStyles] @api public
(Integer? style_index) → bool
Source
# File lib/xlsxrb/elements/styles.rb, line 523 def time_format?(style_index) return false unless style_index?(style_index) precompute! unless @time_flags @time_flags[style_index] || false end
Returns whether the style index represents a time pattern.
@param style_index [Integer, nil] @return [Boolean] @api public
() → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/elements/styles.rb, line 440 def to_h @data.dup end
Converts the styles container to a standard Hash.
@return [Hash] @api public