Class: Writexlsx::Format

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/write_xlsx/format.rb

Constant Summary

Constants included from Utility

Utility::CHAR_WIDTHS, Utility::COL_MAX, Utility::DEFAULT_COL_PIXELS, Utility::MAX_DIGIT_WIDTH, Utility::PADDING, Utility::PERL_TRUE_VALUES, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #convert_date_time, #convert_font_args, #dash_types, delete_files, #escape_url, #fill_properties, #float_to_str, #get_font_latin_attributes, #get_font_style_attributes, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color_from_index, #params_to_font, #pattern_properties, #pixels_to_points, #ptrue?, #put_deprecate_message, #quote_sheetname, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_a_body_pr, #write_a_def_rpr, #write_a_end_para_rpr, #write_a_lst_style, #write_a_p_formula, #write_a_p_pr_formula, #write_a_solid_fill, #write_a_srgb_clr, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_def_rpr_r_pr_common, #write_div, #write_fill, #write_stroke, #write_tx_pr, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xl_string_pixel_width, #xml_str

Constructor Details

#initialize(formats, params = {}) ⇒ Format

:nodoc:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
# File 'lib/write_xlsx/format.rb', line 22

def initialize(formats, params = {})   # :nodoc:
  @formats = formats

  @xf_index       = nil
  @dxf_index      = nil

  @num_format = 'General'
  @num_format_index = 0
  @font_index     = 0
  @font           = 'Calibri'
  @size           = 11
  @bold           = 0
  @italic         = 0
  @color          = 0x0
  @underline      = 0
  @font_strikeout = 0
  @font_outline   = 0
  @font_shadow    = 0
  @font_script    = 0
  @font_family    = 2
  @font_charset   = 0
  @font_scheme    = 'minor'
  @font_condense  = 0
  @font_extend    = 0
  @theme          = 0
  @hyperlink      = 0
  @xf_id          = 0

  @hidden         = 0
  @locked         = 1

  @text_h_align   = 0
  @text_wrap      = 0
  @text_v_align   = 0
  @text_justlast  = 0
  @rotation       = 0

  @fg_color       = 0x00
  @bg_color       = 0x00
  @pattern        = 0
  @fill_index     = 0
  @fill_count     = 0

  @border_index   = 0
  @border_count   = 0

  @bottom         = 0
  @bottom_color   = 0x0
  @diag_border    = 0
  @diag_color     = 0x0
  @diag_type      = 0
  @left           = 0
  @left_color     = 0x0
  @right          = 0
  @right_color    = 0x0
  @top            = 0
  @top_color      = 0x0

  @indent         = 0
  @shrink         = 0
  @merge_range    = 0
  @reading_order  = 0
  @just_distrib   = 0
  @color_indexed  = 0
  @font_only      = 0
  @quote_prefix   = 0

  set_format_properties(params) unless params.empty?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

:nodoc:



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/write_xlsx/format.rb', line 470

def method_missing(name, *args)  # :nodoc:
  method = "#{name}"

  # Check for a valid method names, i.e. "set_xxx_yyy".
  method =~ /set_(\w+)/ or raise "Unknown method: #{method}\n"

  # Match the attribute, i.e. "@xxx_yyy".
  attribute = "@#{::Regexp.last_match(1)}"

  # Check that the attribute exists
  # ........
  value = if method =~ /set\w+color$/    # for "set_property_color" methods
            color(args[0])
          else                            # for "set_xxx" methods
            args[0] || 1
          end

  instance_variable_set(attribute, value)
end

Instance Attribute Details

#bg_colorObject

:nodoc:



17
18
19
# File 'lib/write_xlsx/format.rb', line 17

def bg_color
  @bg_color
end

#boldObject (readonly)

:nodoc:



20
21
22
# File 'lib/write_xlsx/format.rb', line 20

def bold
  @bold
end

#border_indexObject

:nodoc:



15
16
17
# File 'lib/write_xlsx/format.rb', line 15

def border_index
  @border_index
