Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # Author: Tibi Turbureanu
- # License: GPLv3
- require 'rubygems'
- require 'parseexcel'
- require 'active_record'
- require 'yaml'
- require 'elphi/app/models/user.rb'
- ## constants
- # excel file
- spreadsheet_file = 'date_studiu_pilot.xls'
- db_config_file = 'database.tmp.yml'
- # columns id
- name_id = 1
- email_id = 5
- pass_id = 6
- # use database config file to connect to database
- dbconfig = YAML::load(File.open(db_config_file))
- connection = ActiveRecord::Base.establish_connection(dbconfig)
- # parse spreadsheet file
- workbook = Spreadsheet::ParseExcel.parse(spreadsheet_file)
- worksheet = workbook.worksheet(0)
- worksheet.each { |row|
- i = 0
- j = 0
- name = email = pass = ""
- if row != nil
- row.each { |cell|
- if cell != nil
- contents = cell.to_s('latin1')
- case i
- when name_id
- name = contents
- when email_id
- email = contents
- when pass_id
- pass = contents
- end
- end
- i = i + 1
- }
- j = j + 1
- end
- if name != "" and email != "" and pass != ""
- @user = User.new(:login => name, :email => email, :password => pass, :password_confirmation => pass)
- @user.save
- if @user.errors.empty?
- puts "Successfully created new user:"
- puts "Nume: #{name}\nEmail: #{email}\nPass: #{pass}\n\n"
- end
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement