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



603
604
605
606
607
608
# File 'lib/fast_excel.rb', line 603

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


569
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
601
# File 'lib/fast_excel.rb', line 569

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)


641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/fast_excel.rb', line 641

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



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

def font_family
  font_name
end

#font_family=(value) ⇒ Object



666
667
668
# File 'lib/fast_excel.rb', line 666

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

#set_font_size(value) ⇒ Object



655
656
657
658
659
660
# File 'lib/fast_excel.rb', line 655

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