end

#bottomObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def bottom
  @bottom
end

#bottom_colorObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def bottom_color
  @bottom_color
end

#color_indexedObject (readonly)

:nodoc:



12
13
14
# File 'lib/write_xlsx/format.rb', line 12

def color_indexed
  @color_indexed
end

#diag_borderObject

:nodoc:



16
17
18
# File 'lib/write_xlsx/format.rb', line 16

def diag_border
  @diag_border
end

#diag_colorObject (readonly)

:nodoc:



12
13
14
# File 'lib/write_xlsx/format.rb', line 12

def diag_color
  @diag_color
end

#diag_typeObject (readonly)

:nodoc:



12
13
14
# File 'lib/write_xlsx/format.rb', line 12

def diag_type
  @diag_type
end

#dxf_bg_colorObject

:nodoc:



19
20
21
# File 'lib/write_xlsx/format.rb', line 19

def dxf_bg_color
  @dxf_bg_color
end

#dxf_fg_colorObject

:nodoc:



19
20
21
# File 'lib/write_xlsx/format.rb', line 19

def dxf_fg_color
  @dxf_fg_color
end

#dxf_indexObject (readonly)

:nodoc:



10
11
12
# File 'lib/write_xlsx/format.rb', line 10

def dxf_index
  @dxf_index
end

#fg_colorObject

:nodoc:



17
18
19
# File 'lib/write_xlsx/format.rb', line 17

def fg_color
  @fg_color
end

#fill_indexObject

:nodoc:



16
17
18
# File 'lib/write_xlsx/format.rb', line 16

def fill_index
  @fill_index
end

#fontObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def font
  @font
end

#font_condenseObject

:nodoc:



16
17
18
# File 'lib/write_xlsx/format.rb', line 16

def font_condense
  @font_condense
end

#font_extendObject

:nodoc:



16
17
18
# File 'lib/write_xlsx/format.rb', line 16

def font_extend
  @font_extend
end

#font_familyObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def font_family
  @font_family
end

#font_indexObject

:nodoc:



15
16
17
# File 'lib/write_xlsx/format.rb', line 15

def font_index
  @font_index
end

#font_onlyObject (readonly)

:nodoc:



12
13
14
# File 'lib/write_xlsx/format.rb', line 12

def font_only
  @font_only
end

#font_schemeObject (readonly)

:nodoc:



14
15
16
# File 'lib/write_xlsx/format.rb', line 14

def font_scheme
  @font_scheme
end

#font_scriptObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def font_script
  @font_script
end

#font_strikeoutObject (readonly)

:nodoc:



20
21
22
# File 'lib/write_xlsx/format.rb', line 20

def font_strikeout
  @font_strikeout
end

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def hyperlink
  @hyperlink
end

#italicObject (readonly)

:nodoc:



20
21
22
# File 'lib/write_xlsx/format.rb', line 20

def italic
  @italic
end

#leftObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def left
  @left
end

#left_colorObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def left_color
  @left_color
end

#num_formatObject (readonly)

:nodoc:



10
11
12
# File 'lib/write_xlsx/format.rb', line 10

def num_format
  @num_format
end

#num_format_indexObject

:nodoc:



15
16
17
# File 'lib/write_xlsx/format.rb', line 15

def num_format_index
  @num_format_index
end

#patternObject

:nodoc:



17
18
19
# File 'lib/write_xlsx/format.rb', line 17

def pattern
  @pattern
end

#quote_prefixObject

:nodoc:



15
16
17
# File 'lib/write_xlsx/format.rb', line 15

def quote_prefix
  @quote_prefix
end

#rightObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def right
  @right
end

#right_colorObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def right_color
  @right_color
end

#rotationObject (readonly)

:nodoc:



20
21
22
# File 'lib/write_xlsx/format.rb', line 20

def rotation
  @rotation
end

#sizeObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def size
  @size
end

#themeObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def theme
  @theme
end

#topObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def top
  @top
end

#top_colorObject (readonly)

:nodoc:



13
14
15
# File 'lib/write_xlsx/format.rb', line 13

def top_color
  @top_color
end

#underlineObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def underline
  @underline
end

#xf_idObject (readonly)

:nodoc:



11
12
13
# File 'lib/write_xlsx/format.rb', line 11

def xf_id
  @xf_id
end

#xf_indexObject (readonly)

:nodoc:



10
11
12
# File 'lib/write_xlsx/format.rb', line 10

def xf_index
  @xf_index
end

Class Method Details

.color(color_code) ⇒ Object

Used in conjunction with the set_xxx_color methods to convert a color string into a number. Color range is 0..63 but we will restrict it to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15.



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/write_xlsx/format.rb', line 313

def self.color(color_code)
  colors = Colors::COLORS

  # Return the default color if nil,
  return 0x00 unless color_code

  if color_code.respond_to?(:to_str)
    # Return RGB style colors for processing later.
    return color_code if color_code =~ /^#[0-9A-F]{6}$/i

    # or the color string converted to an integer,
    return colors[color_code.downcase.to_sym] if colors[color_code.downcase.to_sym]

    # or the default color if string is unrecognised,
    0x00 if color_code =~ /\D/
  else
    # or an index < 8 mapped into the correct range,
    return color_code + 8 if color_code < 8

    # or the default color if arg is outside range,
    return 0x00 if color_code > 63

    # or an integer in the valid range
    color_code
  end
end

Instance Method Details

#[](attr) ⇒ Object



566
567
568
# File 'lib/write_xlsx/format.rb', line 566

def [](attr)
  instance_variable_get("@#{attr}")
end

#bold?Boolean

Returns:

  • (Boolean)


494
495
496
# File 'lib/write_xlsx/format.rb', line 494

def bold?
  ptrue?(@bold)
end

#border_attributesObject



621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/write_xlsx/format.rb', line 621

def border_attributes
  attributes = []

  # Diagonal borders add attributes to the <border> element.
  if diag_type == 1
    attributes << ['diagonalUp',   1]
  elsif diag_type == 2
    attributes << ['diagonalDown', 1]
  elsif diag_type == 3
    attributes << ['diagonalUp',   1]
    attributes << ['diagonalDown', 1]
  end
  attributes
end

#color(color_code) ⇒ Object



304
305
306
# File 'lib/write_xlsx/format.rb', line 304

def color(color_code)
  Format.color(color_code)
end

#color?Boolean

Returns:

  • (Boolean)


490
491
492
# File 'lib/write_xlsx/format.rb', line 490

def color?
  ptrue?(@color)
end

#copy(other) ⇒ Object

Copy the attributes of another Format object.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/write_xlsx/format.rb', line 95

def copy(other)
  reserve = %i[
    xf_index
    dxf_index
    xdf_format_indices
    palette
  ]
  (instance_variables - reserve).each do |v|
    instance_variable_set(v, other.instance_variable_get(v))
  end
end

#force_text_format?Boolean

Returns:

  • (Boolean)


662
663
664
# File 'lib/write_xlsx/format.rb', line 662

def force_text_format?
  @num_format == 49 # Text format ('@')
end

#get_align_propertiesObject

Return properties for an Style xf <alignment> sub-element.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/write_xlsx/format.rb', line 138

def get_align_properties
  align = []    # Attributes to return

  # Check if any alignment options in the format have been changed.
  if @text_h_align != 0 || @text_v_align != 0 || @indent != 0 ||
     @rotation != 0 || @text_wrap != 0 || @shrink != 0 || @reading_order != 0
    changed = 1
  else
    return
  end

  # Indent is only allowed for some alignment properties. If it is defined
  # for any other alignment or no alignment has been set then default to
  # left alignment.
  @text_h_align = 1 if @indent != 0 && ![1, 3, 7].include?(@text_h_align) && ![1, 3, 5].include?(@text_v_align)

  # Check for properties that are mutually exclusive.
  @shrink       = 0 if @text_wrap != 0
  @shrink       = 0 if @text_h_align == 4    # Fill
  @shrink       = 0 if @text_h_align == 5    # Justify
  @shrink       = 0 if @text_h_align == 7    # Distributed
  @just_distrib = 0 if @text_h_align != 7    # Distributed
  @just_distrib = 0 if @indent != 0

  continuous = 'centerContinuous'

  align << %w[horizontal left]        if @text_h_align == 1
  align << %w[horizontal center]      if @text_h_align == 2
  align << %w[horizontal right]       if @text_h_align == 3
  align << %w[horizontal fill]        if @text_h_align == 4
  align << %w[horizontal justify]     if @text_h_align == 5
  align << ['horizontal', continuous] if @text_h_align == 6
  align << %w[horizontal distributed] if @text_h_align == 7

  align << ['justifyLastLine', 1] if @just_distrib != 0

  # Property 'vertical' => 'bottom' is a default. It sets applyAlignment
  # without an alignment sub-element.
  align << %w[vertical top]         if @text_v_align == 1
  align << %w[vertical center]      if @text_v_align == 2
  align << %w[vertical justify]     if @text_v_align == 4
  align << %w[vertical distributed] if @text_v_align == 5

  align << ['textRotation', @rotation] if @rotation != 0
  align << ['indent',       @indent]   if @indent   != 0

  align << ['wrapText',     1] if @text_wrap != 0
  align << ['shrinkToFit',  1] if @shrink    != 0

  align << ['readingOrder', 1] if @reading_order == 1
  align << ['readingOrder', 2] if @reading_order == 2

  [changed, align]
end

#get_alignment_keyObject

Returns a unique hash key for alignment formats.



274
275
276
# File 'lib/write_xlsx/format.rb', line 274

def get_alignment_key
  [@text_h_align, @text_v_align, @indent, @rotation, @text_wrap, @shrink, @reading_order].join(':')
end

#get_border_keyObject

Returns a unique hash key for a border style. Used by Workbook.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/write_xlsx/format.rb', line 244

def get_border_key
  [
    @bottom,
    @bottom_color,
    @diag_border,
    @diag_color,
    @diag_type,
    @left,
    @left_color,
    @right,
    @right_color,
    @top,
    @top_color
  ].join(':')
end

#get_dxf_indexObject

Returns the index used by Worksheet->_XF()



294
295
296
297
298
299
300
301
302
# File 'lib/write_xlsx/format.rb', line 294

def get_dxf_index
  if @dxf_index
    @dxf_index
  elsif @formats.dxf_index_by_key(get_format_key)
    @formats.dxf_index_by_key(get_format_key)
  else
    @dxf_index = @formats.set_dxf_index_by_key(get_format_key)
  end
end

#get_fill_keyObject

Returns a unique hash key for a fill style. Used by Workbook.



263
264
265
266
267
268
269
# File 'lib/write_xlsx/format.rb', line 263

def get_fill_key
  [
    @pattern,
    @bg_color,
    @fg_color
  ].join(':')
end

#get_font_keyObject

Returns a unique hash key for a font. Used by Workbook.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/write_xlsx/format.rb', line 223

def get_font_key
  [
    @bold,
    @color,
    @font_charset,
    @font_family,
    @font_outline,
    @font_script,
    @font_shadow,
    @font_strikeout,
    @font,
    @italic,
    @size,
    @underline,
    @theme
  ].join(':')
end

#get_format_keyObject

Returns a unique hash key for the Format object.



216
217
218
# File 'lib/write_xlsx/format.rb', line 216

def get_format_key
  [get_font_key, get_border_key, get_fill_key, get_alignment_key, @num_format, @locked, @hidden, @quote_prefix].join(':')
end

#get_protection_propertiesObject

Return properties for an Excel XML <Protection> element.



196
197
198
199
200
201
202
203
# File 'lib/write_xlsx/format.rb', line 196

def get_protection_properties
  attributes = []

  attributes << ['locked', 0] unless ptrue?(@locked)
  attributes << ['hidden', 1] if     ptrue?(@hidden)

  attributes.empty? ? nil : attributes
end

#get_xf_indexObject

Returns the index used by Worksheet->_XF()



281
282
283
284
285
286
287
288
289
# File 'lib/write_xlsx/format.rb', line 281

def get_xf_index
  if @xf_index
    @xf_index
  elsif @formats.xf_index_by_key(get_format_key)
    @formats.xf_index_by_key(get_format_key)
  else
    @xf_index = @formats.set_xf_index_by_key(get_format_key)
  end
end

#has_border(flag) ⇒ Object



518
519
520
# File 'lib/write_xlsx/format.rb', line 518

def has_border(flag)
  @has_border = flag
end

#has_border?Boolean

:nodoc:

Returns:

  • (Boolean)


522
523
524
# File 'lib/write_xlsx/format.rb', line 522

def has_border? # :nodoc:
  @has_border
end

#has_dxf_border(flag) ⇒ Object



526
527
528
# File 'lib/write_xlsx/format.rb', line 526

def has_dxf_border(flag)
  @has_dxf_border = flag
end

#has_dxf_border?Boolean

Returns:

  • (Boolean)


530
531
532
# File 'lib/write_xlsx/format.rb', line 530

def has_dxf_border?
  @has_dxf_border
end

#has_dxf_fill(flag) ⇒ Object



558
559
560
# File 'lib/write_xlsx/format.rb', line 558

def has_dxf_fill(flag)
  @has_dxf_fill = flag
end

#has_dxf_fill?Boolean

Returns:

  • (Boolean)


562
563
564
# File 'lib/write_xlsx/format.rb', line 562

def has_dxf_fill?
  @has_dxf_fill
end

#has_dxf_font(flag) ⇒ Object



542
543
544
# File 'lib/write_xlsx/format.rb', line 542

def has_dxf_font(flag)
  @has_dxf_font = flag
end

#has_dxf_font?Boolean

Returns:

  • (Boolean)


546
547
548
# File 'lib/write_xlsx/format.rb', line 546

def has_dxf_font?
  @has_dxf_font
end

#has_fill(flag) ⇒ Object



550
551
552
# File 'lib/write_xlsx/format.rb', line 550

def has_fill(flag)
  @has_fill = flag
end

#has_fill?Boolean

Returns:

  • (Boolean)


554
555
556
# File 'lib/write_xlsx/format.rb', line 554

def has_fill?
  @has_fill
end

#has_font(flag) ⇒ Object



534
535
536
# File 'lib/write_xlsx/format.rb', line 534

def has_font(flag)
  @has_font = flag
end

#has_font?Boolean

Returns:

  • (Boolean)


538
539
540
# File 'lib/write_xlsx/format.rb', line 538

def has_font?
  @has_font
end

#inspectObject



209
210
211
# File 'lib/write_xlsx/format.rb', line 209

def inspect
  to_s
end

#italic?Boolean

Returns:

  • (Boolean)


498
499
500
# File 'lib/write_xlsx/format.rb', line 498

def italic?
  ptrue?(@italic)
end

#outline?Boolean

Returns:

  • (Boolean)


506
507
508
# File 'lib/write_xlsx/format.rb', line 506

def outline?
  ptrue?(@font_outline)
end

#set_align(location) ⇒ Object

Set cell alignment.



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/write_xlsx/format.rb', line 343

def set_align(location)
  return unless location             # No default

  location = location.downcase

  case location
  when 'left'                         then set_text_h_align(1)
  when 'centre', 'center'             then set_text_h_align(2)
  when 'right'                        then set_text_h_align(3)
  when 'fill'                         then set_text_h_align(4)
  when 'justify'                      then set_text_h_align(5)
  when 'center_across', 'centre_across', 'merge'
    set_text_h_align(6)
  when 'distributed', 'equal_space', 'justify_distributed'
    set_text_h_align(7)
  when 'top'                          then set_text_v_align(1)
  when 'vcentre', 'vcenter'           then set_text_v_align(2)
  when 'bottom'                       then set_text_v_align(3)
  when 'vjustify'                     then set_text_v_align(4)
  when 'vdistributed', 'vequal_space' then set_text_v_align(5)
  end

  @just_distrib = 1 if location == 'justify_distributed'
