README.md 3.92 KB
Newer Older
Alexis Reigel's avatar
Alexis Reigel committed
1
2
[![pipeline status](https://git.panter.ch/open-source/excelsieur/badges/master/pipeline.svg)](https://git.panter.ch/open-source/excelsieur/commits/master)

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

---

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

## Installation

Add this line to your application's Gemfile:

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

And then execute:

    $ bundle

Or install it yourself as:

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

## How to use it

27
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
28
29

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

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

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

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

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

Alexis Reigel's avatar
Alexis Reigel committed
61
62
63
64
65
66
67
68
69
70
71
### 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]
72
# => [#<struct Excelsieur::Error row=3, errors=["First name can't be blank"]>]
Alexis Reigel's avatar
Alexis Reigel committed
73
74
```

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

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

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

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

import.total
# => 3
```

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

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

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


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

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

## License

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