Module: Workbook::Readers::CsvReader

Included in:
Book
Defined in:
lib/workbook/readers/csv_reader.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) load_csv(text)



6
7
8
9
# File 'lib/workbook/readers/csv_reader.rb', line 6

def load_csv text
  csv = text
  parse_csv csv
end

- (Object) parse_csv(csv_raw)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/workbook/readers/csv_reader.rb', line 11

def parse_csv csv_raw
  custom_date_converter = Workbook::Cell.new.string_optimistic_date_converter
  converters = [:float,:integer,:date,:date_time,custom_date_converter]
  csv=nil
  begin
    csv = FasterCSV.parse(csv_raw,{:converters=>converters})
  rescue
    # we're going to have another shot at it...
  end
  
  if csv==nil or csv[0].count == 1 
    csv_excel = FasterCSV.parse(csv_raw,{:converters=>converters,:col_sep=>';'})
    csv = csv_excel if csv_excel[0].count > 1
  end

  self[0]=Workbook::Sheet.new(csv,self) unless sheet.has_contents?
end