Commit 96230763 by Ivan Lan

Add import import_data

parent 1fdab0cf
......@@ -18,24 +18,17 @@ module TalltyImportExport
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
def import_xlsx xlsx_file, associations, **options
# 先处理获取出来Excel每行的数据, line_info
options = options.with_indifferent_access
self.headers = options.delete(:headers) || []
@primary_keys = options.delete(:primary_keys) || headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
process_options(options)
excel_hash = headers.reduce({}) do |h, header|
h[header[:key].to_sym] = header[:name]
h
end
if tallty_excel === xlsx_file
xlsx_file.rows.each_with_excel_hash(excel_hash) do |line_info|
if TalltyImportExport::Excel === xlsx_file
xlsx_file.rows.each_with_excel_hash(@excel_hash) do |line_info|
process_line_info(line_info, associations)
end
else
file_path = xlsx_file.is_a?(String) ? xlsx_file : xlsx_file.path
xlsx = ::Roo::Excelx.new(file_path)
xlsx.each_with_pagename do |_sheetname, sheet|
sheet.each(**excel_hash).with_index do |line_info, index|
sheet.each(**@excel_hash).with_index do |line_info, index|
next if index == 0
process_line_info(line_info, associations)
end
......@@ -43,8 +36,24 @@ module TalltyImportExport
end
end
def tallty_excel
TalltyImportExport::Excel
def import_data data, associations, **options
process_options(options)
TalltyImportExport::Excel::Rows.new(data).each_with_excel_hash(@excel_hash) do |line_info|
process_line_info(line_info, associations)
end
end
def process_options options
options = options.with_indifferent_access
self.headers = options.delete(:headers) || []
@primary_keys = options.delete(:primary_keys) || headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
@excel_hash = headers.reduce({}) do |h, header|
h[header[:key].to_sym] = header[:name]
h
end
options
end
def process_line_info line_info, associations
......
......@@ -17,6 +17,10 @@ module TalltyImportExport
import_instance.import_xlsx(xlsx_file, associations, **opts)
end
def import_data data, associations, **opts
import_instance.import_data(data, associations, **opts)
end
def import_excel_klass
import_instance.tallty_excel
end
......
......@@ -22,7 +22,7 @@ RSpec.describe TalltyImportExport::Importable do
end
def handle_code code, processing_line_info, raw_line_info
"#{code}_#{raw_line_info['sub_code']}"
"#{code}_#{raw_line_info[:sub_code]}"
end
def handle_sum num, processing_line_info, raw_line_info
......@@ -91,4 +91,25 @@ RSpec.describe TalltyImportExport::Importable do
{"name"=>5, "code"=>"20070105_5", "sub_code"=>nil, "meta"=>{"meta1"=>"meta5", "meta2"=>"metaE" }, "sum" => 15 },
])
end
it 'import data' do
@result = []
@data = [
{ '名称' => 1, '学号' => '20070101', '副学号' => 1, 'meta字段1' => 'meta1', 'meta字段2' => 'metaA', '累加值' => 1 },
{ '名称' => 2, '学号' => '20070102', '副学号' => 2, 'meta字段1' => 'meta2', 'meta字段2' => 'metaB', '累加值' => 2 },
{ '名称' => 3, '学号' => '20070103', '副学号' => 3, 'meta字段1' => 'meta3', 'meta字段2' => 'metaC', '累加值' => 3 },
{ '名称' => 4, '学号' => '20070104', '副学号' => 4, 'meta字段1' => 'meta4', 'meta字段2' => 'metaD', '累加值' => 4 },
{ '名称' => 5, '学号' => '20070105', '副学号' => 5, 'meta字段1' => 'meta5', 'meta字段2' => 'metaE', '累加值' => 5 },
]
Test.import_data(@data, @result)
expect(@result).to eq([
{"name"=>1, "code"=>"20070101_1", "sub_code"=>nil, "meta"=>{"meta1"=>"meta1", "meta2"=>"metaA" }},
{"name"=>2, "code"=>"20070102_2", "sub_code"=>nil, "meta"=>{"meta1"=>"meta2", "meta2"=>"metaB" }},
{"name"=>3, "code"=>"20070103_3", "sub_code"=>nil, "meta"=>{"meta1"=>"meta3", "meta2"=>"metaC" }},
{"name"=>4, "code"=>"20070104_4", "sub_code"=>nil, "meta"=>{"meta1"=>"meta4", "meta2"=>"metaD" }},
{"name"=>5, "code"=>"20070105_5", "sub_code"=>nil, "meta"=>{"meta1"=>"meta5", "meta2"=>"metaE" }},
])
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment