Commit c9271cea by Ivan Lan

修复导入表头对应

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