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



537
538
539
540
541
542
# File 'lib/fast_excel.rb', line 537

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


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
530
531
532
533
534
535
# File 'lib/fast_excel.rb', line 503

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)


575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/fast_excel.rb', line 575

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



596
597
598
# File 'lib/fast_excel.rb', line 596

def font_family
  font_name
end

#font_family=(value) ⇒ Object



600
601
602
# File 'lib/fast_excel.rb', line 600

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

#set_font_size(value) ⇒ Object



589
590
591
592
593
594
# File 'lib/fast_excel.rb', line 589

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