class Sepparator::SpreadsheetConverter

Attributes

col_sep[R]

Public Class Methods

new(col_sep: ';') click to toggle source
# File lib/sepparator/spreadsheet_converter.rb, line 10
def initialize(col_sep: ';')
  @col_sep = col_sep
end

Public Instance Methods

convert(csv_path, xls_path, sheet_name: 'from CSV') click to toggle source
# File lib/sepparator/spreadsheet_converter.rb, line 14
def convert(csv_path, xls_path, sheet_name: 'from CSV')
  raise ArgumentError, "file not found: #{csv_path}" unless File.exists?(csv_path)
  create_xlsx(xls_path, sheet_name) do |sheet|
    CSV.foreach(csv_path, col_sep: col_sep, converters: [:numeric, :date_time, :date]) do |csv_row|
      sheet.add_row(csv_row)
    end
  end
end
convert_from_string(csv_string, xls_path, sheet_name: 'from CSV') click to toggle source
# File lib/sepparator/spreadsheet_converter.rb, line 23
def convert_from_string(csv_string, xls_path, sheet_name: 'from CSV')
  create_xlsx(xls_path, sheet_name) do |sheet|
    CSV.parse(csv_string, col_sep: col_sep, converters: [:numeric, :date_time, :date]) do |csv_row|
      sheet.add_row(csv_row)
    end
  end
end

Private Instance Methods

create_xlsx(xls_path, sheet_name) { |sheet| ... } click to toggle source
# File lib/sepparator/spreadsheet_converter.rb, line 32
def create_xlsx(xls_path, sheet_name)
  SimpleXlsx::Serializer.new(xls_path) do |doc|
    doc.add_sheet(sheet_name || 'from CSV') do |sheet|
      yield sheet
    end
  end
end