ruby 导入数据临时代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 导入数据临时代码相关的知识,希望对你有一定的参考价值。
task hgds: :environment do
layer = RegionLayer.find(185561)
begin
book = Roo::Spreadsheet.open("#{Rails.root}/hgds.xlsx", extension: '.xlsx')
rescue
return { msg: '文件打开错误', status: 'f' }
end
layer.regions.map(&:soft_del)
layer.attr_fields.delete_all
id_field_id = AttrField.create!(layer: layer, field: '美团门店ID', is_title: true)&.id
name_field_id = AttrField.create!(layer: layer, field: '门店名称')&.id
if book.nil? || book.last_row.nil? || book.first_row.nil?
return { msg: '文件打开错误', status: 'f' }
end
region_style_id = layer.default_region_style.id
first_row = book.first_row
last_row = book.last_row
(first_row + 1).upto(last_row) do |row|
id = book.cell(row, 1)
name = book.cell(row, 2)
points = book.cell(row, 3)
if id.blank? || name.blank? || points.blank?
next
end
attrs = {
id_field_id.to_s => id,
name_field_id.to_s => name
}
p attrs
if points.include?('=>')
points = JSON.parse(points.split('=>')[0])
else
points = JSON.parse(points)
end
path = []
points&.each do |point|
path << "#{point['y']},#{point['x']}"
end
path = path.join(';')
encoded_path = Geospatial.path_encode(path)
layer.regions.create!({
map_id: layer.map_id,
user_id: layer.user_id,
region_type: 0,
path: path,
encoded_path: encoded_path,
region_style_id: region_style_id,
attrs: attrs
})
end
end
以上是关于ruby 导入数据临时代码的主要内容,如果未能解决你的问题,请参考以下文章
ruby 上海街道数据导入
Ruby on Rails - 从 CSV 文件导入数据
完整版excel上传导入读写批量数据并将反馈结果写入远程exel中
ruby 自定义pryrc与临时文件
Ruby:使用ActiveRecord验证将CSV数据导入DB
ruby 使用Google表格API和服务帐户将数据导入Rails APP