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
a2c2f5d0
Commit
a2c2f5d0
authored
Nov 12, 2021
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 导入和导出支持proc
parent
555ed997
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
9 additions
and
1 deletion
+9
-1
export.rb
lib/tallty_import_export/export.rb
+3
-0
import.rb
lib/tallty_import_export/import.rb
+5
-0
version.rb
lib/tallty_import_export/version.rb
+1
-1
No files found.
lib/tallty_import_export/export.rb
View file @
a2c2f5d0
...
@@ -29,6 +29,7 @@ module TalltyImportExport
...
@@ -29,6 +29,7 @@ module TalltyImportExport
# json: model_payload,代表存储在某一个列中
# json: model_payload,代表存储在某一个列中
# select: [{ label: '已报备', value: 'submitted'}, ...],需要转换的枚举类型
# select: [{ label: '已报备', value: 'submitted'}, ...],需要转换的枚举类型
# source: true,如果source为true,代表从association_record 进行属性查询
# source: true,如果source为true,代表从association_record 进行属性查询
# proc: proc或者lamda,支持call,传入 record 和 context
def
export_xlsx
records
,
**
options
def
export_xlsx
records
,
**
options
records
=
with_scope
records
records
=
with_scope
records
...
@@ -174,6 +175,8 @@ module TalltyImportExport
...
@@ -174,6 +175,8 @@ module TalltyImportExport
try_chain
(
record
,
header
[
:chain
])
try_chain
(
record
,
header
[
:chain
])
elsif
header
[
:json
]
elsif
header
[
:json
]
record
.
send
(
header
[
:json
])[
header
[
:key
]]
record
.
send
(
header
[
:json
])[
header
[
:key
]]
elsif
header
[
:proc
]
&&
header
[
:proc
].
respond_to?
(
:call
)
header
[
:proc
].
call
(
record
)
else
else
try_method
(
record
,
header
[
:key
])
try_method
(
record
,
header
[
:key
])
end
end
...
...
lib/tallty_import_export/import.rb
View file @
a2c2f5d0
...
@@ -16,6 +16,7 @@ module TalltyImportExport
...
@@ -16,6 +16,7 @@ module TalltyImportExport
# primary_key: 是否是主键
# primary_key: 是否是主键
# json: model_payload,代表存储在某一个列中
# json: model_payload,代表存储在某一个列中
# finder: 执行代码,可以查找到相关的对象
# finder: 执行代码,可以查找到相关的对象
# proc: proc或者lamda,支持call,传入 val 和 context
# 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
...
@@ -55,6 +56,7 @@ module TalltyImportExport
...
@@ -55,6 +56,7 @@ module TalltyImportExport
@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
@params
=
options
@params
=
options
context
.
params
=
@params
@excel_hash
=
@headers
.
reduce
({})
do
|
h
,
header
|
@excel_hash
=
@headers
.
reduce
({})
do
|
h
,
header
|
h
[
header
[
:key
].
to_sym
]
=
header
[
:name
]
h
[
header
[
:key
].
to_sym
]
=
header
[
:name
]
...
@@ -67,6 +69,7 @@ module TalltyImportExport
...
@@ -67,6 +69,7 @@ module TalltyImportExport
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
# 转换处理导入的数据格式
# 转换处理导入的数据格式
line_info
=
convert_data
(
line_info
)
line_info
=
convert_data
(
line_info
)
...
@@ -88,6 +91,8 @@ module TalltyImportExport
...
@@ -88,6 +91,8 @@ module TalltyImportExport
if
header
[
:json
]
if
header
[
:json
]
h
[
header
[
:json
]]
||=
{}
h
[
header
[
:json
]]
||=
{}
h
[
header
[
:json
]][
k
]
=
val
h
[
header
[
:json
]][
k
]
=
val
elsif
header
[
:proc
]
&&
header
[
:proc
].
respond_to?
(
:call
)
h
[
k
.
to_sym
]
=
header
[
:proc
].
call
(
val
,
context
)
elsif
header
[
:finder
]
elsif
header
[
:finder
]
# $SAFE = 2
# $SAFE = 2
h
[
k
.
to_sym
]
=
eval
header
[
:finder
]
h
[
k
.
to_sym
]
=
eval
header
[
:finder
]
...
...
lib/tallty_import_export/version.rb
View file @
a2c2f5d0
module
TalltyImportExport
module
TalltyImportExport
VERSION
=
"1.0.2
6
"
VERSION
=
"1.0.2
9
"
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