class CsvXlsxConverter::XlsxToCsv

Public Class Methods

new(input_file) click to toggle source
# File lib/csv_xlsx_converter/converter.rb, line 32
def initialize(input_file)
  raise ArgumentError, "input file is not xlsx" unless CsvXlsxConverter::xlsx_filename? input_file
  @input_file = input_file
end

Public Instance Methods

convert(output_file) click to toggle source
# File lib/csv_xlsx_converter/converter.rb, line 37
def convert(output_file)
  raise ArgumentError, "output file is not csv" unless CsvXlsxConverter::csv_filename? output_file

  CSV.open(output_file, "wb") do |csv|
    workbook = RubyXL::Parser.parse @input_file
    worksheet = workbook[0]

    worksheet.each_with_index do |row, row_idx|
      row_data = []
      (0...row.size).each do |col_idx|
        begin
          cell = row[col_idx]
          val = cell.value
          row_data << val
        rescue NoMethodError
          row_data << ""
        end
      end
      csv << row_data
    end
  end
end