Why I created this ?
While dealing with Excel files recently, I noticed that excel files are never a set of homogenous data, which you can iterate over directly. Rather there are regions of interest whether in the same worksheet or across worksheets, which you want to work upon differently. For e.g. look at the spreadsheet below:
We are not interested in the top header, we just want the country, currency and USD exchange rate so that we can store that in our database in currencies table. The regions are marked in RED. If you were to go iteratively over each row, then lot of custom logic would have to fit in.
What if you could just declare this - Start from row 3 and just give me 1st, 2nd and 4th column. Let’s see this in code:
sheet.on_rows(3..60).pluck_columns([1, 2, 4]).run do |(country, currency_code, exchange_rate)| Currency.create(country: country, code: currency_code, rate: exchange_rate) end
Similarly lets see how we can export the data from our database into an excel:
currencies = Currency.all sheet.on_rows(3..60).after_column(0).fill do |cells, row_index| currency = currencies[row_index] cells.data = [currency.name. currency.code, currency.rate] end
``` You can also create headers and other information declaratively. I will add full running examples soon.
Add this line to your application’s Gemfile:
And then execute:
Or install it yourself as:
$ gem install excel_walker
TODO: Write usage instructions here
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request