Method: Writeexcel::Worksheet#set_column

Defined in:
lib/writeexcel/worksheet.rb

#set_column(*args) ⇒ Object

:call-seq:

set_column(first_col, last_col, width, format, hidden, level, collapsed)
set_column(A1_notation,         width, format, hidden, level, collapsed)

Set the width of a single column or a range of columns. – See also: store_colinfo ++

This method can be used to change the default properties of a single column or a range of columns. All parameters apart from first_col and last_col are optional.

If set_column() is applied to a single column the value of first_col and last_col should be the same. In the case where last_col is zero it is set to the same value as first_col.

It is also possible, and generally clearer, to specify a column range using the form of A1 notation used for columns. See the note about “Cell notation”.

Examples:

worksheet.set_column(0, 0,  20) # Column  A   width set to 20
worksheet.set_column(1, 3,  30) # Columns B-D width set to 30
worksheet.set_column('E:E', 20) # Column  E   width set to 20
worksheet.set_column('F:H', 30) # Columns F-H width set to 30

The width corresponds to the column width value that is specified in Excel. It is approximately equal to the length of a string in the default font of Arial 10. Unfortunately, there is no way to specify “AutoFit” for a column in the Excel file format. This feature is only available at runtime from within Excel.

As usual the format parameter is optional, for additional information, see “CELL FORMATTING”. If you wish to set the format without changing the width you can pass undef as the width parameter:

worksheet.set_column(0, 0, nil, format)

The format parameter will be applied to any cells in the column that don’t have a format. For example

worksheet.set_column('A:A', nil, format1)   # Set format for col 1
worksheet.write('A1', 'Hello')              # Defaults to format1
worksheet.write('A2', 'Hello', format2)     # Keeps format2

If you wish to define a column format in this way you should call the method before any calls to write(). If you call it afterwards it won’t have any effect.

A default row format takes precedence over a default column format

worksheet.set_row(0, nil,        format1)   # Set format for row 1
worksheet.set_column('A:A', nil, format2)   # Set format for col 1
worksheet.write('A1', 'Hello')              # Defaults to format1
worksheet.write('A2', 'Hello')              # Defaults to format2

The hidden parameter should be set to true if you wish to hide a column. This can be used, for example, to hide intermediary steps in a complicated calculation:

worksheet.set_column('D:D', 20,  format, true)
worksheet.set_column('E:E', nil, nil,    true)

The level parameter is used to set the outline level of the column. Outlines are described in “OUTLINES AND GROUPING IN EXCEL”. Adjacent columns with the same outline level are grouped together into a single outline.

The following example sets an outline level of 1 for columns B to G:

worksheet.set_column('B:G', nil, nil, true, 1)

The hidden parameter can also be used to hide collapsed outlined columns when used in conjunction with the level parameter.

worksheet.set_column('B:G', nil, nil, true, 1)

For collapsed outlines you should also indicate which row has the collapsed + symbol using the optional collapsed parameter.

worksheet.set_column('H:H', nil, nil, true, 0, true)

For a more complete example see the outline.pl and outline_collapsed.rb programs in the examples directory of the distro.

Excel allows up to 7 outline levels. Therefore the level parameter should be in the range 0 <= level <= 7.



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/writeexcel/worksheet.rb', line 673

def set_column(*args)
  # Check for a cell reference in A1 notation and substitute row and column
  if args[0] =~ /^\D/
    row1, firstcol, row2, lastcol, *data = substitute_cellref(*args)
  else
    firstcol, lastcol, *data = args
  end

  # Ensure at least firstcol, lastcol and width
  return unless firstcol && lastcol && !data.empty?

  # Assume second column is the same as first if 0. Avoids KB918419 bug.
  lastcol = firstcol if lastcol == 0

  # Ensure 2nd col is larger than first. Also for KB918419 bug.
  firstcol, lastcol = lastcol, firstcol if firstcol > lastcol

  # Limit columns to Excel max of 255.
  firstcol = ColMax - 1 if firstcol > ColMax - 1
  lastcol  = ColMax - 1 if lastcol  > ColMax - 1

  @colinfo << ColInfo.new(firstcol, lastcol, *data)

  # Store the col sizes for use when calculating image vertices taking
  # hidden columns into account. Also store the column formats.
  #
  width, format, hidden = data

  width  ||= 0                    # Ensure width isn't undef.
  width = 0 if hidden && hidden != 0  # Set width to zero if col is hidden

  (firstcol .. lastcol).each do |col|
    @col_sizes[col]   = width
    @col_formats[col] = format if format
  end
end