Commit c9271cea by Ivan Lan

修复导入表头对应

parent b7247174
......@@ -85,23 +85,21 @@ module TalltyImportExport
return
end
key_to_header = val.reduce({}) do |out, header|
key_to_coming_header = val.reduce({}) do |out, header|
out[header.with_indifferent_access[:key].to_sym] = header.with_indifferent_access
out
end
@headers = import_headers.map do |header|
coming_header = key_to_header[header.with_indifferent_access[:key].to_sym]
if coming_header
dup_header = header.dup
dup_header[:key] = coming_header[:key] || dup_header[:key]
dup_header[:name] = coming_header[:name] || dup_header[:name]
dup_header[:json] = coming_header[:json] || dup_header[:json]
dup_header
result = []
val.map do |header|
if (exist_header = import_headers.find { |model_header| model_header[:key] === header[:key] })
result.push(exist_header.merge(name: header[:name], json: header[:json]))
else
header
result.push({ key: header[:key], name: header[:name], json: header[:json]})
end
end
@headers = result
end
def skip val, processing_line_info, raw_line_info
......
......@@ -11,7 +11,7 @@ RSpec.describe TalltyImportExport::Importable do
{ key: 'sub_code', name: '副学号', convert: :skip },
{ key: 'meta1', name: 'meta字段1', json: :meta },
{ key: 'meta2', name: 'meta字段2', json: :meta },
{ key: 'sum', name: '累值', convert: :handle_sum },
{ key: 'sum', name: '累值', convert: :handle_sum },
]
end
end
......@@ -40,7 +40,16 @@ RSpec.describe TalltyImportExport::Importable do
# 4 20070104 4 meta4 metaD
# 5 20070105 5 meta5 metaE
@result = []
Test.import_xlsx('./spec/test1.xlsx', @result, headers: [{ key: 'name', name: '名称' }])
Test.import_xlsx('./spec/test1.xlsx', @result,
headers: [
{ key: 'name', name: '名称' },
{ key: 'code', name: '学号' },
{ key: 'sub_code', name: '副学号' },
{ key: 'meta1', name: 'meta字段1', json: :meta },
{ key: 'meta2', name: 'meta字段2', json: :meta },
{ key: 'sum', name: '累加值' },
]
)
expect(@result).to eq([
{"name"=>1, "code"=>"20070101_1", "sub_code"=>nil, "meta"=>{"meta1"=>"meta1", "meta2"=>"metaA" }, "sum" => 1 },
......@@ -63,7 +72,16 @@ RSpec.describe TalltyImportExport::Importable do
@excel = TalltyImportExport::Excel.new
@excel.load('./spec/test1.xlsx')
Test.import_xlsx(@excel, @result)
Test.import_xlsx(@excel, @result,
headers: [
{ key: 'name', name: '名称' },
{ key: 'code', name: '学号' },
{ key: 'sub_code', name: '副学号' },
{ key: 'meta1', name: 'meta字段1', json: :meta },
{ key: 'meta2', name: 'meta字段2', json: :meta },
{ key: 'sum', name: '累加值' },
]
)
expect(@result).to eq([
{"name"=>1, "code"=>"20070101_1", "sub_code"=>nil, "meta"=>{"meta1"=>"meta1", "meta2"=>"metaA" }, "sum" => 1 },
......
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