Commit 7fdf2df7 by Ivan Lan

完善备注,修复导入

parent c9271cea
...@@ -56,6 +56,7 @@ module TalltyImportExport ...@@ -56,6 +56,7 @@ module TalltyImportExport
mapping = excel_hash.invert mapping = excel_hash.invert
each do |row| each do |row|
line_info = row.reduce({}) do |out, (k, v)| line_info = row.reduce({}) do |out, (k, v)|
next out unless mapping[k]
out[mapping[k]] = v out[mapping[k]] = v
out out
end end
......
...@@ -19,7 +19,7 @@ module TalltyImportExport ...@@ -19,7 +19,7 @@ module TalltyImportExport
def import_xlsx xlsx_file, associations, **options def import_xlsx xlsx_file, associations, **options
# 先处理获取出来Excel每行的数据, line_info # 先处理获取出来Excel每行的数据, line_info
options = options.with_indifferent_access options = options.with_indifferent_access
self.headers = options.delete(:headers) || {} self.headers = options.delete(:headers) || []
@primary_keys = options.delete(:primary_keys) || headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact @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| excel_hash = headers.reduce({}) do |h, header|
......
...@@ -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 }, # 表格里 name 为 累加值
] ]
end end
end end
...@@ -33,12 +33,12 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -33,12 +33,12 @@ RSpec.describe TalltyImportExport::Importable do
end end
it 'import file directly' do it 'import file directly' do
# 名称 学号 副学号 meta字段1 meta字段2 # 名称 学号 副学号 meta字段1 meta字段2 累加值
# 1 20070101 1 meta1 metaA # 1 20070101 1 meta1 metaA 1
# 2 20070102 2 meta2 metaB # 2 20070102 2 meta2 metaB 2
# 3 20070103 3 meta3 metaC # 3 20070103 3 meta3 metaC 3
# 4 20070104 4 meta4 metaD # 4 20070104 4 meta4 metaD 4
# 5 20070105 5 meta5 metaE # 5 20070105 5 meta5 metaE 5
@result = [] @result = []
Test.import_xlsx('./spec/test1.xlsx', @result, Test.import_xlsx('./spec/test1.xlsx', @result,
headers: [ headers: [
...@@ -61,12 +61,12 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -61,12 +61,12 @@ RSpec.describe TalltyImportExport::Importable do
end end
it 'import excel object' do it 'import excel object' do
# 名称 学号 副学号 meta字段1 meta字段2 # 名称 学号 副学号 meta字段1 meta字段2 累加值
# 1 20070101 1 meta1 metaA # 1 20070101 1 meta1 metaA 1
# 2 20070102 2 meta2 metaB # 2 20070102 2 meta2 metaB 2
# 3 20070103 3 meta3 metaC # 3 20070103 3 meta3 metaC 3
# 4 20070104 4 meta4 metaD # 4 20070104 4 meta4 metaD 4
# 5 20070105 5 meta5 metaE # 5 20070105 5 meta5 metaE 5
@result = [] @result = []
@excel = TalltyImportExport::Excel.new @excel = TalltyImportExport::Excel.new
......
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