Method: RubyXL::WorksheetConvenienceMethods#insert_cell

Defined in:
lib/rubyXL/convenience_methods/worksheet.rb

#insert_cell(row = 0, col = 0, data = nil, formula = nil, shift = nil) ⇒ Object

[View source]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubyXL/convenience_methods/worksheet.rb', line 11

def insert_cell(row = 0, col = 0, data = nil, formula = nil, shift = nil)
  validate_workbook
  ensure_cell_exists(row, col)

  case shift
  when nil then # No shifting at all
  when :right then
    sheet_data.rows[row].insert_cell_shift_right(nil, col)
  when :down then
    add_row(sheet_data.size, :cells => Array.new(sheet_data.rows[row].size))
    (sheet_data.size - 1).downto(row + 1) { |index|
      old_row = sheet_data.rows[index - 1]
      if old_row.nil? then
        sheet_data.rows[index] = nil
      else
        new_row = sheet_data.rows[index] || add_row(index)
        new_row.cells[col] = old_row.cells[col]
      end
    }
  else
    raise 'invalid shift option'
  end

  return add_cell(row, col, data, formula)
end