Commit 6eb4b647 by Ivan Lan

feat: 导出与合并单元格 & 值还有错

parent bb9bbe82
...@@ -62,7 +62,8 @@ module TalltyImportExport ...@@ -62,7 +62,8 @@ module TalltyImportExport
export_workbook workbook, records, **options export_workbook workbook, records, **options
end 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) FileUtils.mkdir_p(file_path) unless Dir.exist?(file_path)
file_name = "#{Time.now.strftime('%Y%m%d%H%M%S')}#{@filename}.xlsx" file_name = "#{Time.now.strftime('%Y%m%d%H%M%S')}#{@filename}.xlsx"
file_path_with_name = File.join(file_path, file_name) file_path_with_name = File.join(file_path, file_name)
......
...@@ -12,44 +12,37 @@ module TalltyImportExport ...@@ -12,44 +12,37 @@ module TalltyImportExport
def export_workbook workbook, association_records, **options def export_workbook workbook, association_records, **options
# excel导出样式 # 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 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)
# workbook.add_worksheet(name: _sheet_name) do |sheet| row_index = Axlsx.col_ref(headers.flatten_value.size - 1)
# if respond_to?(:first_header) sheet.merge_cells("A1:#{row_index}1")
# row_index = Axlsx.col_ref(headers.size - 1) sheet.add_row [first_header], style: title1, height: 30
# sheet.merge_cells("A1:#{row_index}1") end
# 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 sheet.add_row headers.flatten_value.map{ |header| header.name }, style: title2, height: 25
# merge_column_hash = {} index += 1
# first_content_row_index = respond_to?(:first_header) ? 2 : 1
index = 0 value_seq_to_axios = {}
formats = []
association_records.each do |association_record| association_records.each do |association_record|
records = @each_method.present? ? records = @each_method.present? ?
(try_method(association_record, @each_method) || [nil]) : (try_method(association_record, @each_method) || [nil]) :
[association_record] [association_record]
records.each do |record| records.each do |record|
row = []
formats = []
index += 1
lines = TalltyImportExport::ExportPayload.new(record, headers: headers) do |payload, header| lines = TalltyImportExport::ExportPayload.new(record, headers: headers) do |payload, header|
# p "payload: #{payload} -- header[:key] #{header[:key]}"
_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)
...@@ -64,67 +57,50 @@ module TalltyImportExport ...@@ -64,67 +57,50 @@ module TalltyImportExport
line.push(false) line.push(false)
end end
line line
# transposed_line.compact
end.transpose.each do |line| end.transpose.each do |line|
# if line.select(&:itself).count > 0
if line.select(&:itself).count > 0 result << line
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]
# end # end
# end end
p '----------------------------------------------------------------'
result.uniq.sort.each { |x| p x }
# 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| result_uniq.each_with_index do |line, row_index|
# _data = header[:source] ? row = []
# handle_data(association_record, header, index) : line.each_with_index do |value, col_index|
# handle_data(record, header, 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] value_seq_to_axios.values.each do |axios_ary|
# # 这里使用二维数组,每个数组里都是列内容相同的各行 if axios_ary.count > 1
# merge_column_hash[col_index] ||= [] top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min]
# if merge_column_hash[col_index].last&.last == index + first_content_row_index - 1 bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max]
# # 说明内容和上面的是延续的,继续加入之前的数组 sheet.merge_cells(
# merge_column_hash[col_index].last << index + first_content_row_index Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last)
# else )
# merge_column_hash[col_index] << [index + first_content_row_index - 1, index + first_content_row_index] end
# 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
end 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 end
def export_headers_result **options def export_headers_result **options
@headers = options[:headers] @headers = options.symbolize_keys[:headers]
super(**options) super(**options)
@headers = TalltyImportExport::Attr::ExportHeader.new({ items: @headers }) @headers = TalltyImportExport::Attr::ExportHeader.new({ items: @headers })
end end
......
# 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 class TalltyImportExport::ExportPayload
attr_reader :value, :context attr_reader :value, :context
...@@ -100,7 +39,6 @@ class TalltyImportExport::ExportPayload ...@@ -100,7 +39,6 @@ class TalltyImportExport::ExportPayload
end end
end end
class TalltyImportExport::ExportPayload::Value class TalltyImportExport::ExportPayload::Value
attr_reader :value, :seq_chain attr_reader :value, :seq_chain
...@@ -117,7 +55,6 @@ class TalltyImportExport::ExportPayload::Value ...@@ -117,7 +55,6 @@ class TalltyImportExport::ExportPayload::Value
end end
end 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
...@@ -179,16 +116,11 @@ class TalltyImportExport::ExportPayload::Cell ...@@ -179,16 +116,11 @@ class TalltyImportExport::ExportPayload::Cell
# context # context
@context = 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| value.seq_chain.each do |s|
unless (@context[:value_seq_to_header_seq_to_value][s]) unless (@context[:value_seq_to_header_seq_to_value][s])
@context[:value_seq_to_header_seq_to_value][s] = {} @context[:value_seq_to_header_seq_to_value][s] = {}
end 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 end
# parent # parent
...@@ -240,19 +172,7 @@ class TalltyImportExport::ExportPayload::Cell ...@@ -240,19 +172,7 @@ class TalltyImportExport::ExportPayload::Cell
end end
def grow_to_line def grow_to_line
# heredity_chain_seq = heredity_chain.map(&:seq)
@context[:flatten_headers].map do |header| @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 target_val = nil
@value.seq_chain.each do |seq| @value.seq_chain.each do |seq|
......
...@@ -80,7 +80,8 @@ RSpec.describe TalltyImportExport::Importable do ...@@ -80,7 +80,8 @@ RSpec.describe TalltyImportExport::Importable do
end end
it 'export' do 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
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