Commit 83431f12 by liyijie

Add exportable class

parent 1aa4599a
......@@ -2,16 +2,22 @@ PATH
remote: .
specs:
tallty_import_export (0.1.0)
caxlsx
roo
roo-xls
zip-zip
GEM
remote: https://gems.ruby-china.com/
specs:
axlsx (1.3.6)
htmlentities (~> 4.3.1)
nokogiri (>= 1.4.1)
rubyzip (>= 0.9.5)
caxlsx (3.0.2)
htmlentities (~> 4.3, >= 4.3.4)
mimemagic (~> 0.3)
nokogiri (~> 1.10, >= 1.10.4)
rubyzip (>= 1.3.0, < 3)
diff-lcs (1.4.4)
htmlentities (4.3.4)
mimemagic (0.3.5)
mini_portile2 (2.4.0)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
......@@ -47,13 +53,9 @@ PLATFORMS
ruby
DEPENDENCIES
axlsx
rake (~> 12.0)
roo
roo-xls
rspec (~> 3.0)
tallty_import_export!
zip-zip
BUNDLED WITH
2.1.4
require "tallty_import_export/version"
require 'axlsx'
module TalltyImportExport
class Error < StandardError; end
# Your code goes here...
autoload :Exportable, 'tallty_import_export/exportable'
autoload :Importable, 'tallty_import_export/importable'
end
......@@ -8,6 +8,7 @@ module TalltyImportExport
# attr_type: 属性的类型
# format: excel是否需要特定的格式,目前主要是类似于身份证号,可以用string
# method: 本地调用的方法
# chain: 对象属性通过链式调用
included do
end
......@@ -31,39 +32,47 @@ module TalltyImportExport
sheet.add_row [first_header], style: title1, height: 40
end
sheet.add_row _titles, style: title2, height: 39
records.each do |record|
sheet.add_row @headers.map{|header| header[:name]}, style: title2, height: 39
records.find_each do |record|
row = []
@headers.each{ |header| row.push(handle_data(record, header)) }
sheet.add_row row, style: title3, height: @row_height, types: @headers.map{|header| header[:format]}
end
sheet.column_widths *@headers.map{|header| header[:width] || @width}
end
end
file_path = File.join(Rails.root, 'public', 'export')
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"
pack.serialize(File.join(file_path, file_name))
url = ActionController::Base.helpers.asset_url("/export/#{file_name}")
return url
end
end
def exportable_defaults(options = {})
options = options.with_indifferent_access
@row_height ||= options.delete(:row_height) || 35
@width ||= options.delete(:width) || 30
@headers = self.respond_to?(:export_headers) ? export_headers.with_indifferent_access : headers.with_indifferent_access
@filename ||= options.delete(:filename)
@headers = self.respond_to?(:export_headers) ? export_headers : headers
@headers.map! {|header| header.with_indifferent_access}
end
# 处理一个记录的数据
def handle_data record, header
data =
if header[:method].present?
send(header[:method], record, header[:key])
elsif header[:chain].present?
header[:chain].reduce(record) do |obj, method|
obj.send(method)
end
else
record.send(header[:key])
end
handle_format(data, header)
rescue
''
end
......@@ -75,6 +84,8 @@ module TalltyImportExport
data ? data.strftime('%F %H:%M') : nil
when 'date'
data ? data.strftime('%F') : nil
else
data
end
end
end
......
......@@ -27,8 +27,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency('zip-zip')
spec.add_development_dependency('axlsx')
spec.add_development_dependency('roo')
spec.add_development_dependency('roo-xls')
spec.add_dependency('zip-zip')
spec.add_dependency('caxlsx')
spec.add_dependency('roo')
spec.add_dependency('roo-xls')
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