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
4b8a78c8
Commit
4b8a78c8
authored
May 29, 2025
by
李文强
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(导出): 支持导出带样式的单元格数据
添加对导出Excel时单元格样式的支持,包括字体颜色、加粗和背景色等 处理 proc 返回的带样式信息的哈希数据时直接返回 添加错误日志记录以便调试
parent
765c88db
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
18 deletions
+55
-18
export.rb
lib/tallty_import_export/export.rb
+55
-18
No files found.
lib/tallty_import_export/export.rb
View file @
4b8a78c8
...
...
@@ -139,12 +139,33 @@ module TalltyImportExport
row
=
[]
formats
=
[]
cell_styles
=
[]
# 存储每个单元格的样式
index
+=
1
headers
.
each_with_index
do
|
header
,
col_index
|
_data
=
header
[
:source
]
?
handle_data
(
association_record
,
header
,
index
)
:
handle_data
(
record
,
header
,
index
)
# 处理带有样式信息的数据
if
_data
.
is_a?
(
Hash
)
&&
_data
.
key?
(
:value
)
&&
_data
.
key?
(
:style
)
# 创建新样式,基于 title3 的样式选项,并添加自定义样式
style_options
=
{
alignment:
{
vertical: :center
,
horizontal: :center
,
wrap_text:
true
},
border:
{
color:
'969696'
,
style: :thin
},
sz:
10
}
# 添加自定义样式
style_options
[
:fg_color
]
=
_data
[
:style
][
:color
]
if
_data
[
:style
][
:color
]
style_options
[
:b
]
=
_data
[
:style
][
:bold
]
if
_data
[
:style
].
key?
(
:bold
)
style_options
[
:bg_color
]
=
_data
[
:style
][
:bg_color
]
if
_data
[
:style
][
:bg_color
]
# 创建新样式
cell_style
=
workbook
.
styles
.
add_style
(
style_options
)
cell_styles
[
col_index
]
=
cell_style
_data
=
_data
[
:value
]
end
if
header
[
:merge
].
present?
&&
last_row
.
present?
&&
_data
==
last_row
[
col_index
]
# 这里使用二维数组,每个数组里都是列内容相同的各行
merge_column_hash
[
col_index
]
||=
[]
...
...
@@ -158,7 +179,13 @@ module TalltyImportExport
row
.
push
(
_data
)
formats
.
push
(
header
[
:format
]
&
.
to_sym
||
(
_data
.
is_a?
(
String
)
?
:
string
:
nil
))
end
sheet
.
add_row
row
,
style:
title3
,
height:
@row_height
,
types:
formats
# 添加行并应用样式
row_cells
=
sheet
.
add_row
(
row
,
style:
title3
,
height:
@row_height
,
types:
formats
).
cells
# 应用自定义样式
cell_styles
.
each_with_index
do
|
style
,
col_index
|
row_cells
[
col_index
].
style
=
style
if
style
end
last_row
=
row
end
end
...
...
@@ -226,23 +253,33 @@ module TalltyImportExport
# 处理一个记录的数据
def
handle_data
(
record
,
header
,
index
=
0
,
**
opts
)
data
=
if
header
[
:key
]
==
'_index'
index
elsif
header
[
:method
].
present?
send
(
header
[
:method
],
record
,
header
)
elsif
header
[
:chain
].
present?
try_chain
(
record
,
header
[
:chain
])
elsif
header
[
:proc
]
&&
header
[
:proc
].
respond_to?
(
:call
)
header
[
:proc
].
call
(
record
,
context
)
else
try_method
(
record
,
header
[
:key
],
prefix:
[
header
[
:prefix
],
header
[
:json
]].
compact
.
join
(
'.'
))
end
data
=
handle_format
(
data
,
header
,
**
opts
)
data
=
handle_data_type
(
data
,
**
opts
)
data
=
handle_select
(
data
,
header
,
**
opts
)
rescue
StandardError
''
begin
data
=
if
header
[
:key
]
==
'_index'
index
elsif
header
[
:method
].
present?
send
(
header
[
:method
],
record
,
header
)
elsif
header
[
:chain
].
present?
try_chain
(
record
,
header
[
:chain
])
elsif
header
[
:proc
]
&&
header
[
:proc
].
respond_to?
(
:call
)
result
=
header
[
:proc
].
call
(
record
,
context
)
# 如果 proc 返回的是带有样式信息的哈希,直接返回
if
result
.
is_a?
(
Hash
)
&&
result
.
key?
(
:value
)
&&
result
.
key?
(
:style
)
return
result
end
result
else
try_method
(
record
,
header
[
:key
],
prefix:
[
header
[
:prefix
],
header
[
:json
]].
compact
.
join
(
'.'
))
end
# 处理普通数据
data
=
handle_format
(
data
,
header
,
**
opts
)
data
=
handle_data_type
(
data
,
**
opts
)
handle_select
(
data
,
header
,
**
opts
)
rescue
StandardError
=>
e
Rails
.
logger
.
error
"Error in handle_data:
#{
e
.
message
}
\n
#{
e
.
backtrace
.
join
(
"
\n
"
)
}
"
if
defined?
(
Rails
)
''
end
end
def
try_chain
(
payload
,
arr
)
...
...
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