Class: Writexlsx::Format

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

Overview

CELL FORMATTING

This section describes the methods and properties that are available for formatting cells in Excel. The properties of a cell that can be formatted include: fonts, colours, patterns, borders, alignment and number formatting.

Creating and using a Format object

Cell formatting is defined through a Format object. Format objects are created by calling the workbook add_format() method as follows:

format1 = workbook.add_format             # Set properties later
format2 = workbook.add_format(props_hash) # Set at creation

The format object holds all the formatting properties that can be applied to a cell, a row or a column. The process of setting these properties is discussed in the next section.

Once a Format object has been constructed and its properties have been set it can be passed as an argument to the worksheet write methods as follows:

worksheet.write( 0, 0, 'One', format )
worksheet.write_string( 1, 0, 'Two', format )
worksheet.write_number( 2, 0, 3, format )
worksheet.write_blank( 3, 0, format )

Formats can also be passed to the worksheet set_row() and set_column() methods to define the default property for a row or column.

worksheet.set_row( 0, 15, format )
worksheet.set_column( 0, 0, 15, format )

Format methods and Format properties

The following table shows the Excel format categories, the formatting properties that can be applied and the equivalent object method:

Category   Description       Property        Method Name
--------   -----------       --------        -----------
Font       Font type         font            set_font()
           Font size         size            set_size()
           Font color        color           set_color()
           Bold              bold            set_bold()
           Italic            italic          set_italic()
           Underline         underline       set_underline()
           Strikeout         font_strikeout  set_font_strikeout()
           Super/Subscript   font_script     set_font_script()
           Outline           font_outline    set_font_outline()
           Shadow            font_shadow     set_font_shadow()

Number     Numeric format    num_format      set_num_format()

Protection Lock cells        locked          set_locked()
           Hide formulas     hidden          set_hidden()

Alignment  Horizontal align  align           set_align()
           Vertical align    valign          set_align()
           Rotation          rotation        set_rotation()
           Text wrap         text_wrap       set_text_wrap()
           Justify last      text_justlast   set_text_justlast()
           Center across     center_across   set_center_across()
           Indentation       indent          set_indent()
           Shrink to fit     shrink          set_shrink()

Pattern    Cell pattern      pattern         set_pattern()
           Background color  bg_color        set_bg_color()
           Foreground color  fg_color        set_fg_color()

Border     Cell border       border          set_border()
           Bottom border     bottom          set_bottom()
           Top border        top             set_top()
           Left border       left            set_left()
           Right border      right           set_right()
           Border color      border_color    set_border_color()
           Bottom color      bottom_color    set_bottom_color()
           Top color         top_color       set_top_color()
           Left color        left_color      set_left_color()
           Right color       right_color     set_right_color()

There are two ways of setting Format properties: by using the object method interface or by setting the property directly. For example, a typical use of the method interface would be as follows:

format = workbook.add_format
format.set_bold
format.set_color( 'red' )

By comparison the properties can be set directly by passing a hash of properties to the Format constructor:

format = workbook.add_format( :bold => 1, :color => 'red' )

or after the Format has been constructed by means of the set_format_properties() method as follows:

format = workbook.add_format
format.set_format_properties( :bold => 1, :color => 'red' )

You can also store the properties in one or more named hashes and pass them to the required method:

font = {
    :font  => 'Arial',
    :size  => 12,
    :color => 'blue',
    :bold  => 1
}

shading = {
    :bg_color => 'green',
    :pattern  => 1
}

format1 = workbook.add_format( font )           # Font only
format2 = workbook.add_format( font, shading )  # Font and shading

The provision of two ways of setting properties might lead you to wonder which is the best way. The method mechanism may be better is you prefer setting properties via method calls (which the author did when the code was first written) otherwise passing properties to the constructor has proved to be a little more flexible and self documenting in practice. An additional advantage of working with property hashes is that it allows you to share formatting between workbook objects as shown in the example above.

Working with formats

The default format is Arial 10 with all other properties off.

Each unique format in WriteXLSX must have a corresponding Format object. It isn’t possible to use a Format with a write() method and then redefine the Format for use at a later stage. This is because a Format is applied to a cell not in its current state but in its final state. Consider the following example:

format = workbook.add_format
format.set_bold
format.set_color( 'red' )
worksheet.write( 'A1', 'Cell A1', format )
format.set_color( 'green' )
worksheet.write( 'B1', 'Cell B1', format )

Cell A1 is assigned the Format format which is initially set to the colour red. However, the colour is subsequently set to green. When Excel displays Cell A1 it will display the final state of the Format which in this case will be the colour green.

In general a method call without an argument will turn a property on, for example:

format1 = workbook.add_format
format1.set_bold         # Turns bold on
format1.set_bold( 1 )    # Also turns bold on
format1.set_bold( 0 )    # Turns bold off

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xf_format_indices = {}, dxf_format_indices = {}, params = {}) ⇒ Format

