User Tools

Site Tools


xls-to-ar.rb
#
#  Copyright 2019 Oleg Borodin  <borodin@unix7.org>
#
require 'roo'
require 'csv'
 
require 'active_record'
require 'sqlite3'
 
source_file = 'schedule.xlsx'
sheet = Roo::Spreadsheet.open(source_file, extension: :xlsx)
sheet.default_sheet = "Кухни"
sheet.parse(headers: true, :clean => true)
 
csv_file = "schedule.csv"
 
csv = sheet.to_csv
file = File.open(csv_file, "w")
file.write csv
file.close
 
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "schedule.sqlite3")
#ActiveRecord::Base.logger = Logger.new(STDOUT)
 
class Schedule < ActiveRecord::Base
end
 
class Migration < ActiveRecord::Migration[6.0]
  def change
    create_table :schedules, force: true do |t|
      t.string :ro
      t.string :ro_code
      #t.timestamps
    end
  end
end
 
Migration.new.change
 
CSV.foreach(csv_file, headers: true) do |row|
    ro = row[1]
    ro_code = row[2]
    Schedule.create(ro: ro, ro_code: ro_code)
end
 
puts Schedule.last.to_json

Result

$ ruby "xls-roo.rb"
-- create_table(:schedules, {:force=>true})
   -> 0.0161s
{"id":2899,"ro":"СЗРО.ст.007295","ro_code":"Кухонная рекламация Питер Стойки - 0483 СЗКУ000000483"}