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
96230763
Commit
96230763
authored
Dec 01, 2020
by
Ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add import import_data
parent
1fdab0cf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
14 deletions
+48
-14
import.rb
lib/tallty_import_export/import.rb
+22
-13
importable.rb
lib/tallty_import_export/importable.rb
+4
-0
importable_spec.rb
spec/importable_spec.rb
+22
-1
No files found.
lib/tallty_import_export/import.rb
View file @
96230763
...
...
@@ -18,24 +18,17 @@ module TalltyImportExport
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
def
import_xlsx
xlsx_file
,
associations
,
**
options
# 先处理获取出来Excel每行的数据, line_info
options
=
options
.
with_indifferent_access
self
.
headers
=
options
.
delete
(
:headers
)
||
[]
@primary_keys
=
options
.
delete
(
:primary_keys
)
||
headers
.
map
{
|
header
|
header
[
:primary_key
]
?
header
[:
key
].
to_sym
:
nil
}.
compact
process_options
(
options
)
excel_hash
=
headers
.
reduce
({})
do
|
h
,
header
|
h
[
header
[
:key
].
to_sym
]
=
header
[
:name
]
h
end
if
tallty_excel
===
xlsx_file
xlsx_file
.
rows
.
each_with_excel_hash
(
excel_hash
)
do
|
line_info
|
if
TalltyImportExport
::
Excel
===
xlsx_file
xlsx_file
.
rows
.
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
process_line_info
(
line_info
,
associations
)
end
else
file_path
=
xlsx_file
.
is_a?
(
String
)
?
xlsx_file
:
xlsx_file
.
path
xlsx
=
::
Roo
::
Excelx
.
new
(
file_path
)
xlsx
.
each_with_pagename
do
|
_sheetname
,
sheet
|
sheet
.
each
(
**
excel_hash
).
with_index
do
|
line_info
,
index
|
sheet
.
each
(
**
@
excel_hash
).
with_index
do
|
line_info
,
index
|
next
if
index
==
0
process_line_info
(
line_info
,
associations
)
end
...
...
@@ -43,8 +36,24 @@ module TalltyImportExport
end
end
def
tallty_excel
TalltyImportExport
::
Excel
def
import_data
data
,
associations
,
**
options
process_options
(
options
)
TalltyImportExport
::
Excel
::
Rows
.
new
(
data
).
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
process_line_info
(
line_info
,
associations
)
end
end
def
process_options
options
options
=
options
.
with_indifferent_access
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
|
h
[
header
[
:key
].
to_sym
]
=
header
[
:name
]
h
end
options
end
def
process_line_info
line_info
,
associations
...
...
lib/tallty_import_export/importable.rb
View file @
96230763
...
...
@@ -17,6 +17,10 @@ module TalltyImportExport
import_instance
.
import_xlsx
(
xlsx_file
,
associations
,
**
opts
)
end
def
import_data
data
,
associations
,
**
opts
import_instance
.
import_data
(
data
,
associations
,
**
opts
)
end
def
import_excel_klass
import_instance
.
tallty_excel
end
...
...
spec/importable_spec.rb
View file @
96230763
...
...
@@ -22,7 +22,7 @@ RSpec.describe TalltyImportExport::Importable do
end
def
handle_code
code
,
processing_line_info
,
raw_line_info
"
#{
code
}
_
#{
raw_line_info
[
'sub_code'
]
}
"
"
#{
code
}
_
#{
raw_line_info
[
:sub_code
]
}
"
end
def
handle_sum
num
,
processing_line_info
,
raw_line_info
...
...
@@ -91,4 +91,25 @@ RSpec.describe TalltyImportExport::Importable do
{
"name"
=>
5
,
"code"
=>
"20070105_5"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta5"
,
"meta2"
=>
"metaE"
},
"sum"
=>
15
},
])
end
it
'import data'
do
@result
=
[]
@data
=
[
{
'名称'
=>
1
,
'学号'
=>
'20070101'
,
'副学号'
=>
1
,
'meta字段1'
=>
'meta1'
,
'meta字段2'
=>
'metaA'
,
'累加值'
=>
1
},
{
'名称'
=>
2
,
'学号'
=>
'20070102'
,
'副学号'
=>
2
,
'meta字段1'
=>
'meta2'
,
'meta字段2'
=>
'metaB'
,
'累加值'
=>
2
},
{
'名称'
=>
3
,
'学号'
=>
'20070103'
,
'副学号'
=>
3
,
'meta字段1'
=>
'meta3'
,
'meta字段2'
=>
'metaC'
,
'累加值'
=>
3
},
{
'名称'
=>
4
,
'学号'
=>
'20070104'
,
'副学号'
=>
4
,
'meta字段1'
=>
'meta4'
,
'meta字段2'
=>
'metaD'
,
'累加值'
=>
4
},
{
'名称'
=>
5
,
'学号'
=>
'20070105'
,
'副学号'
=>
5
,
'meta字段1'
=>
'meta5'
,
'meta字段2'
=>
'metaE'
,
'累加值'
=>
5
},
]
Test
.
import_data
(
@data
,
@result
)
expect
(
@result
).
to
eq
([
{
"name"
=>
1
,
"code"
=>
"20070101_1"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta1"
,
"meta2"
=>
"metaA"
}},
{
"name"
=>
2
,
"code"
=>
"20070102_2"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta2"
,
"meta2"
=>
"metaB"
}},
{
"name"
=>
3
,
"code"
=>
"20070103_3"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta3"
,
"meta2"
=>
"metaC"
}},
{
"name"
=>
4
,
"code"
=>
"20070104_4"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta4"
,
"meta2"
=>
"metaD"
}},
{
"name"
=>
5
,
"code"
=>
"20070105_5"
,
"sub_code"
=>
nil
,
"meta"
=>
{
"meta1"
=>
"meta5"
,
"meta2"
=>
"metaE"
}},
])
end
end
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