Commit b3803f03 by liyijie

feat: 判断数据格式,自动转换excel格式

parent 840c8fbb
...@@ -94,12 +94,13 @@ module TalltyImportExport ...@@ -94,12 +94,13 @@ module TalltyImportExport
index = 0 index = 0
association_records.each do |association_record| association_records.each do |association_record|
row = []
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 index += 1
headers.each_with_index do |header, col_index| headers.each_with_index do |header, col_index|
_data = header[:source] ? _data = header[:source] ?
...@@ -117,8 +118,9 @@ module TalltyImportExport ...@@ -117,8 +118,9 @@ module TalltyImportExport
end end
end end
row.push(_data) row.push(_data)
formats.push(header[:format]&.to_sym || (_data.is_a?(String) ? :string : nil))
end end
sheet.add_row row, style: title3, height: @row_height, types: headers.map{|header| header[:format]&.to_sym} sheet.add_row row, style: title3, height: @row_height, types: formats }
last_row = row last_row = row
end end
end end
...@@ -194,6 +196,7 @@ module TalltyImportExport ...@@ -194,6 +196,7 @@ module TalltyImportExport
try_method(record, header[:key]) try_method(record, header[:key])
end end
data = handle_format(data, header) data = handle_format(data, header)
data = handle_data_type(data)
data = handle_select(data, header) data = handle_select(data, header)
rescue rescue
'' ''
...@@ -222,6 +225,16 @@ module TalltyImportExport ...@@ -222,6 +225,16 @@ module TalltyImportExport
end end
end end
def handle_data_type data
if data.is_a?(Time)
data.in_time_zone.strftime('%F %H:%M')
elsif data.is_a?(Date)
data.in_time_zone.strftime('%F')
else
data
end
end
def handle_select data, header def handle_select data, header
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 }
......
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