Module: FastExcel::FormatExt

Includes:
AttributeHelper
Defined in:
lib/fast_excel.rb

Constant Summary collapse

ALIGN_ENUM =
Libxlsxwriter.enum_type(:format_alignments)
BORDER_ENUM =
Libxlsxwriter.enum_type(:format_borders)

Instance Method Summary collapse

Methods included from AttributeHelper

#fields_hash, #pretty_print, #set

Instance Method Details

#alignObject



521
522
523
524
525
526
# File 'lib/fast_excel.rb', line 521

def align
  {
    horizontal: ALIGN_ENUM.find(self[:text_h_align]),
    vertical:   ALIGN_ENUM.find(self[:text_v_align])
  }
end

#align=(value) ⇒ Object

Can be called as:

format.align = :align_center
format.align = "align_center"
format.align = :center
format.align = :align_center
format.align = {v: "center", h: "center"}

Possible values:

:align_none, :align_left, :align_center, :align_right, :align_fill, :align_justify,
:align_center_across, :align_distributed, :align_vertical_top, :align_vertical_bottom,
:align_vertical_center, :align_vertical_justify, :align_vertical_distributed


487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/fast_excel.rb', line 487

def align=(value)
  value = value.to_sym if value.is_a?(String)

  if value.is_a?(Symbol)
    if ALIGN_ENUM.find(value)
      set_align(value)
    elsif ALIGN_ENUM.find(prefixed = "align_#{value}".to_sym)
      set_align(prefixed)
    else
      raise ArgumentError, "Can not set align = #{value.inspect}, possible values are: #{ALIGN_ENUM.symbols}"
    end
  elsif value.is_a?(Hash)
    if value[:horizontal]
      self.align = "align_#{value[:horizontal].to_s.sub(/^align_/, '')}".to_sym
    end
    if value[:h]
      self.align = "align_#{value[:h].to_s.sub(/^align_/, '')}".to_sym
    end
    if value[:vertical]
      self.align = "align_vertical_#{value[:vertical].to_s.sub(/^align_vertical_/, '')}".to_sym
    end
    if value[:v]
      self.align = "align_vertical_#{value[:v].to_s.sub(/^align_vertical_/, '')}".to_sym
    end
    possible = [:horizontal, :h, :vertical, :v]
    extras = value.keys - possible
    if extras.size > 0
      raise ArgumentError, "Not allowed keys for align: #{extras.inspect}, possible keys: #{possible.inspect}"
    end
  else
    raise ArgumentError, "value must be a symbol or a hash"
  end
end

#border_value(value) ⇒ Object

Raises:

  • (ArgumentError)


559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/fast_excel.rb', line 559

def border_value(value)
  # if a number
  return value if value.is_a?(Numeric) && BORDER_ENUM.find(value)

  orig_value = value
  value = value.to_sym if value.is_a?(String)

  return BORDER_ENUM.find(value) if BORDER_ENUM.find(value)
  return BORDER_ENUM.find(:"border_#{value}") if BORDER_ENUM.find(:"border_#{value}")

  short_symbols = BORDER_ENUM.symbols.map {|s| s.to_s.sub(/^border_/, '').to_sym }
  raise ArgumentError, "Unknown value #{orig_value.inspect} for border. Possible values: #{short_symbols}"
end

#font_familyObject



580
581
582
# File 'lib/fast_excel.rb', line 580

def font_family
  font_name
end

#font_family=(value) ⇒ Object



584
585
586
# File 'lib/fast_excel.rb', line 584

def font_family=(value)
  self.font_name = value
end

#set_font_size(value) ⇒ Object



573
574
575
576
577
578
# File 'lib/fast_excel.rb', line 573

def set_font_size(value)
  if value < 0
    raise ArgumentError, "font size should be >= 0 (use 0 for user default font size)"
  end
  super(value)
end