Method: Writexlsx::Worksheet#set_column
- Defined in:
- lib/write_xlsx/worksheet.rb
#set_column(*args) ⇒ Object
:call-seq:
set_column(firstcol, lastcol, width, format, hidden, level)
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 nil 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 1 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, 1 )
worksheet.set_column( 'E:E', nil, nil, 1 )
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, 0, 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, 1, 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, 0, 0, 1 )
For a more complete example see the outline.rb 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.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 |
# File 'lib/write_xlsx/worksheet.rb', line 703 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 width, format, hidden, level = data # Check that cols are valid and store max and min values with default row. # NOTE: The check shouldn't modify the row dimensions and should only modify # the column dimensions in certain cases. ignore_row = 1 ignore_col = 1 ignore_col = 0 if format.respond_to?(:xf_index) # Column has a format. ignore_col = 0 if width && hidden && hidden != 0 # Column has a width but is hidden check_dimensions_and_update_max_min_values(0, firstcol, ignore_row, ignore_col) check_dimensions_and_update_max_min_values(0, lastcol, ignore_row, ignore_col) # Set the limits for the outline levels (0 <= x <= 7). level ||= 0 level = 0 if level < 0 level = 7 if level > 7 @outline_col_level = level if level > @outline_col_level # Store the column data. @colinfo.push([firstcol, lastcol] + data) # Store the column change to allow optimisations. @col_size_changed = 1 # Store the col sizes for use when calculating image vertices taking # hidden columns into account. Also store the column formats. width ||= 0 # Ensure width isn't nil. 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 |