README.md 4.17 KB
Newer Older
Alexis Reigel's avatar
Alexis Reigel committed
1 2
<img src="docs/excelsieur-logo.png" width="480">

Alexis Reigel's avatar
Alexis Reigel committed
3
[![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
4
[![Gem Version](https://badge.fury.io/rb/excelsieur.svg)](https://badge.fury.io/rb/excelsieur)
Alexis Reigel's avatar
Alexis Reigel committed
5
[![Maintainability](https://api.codeclimate.com/v1/badges/657ecb9ccf29186a1377/maintainability)](https://codeclimate.com/github/panter/excelsieur/maintainability)
Alexis Reigel's avatar
Alexis Reigel committed
6

Immanuel Häussermann's avatar
Immanuel Häussermann committed
7 8 9 10 11 12 13 14 15
---

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

## Installation

Add this line to your application's Gemfile:

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

And then execute:

    $ bundle

Or install it yourself as:

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

## How to use it

29
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
30 31

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

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

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

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

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

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

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

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

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

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

import.total
# => 3
```

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

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

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


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

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

## License

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