This Ruby library has been created in order to merge multiple Excel files (both '.xls' and '.xlsx') into one.
To experiment with that code, run bin/console for an interactive prompt.
Add this line to your application's Gemfile:
gem 'merge_excel'And then execute:
$ bundle
Or install it yourself as:
$ gem install merge_excel
Given excel_files_dir the directory where the Excel files you want to merge are placed and merged_file_path the path or filename of the destination Excel file.
require 'merge_excel'
me = MergeExcel::Excel.new(excel_files_dir)
me.merge merged_file_pathBy default the command will merge:
- all worksheets
- the header line (first row)
- all rows until an empty one
- all columns
Options allows you to control what to merge.
options = {
selector_pattern: "*.xls",
sheets_idxs: [1],
data_rows: {
1 => {
header_row: 0, # or :missing
data_starting_row: 1,
data_first_column: 0,
data_last_column: 5 # omit or :last to get all columns
}
}
}
me = MergeExcel::Excel.new(excel_files_dir, options)
me.merge merged_file_pathIn this example will be merged only '.xls' files, of which only the columns from 0 to 5 of the second sheet.
Note that all indexes (sheets, rows and columns) are 0-based.
-
selector_patternis used byDir.globmethod for filtering files inside the source directory. Default:"*.{xls|xlsx}". -
sheets_idxspermit to select the sheets to import/merge. Pass an array with indexes (0-based) or:all. Default::all. -
pass an
{ sheet_index => rules }hash todata_rowsin order to specify which rows and column you want to import. You can pass:anyin place of thesheet_indexfor default rules. Rules:header_rowset the header row index. Put:missingto omit the header. Default is0data_starting_rowset the index of the first row to import, default is1data_first_columnset the index of the first column to import, default is0data_last_columnset the index of the last column to import, default is:last
-
pass an
{ sheet_index => rules }hash toextra_columnsin order to specify extra columns you want to be added to the final merge (for example you want to insert the filename you get the data). Rules:positiondefine the column position of extra data, you can put at:beginningor:enddatadefine an array of desired extra datatypecan be:filenameif you want to add filename info or:cell_valueif you want to pick extra info from a celllabelallow you to set extra data header name- when you select
:cell_valueyou have to specifysheet_idx,row_idxandcol_idx.
Another example:
options = {
sheets_idxs: :all,
data_rows: {
1 => {
header_row: 0,
data_starting_row: 1,
data_first_column: 0 # all columns
},
:any => {
header_row: :missing,
data_starting_row: 1,
data_first_column: 0,
data_last_column: 5
}
},
extra_columns: {
any: {
position: :beginning,
data: [
{
type: :filename,
label: "Filename"
},
{
type: :cell_value,
label: "Company",
sheet_idx: 0,
row_idx: 10,
col_idx: 3,
}
]
}
}
}After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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.
Bug reports and pull requests are welcome on GitHub at https://github.com/AEEGSI/merge_excel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.