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, collapsed)

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.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/write_xlsx/worksheet.rb', line 302

def set_column(*args)
  # Check for a cell reference in A1 notation and substitute row and column
  # ruby 3.2 no longer handles =~ for various types
  if args[0].respond_to?(:=~) && args[0].to_s =~ /^\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 unless ptrue?(lastcol)

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

  width, format, hidden, level, collapsed = data
  autofit = 0

  # 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 && ptrue?(hidden)         # 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

  # Excel has a maximum column width of 255 characters.
  width = 255.0 if width && width > 255.0

  @outline_col_level = level if level > @outline_col_level

  # Store the column data based on the first column. Padded for sorting.
  (firstcol..lastcol).each do |col|
    @col_info[col] =
      COLINFO.new(width, format, hidden, level, collapsed, autofit)
  end

  # Store the column change to allow optimisations.
  @col_size_changed = true
end