Commit 4b8a78c8 by 李文强

feat(导出): 支持导出带样式的单元格数据

添加对导出Excel时单元格样式的支持,包括字体颜色、加粗和背景色等 处理 proc 返回的带样式信息的哈希数据时直接返回 添加错误日志记录以便调试
parent 765c88db
......@@ -139,12 +139,33 @@ module TalltyImportExport
row = []
formats = []
cell_styles = [] # 存储每个单元格的样式
index += 1
headers.each_with_index do |header, col_index|
_data = header[:source] ?
handle_data(association_record, header, index) :
handle_data(record, header, index)
# 处理带有样式信息的数据
if _data.is_a?(Hash) && _data.key?(:value) && _data.key?(:style)
# 创建新样式,基于 title3 的样式选项,并添加自定义样式
style_options = {
alignment: { vertical: :center, horizontal: :center, wrap_text: true },
border: { color: '969696', style: :thin },
sz: 10
}
# 添加自定义样式
style_options[:fg_color] = _data[:style][:color] if _data[:style][:color]
style_options[:b] = _data[:style][:bold] if _data[:style].key?(:bold)
style_options[:bg_color] = _data[:style][:bg_color] if _data[:style][:bg_color]
# 创建新样式
cell_style = workbook.styles.add_style(style_options)
cell_styles[col_index] = cell_style
_data = _data[:value]
end
if header[:merge].present? && last_row.present? && _data == last_row[col_index]
# 这里使用二维数组,每个数组里都是列内容相同的各行
merge_column_hash[col_index] ||= []
......@@ -158,7 +179,13 @@ module TalltyImportExport
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
# 添加行并应用样式
row_cells = sheet.add_row(row, style: title3, height: @row_height, types: formats).cells
# 应用自定义样式
cell_styles.each_with_index do |style, col_index|
row_cells[col_index].style = style if style
end
last_row = row
end
end
......@@ -226,23 +253,33 @@ module TalltyImportExport
# 处理一个记录的数据
def handle_data(record, header, index = 0, **opts)
data =
if header[:key] == '_index'
index
elsif header[:method].present?
send(header[:method], record, header)
elsif header[:chain].present?
try_chain(record, header[:chain])
elsif header[:proc] && header[:proc].respond_to?(:call)
header[:proc].call(record, context)
else
try_method(record, header[:key], prefix: [header[:prefix], header[:json]].compact.join('.'))
end
data = handle_format(data, header, **opts)
data = handle_data_type(data, **opts)
data = handle_select(data, header, **opts)
rescue StandardError
''
begin
data =
if header[:key] == '_index'
index
elsif header[:method].present?
send(header[:method], record, header)
elsif header[:chain].present?
try_chain(record, header[:chain])
elsif header[:proc] && header[:proc].respond_to?(:call)
result = header[:proc].call(record, context)
# 如果 proc 返回的是带有样式信息的哈希,直接返回
if result.is_a?(Hash) && result.key?(:value) && result.key?(:style)
return result
end
result
else
try_method(record, header[:key], prefix: [header[:prefix], header[:json]].compact.join('.'))
end
# 处理普通数据
data = handle_format(data, header, **opts)
data = handle_data_type(data, **opts)
handle_select(data, header, **opts)
rescue StandardError => e
Rails.logger.error "Error in handle_data: #{e.message}\n#{e.backtrace.join("\n")}" if defined?(Rails)
''
end
end
def try_chain(payload, arr)
......
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