Method: Writexlsx::Worksheet#set_selection

Defined in:
lib/write_xlsx/worksheet.rb

#set_selection(*args) ⇒ Object

:call-seq:

set_selection(cell_or_cell_range)

Set which cell or cells are selected in a worksheet.

This method can be used to specify which cell or cells are selected in a worksheet. The most common requirement is to select a single cell, in which case last_row and last_col can be omitted. The active cell within a selected range is determined by the order in which first and last are specified. It is also possible to specify a cell or a range using A1 notation. See the note about “Cell notation”.

Examples:

worksheet1.set_selection(3, 3)          # 1. Cell D4.
worksheet2.set_selection(3, 3, 6, 6)    # 2. Cells D4 to G7.
worksheet3.set_selection(6, 6, 3, 3)    # 3. Cells G7 to D4.
worksheet4.set_selection('D4')          # Same as 1.
worksheet5.set_selection('D4:G7')       # Same as 2.
worksheet6.set_selection('G7:D4')       # Same as 3.

The default cell selections is (0, 0), ‘A1’.



781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/write_xlsx/worksheet.rb', line 781

def set_selection(*args)
  return if args.empty?

  row_first, col_first, row_last, col_last = row_col_notation(args)
  active_cell = xl_rowcol_to_cell(row_first, col_first)

  if row_last.nil?   # Single cell selection.
    sqref = active_cell
  else               # Range selection.
    # Swap last row/col for first row/col as necessary
    row_first, row_last = row_last, row_first if row_first > row_last
    col_first, col_last = col_last, col_first if col_first > col_last

    # If the first and last cell are the same write a single cell.
    if row_first == row_last && col_first == col_last
      sqref = active_cell
    else
      sqref = xl_range(row_first, col_first, row_last, col_last)
    end
  end

  # Selection isn't set for cell A1.
  return if sqref == 'A1'

  @selections = [ [ nil, active_cell, sqref ] ]
end