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
05d7d2fe
Commit
05d7d2fe
authored
Aug 31, 2022
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: import value strip
parent
d3251130
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
25 deletions
+29
-25
import.rb
lib/tallty_import_export/import.rb
+29
-25
No files found.
lib/tallty_import_export/import.rb
View file @
05d7d2fe
...
@@ -4,7 +4,7 @@ module TalltyImportExport
...
@@ -4,7 +4,7 @@ module TalltyImportExport
require
'roo-xls'
require
'roo-xls'
attr_reader
:klass
,
:context
,
:primary_keys
,
:associations
attr_reader
:klass
,
:context
,
:primary_keys
,
:associations
def
initialize
klass
def
initialize
(
klass
)
@klass
=
klass
@klass
=
klass
@context
=
Context
.
new
({})
@context
=
Context
.
new
({})
end
end
...
@@ -21,13 +21,13 @@ module TalltyImportExport
...
@@ -21,13 +21,13 @@ module TalltyImportExport
# skip: 用来综合使用的数据,但是不导入
# skip: 用来综合使用的数据,但是不导入
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
def
import_xlsx
xlsx_file
,
associations
,
**
options
def
import_xlsx
(
xlsx_file
,
associations
,
**
options
)
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
do
|
line_info
,
associations
|
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
do
|
line_info
,
associations
|
process_line_info
(
line_info
,
associations
)
process_line_info
(
line_info
,
associations
)
end
end
end
end
def
exchange_to_ids
xlsx_file
,
associations
,
**
options
def
exchange_to_ids
(
xlsx_file
,
associations
,
**
options
)
errors
=
[]
errors
=
[]
ids
=
[]
ids
=
[]
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
do
|
line_info
,
associations
|
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
do
|
line_info
,
associations
|
...
@@ -38,17 +38,17 @@ module TalltyImportExport
...
@@ -38,17 +38,17 @@ module TalltyImportExport
error_msg
=
errors
.
map
do
|
line_info
|
error_msg
=
errors
.
map
do
|
line_info
|
"【
#{
@primary_keys
.
map
{
|
key
|
line_info
[
key
]
}
.join(' - ')}】"
"【
#{
@primary_keys
.
map
{
|
key
|
line_info
[
key
]
}
.join(' - ')}】"
end
.
join
(
' '
)
end
.
join
(
' '
)
raise
RecordNotFountError
.
new
(
"以下内容未找到:
#{
error_msg
}
"
)
raise
RecordNotFountError
,
"以下内容未找到:
#{
error_msg
}
"
end
end
return
ids
.
compact
.
uniq
ids
.
compact
.
uniq
end
end
def
process_xlsx_line_info
xlsx_file
,
associations
,
**
options
def
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
@associations
=
associations
@associations
=
associations
# 先处理获取出来Excel每行的数据, line_info
# 先处理获取出来Excel每行的数据, line_info
process_options
(
options
)
process_options
(
options
)
if
TalltyImportExport
::
Excel
===
xlsx_file
if
xlsx_file
.
is_a?
(
TalltyImportExport
::
Excel
)
xlsx_file
.
rows
.
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
xlsx_file
.
rows
.
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
yield
line_info
.
with_indifferent_access
,
associations
yield
line_info
.
with_indifferent_access
,
associations
end
end
...
@@ -58,13 +58,14 @@ module TalltyImportExport
...
@@ -58,13 +58,14 @@ module TalltyImportExport
xlsx
.
each_with_pagename
do
|
_sheetname
,
sheet
|
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
next
if
index
==
0
yield
line_info
.
with_indifferent_access
,
associations
yield
line_info
.
with_indifferent_access
,
associations
end
end
end
end
end
end
end
end
def
import_data
data
,
associations
,
**
options
def
import_data
(
data
,
associations
,
**
options
)
process_options
(
options
)
process_options
(
options
)
TalltyImportExport
::
Excel
::
Rows
.
new
(
data
).
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
TalltyImportExport
::
Excel
::
Rows
.
new
(
data
).
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
process_line_info
(
line_info
.
with_indifferent_access
,
associations
)
process_line_info
(
line_info
.
with_indifferent_access
,
associations
)
...
@@ -75,7 +76,7 @@ module TalltyImportExport
...
@@ -75,7 +76,7 @@ module TalltyImportExport
TalltyImportExport
::
Excel
TalltyImportExport
::
Excel
end
end
def
process_options
options
def
process_options
(
options
)
options
=
import_options
.
merge
(
options
).
with_indifferent_access
options
=
import_options
.
merge
(
options
).
with_indifferent_access
@headers
=
options
.
delete
(
:headers
)
||
import_headers
@headers
=
options
.
delete
(
:headers
)
||
import_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
...
@@ -83,17 +84,17 @@ module TalltyImportExport
...
@@ -83,17 +84,17 @@ module TalltyImportExport
@params
=
options
@params
=
options
context
.
params
=
@params
context
.
params
=
@params
@excel_hash
=
@headers
.
reduce
({})
do
|
h
,
header
|
@excel_hash
=
@headers
.
each_with_object
({})
do
|
header
,
h
|
h
[
header
[
:key
].
to_sym
]
=
header
[
:name
]
h
[
header
[
:key
].
to_sym
]
=
header
[
:name
]
h
end
end
options
options
end
end
def
process_line_info
line_info
,
associations
def
process_line_info
(
line_info
,
associations
)
# 去除空行内容
# 去除空行内容
return
unless
line_info
.
values
.
any?
(
&
:present?
)
return
unless
line_info
.
values
.
any?
(
&
:present?
)
context
.
line_info
=
line_info
context
.
line_info
=
line_info
# 转换处理导入的数据格式
# 转换处理导入的数据格式
line_info
=
convert_data
(
line_info
)
line_info
=
convert_data
(
line_info
)
...
@@ -105,14 +106,15 @@ module TalltyImportExport
...
@@ -105,14 +106,15 @@ module TalltyImportExport
context
.
last_line_info
=
line_info
context
.
last_line_info
=
line_info
end
end
def
convert_data
line_info
def
convert_data
(
line_info
)
info
=
line_info
.
with_indifferent_access
info
=
line_info
.
with_indifferent_access
import_headers_result
.
reduce
({})
do
|
h
,
header
|
import_headers_result
.
each_with_object
({})
do
|
header
,
h
|
k
=
header
[
:key
]
k
=
header
[
:key
]
v
=
info
[
k
]
v
=
info
[
k
]
# header[:convert] = handle_xxx
# header[:convert] = handle_xxx
# handle_xxx(val, processing_line_info, raw_line_info)
# handle_xxx(val, processing_line_info, raw_line_info)
val
=
header
[
:convert
]
?
send
(
header
[:
convert
],
v
,
h
,
info
)
:
v
val
=
header
[
:convert
]
?
send
(
header
[:
convert
],
v
,
h
,
info
)
:
v
val
.
strip!
if
val
.
is_a?
(
String
)
if
header
[
:json
]
if
header
[
:json
]
h
[
header
[
:json
]]
||=
{}
h
[
header
[
:json
]]
||=
{}
h
[
header
[
:json
]][
k
]
=
val
h
[
header
[
:json
]][
k
]
=
val
...
@@ -124,12 +126,11 @@ module TalltyImportExport
...
@@ -124,12 +126,11 @@ module TalltyImportExport
else
else
h
[
k
.
to_sym
]
=
val
h
[
k
.
to_sym
]
=
val
end
end
h
end
.
with_indifferent_access
end
.
with_indifferent_access
end
end
# 通过转换后,数据是否合法,如果不合法,则直接跳过不处理这个数据
# 通过转换后,数据是否合法,如果不合法,则直接跳过不处理这个数据
def
valid?
line_info
def
valid?
(
line_info
)
true
true
end
end
...
@@ -142,12 +143,12 @@ module TalltyImportExport
...
@@ -142,12 +143,12 @@ module TalltyImportExport
_header
.
with_indifferent_access
[
:key
].
to_s
.
in?
(
headers_hash
.
keys
)
_header
.
with_indifferent_access
[
:key
].
to_s
.
in?
(
headers_hash
.
keys
)
end
.
map
do
|
_header
|
end
.
map
do
|
_header
|
_header
=
_header
.
with_indifferent_access
_header
=
_header
.
with_indifferent_access
_header
.
merge
(
headers_hash
[
_header
[
:key
]].
delete_if
{
|
k
,
v
|
v
.
blank?
})
_header
.
merge
(
headers_hash
[
_header
[
:key
]].
delete_if
{
|
_
k
,
v
|
v
.
blank?
})
end
end
else
else
@headers
=
import_headers
(
**
options
.
symbolize_keys
)
@headers
=
import_headers
(
**
options
.
symbolize_keys
)
end
end
rescue
rescue
StandardError
@headers
@headers
end
end
...
@@ -155,8 +156,8 @@ module TalltyImportExport
...
@@ -155,8 +156,8 @@ module TalltyImportExport
{}
{}
end
end
def
import_headers
**
args
def
import_headers
(
**
args
)
@headers
||
klass
.
try
(
:headers
)
||
klass
.
try
(
:model_headers
)
||
(
raise
ArgumentError
.
new
(
'missing import_headers'
)
)
@headers
||
klass
.
try
(
:headers
)
||
klass
.
try
(
:model_headers
)
||
(
raise
ArgumentError
,
'missing import_headers'
)
end
end
# # 只保留 key, name, json, 合并到 import_header
# # 只保留 key, name, json, 合并到 import_header
...
@@ -183,12 +184,12 @@ module TalltyImportExport
...
@@ -183,12 +184,12 @@ module TalltyImportExport
# @headers = result
# @headers = result
# end
# end
def
skip
val
,
processing_line_info
,
raw_line_info
def
skip
(
val
,
processing_line_info
,
raw_line_info
)
# do nothing there, use for header[:convert]
# do nothing there, use for header[:convert]
end
end
### 这个方法是可以由复杂业务进行重载的 ###
### 这个方法是可以由复杂业务进行重载的 ###
def
import_record
line_info
,
associations
def
import_record
(
line_info
,
associations
)
if
primary_keys
.
present?
if
primary_keys
.
present?
_record
=
associations
.
find_or_initialize_by
(
line_info
.
clone
.
extract!
(
*
primary_keys
))
_record
=
associations
.
find_or_initialize_by
(
line_info
.
clone
.
extract!
(
*
primary_keys
))
_record
.
update!
(
line_info
.
clone
.
except!
(
*
primary_keys
,
*
@skip_keys
))
_record
.
update!
(
line_info
.
clone
.
except!
(
*
primary_keys
,
*
@skip_keys
))
...
@@ -197,23 +198,26 @@ module TalltyImportExport
...
@@ -197,23 +198,26 @@ module TalltyImportExport
end
end
end
end
def
exchange_line_info_to_id
line_info
,
associations
,
errors
def
exchange_line_info_to_id
(
line_info
,
associations
,
errors
)
# 去除空行内容
# 去除空行内容
return
unless
line_info
.
values
.
any?
(
&
:present?
)
return
unless
line_info
.
values
.
any?
(
&
:present?
)
context
.
line_info
=
line_info
context
.
line_info
=
line_info
return
unless
primary_keys
.
present?
return
unless
primary_keys
.
present?
ids
=
associations
.
where
(
line_info
.
clone
.
extract!
(
*
primary_keys
)).
pluck
(
:id
)
ids
=
associations
.
where
(
line_info
.
clone
.
extract!
(
*
primary_keys
)).
pluck
(
:id
)
context
.
last_line_info
=
line_info
context
.
last_line_info
=
line_info
errors
<<
line_info
unless
ids
[
0
]
errors
<<
line_info
unless
ids
[
0
]
return
ids
[
0
]
ids
[
0
]
end
end
class
RecordNotFountError
<
StandardError
class
RecordNotFountError
<
StandardError
attr_accessor
:message
attr_accessor
:message
def
initialize
message
def
initialize
(
message
)
@message
=
message
@message
=
message
super
()
super
()
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