README.md 4.01 KB
Newer Older
Alexis Reigel's avatar
Alexis Reigel committed
1
[![pipeline status](https://git.panter.ch/open-source/excelsieur/badges/master/pipeline.svg)](https://git.panter.ch/open-source/excelsieur/commits/master)
Alexis Reigel's avatar
Alexis Reigel committed
2
[![Gem Version](https://badge.fury.io/rb/excelsieur.svg)](https://badge.fury.io/rb/excelsieur)
Alexis Reigel's avatar
Alexis Reigel committed
3

4
<img src="docs/excelsieur-logo.png" width="480">
Immanuel Häussermann's avatar
Immanuel Häussermann committed
5
6
7
8
9
10
11
12
13
14

---

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

## Installation

Add this line to your application's Gemfile:

```ruby
15
gem 'excelsieur'
Immanuel Häussermann's avatar
Immanuel Häussermann committed
16
17
18
19
20
21
22
23
```

And then execute:

    $ bundle

Or install it yourself as:

24
    $ gem install excelsieur
Immanuel Häussermann's avatar
Immanuel Häussermann committed
25
26
27

## How to use it

28
Create a class which declares how columns from an excel sheet map to your ruby object model by extending from the `Excelsieur::Importer` class:
Immanuel Häussermann's avatar
Immanuel Häussermann committed
29
30

```ruby
31
class UserImporter < Excelsieur::Importer
Immanuel Häussermann's avatar
Immanuel Häussermann committed
32
33
34
35
36
37
38
39
40
41
  # 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
```

42
43
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
44
45

```ruby
46
47
48
49
import = UserImport.new
import.run # calls User.create!(row) for each row
```

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

```ruby
result = import.run
result.status
# => :succeeded
result.errors
# => { missing_column: [], model: [] }
result.report
59
# => #<struct Excelsieur::Report inserted=2, failed=0>
Alexis Reigel's avatar
Alexis Reigel committed
60
61
```

Alexis Reigel's avatar
Alexis Reigel committed
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]
73
# => [#<struct Excelsieur::Error row=3, errors=["First name can't be blank"]>]
Alexis Reigel's avatar
Alexis Reigel committed
74
75
```

76
77
78
79
80
81
82
83
84
85
### Report

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

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

import.report
86
# => #<struct Excelsieur::Report inserted=2, failed=1>
87
88
89
90
91

import.total
# => 3
```

Alexis Reigel's avatar
Alexis Reigel committed
92
93
94
95
96
### Transactions

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

```ruby
97
class UserImporter < Excelsieur::Importer
Alexis Reigel's avatar
Alexis Reigel committed
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
130
131
132
  # 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
```


133
134
135
136
137
138
139
### 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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

159
Bug reports and pull requests are welcome on GitHub at https://github.com/panter/excelsieur.
Immanuel Häussermann's avatar
Immanuel Häussermann committed
160
161
162
163

## License

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