Method: Writeexcel::Format#set_align

Defined in:
lib/writeexcel/format.rb

#set_align(align = 'left') ⇒ Object

Set cell alignment.

Default state:      Alignment is off
Default action:     Left alignment
Valid args:         'left'              Horizontal
                    'center'
                    'right'
                    'fill'
                    'justify'
                    'center_across'

                    'top'               Vertical
                    'vcenter'
                    'bottom'
                    'vjustify'

This method is used to set the horizontal and vertical text alignment within a cell. Vertical and horizontal alignments can be combined.

The method is used as follows:

   format = workbook.add_format
   format->set_align('center')
   format->set_align('vcenter')
   worksheet->set_row(0, 30)
   worksheet->write(0, 0, 'X', format)

Text can be aligned across two or more adjacent cells using the center_across property. However, for genuine merged cells it is better to use the merge_range() worksheet method.

The vjustify (vertical justify) option can be used to provide automatic text wrapping in a cell. The height of the cell will be adjusted to accommodate the wrapped text. To specify where the text wraps use the set_text_wrap() method.

For further examples see the ‘Alignment’ worksheet created by formats.rb.



947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
# File 'lib/writeexcel/format.rb', line 947

def set_align(align = 'left')
  case align.to_s.downcase
  when 'left'             then set_text_h_align(1)
  when 'centre', 'center' then set_text_h_align(2)
  when 'right'            then set_text_h_align(3)
  when 'fill'             then set_text_h_align(4)
  when 'justify'          then set_text_h_align(5)
  when 'center_across', 'centre_across' then set_text_h_align(6)
  when 'merge'            then set_text_h_align(6) # S:WE name
  when 'distributed'      then set_text_h_align(7)
  when 'equal_space'      then set_text_h_align(7) # ParseExcel

  when 'top'              then set_text_v_align(0)
  when 'vcentre'          then set_text_v_align(1)
  when 'vcenter'          then set_text_v_align(1)
  when 'bottom'           then set_text_v_align(2)
  when 'vjustify'         then set_text_v_align(3)
  when 'vdistributed'     then set_text_v_align(4)
  when 'vequal_space'     then set_text_v_align(4) # ParseExcel
  else nil
  end
end