Class: Spreet::Handlers::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/spreet/handlers/csv.rb

Class Method Summary collapse

Class Method Details

.read(file, options = {}) ⇒ Object

Read a CSV file and create its Spreet document



9
10
11
12
13
14
15
16
# File 'lib/spreet/handlers/csv.rb', line 9

def self.read(file, options={})
  spreet = Spreet::Document.new
  sheet = spreet.sheets.add
  ::CSV.foreach(file) do |row|
    sheet.row *row
  end
  return spreet
end

.write(spreet, file, options = {}) ⇒ Object

Write a Spreet to a CSV file



20
21
22
23
24
25
26
27
# File 'lib/spreet/handlers/csv.rb', line 20

def self.write(spreet, file, options={})
  sheet = spreet.sheets[options[:sheet]||0]
  ::CSV.open(file, "wb") do |csv|
    sheet.each_row do |row|
      csv << row.collect{|c| c.text}
    end
  end
end