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
6eb4b647
Commit
6eb4b647
authored
May 02, 2022
by
Ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 导出与合并单元格 & 值还有错
parent
bb9bbe82
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
157 deletions
+55
-157
export.rb
lib/tallty_import_export/export.rb
+2
-1
export_form.rb
lib/tallty_import_export/export_form.rb
+50
-74
export_payload.rb
lib/tallty_import_export/export_payload.rb
+1
-81
export_spec.rb
spec/export_spec.rb
+2
-1
No files found.
lib/tallty_import_export/export.rb
View file @
6eb4b647
...
...
@@ -62,7 +62,8 @@ module TalltyImportExport
export_workbook
workbook
,
records
,
**
options
end
file_path
=
File
.
join
(
Rails
.
root
,
'public'
,
'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
)
...
...
lib/tallty_import_export/export_form.rb
View file @
6eb4b647
...
...
@@ -12,44 +12,37 @@ module TalltyImportExport
def
export_workbook
workbook
,
association_records
,
**
options
# excel导出样式
alignment
=
{
vertical: :center
,
horizontal: :center
}
border
=
{
color:
'969696'
,
style: :thin
}
title1
=
workbook
.
styles
.
add_style
(
alignment:
alignment
,
border:
border
,
sz:
12
,
b:
true
)
title2
=
workbook
.
styles
.
add_style
(
alignment:
alignment
,
border:
border
,
bg_color:
"2a5caa"
,
sz:
12
,
fg_color:
"fffffb"
)
title3
=
workbook
.
styles
.
add_style
(
alignment:
alignment
.
merge
(
wrap_text:
true
),
border:
border
,
sz:
10
)
_sheet_name
=
respond_to?
(
:sheet_name
)
?
self
.
sheet_name
:
nil
# alignment = { vertical: :center, horizontal: :center }
# border = { color: '969696', style: :thin }
# title1 = workbook.styles.add_style(alignment: alignment, border: border, sz: 12, b: true)
# title2 = workbook.styles.add_style(alignment: alignment, border: border, bg_color: "2a5caa", sz: 12, fg_color: "fffffb")
# title3 = workbook.styles.add_style(alignment: alignment.merge(wrap_text: true), border: border, sz: 10)
headers
=
export_headers_result
**
options
p
headers
.
flatten_value
.
map
(
&
:key
)
# _sheet_name = respond_to?(:sheet_name) ? self.sheet_name : nil
# workbook.add_worksheet(name: _sheet_name) do |sheet|
# if respond_to?(:first_header)
# row_index = Axlsx.col_ref(headers.size - 1)
# sheet.merge_cells("A1:#{row_index}1")
# sheet.add_row [first_header], style: title1, height: 30
# end
workbook
.
add_worksheet
(
name:
_sheet_name
)
do
|
sheet
|
if
respond_to?
(
:first_header
)
row_index
=
Axlsx
.
col_ref
(
headers
.
flatten_value
.
size
-
1
)
sheet
.
merge_cells
(
"A1:
#{
row_index
}
1"
)
sheet
.
add_row
[
first_header
],
style:
title1
,
height:
30
end
# sheet.add_row headers.map{|header| header[:name]}, style: title2, height: 25
index
=
0
# last_row = nil
# merge_column_hash = {}
# first_content_row_index = respond_to?(:first_header) ? 2 : 1
sheet
.
add_row
headers
.
flatten_value
.
map
{
|
header
|
header
.
name
},
style:
title2
,
height:
25
index
+=
1
index
=
0
value_seq_to_axios
=
{}
formats
=
[]
association_records
.
each
do
|
association_record
|
records
=
@each_method
.
present?
?
(
try_method
(
association_record
,
@each_method
)
||
[
nil
])
:
[
association_record
]
records
.
each
do
|
record
|
row
=
[]
formats
=
[]
index
+=
1
lines
=
TalltyImportExport
::
ExportPayload
.
new
(
record
,
headers:
headers
)
do
|
payload
,
header
|
# p "payload: #{payload} -- header[:key] #{header[:key]}"
_data
=
header
[
:source
]
?
handle_data
(
association_record
,
header
,
index
)
:
handle_data
(
payload
,
header
,
index
)
...
...
@@ -64,67 +57,50 @@ module TalltyImportExport
line
.
push
(
false
)
end
line
# transposed_line.compact
end
.
transpose
.
each
do
|
line
|
if
line
.
select
(
&
:itself
).
count
>
0
result
<<
line
.
map
{
|
x
|
x
||
'________'
}
end
end
# result = []
# cache[0].length.times do
# cache.each_with_index do |line, index|
# result[index] = [] unless result[index]
# result[index] << line[index]
# if line.select(&:itself).count > 0
result
<<
line
# end
# end
p
'----------------------------------------------------------------'
result
.
uniq
.
sort
.
each
{
|
x
|
p
x
}
end
# p '----------------------------------------------------------------'
# result.uniq.each { |x| p x }
result_uniq
=
result
.
uniq
do
|
line
|
line
.
map
(
&
:value
)
end
# headers.each_with_index do |header, col_index|
# _data = header[:source] ?
# handle_data(association_record, header, index) :
# handle_data(record, header, index)
result_uniq
.
each_with_index
do
|
line
,
row_index
|
row
=
[]
line
.
each_with_index
do
|
value
,
col_index
|
row
<<
value
.
value
unless
value_seq_to_axios
[
value
.
seq
]
value_seq_to_axios
[
value
.
seq
]
=
[]
end
value_seq_to_axios
[
value
.
seq
]
<<
[
col_index
,
row_index
+
index
]
formats
.
push
(
headers
.
flatten_value
[
col_index
].
format
&
.
to_sym
||
(
row
.
is_a?
(
String
)
?
:
string
:
nil
))
end
sheet
.
add_row
(
row
,
style:
title3
,
height:
@row_height
,
types:
formats
)
index
+=
1
formats
=
[]
end
end
end
# if header[:merge].present? && last_row.present? && _data == last_row[col_index]
# # 这里使用二维数组,每个数组里都是列内容相同的各行
# merge_column_hash[col_index] ||= []
# if merge_column_hash[col_index].last&.last == index + first_content_row_index - 1
# # 说明内容和上面的是延续的,继续加入之前的数组
# merge_column_hash[col_index].last << index + first_content_row_index
# else
# merge_column_hash[col_index] << [index + first_content_row_index - 1, index + first_content_row_index]
# end
# end
# 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
# last_row = row
value_seq_to_axios
.
values
.
each
do
|
axios_ary
|
if
axios_ary
.
count
>
1
top_right
=
[
axios_ary
.
map
(
&
:first
).
min
,
axios_ary
.
map
(
&
:last
).
min
]
bottom_left
=
[
axios_ary
.
map
(
&
:first
).
max
,
axios_ary
.
map
(
&
:last
).
max
]
sheet
.
merge_cells
(
Axlsx
::
cell_r
(
top_right
.
first
,
top_right
.
last
)
+
':'
+
Axlsx
::
cell_r
(
bottom_left
.
first
,
bottom_left
.
last
)
)
end
end
end
# # 需要根据column进行多行的内容合并
# if merge_column_hash.present?
# merge_column_hash.each do |col_index, row_arr|
# row_arr.each do |arr|
# sheet.merge_cells(
# Axlsx::cell_r(col_index, arr.first) + ':' + Axlsx::cell_r(col_index, arr.last)
# )
# end
# end
# end
# sheet.column_widths(*headers.map{|header| (header[:width] || @width).to_f})
# end
end
def
export_headers_result
**
options
@headers
=
options
[
:headers
]
@headers
=
options
.
symbolize_keys
[
:headers
]
super
(
**
options
)
@headers
=
TalltyImportExport
::
Attr
::
ExportHeader
.
new
({
items:
@headers
})
end
...
...
lib/tallty_import_export/export_payload.rb
View file @
6eb4b647
# class TalltyImportExport::ExportPayload
# # header: 导出时候使用的配置模型
# # payload: 对应的单行数据抽取,如果是list属性,则payload是一个array
# attr_accessor :header, :payload, :children, :parent
# def initialize(header:, payload:, parent: nil, children: [])
# @header = header
# @payload = payload
# @children = []
# @parent = parent
# # 递归生成
# if header.is_a?(TalltyImportExport::Attr::ExportHeader)
# @children = header.items.map do |header_item|
# self.class.new(
# header: header_item,
# payload: payload_value(header: header_item, payload: payload),
# parent: self
# )
# end
# else
# if header.children.present?
# @children = header.children.map do |header_item|
# self.class.new(
# header: header_item,
# payload: payload_value(header: header_item, payload: payload),
# parent: self
# )
# end
# end
# end
# end
# def height
# return 0 if payload.blank?
# if header.children.blank?
# # 单属性,如果有值,为1,没有值为0
# payload.present? ? 1 : 0
# else
# # list属性,payload是array
# # 高度是表格里所有属性的height的最大值
# children_height_arr = header.children.map do |header_child|
# Array(payload).map do |payload_child|
# self.class.new(
# header: header_child,
# payload: payload_value(header: header_child, payload: payload_child),
# ).height
# end.sum
# end.max
# end
# end
# # TODO 获取值的方法
# def payload_value header:, payload:
# payload&.dig(header.key)
# end
# end
class
TalltyImportExport
::
ExportPayload
attr_reader
:value
,
:context
...
...
@@ -100,7 +39,6 @@ class TalltyImportExport::ExportPayload
end
end
class
TalltyImportExport
::
ExportPayload
::
Value
attr_reader
:value
,
:seq_chain
...
...
@@ -117,7 +55,6 @@ class TalltyImportExport::ExportPayload::Value
end
end
# 一个 竖列
class
TalltyImportExport
::
ExportPayload
::
CellColumn
attr_reader
:header
,
:payload
,
:context
,
:cell_clusters
,
:flatten_cells
...
...
@@ -179,16 +116,11 @@ class TalltyImportExport::ExportPayload::Cell
# context
@context
=
context
# unless (@context[:header_seq_to_cell_seq_to_header_seq_to_value_mapping][header.seq])
# @context[:header_seq_to_cell_seq_to_header_seq_to_value_mapping][header.seq] = {}
# end
# @context[:header_seq_to_cell_seq_to_header_seq_to_value_mapping][header.seq][seq] = value
value
.
seq_chain
.
each
do
|
s
|
unless
(
@context
[
:value_seq_to_header_seq_to_value
][
s
])
@context
[
:value_seq_to_header_seq_to_value
][
s
]
=
{}
end
@context
[
:value_seq_to_header_seq_to_value
][
s
][
header
.
seq
]
=
value
.
value
@context
[
:value_seq_to_header_seq_to_value
][
s
][
header
.
seq
]
=
value
# NOTE: value 对象
end
# parent
...
...
@@ -240,19 +172,7 @@ class TalltyImportExport::ExportPayload::Cell
end
def
grow_to_line
# heredity_chain_seq = heredity_chain.map(&:seq)
@context
[
:flatten_headers
].
map
do
|
header
|
# cell_seq_to_header_seq_to_value_mapping = @context[:header_seq_to_cell_seq_to_header_seq_to_value_mapping][header.seq]
# target_val = nil
# heredity_chain_seq.each do |seq|
# if cell_seq_to_header_seq_to_value_mapping[seq]
# target_val = cell_seq_to_header_seq_to_value_mapping[seq]
# end
# end
# target_val
target_val
=
nil
@value
.
seq_chain
.
each
do
|
seq
|
...
...
spec/export_spec.rb
View file @
6eb4b647
...
...
@@ -80,7 +80,8 @@ RSpec.describe TalltyImportExport::Importable do
end
it
'export'
do
TalltyImportExport
::
ExportForm
.
new
.
export_workbook
nil
,
[
@payload
],
{
headers:
@header_h
[
:items
]
}
# TalltyImportExport::ExportForm.new.export_workbook nil, [@payload], { headers: @header_h[:items] }
TalltyImportExport
::
ExportForm
.
new
.
export_xlsx
[
@payload
],
{
headers:
@header_h
[
:items
]
}
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