xlsimport/import.rb

59 lines
1.4 KiB
Ruby
Raw Normal View History

#!/usr/bin/ruby
require 'yaml'
require 'csv'
require 'mysql2'
require './sql.rb'
config = YAML::load_file(File.join(__dir__, 'config.yaml'))
host = config["db_host"]
username = config["db_user"]
password = config["db_password"]
database = config["db_database"]
$database = Mysql2::Client.new(
:host => host,
:username => username,
:password => password,
:database => database
)
def parse_file(input, index_brand, index_name, index_code, index_cross)
input.each_line do |line|
begin
row = CSV.parse(line)
if row == nil
next
end
row = row[0]
if row[index_cross].match(/^\s*$/) then
next
end
brand = row[index_brand]
name = row[index_name]
code = row[index_code]
cross = row[index_cross].split(',')
brand_id = get_brand_id(brand)
part_id = insert_part(name, brand_id, '', code)
cross.each do |item|
cross_id = insert_part(name, brand_id, '', item)
link_crosses(cross_id, part_id)
end
rescue => e
puts e.message
puts e.backtrace.join("\n\t")
next
end
end
end
input = File.open("ИверсАвто.csv")
input.readline # пропускаем шапку CSV
parse_file(input, 0, 2, 5, 4)
input.close unless input.nil?
input = File.open("Автооптима Остатки 14.01.csv")
input.readline # пропускаем шапку CSV
input.readline
input.readline
parse_file(input, 7, 8, 2, 3)
input.close unless input.nil?