end

#set_bold(bold = 1) ⇒ Object



205
206
207
# File 'lib/write_xlsx/format.rb', line 205

def set_bold(bold = 1)
  @bold = ptrue?(bold) ? 1 : 0
end

#set_border(style) ⇒ Object

Set cells borders to the same style



395
396
397
398
399
400
# File 'lib/write_xlsx/format.rb', line 395

def set_border(style)
  set_bottom(style)
  set_top(style)
  set_left(style)
  set_right(style)
end

#set_border_color(color) ⇒ Object

Set cells border to the same color



405
406
407
408
409
410
# File 'lib/write_xlsx/format.rb', line 405

def set_border_color(color)
  set_bottom_color(color)
  set_top_color(color)
  set_left_color(color)
  set_right_color(color)
end

#set_border_info(borders) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/write_xlsx/format.rb', line 455

def set_border_info(borders)
  key = get_border_key

  if borders[key]
    # Border has already been used.
    @border_index = borders[key]
    @has_border   = false
  else
    # This is a new border.
    @border_index = borders.size
    borders[key]  = borders.size
    @has_border   = true
  end
end

#set_center_across(_flag = 1) ⇒ Object

Implements the Excel5 style “merge”.



379
380
381
# File 'lib/write_xlsx/format.rb', line 379

def set_center_across(_flag = 1)
  set_text_h_align(6)
end

#set_font_info(fonts) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/write_xlsx/format.rb', line 440

def set_font_info(fonts)
  key = get_font_key

  if fonts[key]
    # Font has already been used.
    @font_index = fonts[key]
    @has_font   = false
  else
    # This is a new font.
    @font_index = fonts.size
    fonts[key]  = fonts.size
    @has_font   = true
  end
end

#set_format_properties(*properties) ⇒ Object

:call-seq:

set_format_properties( :bold => 1 [, :color => 'red'..] )
set_format_properties( font [, shade, ..])
set_format_properties( :bold => 1, font, ...)
  *) font  = { :color => 'red', :bold => 1 }
     shade = { :bg_color => 'green', :pattern => 1 }

Convert hashes of properties to method calls.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/write_xlsx/format.rb', line 117

def set_format_properties(*properties)   # :nodoc:
  return if properties.empty?

  properties.each do |property|
    property.each do |key, value|
      # Strip leading "-" from Tk style properties e.g. "-color" => 'red'.
      key = key.sub(/^-/, '') if key.respond_to?(:to_str)

      # Create a sub to set the property.
      if value.respond_to?(:to_str) || !value.respond_to?(:+)
        send("set_#{key}", value.to_s)
      else
        send("set_#{key}", value)
      end
    end
  end
end

Set the properties for the hyperlink style. This isn’t a public method. To be fixed when styles are supported.



432
433
434
435
436
437
438
# File 'lib/write_xlsx/format.rb', line 432

def set_hyperlink(hyperlink)
  @xf_id = 1

  set_underline(1)
  set_theme(10)
  @hyperlink = hyperlink
end

#set_merge(_merge = 1) ⇒ Object

This was the way to implement a merge in Excel5. However it should have been called “center_across” and not “merge”. This is now deprecated. Use set_center_across() or better merge_range().



388
389
390
# File 'lib/write_xlsx/format.rb', line 388

def set_merge(_merge = 1)
  set_text_h_align(6)
end

#set_rotation(rotation) ⇒ Object

Set the rotation angle of the text. An alignment property.



415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/write_xlsx/format.rb', line 415

