Method: Writexlsx::Worksheet#set_header

Defined in:
lib/write_xlsx/worksheet.rb

#set_header(string = '', margin = 0.3, options = {}) ⇒ Object

Set the page header caption and optional margin.



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/write_xlsx/worksheet.rb', line 638

def set_header(string = '', margin = 0.3, options = {})
  raise 'Header string must be less than 255 characters' if string.length > 255

  # Replace the Excel placeholder &[Picture] with the internal &G.
  header_footer_string = string.gsub("&[Picture]", '&G')
  # placeholeder /&G/ の数
  placeholder_count = header_footer_string.scan("&G").count
  @page_setup.header = header_footer_string

  @page_setup.header_footer_aligns = options[:align_with_margins] if options[:align_with_margins]

  @page_setup.header_footer_scales = options[:scale_with_doc] if options[:scale_with_doc]

  # Reset the array in case the function is called more than once.
  @header_images = []

  [
    [:image_left, 'LH'], [:image_center, 'CH'], [:image_right, 'RH']
  ].each do |p|
    @header_images << ImageProperty.new(options[p.first], position: p.last) if options[p.first]
  end

  # # placeholeder /&G/ の数
  # placeholder_count = @page_setup.header.scan("&G").count

  raise "Number of header image (#{@header_images.size}) doesn't match placeholder count (#{placeholder_count}) in string: #{@page_setup.header}" if @header_images.size != placeholder_count

  @page_setup.margin_header         = margin || 0.3
  @page_setup.header_footer_changed = true
end