from_excel 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +24 -14
- metadata +23 -12
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
== Overview
|
2
2
|
|
3
3
|
Import excel spreadsheet rows to ruby objects.
|
4
|
+
|
4
5
|
For how to use it, see examples below or see unit tests
|
5
6
|
|
6
7
|
== Install
|
@@ -10,9 +11,13 @@ For how to use it, see examples below or see unit tests
|
|
10
11
|
== Dependencies
|
11
12
|
|
12
13
|
gem 'nokogiri'
|
14
|
+
|
13
15
|
gem 'roo'
|
16
|
+
|
14
17
|
gem 'rubyzip'
|
18
|
+
|
15
19
|
gem 'spreadsheet'
|
20
|
+
|
16
21
|
gem 'google-spreadsheet-ruby'
|
17
22
|
|
18
23
|
(also require 'active_support/inflector')
|
@@ -30,18 +35,17 @@ spreadsheet_file : File object as spreadsheet (ie :File.new ("path_to_spreadshee
|
|
30
35
|
options :
|
31
36
|
:mapping => hash mapping attribute <=> <column name or column index>
|
32
37
|
:title => true|false . Pass false to indicate that columns have no title. True by default.
|
38
|
+
:offset_from_title. First row offset from title
|
33
39
|
:limits => :start => [first_row, first_column], :end => [last_row, last_column]
|
34
|
-
:rules => Procs rules to convert data in cell sheet to desired data for object attribute.
|
35
|
-
For example, transform a string to association id (see example below)
|
40
|
+
:rules => Procs rules to convert data in cell sheet to desired data for object attribute.For example, transform a string to association id (see example below)
|
36
41
|
|
37
42
|
|
38
|
-
<b>Note
|
39
|
-
of data which will be convert to ruby object.</b>
|
43
|
+
<b>Note</b>: If columns have title, by default the row of columns titles is supposed to be the first row before the first row of data which will be converted to ruby object.To change it, pass :offset_from_title => <offset>
|
40
44
|
|
41
45
|
== Basic spreadsheet
|
42
46
|
|
43
47
|
Suppose you have this kind of Excel sheet:
|
44
|
-
A
|
48
|
+
A B C
|
45
49
|
1 First name Last name Age
|
46
50
|
2 Albert Einstein 131
|
47
51
|
3 Leonard De Vinci 558
|
@@ -61,16 +65,20 @@ You get theses users :
|
|
61
65
|
User.new(:first_name => 'Martin', :last_name => 'Heidegger', :age => 121)]
|
62
66
|
|
63
67
|
By default, attributes model are retrieved from columns title with the following simple rule :
|
68
|
+
|
64
69
|
o spaces are converted to "_"
|
70
|
+
|
65
71
|
o words are downcased
|
66
72
|
|
67
|
-
|
68
|
-
|
73
|
+
First Name => :first_name
|
74
|
+
|
75
|
+
Last Name => :last_name
|
69
76
|
|
70
77
|
|
71
78
|
== Mapping attribute - column names
|
72
79
|
|
73
|
-
If a column name can't be simply mapped to attribute model, you can pass a map to.
|
80
|
+
If a column name can't be simply mapped to attribute model, you can pass a map to.
|
81
|
+
|
74
82
|
If, with the last spreadsheet example, "First name" column had "Prenom" instead as title and you want this column
|
75
83
|
to map :first_name attribute of User model, do like this :
|
76
84
|
|
@@ -79,7 +87,9 @@ to map :first_name attribute of User model, do like this :
|
|
79
87
|
== Spreadsheet whithout columns title
|
80
88
|
|
81
89
|
Suppose your spreadsheet has no columns title.
|
90
|
+
|
82
91
|
You can then map columns <=> model attribute via index of column.
|
92
|
+
|
83
93
|
You can also pass the starting cell of data (the first cell of data (excluded title when there is one))
|
84
94
|
|
85
95
|
users = User.from_excel sheet, :mapping => {:first_name => 1, :last_name => 2, :age => 3}, :limits => {:start => [2, 1]}
|
@@ -97,9 +107,9 @@ If not, you must pass a mapping with :attribute => column_index
|
|
97
107
|
|
98
108
|
Suppose user has one adress of class Adress(:street, :town, :zip_code)
|
99
109
|
And you have this spreadsheet :
|
100
|
-
A
|
101
|
-
1 First name
|
102
|
-
2 Albert Einstein
|
110
|
+
A B C D E F
|
111
|
+
1 First name Last name Age Street Town Zip Code
|
112
|
+
2 Albert Einstein 131 17 rue de Brest Quimper 29000
|
103
113
|
So, you want user with user.adress = Adress.new(:stret => ..., :town => ... , :zip_code => ....)
|
104
114
|
Just pass the following mapping :
|
105
115
|
|
@@ -127,9 +137,9 @@ You have roles predifined in your database :
|
|
127
137
|
|
128
138
|
Sheet :
|
129
139
|
|
130
|
-
First name
|
131
|
-
Bobby
|
132
|
-
Gaston
|
140
|
+
First name Last name Rôle
|
141
|
+
Bobby Lapointe Admin
|
142
|
+
Gaston Lagaffe Reader
|
133
143
|
|
134
144
|
|
135
145
|
'Admin' value must be retrieved as a Role with id 1
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: from_excel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 21
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Philippe Cantin
|
@@ -15,11 +15,23 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
date: 2012-01-25 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: roo
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Import Excel spreadsheet to ruby objects
|
23
35
|
email:
|
24
36
|
executables: []
|
25
37
|
|
@@ -38,7 +50,6 @@ files:
|
|
38
50
|
- test/test_bounds.rb
|
39
51
|
- test/test_from_excel.rb
|
40
52
|
- test/test_mapping.rb
|
41
|
-
has_rdoc: true
|
42
53
|
homepage: http://github.com/anoiaque/from_excel
|
43
54
|
licenses: []
|
44
55
|
|
@@ -68,10 +79,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
79
|
requirements: []
|
69
80
|
|
70
81
|
rubyforge_project:
|
71
|
-
rubygems_version: 1.
|
82
|
+
rubygems_version: 1.8.10
|
72
83
|
signing_key:
|
73
84
|
specification_version: 3
|
74
|
-
summary: Import Excel spreadsheet
|
85
|
+
summary: Import Excel spreadsheet to ruby objects
|
75
86
|
test_files:
|
76
87
|
- test/helper.rb
|
77
88
|
- test/model/adress.rb
|