simple-excel-import 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +58 -0
- data/lib/simple-excel-import.rb +88 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6311b3e80c8528884c7a21d17523471361f212b3
|
4
|
+
data.tar.gz: 579c94d75d22592f33e8c778e53b8c541299857a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 799815e2b45c2ff232b79aa9acb433b0f918b9fbba7bb75fe84f3ede29e874fde6a67c43a56eff774d71caefff2827afedaba0666db6fad0e4a16e619423fdd7
|
7
|
+
data.tar.gz: 35b850e5dff2f63aea8710beddcfbb3dc9c32512ef2867f04cbe8baba0b82e2f42aa18be7eb08f37d3cb4900275a957677b7560e4c6963ce70ec6938131ab83b
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
simple-excel-import
|
2
|
+
===================
|
3
|
+
|
4
|
+
|
5
|
+
## Target
|
6
|
+
Get data from excel file and save into database model
|
7
|
+
|
8
|
+
## Support
|
9
|
+
- xls, xlsx format, Microsoft Office 1997 - 2003
|
10
|
+
|
11
|
+
- sxc format, Libre Office
|
12
|
+
|
13
|
+
|
14
|
+
## Install
|
15
|
+
include in Gemfile:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
gem 'simple-excel-import'
|
19
|
+
```
|
20
|
+
|
21
|
+
## How-To
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# declare in model
|
25
|
+
class User
|
26
|
+
simple_excel_import :teacher, :fields => [:tid, :age, :gender, :nation]
|
27
|
+
:default => {
|
28
|
+
:role => :teacher
|
29
|
+
}
|
30
|
+
|
31
|
+
simple_excel_import :student, :fields => [:sid, :age, :gender, :graduated]
|
32
|
+
:default => {
|
33
|
+
:role => :student
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# parse excel file without saving
|
40
|
+
User.parse_excel_teacher(excel_file)
|
41
|
+
# -> return [user, user, user] user array
|
42
|
+
|
43
|
+
|
44
|
+
# parse excel file and save
|
45
|
+
User.import_excel_teacher(excel_file)
|
46
|
+
|
47
|
+
# generate sample excel file and return file object
|
48
|
+
User.get_sample_excel_teacher
|
49
|
+
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
## TODO
|
54
|
+
- support nil field in field list
|
55
|
+
:fields => [:tid, nil, :gender, :nation]
|
56
|
+
|
57
|
+
- more format files, etc, ods, good doc..
|
58
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module SimpleExcelImport
|
3
|
+
module Base
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def simple_excel_import(role, options = {})
|
8
|
+
fields = options[:fields]
|
9
|
+
default = options[:default] || {}
|
10
|
+
|
11
|
+
class_eval %(
|
12
|
+
def self.parse_excel_#{role}(excel_file)
|
13
|
+
_simple_excel_import(#{fields}, #{default}, excel_file)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.import_excel_#{role}(excel_file)
|
17
|
+
models = _simple_excel_import(#{fields}, #{default}, excel_file)
|
18
|
+
models.each do |model|
|
19
|
+
model.save
|
20
|
+
end
|
21
|
+
models
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.get_sample_excel_#{role}
|
25
|
+
_simple_excel_import_generate_sample(#{fields}, #{default})
|
26
|
+
end
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def _simple_excel_import(fields, default, excel_file)
|
32
|
+
spreadsheet = SimpleExcelImport::ImportFile.open_spreadsheet(excel_file)
|
33
|
+
|
34
|
+
models = []
|
35
|
+
(2..spreadsheet.last_row).each do |i|
|
36
|
+
row = spreadsheet.row(i)
|
37
|
+
|
38
|
+
params = {}
|
39
|
+
fields.each_index do |index|
|
40
|
+
params[fields[index]] = row[index]
|
41
|
+
end
|
42
|
+
params.merge! default
|
43
|
+
|
44
|
+
models << self.new(params)
|
45
|
+
end
|
46
|
+
|
47
|
+
models
|
48
|
+
end
|
49
|
+
|
50
|
+
def _simple_excel_import_generate_sample(fields, default)
|
51
|
+
file = Tempfile.open [self.to_s, '.xlsx']
|
52
|
+
|
53
|
+
output = Axlsx::Package.new
|
54
|
+
output.workbook.add_worksheet(:name => 'sheet') do |sheet|
|
55
|
+
field_strs = fields.map do |field|
|
56
|
+
I18n.t("activerecord.attributes.book.#{field}")
|
57
|
+
end
|
58
|
+
|
59
|
+
sheet.add_row field_strs
|
60
|
+
end
|
61
|
+
output.use_shared_strings = true
|
62
|
+
output.serialize(file)
|
63
|
+
file
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module ImportFile
|
69
|
+
class FormatError < Exception; end
|
70
|
+
|
71
|
+
def self.open_spreadsheet(file)
|
72
|
+
extname = File.extname file
|
73
|
+
|
74
|
+
case extname
|
75
|
+
when '.sxc'
|
76
|
+
Roo::Openoffice.new(file.path, nil, :ignore)
|
77
|
+
when '.xls'
|
78
|
+
Roo::Excel.new(file.path, nil, :ignore)
|
79
|
+
when '.xlsx'
|
80
|
+
Roo::Excelx.new(file.path, nil, :ignore)
|
81
|
+
else
|
82
|
+
raise FormatError.new "Unsupported file format #{extname}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
ActiveRecord::Base.send :include, SimpleExcelImport::Base
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-excel-import
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- arlyxiao
|
8
|
+
- fushang318
|
9
|
+
- ben7th
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-03-25 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: roo
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.10.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - '='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 1.10.3
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: axlsx
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 1.3.5
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 1.3.5
|
43
|
+
description: simple-excel-import
|
44
|
+
email: kingla_pei@163.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- lib/simple-excel-import.rb
|
50
|
+
- README.md
|
51
|
+
homepage: https://github.com/mindpin/simple-excel-import
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata: {}
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 2.0.0
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: simple-excel-import
|
75
|
+
test_files: []
|