Advertisement
Guest User

Untitled

a guest
May 10th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Author: Tibi Turbureanu
  3. # License: GPLv3
  4.  
  5. require 'rubygems'
  6. require 'parseexcel'
  7. require 'active_record'
  8. require 'yaml'
  9.  
  10. require 'elphi/app/models/user.rb'
  11.  
  12. ## constants
  13. # excel file
  14. spreadsheet_file = 'date_studiu_pilot.xls'
  15. db_config_file = 'database.tmp.yml'
  16. # columns id
  17. name_id = 1
  18. email_id = 5
  19. pass_id = 6
  20.  
  21. # use database config file to connect to database
  22. dbconfig = YAML::load(File.open(db_config_file))
  23. connection = ActiveRecord::Base.establish_connection(dbconfig)
  24.  
  25. # parse spreadsheet file
  26. workbook = Spreadsheet::ParseExcel.parse(spreadsheet_file)
  27. worksheet = workbook.worksheet(0)
  28.  
  29. worksheet.each { |row|
  30. i = 0
  31. j = 0
  32. name = email = pass = ""
  33. if row != nil
  34. row.each { |cell|
  35. if cell != nil
  36. contents = cell.to_s('latin1')
  37. case i
  38. when name_id
  39. name = contents
  40. when email_id
  41. email = contents
  42. when pass_id
  43. pass = contents
  44. end
  45. end
  46. i = i + 1
  47. }
  48. j = j + 1
  49. end
  50. if name != "" and email != "" and pass != ""
  51. @user = User.new(:login => name, :email => email, :password => pass, :password_confirmation => pass)
  52. @user.save
  53. if @user.errors.empty?
  54. puts "Successfully created new user:"
  55. puts "Nume: #{name}\nEmail: #{email}\nPass: #{pass}\n\n"
  56. end
  57. end
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement