task :get_unit_data => :environment do
require 'roo'
s = Excel.new("unit.xls")
s.default_sheet = s.sheets.first
start_row = s.first_row
end_row = s.last_row
start_column = s.first_column
end_column = s.last_column
(start_row..end_row).each do |row|
unit_name = ""
unit_abbr = ""
is_defalut = false
is_conversion = false
is_equation = false
value = ""
(start_column..end_column).each do |col|
data = s.cell(row,col,s.sheets.first).to_s
next if data.blank?
next if (data=~/\d/)==1
name = data.split("(").first if col ==1
unit_name = data if col==2
unit_abbr = data if col==3
if col > 3
is_defalut = true if data=~/defa/
is_equation = true if data=~/equa/
is_conversion = true if data=~/conver/
value = data if data=~/\d/
end
puts name unless name.blank?
unit_group = UnitGroup.create({:name => name,:company_uid => SYS_UID}) unless name.blank?
end
if (!unit_name.blank?) || (!unit_abbr.blank?)
Unit.transaction do
unit = Unit.new
unit.name = unit_name
unit.abbreviation = unit_abbr
unit.is_default = is_defalut
unit.is_conversion = is_conversion
unit.is_equation = is_equation
unit.value = value
if unit.save
unit_group = UnitGroup.last
unit_group.units << unit
end
end
puts row
end
end
end
require 'roo'
s = Excel.new("unit.xls")
s.default_sheet = s.sheets.first
start_row = s.first_row
end_row = s.last_row
start_column = s.first_column
end_column = s.last_column
(start_row..end_row).each do |row|
unit_name = ""
unit_abbr = ""
is_defalut = false
is_conversion = false
is_equation = false
value = ""
(start_column..end_column).each do |col|
data = s.cell(row,col,s.sheets.first).to_s
next if data.blank?
next if (data=~/\d/)==1
name = data.split("(").first if col ==1
unit_name = data if col==2
unit_abbr = data if col==3
if col > 3
is_defalut = true if data=~/defa/
is_equation = true if data=~/equa/
is_conversion = true if data=~/conver/
value = data if data=~/\d/
end
puts name unless name.blank?
unit_group = UnitGroup.create({:name => name,:company_uid => SYS_UID}) unless name.blank?
end
if (!unit_name.blank?) || (!unit_abbr.blank?)
Unit.transaction do
unit = Unit.new
unit.name = unit_name
unit.abbreviation = unit_abbr
unit.is_default = is_defalut
unit.is_conversion = is_conversion
unit.is_equation = is_equation
unit.value = value
if unit.save
unit_group = UnitGroup.last
unit_group.units << unit
end
end
puts row
end
end
end