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


632
633
634
635
636
637
# File 'lib/fast_excel.rb', line 632

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

598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/fast_excel.rb', line 598

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)

670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/fast_excel.rb', line 670

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


691
692
693
# File 'lib/fast_excel.rb', line 691

def font_family
  font_name
end

#font_family=(value) ⇒ Object


695
696
697
# File 'lib/fast_excel.rb', line 695

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

#set_font_size(value) ⇒ Object


684
685
686
687
688
689
# File 'lib/fast_excel.rb', line 684

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