class FastXlsToHash::ParseXls

Constants

FORMULA_ERROR

Public Class Methods

to_hash(file) click to toggle source
# File lib/fast_xls_to_hash/xls/parse.rb, line 9
def self.to_hash(file)
  book = Spreadsheet.open(file)
  hash = Hash.new{ |hash, key| hash[key] = {} }
  FastXlsToHash.with_xls_style ? with_characters(book, hash) : without_characters(book, hash)

  kill(book)

  hash
end

Private Class Methods

kill(object) click to toggle source
# File lib/fast_xls_to_hash/xls/parse.rb, line 59
def self.kill(object)
  object = nil
  GC.start
end
with_characters(book, hash) click to toggle source
# File lib/fast_xls_to_hash/xls/parse.rb, line 40
def self.with_characters(book, hash)
  book.worksheets.each do |sheet|
    sheet.each_with_index do |row, row_index|
      hash[sheet.name.to_sym]["#{row_index + 1}".to_sym] = {}
      row.each_with_index do |val, index|
        if val.class == Spreadsheet::Formula
          if val.value.class == Spreadsheet::Excel::Error
            hash[sheet.name.to_sym]["#{row_index + 1}".to_sym]["#{Converter.to_characters(index)}".to_sym] = FORMULA_ERROR
            next
          end
          hash[sheet.name.to_sym]["#{row_index + 1}".to_sym]["#{Converter.to_characters(index)}".to_sym] = val.value
        else
          hash[sheet.name.to_sym]["#{row_index + 1}".to_sym]["#{Converter.to_characters(index)}".to_sym] = val
        end
      end
    end
  end
end
without_characters(book, hash) click to toggle source
# File lib/fast_xls_to_hash/xls/parse.rb, line 21
def self.without_characters(book, hash)
  book.worksheets.each do |sheet|
    sheet.each_with_index do |row, row_index|
      hash[sheet.name.to_sym]["row_#{row_index + 1}".to_sym] = {}
      row.each_with_index do |val, index|
        if val.class == Spreadsheet::Formula
          if val.value.class == Spreadsheet::Excel::Error
            hash[sheet.name.to_sym]["row_#{row_index + 1}".to_sym]["column_#{index + 1}".to_sym] = FORMULA_ERROR
            next
          end
          hash[sheet.name.to_sym]["row_#{row_index + 1}".to_sym]["column_#{index + 1}".to_sym] = val.value
        else
          hash[sheet.name.to_sym]["row_#{row_index + 1}".to_sym]["column_#{index + 1}".to_sym] = val
        end
      end
    end
  end
end