Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tallty_import_export
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
tallty_import_export
Commits
6266c3a8
Commit
6266c3a8
authored
Nov 21, 2020
by
Ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
concat options headers with import headers
parent
fff7e592
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
5 deletions
+26
-5
import.rb
lib/tallty_import_export/import.rb
+25
-4
importable_spec.rb
spec/importable_spec.rb
+1
-1
No files found.
lib/tallty_import_export/import.rb
View file @
6266c3a8
...
...
@@ -19,8 +19,7 @@ module TalltyImportExport
def
import_xlsx
xlsx_file
,
associations
,
**
options
# 先处理获取出来Excel每行的数据, line_info
options
=
options
.
with_indifferent_access
self
.
headers
=
options
.
delete
(
:headers
)
||
import_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
|
...
...
@@ -76,11 +75,33 @@ module TalltyImportExport
end
def
import_headers
klass
.
try
(
:headers
)
||
klass
.
try
(
:model_headers
)
klass
.
try
(
:headers
)
||
klass
.
try
(
:model_headers
)
||
(
raise
ArgumentError
.
new
(
'missing import_headers'
))
end
# 只保留 key, name, json, 合并到 import_header
def
headers
=
val
@headers
=
val
.
map
{
|
header
|
header
.
with_indifferent_access
}
if
val
.
empty?
@headers
=
import_headers
.
map
{
|
header
|
header
.
with_indifferent_access
}
return
end
key_to_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
else
header
end
end
end
def
skip
val
,
processing_line_info
,
raw_line_info
...
...
spec/importable_spec.rb
View file @
6266c3a8
...
...
@@ -40,7 +40,7 @@ RSpec.describe TalltyImportExport::Importable do
# 4 20070104 4 meta4 metaD
# 5 20070105 5 meta5 metaE
@result
=
[]
Test
.
import_xlsx
(
'./spec/test1.xlsx'
,
@result
)
Test
.
import_xlsx
(
'./spec/test1.xlsx'
,
@result
,
headers:
[{
key:
'name'
,
name:
'名称'
}]
)
expect
(
@result
).
to
eq
([
{
"name"
=>
1
,
"code"
=>
"20070101_1"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta1"
,
"meta2"
=>
"metaA"
},
"sum"
=>
1
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment