class Xlsxrb::StyleBuilder
Helper class for building cell styles with a fluent DSL. Encapsulates font, fill, border, alignment, and number format properties. @api public
Constants
- FLAT_KEYS_BY_GROUP
-
Flat keys that the nested form of each group replaces; giving both is ambiguous.
- NESTED_OPTION_KEYS
-
Keys accepted inside the nested option hashes.
- OPTION_KEYS
-
Top-level keys accepted by
apply_options!. - UNDERLINE_MAP
- VERT_ALIGN_MAP
Attributes
Hash[Symbol, untyped]
@api private
Hash[Symbol, untyped]
@api private
Hash[Symbol, untyped]
@api private
(String | Symbol)?
Public Class Methods
(Symbol | String | Integer style) → String
Source
# File lib/xlsxrb/style_builder.rb, line 322 def self.border_style!(style) normalized = normalize_border_style(style) return normalized.to_s if normalized suggestion = DidYouMean::SpellChecker.new(dictionary: BORDER_STYLES.keys.map(&:to_s)).correct(style.to_s).first message = "unknown border style: #{style.inspect}" message += "; did you mean #{BORDER_STYLES[suggestion.to_sym].inspect}?" if suggestion raise ArgumentError, message end
Normalizes a border style like normalize_border_style, raising ArgumentError for a style that is not one of the ST_BorderStyle values or their snake_case spellings.
@param style [Symbol, String, Integer] @return [String] @api private
(?String? name) → void
Source
# File lib/xlsxrb/style_builder.rb, line 353 def initialize(name = nil) @name = name @font_props = {} @fill_props = {} @border_props = {} @num_fmt_id = nil @alignment = {} @protection = {} end
(Symbol | String | Integer | nil style) → Symbol?
Source
# File lib/xlsxrb/style_builder.rb, line 305 def self.normalize_border_style(style) return nil if style.nil? return BORDER_INDEX_MAP[style] if style.is_a?(Integer) str = style.to_s.sub(/^border_/, "") sym = str.to_sym BORDER_STYLES[sym] || BORDER_STYLES[str.downcase.to_sym] end
Normalizes a border style symbol, string, or integer index to an OOXML ST_BorderStyle symbol.
@example
StyleBuilder.normalize_border_style(:border_thin) #=> :thin StyleBuilder.normalize_border_style(1) #=> :thin StyleBuilder.normalize_border_style(:medium_dashed) #=> :mediumDashed StyleBuilder.normalize_border_style("double") #=> :double StyleBuilder.normalize_border_style(0) #=> :none
@param style [Symbol, String, Integer, nil] @return [Symbol, nil] @api private
(String | Symbol | Integer | nil color, ?alpha: bool) → String?
Source
# File lib/xlsxrb/style_builder.rb, line 251 def self.normalize_color(color, alpha: true) return nil if color.nil? hex = case color when Symbol, defined?(String) && String str = color.to_s.strip return nil if str.empty? # Strip leading colon (e.g. ":red") str = str.sub(/^:/, "") return nil if str.empty? sym_key = str.downcase.to_sym if CSS_COLORS.key?(sym_key) CSS_COLORS[sym_key] else # Hex string parsing clean_hex = str.sub(/^#/, "") case clean_hex.length when 3 if clean_hex.match?(/\A[0-9a-fA-F]{3}\z/) r, g, b = clean_hex.chars "#{r}#{r}#{g}#{g}#{b}#{b}".upcase end when 6 clean_hex.upcase if clean_hex.match?(/\A[0-9a-fA-F]{6}\z/) when 8 if clean_hex.match?(/\A[0-9a-fA-F]{8}\z/) return alpha ? clean_hex.upcase : clean_hex[2..7].upcase end end end when Integer format("%06X", color & 0xFFFFFF) end return nil unless hex alpha ? "FF#{hex}" : hex end
Normalizes a color definition (CSS color name, hex code, or RGB integer) to an OpenXML hex color string.
@example
StyleBuilder.normalize_color(:navy) #=> "FF000080" StyleBuilder.normalize_color("#FF0000") #=> "FFFF0000" StyleBuilder.normalize_color("#F00") #=> "FFFF0000" StyleBuilder.normalize_color("FF0000", alpha: false) #=> "FF0000" StyleBuilder.normalize_color(0xFF0000) #=> "FFFF0000"
@param color [String, Symbol, Integer, nil] @param alpha [Boolean] Whether to include the alpha channel (“AARRGGBB” vs “RRGGBB”). @return [String, nil] @api private
(untyped color) → String?
Source
# File lib/xlsxrb/style_builder.rb, line 337 def self.resolve_color(color) return nil unless color return Colors.palette_color(color, alpha: true) if color.is_a?(Integer) && defined?(Colors) && color >= 0 && color < Colors::INDEXED_COLORS.size if color.is_a?(Symbol) || (color.is_a?(String) && color.start_with?(":") && color.length > 1) key = color.to_s.sub(/^:/, "").to_sym return COLORS[key] || (CSS_COLORS[key] ? "FF#{CSS_COLORS[key]}" : nil) || color.to_s end return normalize_color(color, alpha: true) || color.to_s if color.is_a?(String) && color.start_with?("#") color.to_s end
Resolves a color symbol, string, or prefix to an aRGB hex string.
@param color [Symbol, String, Object, nil] @return [String, nil]
Public Instance Methods
() → (String | Symbol | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 1038 def align_horizontal @alignment[:horizontal] end
Gets horizontal alignment. @return [String, Symbol, nil] @api public
(String | Symbol value) → (String | Symbol)
Source
# File lib/xlsxrb/style_builder.rb, line 1047 def align_horizontal=(value) @alignment[:horizontal] = value end
Sets horizontal alignment. @param value [String, Symbol] @return [String, Symbol] @api public
() → (String | Symbol | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 1055 def align_vertical @alignment[:vertical] end
Gets vertical alignment. @return [String, Symbol, nil] @api public
(String | Symbol value) → (String | Symbol)
Source
# File lib/xlsxrb/style_builder.rb, line 1064 def align_vertical=(value) @alignment[:vertical] = value end
Sets vertical alignment. @param value [String, Symbol] @return [String, Symbol] @api public
() → Hash[Symbol, untyped]
(?horizontal: (String | Symbol)?, ?vertical: (String | Symbol)?, ?wrap_text: bool?, ?text_rotation: Integer?, ?indent: Integer?, ?shrink_to_fit: bool?, ?reading_order: Integer?, ?justify_last_line: bool?, ?relative_indent: Integer?) → void
Source
# File lib/xlsxrb/style_builder.rb, line 1008 def alignment(horizontal: nil, vertical: nil, wrap_text: nil, text_rotation: nil, indent: nil, shrink_to_fit: nil, reading_order: nil, justify_last_line: nil, relative_indent: nil) return @alignment if horizontal.nil? && vertical.nil? && wrap_text.nil? && text_rotation.nil? && indent.nil? && shrink_to_fit.nil? && reading_order.nil? && justify_last_line.nil? && relative_indent.nil? self.align_horizontal = horizontal unless horizontal.nil? self.align_vertical = vertical unless vertical.nil? self.wrap_text = wrap_text unless wrap_text.nil? self.text_rotation = text_rotation unless text_rotation.nil? self.indent = indent unless indent.nil? self.shrink_to_fit = shrink_to_fit unless shrink_to_fit.nil? @alignment[:reading_order] = reading_order unless reading_order.nil? @alignment[:justify_last_line] = justify_last_line unless justify_last_line.nil? @alignment[:relative_indent] = relative_indent unless relative_indent.nil? nil end
Configures or returns alignment properties. When called with 0 arguments/keywords, returns the current alignment Hash. When called with keyword arguments, merges the properties and returns nil.
@param horizontal [String, Symbol, nil] Horizontal alignment (:left, :center, :right, :fill, :justify, etc.). @param vertical [String, Symbol, nil] Vertical alignment (:top, :center, :bottom, :justify, :distributed). @param wrap_text [Boolean, nil] Whether to wrap text within the cell. @param text_rotation [Integer, nil] Text rotation in degrees (0-180 or 255 for vertical). @param indent [Integer, nil] Indentation level (0-250). @param shrink_to_fit [Boolean, nil] Whether to shrink text to fit cell width. @param reading_order [Integer, nil] Reading order (0: context, 1: left-to-right, 2: right-to-left). @param justify_last_line [Boolean, nil] Whether to justify the last line of text. @param relative_indent [Integer, nil] Relative indentation level. @return [Hash, void] @api public
(Hash[Symbol, untyped] opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/style_builder.rb, line 1029 def alignment=(opts) alignment(**opts) opts end
Sets alignment options.
@param opts [Hash] @return [Hash] @api public
(**untyped) → self
Source
# File lib/xlsxrb/style_builder.rb, line 440 def apply_options!(**opts) check_options!(opts) if opts.key?(:font) font_opts = opts[:font] || {} self.bold = font_opts[:bold] if font_opts.key?(:bold) self.italic = font_opts[:italic] if font_opts.key?(:italic) self.font_size = font_opts[:size] if font_opts.key?(:size) self.font_name = font_opts[:name] if font_opts.key?(:name) self.font_color = font_opts[:color] if font_opts.key?(:color) self.underline = font_opts[:underline] if font_opts.key?(:underline) self.strike = font_opts[:strike] if font_opts.key?(:strike) self.vert_align = font_opts[:vert_align] if font_opts.key?(:vert_align) self.superscript = font_opts[:superscript] if font_opts.key?(:superscript) self.subscript = font_opts[:subscript] if font_opts.key?(:subscript) else self.bold = opts[:bold] if opts.key?(:bold) self.italic = opts[:italic] if opts.key?(:italic) self.font_size = opts[:font_size] if opts.key?(:font_size) self.font_name = opts[:font_name] if opts.key?(:font_name) self.font_color = opts[:font_color] if opts.key?(:font_color) self.underline = opts[:underline] if opts.key?(:underline) self.strike = opts[:strike] if opts.key?(:strike) self.vert_align = opts[:vert_align] if opts.key?(:vert_align) self.superscript = opts[:superscript] if opts.key?(:superscript) self.subscript = opts[:subscript] if opts.key?(:subscript) end if opts.key?(:fill) fill_opts = opts[:fill] || {} if fill_opts.key?(:color) self.fill_color = fill_opts[:color] else fill_pattern( fill_opts[:pattern] || "solid", fg_color: fill_opts[:fg_color], bg_color: fill_opts[:bg_color] ) end else self.fill_color = opts[:fill_color] if opts.key?(:fill_color) if opts.key?(:fill_pattern) pattern = opts[:fill_pattern] || {} fill_pattern( pattern[:pattern] || "solid", fg_color: pattern[:fg_color], bg_color: pattern[:bg_color] ) end end fill_gradient(**opts[:fill_gradient]) if opts.key?(:fill_gradient) && opts[:fill_gradient] if opts.key?(:border) border_opts = opts[:border] || {} border_all(**border_opts[:all]) if border_opts.key?(:all) && border_opts[:all] border_left(**border_opts[:left]) if border_opts.key?(:left) && border_opts[:left] border_right(**border_opts[:right]) if border_opts.key?(:right) && border_opts[:right] border_top(**border_opts[:top]) if border_opts.key?(:top) && border_opts[:top] border_bottom(**border_opts[:bottom]) if border_opts.key?(:bottom) && border_opts[:bottom] else border_all(**opts[:border_all]) if opts.key?(:border_all) && opts[:border_all] border_left(**opts[:border_left]) if opts.key?(:border_left) && opts[:border_left] border_right(**opts[:border_right]) if opts.key?(:border_right) && opts[:border_right] border_top(**opts[:border_top]) if opts.key?(:border_top) && opts[:border_top] border_bottom(**opts[:border_bottom]) if opts.key?(:border_bottom) && opts[:border_bottom] end self.number_format = opts[:number_format] if opts.key?(:number_format) if opts.key?(:alignment) align_opts = opts[:alignment] || {} self.align_horizontal = align_opts[:horizontal] if align_opts.key?(:horizontal) self.align_vertical = align_opts[:vertical] if align_opts.key?(:vertical) self.wrap_text = align_opts[:wrap_text] if align_opts.key?(:wrap_text) self.text_rotation = align_opts[:text_rotation] if align_opts.key?(:text_rotation) self.indent = align_opts[:indent] if align_opts.key?(:indent) self.shrink_to_fit = align_opts[:shrink_to_fit] if align_opts.key?(:shrink_to_fit) else self.align_horizontal = opts[:align_horizontal] if opts.key?(:align_horizontal) self.align_vertical = opts[:align_vertical] if opts.key?(:align_vertical) self.wrap_text = opts[:wrap_text] if opts.key?(:wrap_text) self.text_rotation = opts[:text_rotation] if opts.key?(:text_rotation) self.indent = opts[:indent] if opts.key?(:indent) self.shrink_to_fit = opts[:shrink_to_fit] if opts.key?(:shrink_to_fit) end if opts.key?(:protection) prot_opts = opts[:protection] || {} self.locked = prot_opts[:locked] if prot_opts.key?(:locked) self.hidden = prot_opts[:hidden] if prot_opts.key?(:hidden) else self.locked = opts[:locked] if opts.key?(:locked) self.hidden = opts[:hidden] if opts.key?(:hidden) end self end
Applies option-style definitions so callers can use add_style(name, **opts) as an alternative to block-based fluent chaining. Applies option-style definitions so callers can use add_style(name, **opts) as an alternative to block-based fluent chaining. @param opts [Hash] The styling options. @return [self] @api public
(bool value) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 577 def bold=(value) @font_props[:bold] = value ? true : false end
Sets the font to bold. @param value [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 585 def bold? @font_props[:bold] ? true : false end
Returns whether bold is enabled. @return [Boolean] @api public
() → Hash[Symbol, untyped]
(?all: Hash[Symbol, untyped]?, ?left: Hash[Symbol, untyped]?, ?right: Hash[Symbol, untyped]?, ?top: Hash[Symbol, untyped]?, ?bottom: Hash[Symbol, untyped]?, ?diagonal: Hash[Symbol, untyped]?) → void
Source
# File lib/xlsxrb/style_builder.rb, line 885 def border(all: nil, left: nil, right: nil, top: nil, bottom: nil, diagonal: nil) return @border_props if all.nil? && left.nil? && right.nil? && top.nil? && bottom.nil? && diagonal.nil? border_all(**all) if all border_left(**left) if left border_right(**right) if right border_top(**top) if top border_bottom(**bottom) if bottom border_diagonal(**diagonal) if diagonal nil end
Configures multiple border properties or returns current border properties.
@param all [Hash, nil] Border options for all sides. @param left [Hash, nil] Options for left border. @param right [Hash, nil] Options for right border. @param top [Hash, nil] Options for top border. @param bottom [Hash, nil] Options for bottom border. @param diagonal [Hash, nil] Options for diagonal border. @return [Hash, void] @api public
(Hash[Symbol, untyped] opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/style_builder.rb, line 902 def border=(opts) border(**opts) opts end
Sets border options. @param opts [Hash] @return [Hash] @api public
(?style: String | Symbol | Integer, ?color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 913 def border_all(style: "thin", color: nil) norm_style = self.class.border_style!(style) color_opt = color ? { color: resolve_color(color) } : {} @border_props[:left] = { style: norm_style, **color_opt } @border_props[:right] = { style: norm_style, **color_opt } @border_props[:top] = { style: norm_style, **color_opt } @border_props[:bottom] = { style: norm_style, **color_opt } nil end
Sets all borders. @param style [String, Symbol, Integer] The border style. @param color [String, Symbol, nil] The color. @return [void] @api public
(?style: String | Symbol | Integer, ?color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 965 def border_bottom(style: "thin", color: nil) norm_style = self.class.border_style!(style) @border_props[:bottom] = { style: norm_style, color: resolve_color(color) }.compact nil end
Sets the bottom border. @param style [String, Symbol, Integer] The border style. @param color [String, Symbol, nil] The color. @return [void] @api public
(?style: String | Symbol | Integer, ?color: String | Symbol | nil, ?up: bool, ?down: bool) → void
Source
# File lib/xlsxrb/style_builder.rb, line 980 def border_diagonal(style: "thin", color: nil, up: false, down: false) norm_style = self.class.border_style!(style) @border_props[:diagonal] = { style: norm_style, color: resolve_color(color) }.compact @border_props[:diagonal_up] = true if up @border_props[:diagonal_down] = true if down nil end
rubocop:disable Naming/MethodParameterName Sets diagonal borders. @param style [String, Symbol, Integer] The border style. @param color [String, Symbol, nil] The color. @param up [Boolean] Diagonal up. @param down [Boolean] Diagonal down. @return [void] @api public
(?style: String | Symbol | Integer, ?color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 929 def border_left(style: "thin", color: nil) norm_style = self.class.border_style!(style) @border_props[:left] = { style: norm_style, color: resolve_color(color) }.compact nil end
Sets the left border. @param style [String, Symbol, Integer] The border style. @param color [String, Symbol, nil] The color. @return [void] @api public
(?style: String | Symbol | Integer, ?color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 941 def border_right(style: "thin", color: nil) norm_style = self.class.border_style!(style) @border_props[:right] = { style: norm_style, color: resolve_color(color) }.compact nil end
Sets the right border. @param style [String, Symbol, Integer] The border style. @param color [String, Symbol, nil] The color. @return [void] @api public
(?style: String | Symbol | Integer, ?color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 953 def border_top(style: "thin", color: nil) norm_style = self.class.border_style!(style) @border_props[:top] = { style: norm_style, color: resolve_color(color) }.compact nil end
Sets the top border. @param style [String, Symbol, Integer] The border style. @param color [String, Symbol, nil] The color. @return [void] @api public
() → Hash[Symbol, untyped]
(?pattern: untyped, ?fg_color: String | Symbol | nil, ?bg_color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 829 def fill(pattern: nil, fg_color: nil, bg_color: nil) return @fill_props if pattern.nil? && fg_color.nil? && bg_color.nil? pat = pattern || "solid" fill_pattern(pat, fg_color: resolve_color(fg_color), bg_color: resolve_color(bg_color)) nil end
Configures fill properties or returns current fill properties. @param pattern [String, Symbol, nil] @param fg_color [String, Symbol, nil] @param bg_color [String, Symbol, nil] @return [Hash, void] @api public
(untyped opts) → untyped
Source
# File lib/xlsxrb/style_builder.rb, line 842 def fill=(opts) if opts.is_a?(Hash) fill(**opts) else fill(pattern: opts) end opts end
Sets fill options. @param opts [Hash, Object] @return [Object] @api public
() → String?
Source
# File lib/xlsxrb/style_builder.rb, line 804 def fill_color @fill_props[:fg_color] end
Gets a solid fill color. @return [String, nil] @api public
(String | Symbol color) → (String | Symbol)
Source
# File lib/xlsxrb/style_builder.rb, line 813 def fill_color=(color) @fill_props.delete(:gradient) @fill_props.delete(:bg_color) @fill_props[:pattern] = "solid" @fill_props[:fg_color] = resolve_color(color) color end
Sets a solid fill color. @param color [String, Symbol] @return [String, Symbol] @api public
() → Hash[Symbol, untyped]?
(?type: untyped, ?degree: Numeric | nil, ?stops: Array[untyped]?) → void
Source
# File lib/xlsxrb/style_builder.rb, line 859 def fill_gradient(type: nil, degree: nil, stops: []) return @fill_props[:gradient] if type.nil? && degree.nil? && (stops.nil? || stops.empty?) @fill_props.clear @fill_props[:gradient] = { type: type, degree: degree, stops: stops }.compact nil end
Sets or gets a gradient fill. @param type [String, nil] The gradient type. @param degree [Numeric, nil] The degree. @param stops [Array, nil] The gradient stops. @return [Hash, void, nil] @api public
() → (String | Symbol | untyped)
(?untyped pattern, ?fg_color: String | Symbol | nil, ?bg_color: String | Symbol | nil) → void
Source
# File lib/xlsxrb/style_builder.rb, line 775 def fill_pattern(pattern = nil, fg_color: nil, bg_color: nil) return @fill_props[:pattern] if pattern.nil? && fg_color.nil? && bg_color.nil? @fill_props.delete(:gradient) @fill_props[:pattern] = pattern if pattern @fill_props[:fg_color] = fg_color if fg_color @fill_props[:bg_color] = bg_color if bg_color nil end
Sets or gets the fill pattern. @param pattern [String, Symbol, nil] The pattern type. @param fg_color [String, Symbol, nil] The foreground color. @param bg_color [String, Symbol, nil] The background color. @return [String, Symbol, void, nil] @api public
(untyped pattern) → untyped
Source
# File lib/xlsxrb/style_builder.rb, line 790 def fill_pattern=(pattern) if pattern.is_a?(Hash) fill_pattern(pattern[:pattern] || "solid", fg_color: pattern[:fg_color], bg_color: pattern[:bg_color]) else @fill_props.delete(:gradient) @fill_props[:pattern] = pattern end pattern end
Sets the fill pattern. @param pattern [String, Symbol, Hash] @return [Object] @api public
() → Hash[Symbol, untyped]
(**untyped) → void
Source
# File lib/xlsxrb/style_builder.rb, line 546 def font(**opts) return @font_props if opts.empty? self.bold = opts[:bold] if opts.key?(:bold) self.italic = opts[:italic] if opts.key?(:italic) self.font_size = opts[:size] if opts.key?(:size) self.font_name = opts[:name] if opts.key?(:name) self.font_color = opts[:color] if opts.key?(:color) self.underline = opts[:underline] if opts.key?(:underline) self.strike = opts[:strike] if opts.key?(:strike) self.vert_align = opts[:vert_align] if opts.key?(:vert_align) self.superscript = opts[:superscript] if opts.key?(:superscript) self.subscript = opts[:subscript] if opts.key?(:subscript) nil end
Configures multiple font properties at once or returns font properties. @param opts [Hash] The font properties. @return [Hash, void] @api public
(Hash[Symbol, untyped] opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/style_builder.rb, line 567 def font=(opts) font(**opts) opts end
Sets font options. @param opts [Hash] @return [Hash] @api public
() → String?
Source
# File lib/xlsxrb/style_builder.rb, line 646 def font_color @font_props[:color] end
Gets the font color. @return [String, nil] @api public
(String | Symbol color) → (String | Symbol)
Source
# File lib/xlsxrb/style_builder.rb, line 655 def font_color=(color) @font_props[:color] = resolve_color(color) color end
Sets the font color. @param color [String, Symbol] @return [String, Symbol] @api public
() → String?
Source
# File lib/xlsxrb/style_builder.rb, line 629 def font_name @font_props[:name] end
Gets the font name. @return [String, nil] @api public
(String name) → String
Source
# File lib/xlsxrb/style_builder.rb, line 638 def font_name=(name) @font_props[:name] = name end
Sets the font name. @param name [String] @return [String] @api public
() → (Integer | Float | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 610 def font_size @font_props[:size] end
Gets the font size in points. @return [Integer, Float, nil] @api public
(Numeric size_value) → Numeric
Source
# File lib/xlsxrb/style_builder.rb, line 619 def font_size=(size_value) val = size_value.is_a?(Float) && (size_value % 1).zero? ? size_value.to_i : size_value @font_props[:size] = val size_value end
Sets the font size in points. @param size_value [Integer, Float] The size in points (supports fractional sizes such as 10.5). @return [Integer, Float] @api public
() → (Integer | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 1123 def indent @alignment[:indent] end
Gets text indent. @return [Integer, nil] @api public
(Numeric value) → Numeric
Source
# File lib/xlsxrb/style_builder.rb, line 1132 def indent=(value) @alignment[:indent] = value.to_i end
Sets text indent. @param value [Numeric] @return [Numeric] @api public
(StyleBuilder source) → void
Source
# File lib/xlsxrb/style_builder.rb, line 409 def initialize_copy(source) super @font_props = @font_props.dup @fill_props = @fill_props.dup @border_props = @border_props.dup @alignment = @alignment.dup @protection = @protection.dup end
Copies the option hashes, so a copy can change without affecting the original.
(bool value) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 594 def italic=(value) @font_props[:italic] = value ? true : false end
Sets the font to italic. @param value [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 602 def italic? @font_props[:italic] ? true : false end
Returns whether italic is enabled. @return [Boolean] @api public
(bool value) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1173 def locked=(value) @protection[:locked] = value ? true : false end
Sets whether the cell is locked.
@param value [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1181 def locked? @protection[:locked] != false end
Returns whether the cell is locked. @return [Boolean] @api public
() → (String | Integer | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 1209 def number_format @num_fmt_id end
Gets the number format. @return [String, Integer, nil] @api public
(String | Integer num_fmt_id) → (String | Integer)
Source
# File lib/xlsxrb/style_builder.rb, line 1218 def number_format=(num_fmt_id) @num_fmt_id = num_fmt_id end
Sets the number format. @param num_fmt_id [String, Integer] @return [String, Integer] @api public
() → Hash[Symbol, untyped]
(?locked: bool?, ?hidden: bool?) → void
Source
# File lib/xlsxrb/style_builder.rb, line 1148 def protection(locked: nil, hidden: nil) return @protection if locked.nil? && hidden.nil? self.locked = locked unless locked.nil? self.hidden = hidden unless hidden.nil? nil end
Configures or returns cell protection properties. When called with 0 arguments/keywords, returns the current protection Hash. When called with keyword arguments, merges the properties and returns nil.
@param locked [Boolean, nil] @param hidden [Boolean, nil] @return [Hash, void] @api public
(Hash[Symbol, untyped] opts) → Hash[Symbol, untyped]
Source
# File lib/xlsxrb/style_builder.rb, line 1162 def protection=(opts) protection(**opts) opts end
Sets protection options.
@param opts [Hash] @return [Hash] @api public
(untyped writer) → Integer
Source
# File lib/xlsxrb/style_builder.rb, line 1225 def register_with(writer) font_id = 0 fill_id = 0 border_id = 0 font_id = writer.add_font(**@font_props) if @font_props.any? fill_id = writer.add_fill(**@fill_props) if @fill_props.any? border_id = writer.add_border(**@border_props) if @border_props.any? resolved_num_fmt_id = if @num_fmt_id.is_a?(String) writer.add_number_format(@num_fmt_id) else @num_fmt_id end cell_style_opts = { num_fmt_id: resolved_num_fmt_id, font_id: font_id, fill_id: fill_id, border_id: border_id } cell_style_opts[:alignment] = @alignment if @alignment.any? cell_style_opts[:protection] = @protection if @protection.any? writer.add_cell_style(**cell_style_opts) end
Register this style with the given Writer, returning the style_id.
- writer
-
Xlsxrb::Ooxml::Writerinstance
(StyleBuilder other) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1258 def same_format?(other) format_signature == other.format_signature end
Returns whether other describes the same cell format, whatever the names.
@param other [StyleBuilder] @return [Boolean] @api private
(bool value) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1090 def shrink_to_fit=(value) @alignment[:shrink_to_fit] = value ? true : false end
Sets shrink to fit. @param value [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1098 def shrink_to_fit? @alignment[:shrink_to_fit] ? true : false end
Returns whether shrink to fit is enabled. @return [Boolean] @api public
(bool value) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 698 def strike=(value) @font_props[:strike] = value ? true : false end
Sets the font strikethrough. @param value [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 706 def strike? @font_props[:strike] ? true : false end
Returns whether strikethrough is enabled. @return [Boolean] @api public
(untyped val) → untyped
Source
# File lib/xlsxrb/style_builder.rb, line 752 def subscript=(val) @font_props[:vert_align] = val ? "subscript" : "baseline" val end
Sets the font to subscript. @param val [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 761 def subscript? @font_props[:vert_align] == "subscript" end
Returns whether subscript is enabled. @return [Boolean] @api public
(untyped val) → untyped
Source
# File lib/xlsxrb/style_builder.rb, line 734 def superscript=(val) @font_props[:vert_align] = val ? "superscript" : "baseline" val end
Sets the font to superscript. @param val [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 743 def superscript? @font_props[:vert_align] == "superscript" end
Returns whether superscript is enabled. @return [Boolean] @api public
() → (Numeric | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 1106 def text_rotation @alignment[:text_rotation] end
Gets text rotation. @return [Numeric, nil] @api public
(Numeric value) → Numeric
Source
# File lib/xlsxrb/style_builder.rb, line 1115 def text_rotation=(value) @alignment[:text_rotation] = value end
Sets text rotation. @param value [Numeric] @return [Numeric] @api public
() → (String | Symbol | bool | nil)
Source
# File lib/xlsxrb/style_builder.rb, line 664 def underline @font_props[:underline] end
Gets the font underline style. @return [String, Symbol, Boolean, nil] @api public
(String | Symbol | bool val) → (String | Symbol | bool)
Source
# File lib/xlsxrb/style_builder.rb, line 673 def underline=(val) mapped = if [true, "single"].include?(val) "single" elsif val == false nil else UNDERLINE_MAP[val.to_sym] || val.to_s end @font_props[:underline] = mapped val end
Sets the font underline style. @param val [String, Symbol, Boolean] @return [String, Symbol, Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 689 def underline? !@font_props[:underline].nil? && @font_props[:underline] != false end
Returns whether underline is enabled. @return [Boolean] @api public
() → String?
Source
# File lib/xlsxrb/style_builder.rb, line 714 def vert_align @font_props[:vert_align] end
Gets the vertical alignment of the font. @return [String, nil] @api public
(untyped val) → untyped
Source
# File lib/xlsxrb/style_builder.rb, line 723 def vert_align=(val) mapped = VERT_ALIGN_MAP[val.to_sym] || val.to_s @font_props[:vert_align] = mapped val end
Sets the vertical alignment of the font. @param val [String, Symbol] @return [Object] @api public
(String name, String | Integer number_format) → StyleBuilder
Source
# File lib/xlsxrb/style_builder.rb, line 425 def with_number_format(name, number_format) copy = dup copy.instance_variable_set(:@name, name) copy.number_format = number_format copy end
Returns a copy of this style named name that uses number_format.
@param name [String] Name of the copy. @param number_format [String, Integer] Number format of the copy. @return [StyleBuilder] @api private
(bool value) → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1073 def wrap_text=(value) @alignment[:wrap_text] = value ? true : false end
Sets text wrapping. @param value [Boolean] @return [Boolean] @api public
() → bool
Source
# File lib/xlsxrb/style_builder.rb, line 1081 def wrap_text? @alignment[:wrap_text] ? true : false end
Returns whether text wrapping is enabled. @return [Boolean] @api public
Protected Instance Methods
() → Array[untyped]
Source
# File lib/xlsxrb/style_builder.rb, line 1265 def format_signature [@font_props, @fill_props, @border_props, @num_fmt_id, @alignment, @protection] end