Skip to content

Instantly share code, notes, and snippets.

@djvs
Last active November 30, 2015 21:13
Show Gist options
  • Save djvs/c52e7d98006895da2d9a to your computer and use it in GitHub Desktop.
Save djvs/c52e7d98006895da2d9a to your computer and use it in GitHub Desktop.
Convert xlsx spreadsheets to Neo4jrb models and import them (may need work to improve type & attribute name handling)
# this script comes in 2 parts - you will have to reload your environment to open the models before running the last ~25 lines block!
#CONFIG
@filedir = "/location/of/xlsx/files/"
@modelsdir = "/rails/apps/myapp/app/models/fromxlsx/"
files = Dir.glob(@filedir + "*")
fieldnames = {}
klasses = {}
# FILES TO CLASSES
convert = {}
files.each do |f|
puts "Please enter the class name to use for #{f.gsub(@filedir,'')}:"
convert[f] = gets.strip
puts "OK, thanks, got #{convert[f]}.\n"
end
# GET CLASS DEFINITIONS
files.each do |f|
xlsx = Roo::Spreadsheet.open(f)
sh = xlsx.sheet(0)
reference = sh.row(1)
kname = convert[f]
puts kname
puts reference.inspect
fieldnames[kname] = reference.map{|x|x.downcase}
arrs = []
(2...100).to_a.each do |x|
arrs << sh.row(x)
end
klasses[kname] = {}
(0..(sh.row(1).length - 1)).to_a.each do |i|
values = arrs.map{|x|x[i]}
if Set.new(values.select{|x|!x.nil?}) == Set.new([0,1])
myklass = 'Boolean'
else
myklass = arrs.map{|x|x[i].class}.uniq.select{|x| x != NilClass }.first.to_s
end
klasses[kname][reference[i].downcase] = myklass
end
end
# WRITE CLASSES
klasses.each do |k,v|
lcaseclass = k.downcase
str = "class #{k}
include Neo4j::ActiveNode
"
v.each do |k1,v1|
v1 = "DateTime" if v1 == "Date" # rails/neo data type fix
v1 = "Integer" if v1 == "Fixnum" # rails/neo data type fix
k1 = "dosuppress" if k1 == "suppress" # neo4j method name conflict
if v1.to_s.strip == ""
str << " property :#{k1}\n"
else
str << " property :#{k1}, type: #{v1}\n"
end
end
str << "end"
addr = @modelsdir + lcaseclass + ".rb"
unless File.exist?(addr)
File.open(addr, 'w') {|f| f.write(str)}
end
end
renames = {}
renames["suppress"] = "dosuppress"
raise StandardError, "Please exit and reopen your shell!"
files.each do |f|
xlsx = Roo::Spreadsheet.open(f)
sh = xlsx.sheet(0)
reference = sh.row(1).map{|x|x.downcase}
kname = convert[f]
begin
klass = kname.constantize
puts reference.inspect
puts kname
sh.each_with_index do |r,i|
if i > 1
zip = reference.zip(r)
obj = klass.new
zip.each do |col|
renames.each do |k,v|
col[0] = v if col[0] == k
end
obj.public_send(col[0]+"=",col[1])
end
obj.save
end
end
rescue NameError
puts "Models not initialized yet! Please run this last block of the script after reloading your shell."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment