Module: Workbook::Writers::CsvTableWriter

Included in:
Table
Defined in:
lib/workbook/writers/csv_table_writer.rb

Instance Method Summary collapse

Instance Method Details

#to_csv(options = {}) ⇒ String

Output the current workbook to CSV format

Parameters:

  • options (Hash) (defaults to: {})

    (not used)

Returns:

  • (String)

    csv (comma separated values in a string)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/workbook/writers/csv_table_writer.rb', line 14

def to_csv options={}
  csv = ""
  options = {}.merge options
  self.each_with_index do |r, ri|
    line=nil
    begin
      line = CSV::generate_line(r.collect{|c| c.value if c},{:row_sep=>""})
    rescue TypeError
      line = CSV::generate_line(r.collect{|c| c.value if c})
    end
    csv += "#{line}\n"
  end
  csv
end

#write_to_csv(filename = "#{title}.csv", options = {}) ⇒ String

Write the current workbook to CSV format

Parameters:

  • filename (String) (defaults to: "#{title}.csv")
  • options (Hash) (defaults to: {})

    see #to_csv

Returns:

  • (String)

    filename



34
35
36
37
# File 'lib/workbook/writers/csv_table_writer.rb', line 34

def write_to_csv filename="#{title}.csv", options={}
  File.open(filename, 'w') {|f| f.write(to_csv(options)) }
  return filename
end