Class: Spreadsheet::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/protk/spreadsheet_extensions.rb

Overview

Add a method to the Spreadsheet::Worksheet class to insert a column

Instance Method Summary collapse

Instance Method Details

#insert_column(col, index) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/protk/spreadsheet_extensions.rb', line 4

def insert_column(col,index)
  # First check to see if the length of the column equals the number of rows
  if ( col.length!=self.rows.length && self.rows.length!=0)
    raise "The length of column #{col.length} does not equal the number of rows #{self.rows.length}"
  end
  if ( col.class!=Array || index.class!=Fixnum)
    raise "Wrong arguments. Requires a column array and an integer index"
  end
  
  # Check for special case where there are no rows yet and if so then insert as new rows
  if ( self.rows.length==0)
    col.each_index { |i|
      self.insert_row(i,[col[i]])        
    }
  else
    # Insert the column row by row. Probably inefficient but it works
    rowi=0
    self.each {|row|
      row.insert(index,col[rowi])        
      rowi+=1
    }
  end
end