Commit bde58662 by Ivan Lan

feat: 实现 header 合并

parent e638cb04
...@@ -50,11 +50,51 @@ module TalltyImportExport ...@@ -50,11 +50,51 @@ module TalltyImportExport
attr_json :items, ExportHeaderItem.to_type, array: true attr_json :items, ExportHeaderItem.to_type, array: true
attr_reader :header_seq_to_axios
def flatten_value def flatten_value
items.map do |item| items.map do |item|
item.calcute_flatten_value(0, []) item.calcute_flatten_value(0, [])
end.flatten end.flatten
end end
def height
flatten_value.map(&:depth).max + 1
end
def header_lines
result = []
height.times.each do |row_index|
col_index = 0
next_line = flatten_value.map do |header|
next_header = [*header.try(:parent_path), header]&.[](row_index)
@header_seq_to_axios ||= {}
if next_header
@header_seq_to_axios[next_header.seq] ||= []
@header_seq_to_axios[next_header.seq].push([col_index, row_index])
end
# 处理最后一列
if (row_index === height - 1)
@header_seq_to_axios[header.seq] ||= []
@header_seq_to_axios[header.seq].push([col_index, row_index])
end
col_index += 1
# 下一级继续递归 或 保持自己以合并
next_header || header
end
result.push(next_line)
end
result
end
end end
end end
end end
...@@ -21,7 +21,6 @@ module TalltyImportExport ...@@ -21,7 +21,6 @@ module TalltyImportExport
header_obj = export_headers_result **options header_obj = export_headers_result **options
workbook.add_worksheet(name: _sheet_name) do |sheet| workbook.add_worksheet(name: _sheet_name) do |sheet|
if respond_to?(:first_header) if respond_to?(:first_header)
row_index = Axlsx.col_ref(header_obj.flatten_value.size - 1) row_index = Axlsx.col_ref(header_obj.flatten_value.size - 1)
...@@ -31,8 +30,22 @@ module TalltyImportExport ...@@ -31,8 +30,22 @@ module TalltyImportExport
index = 0 index = 0
sheet.add_row header_obj.flatten_value.map{ |header| header.name }, style: title2, height: 25 header_obj.header_lines.each do |header_line|
index += 1 sheet.add_row(header_line.map(&:name), style: title2, height: 25)
index += 1
end
# 合并相同 header
p header_obj.header_seq_to_axios.values
header_obj.header_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
value_seq_to_axios = {} value_seq_to_axios = {}
......
...@@ -54,28 +54,28 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -54,28 +54,28 @@ RSpec.describe TalltyImportExport::Importable do
} }
@header_h = { @header_h = {
items: [ items: [
{ key: :a }, { key: :a, name: :a },
{ key: :b }, { key: :b, name: :b },
{ key: :c, children: [ { key: :c, name: :c, children: [
{ key: :c1 }, { key: :c1, name: :c1 },
{ key: :c2 }, { key: :c2, name: :c2 },
{ key: :c3 }, { key: :c3, name: :c3 },
] }, ] },
{ key: :d, children: [ { key: :d, name: :d, children: [
{ key: :d1 }, { key: :d1, name: :d1 },
{ key: :d2 }, { key: :d2, name: :d2 },
] }, ] },
{ key: :e, children: [ { key: :e, name: :e, children: [
{ key: :e1 }, { key: :e1, name: :e1 },
{ key: :e2 }, { key: :e2, name: :e2 },
{ key: :e3, children: [ { key: :e3, name: :e3, children: [
{ key: :e31 }, { key: :e31, name: :e31 },
{ key: :e32 }, { key: :e32, name: :e32 },
{ key: :e33 }, { key: :e33, name: :e33 },
]}, ]},
{ key: :e4 }, { key: :e4, name: :e4 },
] }, ] },
{ key: :f }, { key: :f, name: :f },
] ]
} }
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment