Commit d7156933 by Ivan Lan

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

parent 6eb4b647
...@@ -31,6 +31,10 @@ module TalltyImportExport ...@@ -31,6 +31,10 @@ module TalltyImportExport
end end
end end
def cluster_seqs
calcute_flatten_value(0, []).map(&:seq)
end
def seq def seq
@seq ||= SecureRandom.uuid @seq ||= SecureRandom.uuid
end end
......
...@@ -19,19 +19,19 @@ module TalltyImportExport ...@@ -19,19 +19,19 @@ module TalltyImportExport
title3 = workbook.styles.add_style(alignment: alignment.merge(wrap_text: true), border: border, sz: 10) 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 _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| workbook.add_worksheet(name: _sheet_name) do |sheet|
if respond_to?(:first_header) 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.merge_cells("A1:#{row_index}1")
sheet.add_row [first_header], style: title1, height: 30 sheet.add_row [first_header], style: title1, height: 30
end end
index = 0 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 index += 1
value_seq_to_axios = {} value_seq_to_axios = {}
...@@ -42,42 +42,51 @@ module TalltyImportExport ...@@ -42,42 +42,51 @@ module TalltyImportExport
[association_record] [association_record]
records.each do |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] ? _data = header[:source] ?
handle_data(association_record, header, index) : handle_data(association_record, header, index) :
handle_data(payload, header, index) handle_data(payload, header, index)
end.lines end.lines
result = [] # lines.each { |x| p x.map { |x| x.try(:value) || '________' } }
# result = []
lines.transpose.map do |transposed_line|
line = transposed_line # lines.transpose.map do |transposed_line|
while line[0].nil? # line = transposed_line
line.shift # while line[0].nil?
line.push(false) # line.shift
end # line.push(false)
line # end
end.transpose.each do |line| # line
# if line.select(&:itself).count > 0 # end.transpose.each do |line|
result << line # if line.select(&:itself).count > 0
# end # result << line
end # end
# end
# p '----------------------------------------------------------------'
# result.uniq.each { |x| p x }
result_uniq = result.uniq do |line| # result_uniq = result.uniq do |line|
line.map(&:value) # line.map { |x| x.try(:value) }
end # end#.sort do |a, b|
#a.map { |x| x.try(:value) } <=> b.map { |x| x.try(:value) }
result_uniq.each_with_index do |line, row_index| #end
p '----------------------------------------------------------------'
lines.each { |x| p x.map { |x| x.try(:value) || '________' } }
lines.each_with_index do |line, row_index|
row = [] row = []
line.each_with_index do |value, col_index| line.each_with_index do |value, col_index|
row << value.value if (TalltyImportExport::ExportPayload::Value === value)
unless value_seq_to_axios[value.seq] row << value.value
value_seq_to_axios[value.seq] = [] 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 end
value_seq_to_axios[value.seq] << [col_index , row_index + index] formats.push(header_obj.flatten_value[col_index].format&.to_sym || (row.is_a?(String) ? :string : nil))
formats.push(headers.flatten_value[col_index].format&.to_sym || (row.is_a?(String) ? :string : nil))
end end
sheet.add_row(row, style: title3, height: @row_height, types: formats) sheet.add_row(row, style: title3, height: @row_height, types: formats)
index += 1 index += 1
...@@ -86,15 +95,15 @@ module TalltyImportExport ...@@ -86,15 +95,15 @@ module TalltyImportExport
end end
end end
value_seq_to_axios.values.each do |axios_ary| # value_seq_to_axios.values.each do |axios_ary|
if axios_ary.count > 1 # if axios_ary.count > 1
top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min] # top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min]
bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max] # bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max]
sheet.merge_cells( # sheet.merge_cells(
Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last) # Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last)
) # )
end # end
end # end
end end
end end
......
require 'matrix'
class TalltyImportExport::ExportPayload class TalltyImportExport::ExportPayload
attr_reader :value, :context attr_reader :value, :context
def initialize(val, headers:, &value_handler) def initialize(val, header:, &value_handler)
@value = TalltyImportExport::ExportPayload::Value.new(val) @value = TalltyImportExport::ExportPayload::Value.new(val)
@headers = headers.items # TalltyImportExport::Attr::ExportHeaderItem array @headers = header.items # TalltyImportExport::Attr::ExportHeaderItem array
@flatten_headers = headers.flatten_value @flatten_headers = header.flatten_value
@value_handler = value_handler @value_handler = value_handler
@context = { @context = {
# header_seq_to_cell_seq_to_header_seq_to_value_mapping: {}, header_seq_to_value_seq_to_value: {},
value_seq_to_header_seq_to_value: {}, value_inherited_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,
flatten_headers: @flatten_headers, flatten_headers: @flatten_headers,
} }
end end
...@@ -31,23 +25,38 @@ class TalltyImportExport::ExportPayload ...@@ -31,23 +25,38 @@ class TalltyImportExport::ExportPayload
) )
end end
max_height_cell_column = cell_columns.map(&:flatten_cells).max do |a, b| # 分离的,每个初始 header 为一个矩阵
a.count <=> b.count 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 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
end end
class TalltyImportExport::ExportPayload::Value 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 @value = val
@seq_chain = [*seq_chain, seq] @seq_chain = inherited_seq ? 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
end end
def seq def seq
...@@ -57,7 +66,7 @@ end ...@@ -57,7 +66,7 @@ end
# 一个 竖列 # 一个 竖列
class TalltyImportExport::ExportPayload::CellColumn 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 def initialize header, value, context, &value_handler
@header = header @header = header
...@@ -92,13 +101,17 @@ class TalltyImportExport::ExportPayload::CellColumn ...@@ -92,13 +101,17 @@ class TalltyImportExport::ExportPayload::CellColumn
@flatten_cells = [] @flatten_cells = []
cell_divise cell_divise
end end
def cell_divise def cell_divise
@flatten_cells = @cell_clusters.map do |cell_cluster| @flatten_cells = @cell_clusters.map do |cell_cluster|
cell_cluster.divise.cell_cluster cell_cluster.divise.cell_cluster
end.flatten 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 self
end end
end end
...@@ -117,12 +130,18 @@ class TalltyImportExport::ExportPayload::Cell ...@@ -117,12 +130,18 @@ class TalltyImportExport::ExportPayload::Cell
@context = context @context = context
value.seq_chain.each do |s| value.seq_chain.each do |s|
unless (@context[:value_seq_to_header_seq_to_value][s]) # s = value.seq
@context[:value_seq_to_header_seq_to_value][s] = {} unless (@context[:header_seq_to_value_seq_to_value][header.seq])
@context[:header_seq_to_value_seq_to_value][header.seq] = {}
end 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 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
@parent_path = parent_path @parent_path = parent_path
...@@ -133,8 +152,12 @@ class TalltyImportExport::ExportPayload::Cell ...@@ -133,8 +152,12 @@ class TalltyImportExport::ExportPayload::Cell
@seq ||= SecureRandom.uuid @seq ||= SecureRandom.uuid
end end
def heredity_chain def depth
[*self.parent_path, self] @parent_path.count
end
def header_chain_seqs
[*@parent_path, self].map { |cell| cell.header.cluster_seqs }.reduce(:concat)
end end
def divise def divise
...@@ -145,7 +168,7 @@ class TalltyImportExport::ExportPayload::Cell ...@@ -145,7 +168,7 @@ class TalltyImportExport::ExportPayload::Cell
val.map do |val| val.map do |val|
TalltyImportExport::ExportPayload::Cell.new( TalltyImportExport::ExportPayload::Cell.new(
child_header, child_header,
TalltyImportExport::ExportPayload::Value.new(val, @value.seq_chain), TalltyImportExport::ExportPayload::Value.new(val, @value.seq_chain, inherited_seq: true),
@context, @context,
[*self.parent_path, self], [*self.parent_path, self],
&@value_handler &@value_handler
...@@ -172,16 +195,39 @@ class TalltyImportExport::ExportPayload::Cell ...@@ -172,16 +195,39 @@ class TalltyImportExport::ExportPayload::Cell
end end
def grow_to_line def grow_to_line
@context[:flatten_headers].map do |header| @context[:flatten_headers].
target_val = nil select do |header|
header_chain_seqs.include?(header.seq)
@value.seq_chain.each do |seq| end.
if @context[:value_seq_to_header_seq_to_value][seq]&.[](header.seq) map do |header|
target_val = @context[:value_seq_to_header_seq_to_value][seq][header.seq] target_value = nil
end
# 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 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 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