Commit d0a65122 by liyijie

fix: 解决try_chain报错

parent 3fb7db33
...@@ -2,7 +2,7 @@ module TalltyImportExport ...@@ -2,7 +2,7 @@ module TalltyImportExport
class Export class Export
attr_reader :klass, :context attr_reader :klass, :context
def initialize klass def initialize(klass)
@klass = klass @klass = klass
@context = Context.new({}) @context = Context.new({})
end end
...@@ -34,7 +34,7 @@ module TalltyImportExport ...@@ -34,7 +34,7 @@ module TalltyImportExport
# proc: proc或者lamda,支持call,传入 record 和 context # proc: proc或者lamda,支持call,传入 record 和 context
# list: 对于list布局的,进行嵌套,生成子表格 # list: 对于list布局的,进行嵌套,生成子表格
def export_xlsx records, **options def export_xlsx(records, **options)
records = with_scope records records = with_scope records
process_options(options) process_options(options)
...@@ -46,14 +46,16 @@ module TalltyImportExport ...@@ -46,14 +46,16 @@ module TalltyImportExport
if @group_by.present? if @group_by.present?
if records.is_a?(Array) if records.is_a?(Array)
records.group_by { |record| record.send(@group_by)}.each do |key, group_records| records.group_by { |record| record.send(@group_by) }.each do |key, group_records|
next unless key.present? next unless key.present?
@group_key = key @group_key = key
export_workbook workbook, group_records, **options export_workbook workbook, group_records, **options
end end
else else
records.group(@group_by).count.keys.each do |key| records.group(@group_by).count.keys.each do |key|
next unless key.present? next unless key.present?
@group_key = key @group_key = key
export_workbook workbook, records.ransack("#{@group_where}" => key).result, **options export_workbook workbook, records.ransack("#{@group_where}" => key).result, **options
end end
...@@ -72,16 +74,16 @@ module TalltyImportExport ...@@ -72,16 +74,16 @@ module TalltyImportExport
end end
end end
def export_workbook workbook, association_records, **options def export_workbook(workbook, association_records, **options)
# excel导出样式 # excel导出样式
alignment = { vertical: :center, horizontal: :center } alignment = { vertical: :center, horizontal: :center }
border = { color: '969696', style: :thin } border = { color: '969696', style: :thin }
title1 = workbook.styles.add_style(alignment: alignment, border: border, sz: 12, b: true) 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") 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) 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)
_sheet_name = respond_to?(:sheet_name) ? self.sheet_name : nil _sheet_name = respond_to?(:sheet_name) ? sheet_name : nil
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)
...@@ -90,7 +92,7 @@ module TalltyImportExport ...@@ -90,7 +92,7 @@ module TalltyImportExport
sheet.add_row [first_header], style: title1, height: 30 sheet.add_row [first_header], style: title1, height: 30
end end
sheet.add_row headers.map{|header| header[:name]}, style: title2, height: 25 sheet.add_row headers.map { |header| header[:name] }, style: title2, height: 25
last_row = nil last_row = nil
merge_column_hash = {} merge_column_hash = {}
...@@ -133,12 +135,12 @@ module TalltyImportExport ...@@ -133,12 +135,12 @@ module TalltyImportExport
merge_column_hash.each do |col_index, row_arr| merge_column_hash.each do |col_index, row_arr|
row_arr.each do |arr| row_arr.each do |arr|
sheet.merge_cells( sheet.merge_cells(
Axlsx::cell_r(col_index, arr.first) + ':' + Axlsx::cell_r(col_index, arr.last) Axlsx.cell_r(col_index, arr.first) + ':' + Axlsx.cell_r(col_index, arr.last),
) )
end end
end end
end end
sheet.column_widths(*headers.map{|header| (header[:width] || @width).to_f}) sheet.column_widths(*headers.map { |header| (header[:width] || @width).to_f })
end end
end end
...@@ -147,7 +149,7 @@ module TalltyImportExport ...@@ -147,7 +149,7 @@ module TalltyImportExport
{} {}
end end
def process_options options = {} def process_options(options = {})
options = export_options.merge(options).with_indifferent_access options = export_options.merge(options).with_indifferent_access
@row_height ||= options.delete(:row_height) || 25 @row_height ||= options.delete(:row_height) || 25
...@@ -161,30 +163,30 @@ module TalltyImportExport ...@@ -161,30 +163,30 @@ module TalltyImportExport
context.params = @params context.params = @params
end end
def with_scope records def with_scope(records)
records records
end end
def export_headers_result **options def export_headers_result(**options)
if @headers.present? && @group_key.blank? if @headers.present? && @group_key.blank?
headers_hash = @headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access headers_hash = @headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access
export_headers(**options.symbolize_keys).select do |_header| export_headers(**options.symbolize_keys).select do |_header|
_header.with_indifferent_access[:key].to_s.in?(headers_hash.keys) _header.with_indifferent_access[:key].to_s.in?(headers_hash.keys)
end.map do |_header| end.map do |_header|
_header = _header.with_indifferent_access _header = _header.with_indifferent_access
_header.merge(headers_hash[_header[:key]].delete_if { |k, v| v.blank? }) _header.merge(headers_hash[_header[:key]].delete_if { |_k, v| v.blank? })
end end
else else
@headers = export_headers(**options.symbolize_keys) @headers = export_headers(**options.symbolize_keys)
end end
end end
def export_headers **args def export_headers(**args)
@headers || klass.try(:headers) || klass.try(:model_headers) @headers || klass.try(:headers) || klass.try(:model_headers)
end end
# 处理一个记录的数据 # 处理一个记录的数据
def handle_data record, header, index=0, **opts def handle_data(record, header, index = 0, **opts)
data = data =
if header[:key] == '_index' if header[:key] == '_index'
index index
...@@ -200,28 +202,36 @@ module TalltyImportExport ...@@ -200,28 +202,36 @@ module TalltyImportExport
data = handle_format(data, header, **opts) data = handle_format(data, header, **opts)
data = handle_data_type(data, **opts) data = handle_data_type(data, **opts)
data = handle_select(data, header, **opts) data = handle_select(data, header, **opts)
rescue rescue StandardError
'' ''
end end
def try_chain record, arr def try_chain(record, arr)
arr.reduce(record) do |r, m| arr.reduce(record) do |r, m|
if r.is_a?(Array) if r.is_a?(Array)
r.try(m) || r.try(:[], m.to_i) r.try(m) || r.try(:[], m.to_i)
else else
r.try(:[], m) || r.try(:[], m.to_sym) || r.try(m) begin
r.try(:[], m)
rescue StandardError
nil
end || begin
r.try(:[], m.to_sym)
rescue StandardError
nil
end || r.try(m)
end end
end end
end end
def try_method record, method, prefix: nil def try_method(record, method, prefix: nil)
prefix_arr = prefix.to_s.split(/\./) prefix_arr = prefix.to_s.split(/\./)
arr = method.to_s.split(/\./) arr = method.to_s.split(/\./)
try_chain record, prefix_arr + arr try_chain record, prefix_arr + arr
end end
# 根据数据类型 attr_type 进行数据的格式化 # 根据数据类型 attr_type 进行数据的格式化
def handle_format data, header, **opts def handle_format(data, header, **opts)
case header[:attr_type].to_s case header[:attr_type].to_s
when 'string' when 'string'
data.to_s data.to_s
...@@ -234,7 +244,7 @@ module TalltyImportExport ...@@ -234,7 +244,7 @@ module TalltyImportExport
end end
end end
def handle_data_type data, **opts def handle_data_type(data, **opts)
if data.is_a?(Time) if data.is_a?(Time)
data.in_time_zone.strftime('%F %H:%M') data.in_time_zone.strftime('%F %H:%M')
elsif data.is_a?(Date) elsif data.is_a?(Date)
...@@ -257,7 +267,7 @@ module TalltyImportExport ...@@ -257,7 +267,7 @@ module TalltyImportExport
end end
end end
def handle_select data, header, **opts def handle_select(data, header, **opts)
if header[:select].present? if header[:select].present?
select_option = header[:select].find { |option| option[:value].to_s == data.to_s } select_option = header[:select].find { |option| option[:value].to_s == data.to_s }
select_option.present? ? select_option[:label] : data select_option.present? ? select_option[:label] : data
......
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