def set_rotation(rotation)
  if rotation == 270
    rotation = 255
  elsif rotation.between?(-90, 90)
    rotation = -rotation + 90 if rotation < 0
  else
    raise "Rotation #{rotation} outside range: -90 <= angle <= 90"
    rotation = 0
  end

  @rotation = rotation
end

#set_valign(location) ⇒ Object

Set vertical cell alignment. This is required by the set_properties() method to differentiate between the vertical and horizontal properties.



372
373
374
# File 'lib/write_xlsx/format.rb', line 372

def set_valign(location)
  set_align(location)
end

#shadow?Boolean

Returns:

  • (Boolean)


510
511
512
# File 'lib/write_xlsx/format.rb', line 510

def shadow?
  ptrue?(@font_shadow)
end

#strikeout?Boolean

Returns:

  • (Boolean)


502
503
504
# File 'lib/write_xlsx/format.rb', line 502

def strikeout?
  ptrue?(@font_strikeout)
end

#underline?Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/write_xlsx/format.rb', line 514

def underline?
  ptrue?(@underline)
end

#write_font(writer, worksheet, dxf_format = nil) ⇒ Object

:nodoc:



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/write_xlsx/format.rb', line 570

def write_font(writer, worksheet, dxf_format = nil) # :nodoc:
  writer.tag_elements('font') do
    # The condense and extend elements are mainly used in dxf formats.
    write_condense(writer) if ptrue?(@font_condense)
    write_extend(writer)   if ptrue?(@font_extend)

    write_font_shapes(writer)

    writer.empty_tag('sz', [['val', size]]) unless dxf_format

    if theme == -1
    # Ignore for excel2003_style
    elsif ptrue?(theme)
      write_color('theme', theme, writer)
    elsif ptrue?(@color_indexed)
      write_color('indexed', @color_indexed, writer)
    elsif ptrue?(@color)
      color = worksheet.palette_color(@color)
      if color != 'Automatic'
        write_color('rgb', color, writer)
      end
    elsif !ptrue?(dxf_format)
      write_color('theme', 1, writer)
    end

    unless ptrue?(dxf_format)
      writer.empty_tag('name', [['val', @font]])
      write_font_family_scheme(writer)
    end
  end
end

#write_font_rpr(writer, worksheet) ⇒ Object

:nodoc:



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/write_xlsx/format.rb', line 602

def write_font_rpr(writer, worksheet) # :nodoc:
  writer.tag_elements('rPr') do
    write_font_shapes(writer)
    writer.empty_tag('sz', [['val', size]])

    if ptrue?(theme)
      write_color('theme', theme, writer)
    elsif ptrue?(@color)
      color = worksheet.palette_color(@color)
      write_color('rgb', color, writer)
    else
      write_color('theme', 1, writer)
    end

    writer.empty_tag('rFont', [['val', @font]])
    write_font_family_scheme(writer)
  end
end

#xf_attributesObject



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/write_xlsx/format.rb', line 636

def xf_attributes
  attributes = [
    ['numFmtId', num_format_index],
    ['fontId', font_index],
    ['fillId', fill_index],
    ['borderId', border_index],
    ['xfId', xf_id]
  ]
  attributes << ['quotePrefix', 1] if ptrue?(quote_prefix)
  attributes << ['applyNumberFormat', 1] if num_format_index > 0
  # Add applyFont attribute if XF format uses a font element.
  attributes << ['applyFont', 1] if font_index > 0 && !ptrue?(@hyperlink)
  # Add applyFill attribute if XF format uses a fill element.
  attributes << ['applyFill', 1] if fill_index > 0
  # Add applyBorder attribute if XF format uses a border element.
  attributes << ['applyBorder', 1] if border_index > 0

  # Check if XF format has alignment properties set.
  apply_align, _align = get_align_properties
  # We can also have applyAlignment without a sub-element.
  attributes << ['applyAlignment', 1] if apply_align || ptrue?(@hyperlink)
  attributes << ['applyProtection', 1] if get_protection_properties || ptrue?(hyperlink)

  attributes
end