Commit 83431f12 by liyijie

Add exportable class

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