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
e9126beb
Commit
e9126beb
authored
Dec 25, 2020
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exportable support sheets
parent
9e3a100d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
17 deletions
+50
-17
export.rb
lib/tallty_import_export/export.rb
+48
-15
exportable.rb
lib/tallty_import_export/exportable.rb
+2
-2
No files found.
lib/tallty_import_export/export.rb
View file @
e9126beb
module
TalltyImportExport
class
Export
attr_reader
:klass
,
:headers
attr_reader
:klass
def
initialize
klass
@klass
=
klass
end
# [
# { key: 'entry_user_name', name: '被考核人', chain: [:entry, :user_name] },
# { key: 'entry_user_code', name: '被考核人工号' },
# { key: 'entry_user_department_name', name: '被考核人部门' },
# { key: 'dimension_name', name: '考核维度' },
# { key: 'user_name', name: '考核人' },
# { key: 'user_code', name: '考核人工号' },
# { key: 'user_department_name', name: '考核人部门' },
# { key: 'state', name: '考核状态', method: :state_zh },
# { key: 'score', name: '考核分' },
# ]
# export_headers / headers
# key: 属性的英文名
# name: 属性的中文名
...
...
@@ -13,20 +24,49 @@ module TalltyImportExport
# format: excel是否需要特定的格式,目前主要是类似于身份证号,可以用string
# method: 导出时本地调用的方法
# chain: 导出时对象属性通过链式调用
# index: 数组方式,需要嵌套拿到里面的
def
export_xlsx
records
,
options
=
{}
def
export_xlsx
records
,
**
options
exportable_defaults
(
options
)
options
=
options
.
with_indifferent_access
Axlsx
::
Package
.
new
do
|
pack
|
workbook
=
pack
.
workbook
if
@group_by
.
present?
if
records
.
is_a?
(
Array
)
records
.
group_by
{
|
record
|
record
.
send
(
@group_by
)}.
each
do
|
key
,
group_records
|
@group_key
=
key
export_workbook
workbook
,
group_records
end
else
records
.
group
(
@group_by
).
count
.
keys
.
each
do
|
key
|
@group_key
=
key
export_workbook
workbook
,
records
.
where
(
@group_by
=>
key
)
end
end
else
export_workbook
workbook
,
records
end
file_path
=
File
.
join
(
Rails
.
root
,
'public'
,
'export'
)
FileUtils
.
mkdir_p
(
file_path
)
unless
Dir
.
exist?
(
file_path
)
file_name
=
"
#{
Time
.
now
.
strftime
(
'%Y%m%d%H%M%S'
)
}#{
@filename
}
.xlsx"
pack
.
serialize
(
File
.
join
(
file_path
,
file_name
))
url
=
ActionController
::
Base
.
helpers
.
asset_url
(
"/export/
#{
file_name
}
"
)
return
url
end
end
def
export_workbook
workbook
,
records
# excel导出样式
alignment
=
{
vertical: :center
,
horizontal: :center
}
border
=
{
color:
'969696'
,
style: :thin
}
title1
=
workbook
.
styles
.
add_style
(
alignment:
alignment
,
border:
border
,
sz:
16
,
b:
true
)
title2
=
workbook
.
styles
.
add_style
(
alignment:
alignment
,
border:
border
,
bg_color:
"2a5caa"
,
sz:
16
,
fg_color:
"fffffb"
)
title3
=
workbook
.
styles
.
add_style
(
alignment:
alignment
.
merge
(
wrap_text:
true
),
border:
border
,
sz:
14
)
headers
=
export_headers
workbook
.
add_worksheet
do
|
sheet
|
if
respond_to?
(
:first_header
)
...
...
@@ -35,22 +75,15 @@ module TalltyImportExport
sheet
.
add_row
[
first_header
],
style:
title1
,
height:
40
end
sheet
.
add_row
@
headers
.
map
{
|
header
|
header
[
:name
]},
style:
title2
,
height:
39
sheet
.
add_row
headers
.
map
{
|
header
|
header
[
:name
]},
style:
title2
,
height:
39
each_method
=
records
.
is_a?
(
Array
)
?
:
each
:
:find_each
records
.
send
(
each_method
)
do
|
record
|
row
=
[]
headers
.
each
{
|
header
|
row
.
push
(
handle_data
(
record
,
header
))
}
sheet
.
add_row
row
,
style:
title3
,
height:
@row_height
,
types:
@headers
.
map
{
|
header
|
header
[
:format
]}
end
sheet
.
column_widths
*
@headers
.
map
{
|
header
|
header
[
:width
]
||
@width
}
sheet
.
add_row
row
,
style:
title3
,
height:
@row_height
,
types:
headers
.
map
{
|
header
|
header
[
:format
]}
end
file_path
=
File
.
join
(
Rails
.
root
,
'public'
,
'export'
)
FileUtils
.
mkdir_p
(
file_path
)
unless
Dir
.
exist?
(
file_path
)
file_name
=
"
#{
Time
.
now
.
strftime
(
'%Y%m%d%H%M%S'
)
}#{
@filename
}
.xlsx"
pack
.
serialize
(
File
.
join
(
file_path
,
file_name
))
url
=
ActionController
::
Base
.
helpers
.
asset_url
(
"/export/
#{
file_name
}
"
)
return
url
sheet
.
column_widths
*
headers
.
map
{
|
header
|
header
[
:width
]
||
@width
}
end
end
...
...
@@ -60,7 +93,7 @@ module TalltyImportExport
@row_height
||=
options
.
delete
(
:row_height
)
||
35
@width
||=
options
.
delete
(
:width
)
||
30
@filename
||=
options
.
delete
(
:filename
)
self
.
headers
=
export_headers
@group_by
||=
options
.
delete
(
:group_by
)
end
def
headers
=
val
...
...
@@ -68,14 +101,14 @@ module TalltyImportExport
end
def
export_headers
klass
.
try
(
:headers
)
||
klass
.
try
(
:model_headers
)
@headers
||
klass
.
try
(
:headers
)
||
klass
.
try
(
:model_headers
)
end
# 处理一个记录的数据
def
handle_data
record
,
header
data
=
if
header
[
:method
].
present?
send
(
header
[
:method
],
record
,
header
[
:key
]
)
send
(
header
[
:method
],
record
,
header
)
elsif
header
[
:chain
].
present?
header
[
:chain
].
reduce
(
record
)
do
|
obj
,
method
|
obj
.
send
(
method
)
...
...
lib/tallty_import_export/exportable.rb
View file @
e9126beb
...
...
@@ -13,8 +13,8 @@ module TalltyImportExport
const_get
(
'Export'
).
new
(
self
)
end
def
export_xlsx
*
arg
s
export_instance
.
export_xlsx
(
*
arg
s
)
def
export_xlsx
records
,
**
option
s
export_instance
.
export_xlsx
(
records
,
**
option
s
)
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