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
c2d0ebb0
Commit
c2d0ebb0
authored
Dec 10, 2021
by
Ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add import exchange_to_ids
parent
6e443d2c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
2 deletions
+46
-2
import.rb
lib/tallty_import_export/import.rb
+46
-2
No files found.
lib/tallty_import_export/import.rb
View file @
c2d0ebb0
...
...
@@ -21,13 +21,35 @@ module TalltyImportExport
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
def
import_xlsx
xlsx_file
,
associations
,
**
options
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
do
|
line_info
,
associations
|
process_line_info
(
line_info
,
associations
)
end
end
def
exchange_to_ids
xlsx_file
,
associations
,
**
options
errors
=
[]
ids
=
[]
process_xlsx_line_info
(
xlsx_file
,
associations
,
**
options
)
do
|
line_info
,
associations
|
ids
<<
exchange_line_info_to_id
(
line_info
,
associations
,
errors
)
end
if
errors
.
any?
error_msg
=
errors
.
map
do
|
line_info
|
"【
#{
@primary_keys
.
map
{
|
key
|
line_info
[
key
]
}
.join(' - ')}】"
end
.
join
(
' '
)
raise
RecordNotFountError
.
new
(
"以下内容未找到:
#{
error_msg
}
"
)
end
return
ids
.
compact
.
uniq
end
def
process_xlsx_line_info
xlsx_file
,
associations
,
**
options
@associations
=
associations
# 先处理获取出来Excel每行的数据, line_info
process_options
(
options
)
if
TalltyImportExport
::
Excel
===
xlsx_file
xlsx_file
.
rows
.
each_with_excel_hash
(
@excel_hash
)
do
|
line_info
|
process_line_info
(
line_info
.
with_indifferent_access
,
associations
)
yield
line_info
.
with_indifferent_access
,
associations
end
else
file_path
=
xlsx_file
.
is_a?
(
String
)
?
xlsx_file
:
xlsx_file
.
path
...
...
@@ -35,7 +57,7 @@ module TalltyImportExport
xlsx
.
each_with_pagename
do
|
_sheetname
,
sheet
|
sheet
.
each
(
**
@excel_hash
).
with_index
do
|
line_info
,
index
|
next
if
index
==
0
process_line_info
(
line_info
.
with_indifferent_access
,
associations
)
yield
line_info
.
with_indifferent_access
,
associations
end
end
end
...
...
@@ -174,5 +196,27 @@ module TalltyImportExport
associations
.
create!
(
line_info
.
clone
.
except!
(
*
@skip_keys
))
end
end
def
exchange_line_info_to_id
line_info
,
associations
,
errors
# 去除空行内容
return
unless
line_info
.
values
.
any?
(
&
:present?
)
context
.
line_info
=
line_info
return
unless
primary_keys
.
present?
ids
=
associations
.
where
(
line_info
.
clone
.
extract!
(
*
primary_keys
)).
pluck
(
:id
)
context
.
last_line_info
=
line_info
errors
<<
line_info
unless
ids
[
0
]
return
ids
[
0
]
end
class
RecordNotFountError
<
StandardError
attr_accessor
:message
def
initialize
message
@message
=
message
super
()
end
end
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