Skip to content

Instantly share code, notes, and snippets.

@felipecabargas
Created November 27, 2013 00:33
Show Gist options
  • Save felipecabargas/7668818 to your computer and use it in GitHub Desktop.
Save felipecabargas/7668818 to your computer and use it in GitHub Desktop.
DatabaseImport test
require 'rubygems'
require 'roo'
s = Roo::Excelx.new("../Hacking/Rails/mpnr/data/Ford.xlsx") #Open the spreadsheet
s.default_sheet = s.sheets.first #Select the page to work with
att_col = s.column(1) #First Column (Attributes Hierachy)
#Gives you a count of cols & rows
col_number = s.last_column()
row_number = s.last_row()
#Necesary arrays to work after
attributes = Array.new
primary_classes = Array.new
secondary_classes = Array.new
#This method creates an array with all the classes
for n in 1..(row_number)
if s.font('A',n).underline?
c = s.cell('A',n)
c = c.tr("/"," ").tr("-","").tr(" ","")
primary_classes.push(c)
end
end
main_hash = primary_classes.each_with_object({ }) { |k, h| h[k] = [ ] }
key = ""
#This method injects secondary hashes into the primary one
for n in 1..(row_number)
if s.font('A',n).bold?
if s.font('A',n).underline?
secondary_hash = secondary_classes.each_with_object({ }) { |k, h| h[k] = [ ] } # Converts array of secondary_classes to hash
main_hash[key] = secondary_hash # Puts the secondary_hash inside the correct main_hash entry
secondary_classes = Array.new # Cleans the array
key = s.cell('A',n).to_s
else
c = s.cell('A',n)
c = c.tr("/"," ").tr("-","").tr(" ","")
secondary_classes.push(c)
end
end
end
for n in 15..(row_number)
if s.font('A',n).bold?
if s.font('A',n).underline?
attributes.each_with_index { |attribute, index| main_hash[attribute.to_sym] = nil }
attributes = Array.new
else
attributes.each_with_index { |attribute, index| secondary_hash[attribute.to_sym] = nil }
attributes = Array.new
end
else
a = s.cell('A',n)
if a.nil?
puts ""
else
a = a.downcase.tr("/"," ").tr("-","").tr("("," ").tr(")","").tr(" ","_")
attributes.push(:"#{a}")
end
end
end
print main_hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment