Class: TransformXlsx::TwoDimensional

Inherits:
DataType
  • Object
show all
Defined in:
lib/transform_xlsx/two_dimensional.rb

Instance Attribute Summary

Attributes inherited from DataType

#data, #start_row

Instance Method Summary collapse

Methods inherited from DataType

#to_hash, #to_json, #validate_input

Constructor Details

#initialize(file_path, sheet_number, start_row) ⇒ TwoDimensional

Go through each row and set first element in row as column, and rest of elements as values corresponding to each element in first row . data(r,c) = value



9
10
11
# File 'lib/transform_xlsx/two_dimensional.rb', line 9

def initialize file_path, sheet_number, start_row
  super( file_path, sheet_number, start_row)
end

Instance Method Details

#process_dataObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/transform_xlsx/two_dimensional.rb', line 13

def process_data
  hashed = Hash.new
  sheet = nil
  @data.each do |sheet_name, sheet|
    hashed[sheet_name] = {}
    columns = sheet[@start_row - 1].compact
    columns.each do |key|
      hashed[sheet_name][key] = {}
    end
    #exclude first row(columns) from further processing
    sheet.each_with_index do |row, idx|
      if (row.compact != [] and idx != 0)
        key1 = row.compact.first
        row.each_with_index do |v,i|
          if i != 0
            hashed[sheet_name][columns[i-1]].merge!({key1 => v}) 
          end
        end
      end
    end
  end
  hashed
end