Commit d7156933 by Ivan Lan

feat: 数据正确 未合并单元格

parent 6eb4b647
......@@ -31,6 +31,10 @@ module TalltyImportExport
end
end
def cluster_seqs
calcute_flatten_value(0, []).map(&:seq)
end
def seq
@seq ||= SecureRandom.uuid
end
......
......@@ -19,19 +19,19 @@ module TalltyImportExport
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
headers = export_headers_result **options
header_obj = export_headers_result **options
workbook.add_worksheet(name: _sheet_name) do |sheet|
if respond_to?(:first_header)
row_index = Axlsx.col_ref(headers.flatten_value.size - 1)
row_index = Axlsx.col_ref(header_obj.flatten_value.size - 1)
sheet.merge_cells("A1:#{row_index}1")
sheet.add_row [first_header], style: title1, height: 30
end
index = 0
sheet.add_row headers.flatten_value.map{ |header| header.name }, style: title2, height: 25
sheet.add_row header_obj.flatten_value.map{ |header| header.name }, style: title2, height: 25
index += 1
value_seq_to_axios = {}
......@@ -42,42 +42,51 @@ module TalltyImportExport
[association_record]
records.each do |record|
lines = TalltyImportExport::ExportPayload.new(record, headers: headers) do |payload, header|
lines = TalltyImportExport::ExportPayload.new(record, header: header_obj) do |payload, header|
_data = header[:source] ?
handle_data(association_record, header, index) :
handle_data(payload, header, index)
end.lines
result = []
lines.transpose.map do |transposed_line|
line = transposed_line
while line[0].nil?
line.shift
line.push(false)
end
line
end.transpose.each do |line|
# if line.select(&:itself).count > 0
result << line
# end
end
# p '----------------------------------------------------------------'
# result.uniq.each { |x| p x }
result_uniq = result.uniq do |line|
line.map(&:value)
end
result_uniq.each_with_index do |line, row_index|
# lines.each { |x| p x.map { |x| x.try(:value) || '________' } }
# result = []
# lines.transpose.map do |transposed_line|
# line = transposed_line
# while line[0].nil?
# line.shift
# line.push(false)
# end
# line
# end.transpose.each do |line|
# if line.select(&:itself).count > 0
# result << line
# end
# end
# result_uniq = result.uniq do |line|
# line.map { |x| x.try(:value) }
# end#.sort do |a, b|
#a.map { |x| x.try(:value) } <=> b.map { |x| x.try(:value) }
#end
p '----------------------------------------------------------------'
lines.each { |x| p x.map { |x| x.try(:value) || '________' } }
lines.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] = []
if (TalltyImportExport::ExportPayload::Value === value)
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]
else
row << nil
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))
formats.push(header_obj.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
......@@ -86,15 +95,15 @@ module TalltyImportExport
end
end
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
# 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
end
......
require 'matrix'
class TalltyImportExport::ExportPayload
attr_reader :value, :context
def initialize(val, headers:, &value_handler)
def initialize(val, header:, &value_handler)
@value = TalltyImportExport::ExportPayload::Value.new(val)
@headers = headers.items # TalltyImportExport::Attr::ExportHeaderItem array
@flatten_headers = headers.flatten_value
@headers = header.items # TalltyImportExport::Attr::ExportHeaderItem array
@flatten_headers = header.flatten_value
@value_handler = value_handler
@context = {
# header_seq_to_cell_seq_to_header_seq_to_value_mapping: {},
value_seq_to_header_seq_to_value: {},
# header_seq_to_header_mapping: @headers.reduce({}) do |out, header|
# out[header.seq] = header
# out
# end,
# header_seq_to_children_mapping: @headers.reduce({}) do |out, header|
# out[header.seq] = header.children
# out
# end,
header_seq_to_value_seq_to_value: {},
value_inherited_seq_to_header_seq_to_value: {},
flatten_headers: @flatten_headers,
}
end
......@@ -31,23 +25,38 @@ class TalltyImportExport::ExportPayload
)
end
max_height_cell_column = cell_columns.map(&:flatten_cells).max do |a, b|
a.count <=> b.count
# 分离的,每个初始 header 为一个矩阵
matrixes = cell_columns.map do |cell_column|
cell_column.tallest_cell_cluster.map(&:grow_to_line)
end
# 计算所有矩阵中最高高度,用于填充
max_matrix_height = matrixes.map(&:count).max
# 填充各个矩阵至等高
detached_matrixes = matrixes.map do |ragged_col|
col = ragged_col.uniq
while col.count < max_matrix_height
col.push(col[0].count.times.to_a)
end
Matrix[*col]
end
max_height_cell_column.map(&:grow_to_line)
result = detached_matrixes.reduce do |out, matrix|
Matrix.hstack(out, matrix)
end.to_a
# result.map { |x| p x.map { |xx| xx.try(:value)}}
result
end
end
class TalltyImportExport::ExportPayload::Value
attr_reader :value, :seq_chain
attr_reader :value, :seq_chain#, :inherited_seq
def initialize val, seq_chain=[]
def initialize(val, seq_chain=[], inherited_seq: false)
@value = val
@seq_chain = [*seq_chain, seq]
# flatten_cells.each do |cell|
# context[:cell_seq_to_header_seq_to_value][cell.seq] = {} unless context[:cell_seq_to_header_seq_to_value][cell.seq]
# context[:cell_seq_to_header_seq_to_value][cell.seq][header.seq] = value
@seq_chain = inherited_seq ? seq_chain : [*seq_chain, seq]
end
def seq
......@@ -57,7 +66,7 @@ end
# 一个 竖列
class TalltyImportExport::ExportPayload::CellColumn
attr_reader :header, :payload, :context, :cell_clusters, :flatten_cells
attr_reader :header, :payload, :context, :cell_clusters, :flatten_cells, :tallest_cell_cluster
def initialize header, value, context, &value_handler
@header = header
......@@ -92,13 +101,17 @@ class TalltyImportExport::ExportPayload::CellColumn
@flatten_cells = []
cell_divise
end
def cell_divise
@flatten_cells = @cell_clusters.map do |cell_cluster|
cell_cluster.divise.cell_cluster
end.flatten
depth_to_cells_mapping = @flatten_cells.group_by { |cell| cell.depth }
@tallest_cell_cluster = depth_to_cells_mapping.values.max { |a, b| a.count <=> b.count }
self
end
end
......@@ -117,12 +130,18 @@ class TalltyImportExport::ExportPayload::Cell
@context = context
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] = {}
# s = value.seq
unless (@context[:header_seq_to_value_seq_to_value][header.seq])
@context[:header_seq_to_value_seq_to_value][header.seq] = {}
end
@context[:value_seq_to_header_seq_to_value][s][header.seq] = value # NOTE: value 对象
@context[:header_seq_to_value_seq_to_value][header.seq][s] = value # NOTE: value 对象
end
# unless (@context[:value_inherited_seq_to_header_seq_to_value][value.inherited_seq])
# @context[:value_inherited_seq_to_header_seq_to_value][value.inherited_seq] = {}
# end
# @context[:value_inherited_seq_to_header_seq_to_value][value.inherited_seq][header.seq] = value # NOTE: value 对象
# parent
@parent_path = parent_path
......@@ -133,8 +152,12 @@ class TalltyImportExport::ExportPayload::Cell
@seq ||= SecureRandom.uuid
end
def heredity_chain
[*self.parent_path, self]
def depth
@parent_path.count
end
def header_chain_seqs
[*@parent_path, self].map { |cell| cell.header.cluster_seqs }.reduce(:concat)
end
def divise
......@@ -145,7 +168,7 @@ class TalltyImportExport::ExportPayload::Cell
val.map do |val|
TalltyImportExport::ExportPayload::Cell.new(
child_header,
TalltyImportExport::ExportPayload::Value.new(val, @value.seq_chain),
TalltyImportExport::ExportPayload::Value.new(val, @value.seq_chain, inherited_seq: true),
@context,
[*self.parent_path, self],
&@value_handler
......@@ -172,16 +195,39 @@ class TalltyImportExport::ExportPayload::Cell
end
def grow_to_line
@context[:flatten_headers].map do |header|
target_val = nil
@value.seq_chain.each do |seq|
if @context[:value_seq_to_header_seq_to_value][seq]&.[](header.seq)
target_val = @context[:value_seq_to_header_seq_to_value][seq][header.seq]
end
@context[:flatten_headers].
select do |header|
header_chain_seqs.include?(header.seq)
end.
map do |header|
target_value = nil
# if @context[:value_inherited_seq_to_header_seq_to_value][@value.inherited_seq]&.[](header.seq)
# target_value = @context[:value_inherited_seq_to_header_seq_to_value][@value.inherited_seq][header.seq]
# end
@value.seq_chain.uniq.reverse.each do |seq|
next if target_value
# seq = @value.seq
if @context[:header_seq_to_value_seq_to_value][header.seq]&.[](seq)
target_value = @context[:header_seq_to_value_seq_to_value][header.seq][seq]
end
end
target_val
# require 'irb'
# if target_value == 'c1_4____'
# binding.irb
# end
# @value.seq_chain.uniq.reverse.each do |seq|
# next if target_value
# if @context[:header_seq_to_value_seq_to_value][seq]&.[](header.seq)
# target_value = @context[:header_seq_to_value_seq_to_value][seq][header.seq]
# end
# end
target_value
end
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