Commit 7fdf2df7 by Ivan Lan

完善备注,修复导入

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