README.md 3.76 KB
Newer Older
Immanuel Häussermann's avatar
Immanuel Häussermann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<img src="docs/excelsior-logo.png" width="480">

---

A straightforward way to import data from an excel sheet into your ruby app.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'excelsior'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install excelsior

## How to use it

Create a class which declares how columns from an excel sheet map to your ruby object model by extending from the `Excelsior::Importer` class:

```ruby
class UserImporter < Excelsior::Importer
  # declare the source file
  source "static/ftp/users.xlsx"

  # declare the mapping
  map "First Name", to: :firstname
  map "Last Name", to: :lastname
  map "E-Mail", to: :email
end
```

39
40
Create an instance of your import and run it. By default it infers the model
to be imported from the classname, e.g.:
Immanuel Häussermann's avatar
Immanuel Häussermann committed
41
42

```ruby
43
44
45
46
import = UserImport.new
import.run # calls User.create!(row) for each row
```

Alexis Reigel's avatar
Alexis Reigel committed
47
48
49
50
51
52
53
54
55
56
57
58
The result is an instance of `Excelsior::Result`:

```ruby
result = import.run
result.status
# => :succeeded
result.errors
# => { missing_column: [], model: [] }
result.report
# => #<struct Excelsior::Report inserted=2, failed=0>
```

Alexis Reigel's avatar
Alexis Reigel committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
### Model validation

A summary of the `ActiveRecord` model validations is available after running
the importer. The `Error` objects indicates the failed excel row and the
corresponding errors.

```ruby
import = UserImport.new
import.run

import.errors[:model]
# => [#<struct Excelsior::Error row=3, errors=["First name can't be blank"]>]
```

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
### Report

A summary of the successfully inserted and failed records is available after
running the importer.

```ruby
import = UserImport.new
import.run

import.report
# => #<struct Excelsior::Report inserted=2, failed=1>

import.total
# => 3
```

Alexis Reigel's avatar
Alexis Reigel committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
### Transactions

When setting `transaction true` no record is inserted if any one of them has an error.

```ruby
class UserImporter < Excelsior::Importer
  # declare the source file
  source "static/ftp/users.xlsx"

  # only insert all rows if none of them have an error
  transaction true

  # declare the mapping
  map "First Name", to: :firstname
  map "Last Name", to: :lastname
  map "E-Mail", to: :email
end
```

If a block is passed to `run` the block needs to raise an error in order to
roll back the transaction.

This means that the following will trigger a rollback if the model is not
valid:

```ruby
UserImport.new.run do |row|
  User.create!(row)
end
```

On the other hand, the following won't trigger a rollback if the model is
invalid:

```ruby
UserImport.new.run do |row|
  User.create(row)
end
```


130
131
132
133
134
135
136
### Extended API

You may want to pass an excel file per instance. You can also define your own
import behavior by passing a block to the `run` method:

```ruby
import = UserImport.new("users/all.xlsx")
Immanuel Häussermann's avatar
Immanuel Häussermann committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import.run do |row|
  User.create!(row) # raise an exception if the data doesn't match your expectations
end
```

## Limitations
Be aware of a few limitations when considering this gem:
- only supports the first sheet in an excel file
- only supports `.xlsx` file format
- no export, just import

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/manufaktor/excelsior.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).