class Xlsxrb::StyleBuilder
Helper class for building cell styles with a fluent DSL. Encapsulates font, fill, border, alignment, and number format properties.
Attributes
Public Class Methods
Source
# File lib/xlsxrb/style_builder.rb, line 7 def initialize(name = nil) @name = name @font_props = {} @fill_props = {} @border_props = {} @num_fmt_id = nil @alignment = {} end
Public Instance Methods
Source
# File lib/xlsxrb/style_builder.rb, line 173 def align_horizontal(value) @alignment[:horizontal] = value self end
— Alignment Properties —
Source
# File lib/xlsxrb/style_builder.rb, line 178 def align_vertical(value) @alignment[:vertical] = value self end
Source
# File lib/xlsxrb/style_builder.rb, line 20 def apply_options!(**opts) bold(opts[:bold]) if opts.key?(:bold) italic(opts[:italic]) if opts.key?(:italic) size(opts[:size]) if opts.key?(:size) font_name(opts[:font_name]) if opts.key?(:font_name) font_color(opts[:font_color]) if opts.key?(:font_color) underline(opts[:underline]) if opts.key?(:underline) strike(opts[:strike]) if opts.key?(:strike) vert_align(opts[:vert_align]) if opts.key?(:vert_align) 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 fill_gradient(**opts[:fill_gradient]) if opts.key?(:fill_gradient) && opts[:fill_gradient] 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] number_format(opts[:number_format]) if opts.key?(:number_format) align_horizontal(opts[:align_horizontal]) if opts.key?(:align_horizontal) align_vertical(opts[:align_vertical]) if opts.key?(:align_vertical) wrap_text(opts[:wrap_text]) if opts.key?(:wrap_text) text_rotation(opts[:text_rotation]) if opts.key?(:text_rotation) indent(opts[:indent]) if opts.key?(:indent) shrink_to_fit(opts[:shrink_to_fit]) if opts.key?(:shrink_to_fit) self end
Applies option-style definitions so callers can use add_style(name, **opts) as an alternative to block-based fluent chaining.
Source
# File lib/xlsxrb/style_builder.rb, line 62 def bold(value = true) @font_props[:bold] = value self end
rubocop:disable Style/OptionalBooleanParameter
# File lib/xlsxrb/style_builder.rb, line 133 def border_all(style: "thin", color: nil) color_opt = color ? { color: color } : {} @border_props[:left] = { style: style, **color_opt } @border_props[:right] = { style: style, **color_opt } @border_props[:top] = { style: style, **color_opt } @border_props[:bottom] = { style: style, **color_opt } self end
— Border Properties —
# File lib/xlsxrb/style_builder.rb, line 157 def border_bottom(style: "thin", color: nil) @border_props[:bottom] = { style: style, color: color }.compact self end
# File lib/xlsxrb/style_builder.rb, line 163 def border_diagonal(style: "thin", color: nil, up: false, down: false) @border_props[:diagonal] = { style: style, color: color }.compact @border_props[:diagonal_up] = true if up @border_props[:diagonal_down] = true if down self end
rubocop:disable Naming/MethodParameterName
# File lib/xlsxrb/style_builder.rb, line 142 def border_left(style: "thin", color: nil) @border_props[:left] = { style: style, color: color }.compact self end
# File lib/xlsxrb/style_builder.rb, line 147 def border_right(style: "thin", color: nil) @border_props[:right] = { style: style, color: color }.compact self end
# File lib/xlsxrb/style_builder.rb, line 152 def border_top(style: "thin", color: nil) @border_props[:top] = { style: style, color: color }.compact self end
# File lib/xlsxrb/style_builder.rb, line 118 def fill(pattern: "solid", fg_color: nil, bg_color: nil) fill_pattern(pattern, fg_color: fg_color, bg_color: bg_color) end
Source
# File lib/xlsxrb/style_builder.rb, line 112 def fill_color(color) @fill_props[:pattern] = "solid" @fill_props[:fg_color] = color self end
# File lib/xlsxrb/style_builder.rb, line 122 def fill_gradient(type:, degree: nil, stops: []) @fill_props[:gradient] = { type: type, degree: degree, stops: stops }.compact self end
# File lib/xlsxrb/style_builder.rb, line 105 def fill_pattern(pattern, fg_color: nil, bg_color: nil) @fill_props[:pattern] = pattern @fill_props[:fg_color] = fg_color if fg_color @fill_props[:bg_color] = bg_color if bg_color self end
— Fill Properties —
Source
# File lib/xlsxrb/style_builder.rb, line 82 def font_color(color) @font_props[:color] = color self end
Source
# File lib/xlsxrb/style_builder.rb, line 77 def font_name(name) @font_props[:name] = name self end
Source
# File lib/xlsxrb/style_builder.rb, line 200 def indent(value) @alignment[:indent] = value.to_i self end
Source
# File lib/xlsxrb/style_builder.rb, line 67 def italic(value = true) @font_props[:italic] = value self end
Source
# File lib/xlsxrb/style_builder.rb, line 207 def number_format(num_fmt_id) @num_fmt_id = num_fmt_id self end
— Number Format —
Also aliased as: num_fmt
Source
# File lib/xlsxrb/style_builder.rb, line 215 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? writer.add_cell_style(**cell_style_opts) end
Register this style with the given Writer, returning the style_id.
- writer
-
Xlsxrb::Ooxml::Writerinstance
Source
# File lib/xlsxrb/style_builder.rb, line 189 def shrink_to_fit(value = true) @alignment[:shrink_to_fit] = value self end
Source
# File lib/xlsxrb/style_builder.rb, line 72 def size(size_value) @font_props[:sz] = size_value.to_i self end
Source
# File lib/xlsxrb/style_builder.rb, line 92 def strike(value = true) @font_props[:strike] = value self end
Source
# File lib/xlsxrb/style_builder.rb, line 195 def text_rotation(value) @alignment[:text_rotation] = value self end
rubocop:enable Style/OptionalBooleanParameter
Source
# File lib/xlsxrb/style_builder.rb, line 87 def underline(val = "single") @font_props[:underline] = val self end
Source
# File lib/xlsxrb/style_builder.rb, line 97 def vert_align(value) @font_props[:vert_align] = value self end
Source
# File lib/xlsxrb/style_builder.rb, line 184 def wrap_text(value = true) @alignment[:wrap_text] = value self end
rubocop:disable Style/OptionalBooleanParameter