Turning Excel spreadsheets into hashes
Latest commit 914d860 Apr 9, 2015 @jumph4x Shameless self promotion
Failed to load latest commit information.
spec update tests to use the new expect method instead of should Apr 9, 2015
.gitignore Initial commit May 14, 2012
.rspec Initial commit May 14, 2012
Gemfile add Gemfile Apr 7, 2015
README.md Shameless self promotion Apr 9, 2015
Rakefile Initial commit May 14, 2012


Batch Factory

Built on top of the roo gem for easy abstraction of Excel and OpenOffice spreadsheets as tabular data input. Assumes the first row to be headings with keys and forms clean Ruby hashmaps for all consecutive rows using cells as values for respective keys.


Common scenarios include:

  • Batch creates\updates\deletes through an ORM (ActiveRecord, DataMapper, Mongoid, anything that accepts hashes for a given record)



require 'batch_factory'
hash_worksheet = BatchFactory.from_file 'path/to/some/spreadsheet.xls'

To open particular sheet specify the sheet_number option. By default it opens the first sheet (sheet_number: 0). E.g this will open the second sheet:

hash_worksheet = BatchFactory.from_file 'path/to/some/spreadsheet.xls', sheet_number: 1

If your data doesn't include headings, add them by passing an optional array:

hash_worksheet = BatchFactory.from_file 'path/to/some/spreadsheet.xls',
  keys: [:name, :address, :phone]

Spreadsheet type is detected by the file extension (xls, xlsx, ods), files without extension considered csv. Also, this can be specified explicitly with the filetype option:

hash_worksheet = BatchFactory.from_file 'http://somecloud.com/path/spreadsheet', filetype: 'xls'

Then, display headings from row 1 that BatchFactory used as hash keys for each row:


Show all extracted hashes (one per row starting with row 2):


Iterate over it like an array (or enumerable). Or, in fact, any method that either respond to:

hash_worksheet.each do |hash_row|
  puts hash_row.inspect

Note: this gem pulls in Activesupport's indefferent access for hashes, so you can access each row's data with Symbols just like Strings. Enjoy.


gem install batch_factory

Or in a Gemfile if you always want the freshest cut:

gem 'batch_factory', :git => 'git://github.com/jumph4x/batch-factory.git'


Because the business world still runs on spreadsheets and Excel. This allows executives to effectively interact with your software library or webapp.


A project by Downshift Labs, Ruby on Rails, Performance tuning and Spree Commerce projects.