:nodoc:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/write_xlsx/format.rb', line 170

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

  @xf_index       = nil
  @dxf_index      = nil

  @num_format     = 0
  @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

  @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

  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:



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

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 = "@#{$1}"

  # Check that the attribute exists
  # ........
  if method =~ /set\w+color$/    # for "set_property_color" methods
    value = get_color(args[0])
  else                            # for "set_xxx" methods
    value = args[0].nil? ? 1 : args[0]
  end
  if value.respond_to?(:to_str) || !value.respond_to?(:+)
    s = %Q!#{attribute} = "#{value.to_s}"!
  else
    s = %Q!#{attribute} =   #{value.to_s}!
  end
  eval s
end

Instance Attribute Details

#bg_colorObject

:nodoc:



168
169
170
# File 'lib/write_xlsx/format.rb', line 168

def bg_color
  @bg_color
end

#border_indexObject

:nodoc:



166
167
168
# File 'lib/write_xlsx/format.rb', line 166

def border_index
  @border_index
end

#bottomObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def bottom
  @bottom
end

#bottom_colorObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def bottom_color
  @bottom_color
end

#colorObject (readonly)

:nodoc:



163
164
165
# File 'lib/write_xlsx/format.rb', line 163

def color
  @color
end

#color_indexedObject (readonly)

:nodoc:



163
164
165
# File 'lib/write_xlsx/format.rb', line 163

def color_indexed
  @color_indexed
end

#diag_borderObject

:nodoc:



167
168
169
# File 'lib/write_xlsx/format.rb', line 167

def diag_border
  @diag_border
end

#diag_colorObject (readonly)

:nodoc:



163
164
165
# File 'lib/write_xlsx/format.rb', line 163

def diag_color
  @diag_color
end

#diag_typeObject (readonly)

:nodoc:



163
164
165
# File 'lib/write_xlsx/format.rb', line 163

def diag_type
  @diag_type
end

#dxf_indexObject (readonly)

:nodoc:



161
162
163
# File 'lib/write_xlsx/format.rb', line 161

def dxf_index
  @dxf_index
end

#fg_colorObject

:nodoc:



168
169
170
# File 'lib/write_xlsx/format.rb', line 168

def fg_color
  @fg_color
end

#fill_indexObject

:nodoc:



167
168
169
# File 'lib/write_xlsx/format.rb', line 167

def fill_index
  @fill_index
end

#fontObject (readonly)

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def font
  @font
end

#font_condenseObject

:nodoc:



167
168
169
# File 'lib/write_xlsx/format.rb', line 167

def font_condense
  @font_condense
end

#font_extendObject

:nodoc:



167
168
169
# File 'lib/write_xlsx/format.rb', line 167

def font_extend
  @font_extend
end

#font_familyObject (readonly)

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def font_family
  @font_family
end

#font_indexObject

:nodoc:



166
167
168
# File 'lib/write_xlsx/format.rb', line 166

def font_index
  @font_index
end

#font_onlyObject (readonly)

:nodoc:



163
164
165
# File 'lib/write_xlsx/format.rb', line 163

def font_only
  @font_only
end

#font_schemeObject (readonly)

:nodoc:



165
166
167
# File 'lib/write_xlsx/format.rb', line 165

def font_scheme
  @font_scheme
end

#font_scriptObject (readonly)

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def font_script
  @font_script
end

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def hyperlink
  @hyperlink
end

#leftObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def left
  @left
end

#left_colorObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def left_color
  @left_color
end

#num_formatObject (readonly)

:nodoc:



161
162
163
# File 'lib/write_xlsx/format.rb', line 161

def num_format
  @num_format
end

#num_format_indexObject

:nodoc:



166
167
168
# File 'lib/write_xlsx/format.rb', line 166

def num_format_index
  @num_format_index
end

#patternObject

:nodoc:



168
169
170
# File 'lib/write_xlsx/format.rb', line 168

def pattern
  @pattern
end

#rightObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def right
  @right
end

#right_colorObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def right_color
  @right_color
end

#sizeObject (readonly)

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def size
  @size
end

#themeObject (readonly)

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def theme
  @theme
end

#topObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def top
  @top
end

#top_colorObject (readonly)

:nodoc:



164
165
166
# File 'lib/write_xlsx/format.rb', line 164

def top_color
  @top_color
end

#underlineObject (readonly)

:nodoc:



162
163
164
# File 'lib/write_xlsx/format.rb', line 162

def underline
  @underline
end

#xf_indexObject (readonly)

:nodoc:



161
162
163
# File 'lib/write_xlsx/format.rb', line 161

def xf_index
  @xf_index
end

Class Method Details

.get_color(color) ⇒ 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.



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/write_xlsx/format.rb', line 471

def self.get_color(color)

  colors = Colors::COLORS

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

    # Return the default color if undef,
    return 0x00 unless color

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

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

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

    # or an integer in the valid range
    return color
  end
end

Instance Method Details

#bold?Boolean

Returns:

  • (Boolean)


627
628
629
# File 'lib/write_xlsx/format.rb', line 627

def bold?
  bool_both_ruby_and_perl?(@bold)
end

#bool_both_ruby_and_perl?(val) ⇒ Boolean

Returns:

  • (Boolean)


651
652
653
654
655
# File 'lib/write_xlsx/format.rb', line 651

def bool_both_ruby_and_perl?(val)
  return false unless val
  return false if val == 0
  true
end

#get_align_propertiesObject

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



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/write_xlsx/format.rb', line 281

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 horizontal left, right and distributed. 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)

  # 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 << 'horizontal' << 'left'        if @text_h_align == 1
  align << 'horizontal' << 'center'      if @text_h_align == 2
  align << 'horizontal' << 'right'       if @text_h_align == 3
  align << 'horizontal' << 'fill'        if @text_h_align == 4
  align << 'horizontal' << 'justify'     if @text_h_align == 5
  align << 'horizontal' << continuous    if @text_h_align == 6
  align << '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 << 'vertical' << 'top'         if @text_v_align == 1
  align << 'vertical' << 'center'      if @text_v_align == 2
  align << 'vertical' << 'justify'     if @text_v_align == 4
  align << 'vertical' << 'distributed' if @text_v_align == 5

  align << 'indent' <<       @indent   if @indent   != 0
  align << 'textRotation' << @rotation if @rotation != 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

  return changed, align
end

#get_alignment_keyObject

Returns a unique hash key for alignment formats.



416
417
418
# File 'lib/write_xlsx/format.rb', line 416

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.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/write_xlsx/format.rb', line 386

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_color(color) ⇒ Object



462
463
464
# File 'lib/write_xlsx/format.rb', line 462

def get_color(color)
  Format.get_color(color)
end

#get_dxf_indexObject

Returns the index used by Worksheet->_XF()



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/write_xlsx/format.rb', line 444

def get_dxf_index
  if @dxf_index
      @dxf_index
  else
    key  = get_format_key
    indices = @dxf_format_indices

    if indices[key]
      indices[key]
    else
      index = indices.size
      indices[key] = index
      @dxf_index = index
      index
    end
  end
end

#get_fill_keyObject

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



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

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.



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/write_xlsx/format.rb', line 366

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

#get_format_keyObject

Returns a unique hash key for the Format object.



359
360
361
# File 'lib/write_xlsx/format.rb', line 359

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

#get_protection_propertiesObject

Return properties for an Excel XML <Protection> element.



339
340
341
342
343
344
345
346
# File 'lib/write_xlsx/format.rb', line 339

def get_protection_properties
  attributes = []

  attributes << 'locked' << 0 if     @locked == 0
  attributes << 'hidden' << 1 unless @hidden == 0

  attributes.empty? ? nil : attributes
end

#get_xf_indexObject

Returns the index used by Worksheet->_XF()



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/write_xlsx/format.rb', line 423

def get_xf_index
  if @xf_index
    @xf_index
  else
    key = get_format_key
    indices_href = @xf_format_indices

    if indices_href[key]
      indices_href[key]
    else
      index = 1 + indices_href.keys.size
      indices_href[key] = index
      @xf_index = index
      index
    end
  end
end

#has_border(flag) ⇒ Object



657
658
659
# File 'lib/write_xlsx/format.rb', line 657

def has_border(flag)
  @has_border = flag
end

#has_border?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def has_border? # :nodoc:
  @has_border
end

#has_dxf_border(flag) ⇒ Object



665
666
667
# File 'lib/write_xlsx/format.rb', line 665

def has_dxf_border(flag)
  @has_dxf_border = flag
end

#has_dxf_border?Boolean

Returns:

  • (Boolean)


669
670
671
# File 'lib/write_xlsx/format.rb', line 669

def has_dxf_border?
  @has_dxf_border
end

#has_dxf_fill(flag) ⇒ Object



697
698
699
# File 'lib/write_xlsx/format.rb', line 697

def has_dxf_fill(flag)
  @has_dxf_fill = flag
end

#has_dxf_fill?Boolean

Returns:

  • (Boolean)


701
702
703
# File 'lib/write_xlsx/format.rb', line 701

def has_dxf_fill?
  @has_dxf_fill
end

#has_dxf_font(flag) ⇒ Object



681
682
683
# File 'lib/write_xlsx/format.rb', line 681

def has_dxf_font(flag)
  @has_dxf_font = flag
end

#has_dxf_font?Boolean

Returns:

  • (Boolean)


685
686
687
# File 'lib/write_xlsx/format.rb', line 685

def has_dxf_font?
  @has_dxf_font
end

#has_fill(flag) ⇒ Object



689
690
691
# File 'lib/write_xlsx/format.rb', line 689

def has_fill(flag)
  @has_fill = flag
end

#has_fill?Boolean

Returns:

  • (Boolean)


693
694
695
# File 'lib/write_xlsx/format.rb', line 693

def has_fill?
  @has_fill
end

#has_font(flag) ⇒ Object



673
674
675
# File 'lib/write_xlsx/format.rb', line 673

def has_font(flag)
  @has_font = flag
end

#has_font?Boolean

Returns:

  • (Boolean)


677
678
679
# File 'lib/write_xlsx/format.rb', line 677

def has_font?
  @has_font
end

#inspectObject



352
353
354
# File 'lib/write_xlsx/format.rb', line 352

def inspect
  to_s
end

#italic?Boolean

Returns:

  • (Boolean)


631
632
633
# File 'lib/write_xlsx/format.rb', line 631

def italic?
  bool_both_ruby_and_perl?(@italic)
end

#outline?Boolean

Returns:

  • (Boolean)


639
640
641
# File 'lib/write_xlsx/format.rb', line 639

def outline?
  bool_both_ruby_and_perl?(@font_outline)
end

#set_align(location) ⇒ Object

Set cell alignment.



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/write_xlsx/format.rb', line 502

def set_align(location)
  return unless location             # No default

  location.downcase!

  set_text_h_align(1) if location == 'left'
  set_text_h_align(2) if location == 'centre'
  set_text_h_align(2) if location == 'center'
  set_text_h_align(3) if location == 'right'
  set_text_h_align(4) if location == 'fill'
  set_text_h_align(5) if location == 'justify'
  set_text_h_align(6) if location == 'center_across'
  set_text_h_align(6) if location == 'centre_across'
  set_text_h_align(6) if location == 'merge'              # Legacy.
  set_text_h_align(7) if location == 'distributed'
  set_text_h_align(7) if location == 'equal_space'        # S::PE.
  set_text_h_align(7) if location == 'justify_distributed'

  @just_distrib =   1 if location == 'justify_distributed'

  set_text_v_align(1) if location == 'top'
  set_text_v_align(2) if location == 'vcentre'
  set_text_v_align(2) if location == 'vcenter'
  set_text_v_align(3) if location == 'bottom'
  set_text_v_align(4) if location == 'vjustify'
  set_text_v_align(5) if location == 'vdistributed'
  set_text_v_align(5) if location == 'vequal_space'    # S::PE.
end

#set_bold(bold = 1) ⇒ Object



348
349
350
# File 'lib/write_xlsx/format.rb', line 348

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

#set_border(style) ⇒ Object

Set cells borders to the same style



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

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



568
569
570
571
572
573
# File 'lib/write_xlsx/format.rb', line 568

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

#set_center_across(flag = 1) ⇒ Object

Implements the Excel5 style “merge”.



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

def set_center_across(flag = 1)
  set_text_h_align(6)
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.

The properties of an existing Format object can be also be set by means of set_format_properties():

format = workbook.add_format
format.set_format_properties(:bold => 1, :color => 'red');

However, this method is here mainly for legacy reasons. It is preferable to set the properties in the format constructor:

format = workbook.add_format(:bold => 1, :color => 'red');


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/write_xlsx/format.rb', line 260

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?(:+)
        s = "set_#{key}('#{value}')"
      else
        s = "set_#{key}(#{value})"
      end
      eval s
    end
  end
end

Set the properties for the hyperlink style. TODO. This doesn’t currently work. Fix it when styles are supported.



595
596
597
598
599
600
601
# File 'lib/write_xlsx/format.rb', line 595

def set_hyperlink
  @hyperlink = 1

  set_underline(1)
  set_theme(10)
  set_align('top')
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().



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

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.



578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/write_xlsx/format.rb', line 578

def set_rotation(rotation)
  if rotation == 270
    rotation = 255
  elsif rotation >= -90 || rotation <= 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.



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

def set_valign(location)
  set_align(location)
end

#shadow?Boolean

Returns:

  • (Boolean)


643
644
645
# File 'lib/write_xlsx/format.rb', line 643

def shadow?
  bool_both_ruby_and_perl?(@font_shadow)
end

#strikeout?Boolean

Returns:

  • (Boolean)


635
636
637
# File 'lib/write_xlsx/format.rb', line 635

def strikeout?
  bool_both_ruby_and_perl?(@font_strikeout)
end

#underline?Boolean

Returns:

  • (Boolean)


647
648
649
# File 'lib/write_xlsx/format.rb', line 647

def underline?
  bool_both_ruby_and_perl?(@underline)
end