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
9a8394c5
Commit
9a8394c5
authored
May 07, 2022
by
Ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 使用 value_handler 替换直接取值
parent
6386eaf8
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
20 additions
and
19 deletions
+20
-19
export.rb
lib/tallty_import_export/export.rb
+10
-10
export_form.rb
lib/tallty_import_export/export_form.rb
+3
-3
export_payload.rb
lib/tallty_import_export/export_payload.rb
+2
-2
cell.rb
lib/tallty_import_export/export_payload/cell.rb
+2
-2
cell_column.rb
lib/tallty_import_export/export_payload/cell_column.rb
+3
-2
No files found.
lib/tallty_import_export/export.rb
View file @
9a8394c5
...
...
@@ -62,8 +62,8 @@ module TalltyImportExport
export_workbook
workbook
,
records
,
**
options
end
file_path
=
File
.
join
(
Rails
.
root
,
'public'
,
'export'
)
#
file_path = File.join('/Users/mushu/', 'export')
#
file_path = File.join(Rails.root, 'public', 'export')
file_path
=
File
.
join
(
'/Users/mushu/'
,
'export'
)
FileUtils
.
mkdir_p
(
file_path
)
unless
Dir
.
exist?
(
file_path
)
file_name
=
"
#{
Time
.
now
.
strftime
(
'%Y%m%d%H%M%S'
)
}#{
@filename
}
.xlsx"
file_path_with_name
=
File
.
join
(
file_path
,
file_name
)
...
...
@@ -184,7 +184,7 @@ module TalltyImportExport
end
# 处理一个记录的数据
def
handle_data
record
,
header
,
index
=
0
def
handle_data
record
,
header
,
index
=
0
,
**
opts
data
=
if
header
[
:key
]
==
'_index'
index
...
...
@@ -197,9 +197,9 @@ module TalltyImportExport
else
try_method
(
record
,
header
[
:key
],
prefix:
[
header
[
:prefix
],
header
[
:json
]].
compact
.
join
(
'.'
))
end
data
=
handle_format
(
data
,
header
)
data
=
handle_data_type
(
data
)
data
=
handle_select
(
data
,
header
)
data
=
handle_format
(
data
,
header
,
**
opts
)
data
=
handle_data_type
(
data
,
**
opts
)
data
=
handle_select
(
data
,
header
,
**
opts
)
rescue
''
end
...
...
@@ -215,7 +215,7 @@ module TalltyImportExport
end
# 根据数据类型 attr_type 进行数据的格式化
def
handle_format
data
,
header
def
handle_format
data
,
header
,
**
opts
case
header
[
:attr_type
].
to_s
when
'string'
data
.
to_s
...
...
@@ -228,7 +228,7 @@ module TalltyImportExport
end
end
def
handle_data_type
data
def
handle_data_type
data
,
**
opts
if
data
.
is_a?
(
Time
)
data
.
in_time_zone
.
strftime
(
'%F %H:%M'
)
elsif
data
.
is_a?
(
Date
)
...
...
@@ -237,7 +237,7 @@ module TalltyImportExport
'是'
elsif
data
.
is_a?
(
FalseClass
)
'否'
elsif
data
.
is_a?
(
Array
)
elsif
data
.
is_a?
(
Array
)
&&
!
opts
[
:keep_array
]
if
data
.
first
.
is_a?
(
Hash
)
&&
data
.
first
&
.
dig
(
'url'
).
present?
# 文件array
data
.
map
do
|
item
|
...
...
@@ -251,7 +251,7 @@ module TalltyImportExport
end
end
def
handle_select
data
,
header
def
handle_select
data
,
header
,
**
opts
if
header
[
:select
].
present?
select_option
=
header
[
:select
].
find
{
|
option
|
option
[
:value
].
to_s
==
data
.
to_s
}
select_option
.
present?
?
select_option
[:
label
]
:
data
...
...
lib/tallty_import_export/export_form.rb
View file @
9a8394c5
...
...
@@ -63,10 +63,10 @@ module TalltyImportExport
records
.
each
do
|
record
|
value_living_alone_col_index_to_value_count
=
{}
payload
=
TalltyImportExport
::
ExportPayload
.
new
(
record
,
header:
header_obj
)
do
|
payload
,
header
|
payload
=
TalltyImportExport
::
ExportPayload
.
new
(
record
,
header:
header_obj
)
do
|
payload
,
header
,
**
opts
|
_data
=
header
[
:source
]
?
handle_data
(
association_record
,
header
,
index
)
:
handle_data
(
payload
,
header
,
index
)
handle_data
(
association_record
,
header
,
index
,
**
opts
)
:
handle_data
(
payload
,
header
,
index
,
**
opts
)
end
...
...
lib/tallty_import_export/export_payload.rb
View file @
9a8394c5
...
...
@@ -42,8 +42,8 @@ class TalltyImportExport::ExportPayload
# 填充各个矩阵至等高
detached_matrixes
=
matrixes
.
map
do
|
ragged_col
|
col
=
ragged_col
.
uniq
while
col
.
count
<
@max_matrix_height
&&
col
[
0
]
col
.
push
(
col
[
0
].
count
.
times
.
to_a
)
while
col
.
count
<
@max_matrix_height
col
.
push
(
(
col
[
0
].
try
(
:count
)
||
1
)
.
times
.
to_a
)
end
Matrix
[
*
col
]
end
...
...
lib/tallty_import_export/export_payload/cell.rb
View file @
9a8394c5
...
...
@@ -44,8 +44,8 @@ class TalltyImportExport::ExportPayload::Cell
def
divise
if
@header
.
children
.
present?
&&
@value
.
value
new_cells
=
@header
.
children
.
map
do
|
child_header
|
if
(
child_header
.
children
.
present?
)
val
=
@value
.
value
[
child_header
.
key
.
to_sym
]
val
=
@value_handler
.
call
(
@value
.
value
,
child_header
.
as_json
.
symbolize_keys
,
keep_array:
true
)
if
(
child_header
.
children
.
present?
&&
val
.
is_a?
(
Array
))
val
.
map
do
|
val
|
cell
=
TalltyImportExport
::
ExportPayload
::
Cell
.
new
(
child_header
,
...
...
lib/tallty_import_export/export_payload/cell_column.rb
View file @
9a8394c5
...
...
@@ -9,8 +9,9 @@ class TalltyImportExport::ExportPayload::CellColumn
@cell_clusters
=
[]
@value_handler
=
value_handler
if
header
.
children
&&
@value
.
value
@cell_clusters
=
(
@value
.
value
[
header
.
key
.
to_sym
]
||
[]).
map
do
|
val
|
val
=
@value_handler
.
call
(
@value
.
value
,
@header
.
as_json
.
symbolize_keys
,
keep_array:
true
)
if
header
.
children
&&
val
.
is_a?
(
Array
)
@cell_clusters
=
val
.
map
do
|
val
|
TalltyImportExport
::
ExportPayload
::
Cell
.
new
(
@header
,
TalltyImportExport
::
ExportPayload
::
Value
.
new
(
val
,
@value
.
chain
),
...
...
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