Method: RubyXL::Worksheet#insert_row

Defined in:
lib/rubyXL/worksheet.rb

#insert_row(row_index = 0) ⇒ Object

inserts row at row_index, pushes down, copies style from below (row previously at that index) USE OF THIS METHOD will break formulas which reference cells which are being “pushed down”



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/rubyXL/worksheet.rb', line 515

def insert_row(row_index=0)
  validate_workbook
  validate_nonnegative(row_index)

  increase_rows(row_index)

  @sheet_data.insert(row_index,Array.new(@sheet_data[row_index].size))

  row_num = row_index+1

  #copy cell styles from row above, (or below if first row)
  @sheet_data[row_index].each_index do |i|
    if row_index > 0
      old_cell = @sheet_data[row_index-1][i]
    else
      old_cell = @sheet_data[row_index+1][i]
    end

    unless old_cell.nil?
      #only add cell if style exists, not copying content

      if @row_styles[(row_num+1).to_s].nil?
        @row_styles[(row_num+1).to_s] = {:style=>0}
      end
      if old_cell.style_index != 0 && old_cell.style_index.to_s != @row_styles[(row_num+1).to_s][:style].to_s
        c = Cell.new(self,row_index,i)
        c.style_index = old_cell.style_index
        @sheet_data[row_index][i] = c
      end
    end
  end

  #copy row styles from row above, (or below if first row)
  (@row_styles.size+1).downto(row_num+1) do |i|
    @row_styles[i.to_s] = @row_styles[(i-1).to_s]
  end
  if row_index > 0
    @row_styles[row_num.to_s] = @row_styles[(row_num-1).to_s]
  else
    @row_styles[row_num.to_s] = nil#@row_styles[(row_num+1).to_s]
  end

  #update row value for all rows below
  (row_index+1).upto(@sheet_data.size-1) do |i|
    row = @sheet_data[i]
    row.each do |c|
      unless c.nil?
        c.row += 1
      end
    end
  end

  return @sheet_data[row_index]
end