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_path
By 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_path
In 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_pattern
is used byDir.glob
method for filtering files inside the source directory. Default:"*.{xls|xlsx}"
. -
sheets_idxs
permit 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_rows
in order to specify which rows and column you want to import. You can pass:any
in place of thesheet_index
for default rules. Rules:header_row
set the header row index. Put:missing
to omit the header. Default is0
data_starting_row
set the index of the first row to import, default is1
data_first_column
set the index of the first column to import, default is0
data_last_column
set the index of the last column to import, default is:last
-
pass an
{ sheet_index => rules }
hash toextra_columns
in 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:position
define the column position of extra data, you can put at:beginning
or:end
data
define an array of desired extra datatype
can be:filename
if you want to add filename info or:cell_value
if you want to pick extra info from a celllabel
allow you to set extra data header name- when you select
:cell_value
you have to specifysheet_idx
,row_idx
andcol_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.