Class: Xlsxrb::StyleBuilder
- Inherits:
-
Object
- Object
- Xlsxrb::StyleBuilder
- Defined in:
- lib/xlsxrb/style_builder.rb,
sig/generated/xlsxrb/style_builder.rbs
Overview
Helper class for building cell styles with a fluent DSL. Encapsulates font, fill, border, alignment, and number format properties.
Constant Summary collapse
- COLORS =
{ black: "FF000000", white: "FFFFFFFF", red: "FFFF0000", green: "FF00FF00", blue: "FF0000FF", yellow: "FFFFFF00", cyan: "FF00FFFF", magenta: "FFFF00FF", gray: "FF808080", grey: "FF808080" }.freeze
Instance Attribute Summary collapse
- #alignment ⇒ Object readonly
- #border_props ⇒ Object readonly
- #fill_props ⇒ Object readonly
- #font_props ⇒ Object readonly
- #name ⇒ Object readonly
- #num_fmt_id ⇒ Object readonly
Instance Method Summary collapse
-
#align_horizontal(value) ⇒ self
Sets horizontal alignment.
-
#align_vertical(value) ⇒ self
Sets vertical alignment.
-
#apply_options!(**opts) ⇒ self
Applies option-style definitions so callers can use add_style(name, **opts) as an alternative to block-based fluent chaining.
-
#bold(value = true) ⇒ self
rubocop:disable Style/OptionalBooleanParameter Sets the font to bold.
-
#border(**opts) ⇒ self
Configures multiple border properties.
-
#border_all(style: "thin", color: nil) ⇒ self
Sets all borders.
-
#border_bottom(style: "thin", color: nil) ⇒ self
Sets the bottom border.
-
#border_diagonal(style: "thin", color: nil, up: false, down: false) ⇒ self
rubocop:disable Naming/MethodParameterName Sets diagonal borders.
-
#border_left(style: "thin", color: nil) ⇒ self
Sets the left border.
-
#border_right(style: "thin", color: nil) ⇒ self
Sets the right border.
-
#border_top(style: "thin", color: nil) ⇒ self
Sets the top border.
-
#fill(pattern: "solid", fg_color: nil, bg_color: nil) ⇒ self
: (?pattern: String | Symbol, ?fg_color: String | Symbol | nil, ?bg_color: String | Symbol | nil) -> self.
-
#fill_color(color) ⇒ self
Sets a solid fill color.
-
#fill_gradient(type:, degree: nil, stops: []) ⇒ self
Sets a gradient fill.
-
#fill_pattern(pattern, fg_color: nil, bg_color: nil) ⇒ self
Sets the fill pattern.
-
#font(**opts) ⇒ self
Configures multiple font properties at once.
-
#font_color(color) ⇒ self
Sets the font color.
-
#font_name(name) ⇒ self
Sets the font name.
-
#indent(value) ⇒ self
Sets text indent.
-
#initialize(name = nil) ⇒ StyleBuilder
constructor
: (?String? name) -> void.
-
#italic(value = true) ⇒ self
Sets the font to italic.
-
#number_format(num_fmt_id) ⇒ self
(also: #num_fmt)
Sets the number format.
-
#register_with(writer) ⇒ Integer
Register this style with the given Writer, returning the style_id.
- #resolve_color(color) ⇒ Object
-
#shrink_to_fit(value = true) ⇒ self
Sets shrink to fit.
-
#size(size_value) ⇒ self
Sets the font size.
-
#strike(value = true) ⇒ self
Sets the font strikethrough.
-
#text_rotation(value) ⇒ self
Sets text rotation.
-
#underline(val = "single") ⇒ self
Sets the font underline style.
-
#vert_align(value) ⇒ self
Sets the vertical alignment of the font.
-
#wrap_text(value = true) ⇒ self
rubocop:disable Style/OptionalBooleanParameter Sets text wrapping.
Constructor Details
#initialize(name = nil) ⇒ StyleBuilder
: (?String? name) -> void
34 35 36 37 38 39 40 41 |
# File 'lib/xlsxrb/style_builder.rb', line 34 def initialize(name = nil) @name = name @font_props = {} @fill_props = {} @border_props = {} @num_fmt_id = nil @alignment = {} end |
Instance Attribute Details
#alignment ⇒ Object (readonly)
43 44 45 |
# File 'lib/xlsxrb/style_builder.rb', line 43 def alignment @alignment end |
#border_props ⇒ Object (readonly)
43 44 45 |
# File 'lib/xlsxrb/style_builder.rb', line 43 def border_props @border_props end |
#fill_props ⇒ Object (readonly)
43 44 45 |
# File 'lib/xlsxrb/style_builder.rb', line 43 def fill_props @fill_props end |
#font_props ⇒ Object (readonly)
43 44 45 |
# File 'lib/xlsxrb/style_builder.rb', line 43 def font_props @font_props end |
#name ⇒ Object (readonly)
43 44 45 |
# File 'lib/xlsxrb/style_builder.rb', line 43 def name @name end |
#num_fmt_id ⇒ Object (readonly)
43 44 45 |
# File 'lib/xlsxrb/style_builder.rb', line 43 def num_fmt_id @num_fmt_id end |
Instance Method Details
#align_horizontal(value) ⇒ self
Sets horizontal alignment. : (String | Symbol) -> self
390 391 392 393 |
# File 'lib/xlsxrb/style_builder.rb', line 390 def align_horizontal(value) @alignment[:horizontal] = value self end |
#align_vertical(value) ⇒ self
Sets vertical alignment. : (String | Symbol) -> self
400 401 402 403 |
# File 'lib/xlsxrb/style_builder.rb', line 400 def align_vertical(value) @alignment[:vertical] = value self end |
#apply_options!(**opts) ⇒ self
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. : (**untyped) -> self
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/xlsxrb/style_builder.rb', line 53 def (**opts) if opts.key?(:font) font_opts = opts[:font] || {} bold(font_opts[:bold]) if font_opts.key?(:bold) italic(font_opts[:italic]) if font_opts.key?(:italic) size(font_opts[:size]) if font_opts.key?(:size) font_name(font_opts[:name]) if font_opts.key?(:name) font_color(font_opts[:color]) if font_opts.key?(:color) underline(font_opts[:underline]) if font_opts.key?(:underline) strike(font_opts[:strike]) if font_opts.key?(:strike) vert_align(font_opts[:vert_align]) if font_opts.key?(:vert_align) else 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) end if opts.key?(:fill) fill_opts = opts[:fill] || {} if fill_opts.key?(:color) 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 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 number_format(opts[:number_format]) if opts.key?(:number_format) if opts.key?(:alignment) align_opts = opts[:alignment] || {} align_horizontal(align_opts[:horizontal]) if align_opts.key?(:horizontal) align_vertical(align_opts[:vertical]) if align_opts.key?(:vertical) wrap_text(align_opts[:wrap_text]) if align_opts.key?(:wrap_text) text_rotation(align_opts[:text_rotation]) if align_opts.key?(:text_rotation) indent(align_opts[:indent]) if align_opts.key?(:indent) shrink_to_fit(align_opts[:shrink_to_fit]) if align_opts.key?(:shrink_to_fit) else 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) end self end |
#bold(value = true) ⇒ self
rubocop:disable Style/OptionalBooleanParameter Sets the font to bold. : (?bool) -> self
161 162 163 164 |
# File 'lib/xlsxrb/style_builder.rb', line 161 def bold(value = true) @font_props[:bold] = value self end |
#border(**opts) ⇒ self
Configures multiple border properties. : (**untyped) -> self
298 299 300 301 302 303 304 305 |
# File 'lib/xlsxrb/style_builder.rb', line 298 def border(**opts) border_all(**opts[:all]) if opts.key?(:all) && opts[:all] border_left(**opts[:left]) if opts.key?(:left) && opts[:left] border_right(**opts[:right]) if opts.key?(:right) && opts[:right] border_top(**opts[:top]) if opts.key?(:top) && opts[:top] border_bottom(**opts[:bottom]) if opts.key?(:bottom) && opts[:bottom] self end |
#border_all(style: "thin", color: nil) ⇒ self
Sets all borders. : (?style: String | Symbol, ?color: String | Symbol | nil) -> self
313 314 315 316 317 318 319 320 |
# File 'lib/xlsxrb/style_builder.rb', line 313 def border_all(style: "thin", color: nil) color_opt = color ? { color: resolve_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_bottom(style: "thin", color: nil) ⇒ self
Sets the bottom border. : (?style: String | Symbol, ?color: String | Symbol | nil) -> self
361 362 363 364 |
# File 'lib/xlsxrb/style_builder.rb', line 361 def border_bottom(style: "thin", color: nil) @border_props[:bottom] = { style: style, color: resolve_color(color) }.compact self end |
#border_diagonal(style: "thin", color: nil, up: false, down: false) ⇒ self
rubocop:disable Naming/MethodParameterName Sets diagonal borders. : (?style: String | Symbol, ?color: String | Symbol | nil, ?up: bool, ?down: bool) -> self
375 376 377 378 379 380 |
# File 'lib/xlsxrb/style_builder.rb', line 375 def border_diagonal(style: "thin", color: nil, up: false, down: false) @border_props[:diagonal] = { style: style, color: resolve_color(color) }.compact @border_props[:diagonal_up] = true if up @border_props[:diagonal_down] = true if down self end |
#border_left(style: "thin", color: nil) ⇒ self
Sets the left border. : (?style: String | Symbol, ?color: String | Symbol | nil) -> self
328 329 330 331 |
# File 'lib/xlsxrb/style_builder.rb', line 328 def border_left(style: "thin", color: nil) @border_props[:left] = { style: style, color: resolve_color(color) }.compact self end |
#border_right(style: "thin", color: nil) ⇒ self
Sets the right border. : (?style: String | Symbol, ?color: String | Symbol | nil) -> self
339 340 341 342 |
# File 'lib/xlsxrb/style_builder.rb', line 339 def border_right(style: "thin", color: nil) @border_props[:right] = { style: style, color: resolve_color(color) }.compact self end |
#border_top(style: "thin", color: nil) ⇒ self
Sets the top border. : (?style: String | Symbol, ?color: String | Symbol | nil) -> self
350 351 352 353 |
# File 'lib/xlsxrb/style_builder.rb', line 350 def border_top(style: "thin", color: nil) @border_props[:top] = { style: style, color: resolve_color(color) }.compact self end |
#fill(pattern: "solid", fg_color: nil, bg_color: nil) ⇒ self
: (?pattern: String | Symbol, ?fg_color: String | Symbol | nil, ?bg_color: String | Symbol | nil) -> self
271 272 273 |
# File 'lib/xlsxrb/style_builder.rb', line 271 def fill(pattern: "solid", fg_color: nil, bg_color: nil) fill_pattern(pattern, fg_color: resolve_color(fg_color), bg_color: resolve_color(bg_color)) end |
#fill_color(color) ⇒ self
Sets a solid fill color. : (String | Symbol) -> self
264 265 266 267 268 |
# File 'lib/xlsxrb/style_builder.rb', line 264 def fill_color(color) @fill_props[:pattern] = "solid" @fill_props[:fg_color] = resolve_color(color) self end |
#fill_gradient(type:, degree: nil, stops: []) ⇒ self
Sets a gradient fill. : (type: String, ?degree: Numeric | nil, ?stops: Array) -> self
282 283 284 285 286 287 288 289 |
# File 'lib/xlsxrb/style_builder.rb', line 282 def fill_gradient(type:, degree: nil, stops: []) @fill_props[:gradient] = { type: type, degree: degree, stops: stops }.compact self end |
#fill_pattern(pattern, fg_color: nil, bg_color: nil) ⇒ self
Sets the fill pattern. Configures fill properties. : (String | Symbol pattern, ?fg_color: String | Symbol | nil, ?bg_color: String | Symbol | nil) -> self
252 253 254 255 256 257 |
# File 'lib/xlsxrb/style_builder.rb', line 252 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 |
#font(**opts) ⇒ self
Configures multiple font properties at once. : (**untyped) -> self
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/xlsxrb/style_builder.rb', line 143 def font(**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[:name]) if opts.key?(:name) font_color(opts[:color]) if opts.key?(: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) self end |
#font_color(color) ⇒ self
Sets the font color. : (String | Symbol) -> self
201 202 203 204 |
# File 'lib/xlsxrb/style_builder.rb', line 201 def font_color(color) @font_props[:color] = resolve_color(color) self end |
#font_name(name) ⇒ self
Sets the font name. : (String) -> self
191 192 193 194 |
# File 'lib/xlsxrb/style_builder.rb', line 191 def font_name(name) @font_props[:name] = name self end |
#indent(value) ⇒ self
Sets text indent. : (Numeric) -> self
442 443 444 445 |
# File 'lib/xlsxrb/style_builder.rb', line 442 def indent(value) @alignment[:indent] = value.to_i self end |
#italic(value = true) ⇒ self
Sets the font to italic. : (?bool) -> self
171 172 173 174 |
# File 'lib/xlsxrb/style_builder.rb', line 171 def italic(value = true) @font_props[:italic] = value self end |
#number_format(num_fmt_id) ⇒ self Also known as: num_fmt
Sets the number format. : (String | Integer) -> self
454 455 456 457 |
# File 'lib/xlsxrb/style_builder.rb', line 454 def number_format(num_fmt_id) @num_fmt_id = num_fmt_id self end |
#register_with(writer) ⇒ Integer
Register this style with the given Writer, returning the style_id.
- writer
Xlsxrb::Ooxml::Writer instance
: (untyped writer) -> Integer
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 |
# File 'lib/xlsxrb/style_builder.rb', line 463 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 |
#resolve_color(color) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/xlsxrb/style_builder.rb', line 23 def resolve_color(color) return nil unless color 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] || color.to_s end color.to_s end |
#shrink_to_fit(value = true) ⇒ self
Sets shrink to fit. : (?bool) -> self
421 422 423 424 |
# File 'lib/xlsxrb/style_builder.rb', line 421 def shrink_to_fit(value = true) @alignment[:shrink_to_fit] = value self end |
#size(size_value) ⇒ self
Sets the font size. : (Numeric) -> self
181 182 183 184 |
# File 'lib/xlsxrb/style_builder.rb', line 181 def size(size_value) @font_props[:sz] = size_value.to_i self end |
#strike(value = true) ⇒ self
Sets the font strikethrough. : (?bool) -> self
221 222 223 224 |
# File 'lib/xlsxrb/style_builder.rb', line 221 def strike(value = true) @font_props[:strike] = value self end |
#text_rotation(value) ⇒ self
Sets text rotation. : (Numeric) -> self
432 433 434 435 |
# File 'lib/xlsxrb/style_builder.rb', line 432 def text_rotation(value) @alignment[:text_rotation] = value self end |
#underline(val = "single") ⇒ self
Sets the font underline style. : (?String) -> self
211 212 213 214 |
# File 'lib/xlsxrb/style_builder.rb', line 211 def underline(val = "single") @font_props[:underline] = val self end |
#vert_align(value) ⇒ self
Sets the vertical alignment of the font. : (String) -> self
231 232 233 234 |
# File 'lib/xlsxrb/style_builder.rb', line 231 def vert_align(value) @font_props[:vert_align] = value self end |
#wrap_text(value = true) ⇒ self
rubocop:disable Style/OptionalBooleanParameter Sets text wrapping. : (?bool) -> self
411 412 413 414 |
# File 'lib/xlsxrb/style_builder.rb', line 411 def wrap_text(value = true) @alignment[:wrap_text] = value self end |