merge_excel 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b4b332e7aeffcf25e4d60ba3da613d352671400
4
+ data.tar.gz: 342ce48decad71444ff3a7c62f80d61e9d58d335
5
+ SHA512:
6
+ metadata.gz: 55554b291704b54902b1f6302397f07d3657199335d802e9083d152dd0dda171be852401454bdc3184805bdaa65ff3aa79e11b4f1d09a88baf6a0985e67fb9e6
7
+ data.tar.gz: 9463f8e58c96a1ca822a90f4f5265e1664b89bbe6d4c4608d3eec1dbf86f32c13ddb7e7fc798be0deb540fe6c30aa9ac5ece567647c8c1e4ea004237a85f2120
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/results/
11
+ /palestra.rb
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in merge_excel.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Iwan Buetti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # MergeExcel
2
+
3
+ This Ruby library has been created in order to merge multiple Excel files (both '.xls' and '.xlsx') into one.
4
+ To experiment with that code, run `bin/console` for an interactive prompt.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'merge_excel'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install merge_excel
22
+
23
+ ## Usage
24
+
25
+
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ 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](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "merge_excel"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ module MergeExcel
2
+ class DataRow
3
+ attr_reader :array
4
+
5
+ def initialize(array, extra_data_hash, book)
6
+ if extra_data_hash
7
+ extra_array = extra_data_hash.data.map do |e|
8
+ case e.type
9
+ when :filename
10
+ book.filename
11
+ when :cell_value
12
+ book.cell_value_at(e.sheet_idx, e.row_idx, e.col_idx)
13
+ else
14
+ # error?
15
+ end
16
+ end
17
+ case extra_data_hash.position
18
+ when :beginning
19
+ @array = extra_array + array
20
+ else
21
+ @array = array + extra_array
22
+ end
23
+ else
24
+ @array = array
25
+ end
26
+ end
27
+
28
+ def to_a
29
+ @array.to_a
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module MergeExcel
2
+ class Excel
3
+ attr_reader :files
4
+
5
+ def initialize(hash, selector_pattern="*.{xls,xlsx}")
6
+ @settings = Settings::Parser.new(hash)
7
+ @files = Dir.glob(File.join(@settings.input_dir, selector_pattern))
8
+ end
9
+
10
+ def merge
11
+ wbook = WBook.new(@settings)
12
+ @files.each do |i_xls_filepath|
13
+ puts i_xls_filepath
14
+ wbook.import_data(i_xls_filepath)
15
+ end
16
+ wbook.export @settings.output_file_path
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module RubyXL
2
+ class Workbook
3
+ def add_worksheet_at(idx, name)
4
+ sheet = Worksheet.new(sheet_name: name, workbook: self)
5
+ worksheets[idx] = sheet
6
+ sheet
7
+ end
8
+
9
+ def cell_value_at(sheet_idx, row_idx, col_idx)
10
+ c = worksheets[sheet_idx][row_idx][col_idx]
11
+ c && c.value
12
+ end
13
+
14
+ def count_worksheets
15
+ worksheets.size
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module RubyXL
2
+ class Worksheet
3
+ def row_values(row_idx, from_col_idx=0, to_col_idx=-1)
4
+ values = sheet_data[row_idx] && sheet_data[row_idx].cells[from_col_idx..to_col_idx].map{|c| c && c.value}
5
+ return nil if values.nil? || values.empty? || values.all?(&:nil?)
6
+ values
7
+ end
8
+
9
+ def add_row_at(row_idx, cells_array)
10
+ cells_array.to_a.each_with_index do |cell, col_idx|
11
+ if cell.is_a? DateTime
12
+ c = add_cell(row_idx, col_idx)
13
+ c.set_number_format('yyyy-mm-dd')
14
+ c.change_contents(cell)
15
+ else
16
+ add_cell(row_idx, col_idx, cell)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ module Spreadsheet
2
+ class Workbook
3
+ def add_worksheet_at(idx, name)
4
+ ws = Worksheet.new(name: name)
5
+ add_worksheet(ws)
6
+ end
7
+
8
+ def cell_value_at(sheet_idx, row_idx, col_idx)
9
+ worksheet(sheet_idx).row(row_idx)[col_idx]
10
+ end
11
+
12
+ def count_worksheets
13
+ worksheets.size
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module Spreadsheet
2
+ class Worksheet
3
+ def sheet_name
4
+ name
5
+ end
6
+
7
+ def row_values(row_idx, from_col_idx=0, to_col_idx=-1)
8
+ values = row(row_idx) && row(row_idx).to_a[from_col_idx..to_col_idx]
9
+ return nil if values.nil? || values.empty? || values.all?(&:nil?)
10
+ values
11
+ end
12
+
13
+ def add_row_at(row_idx, cells_array)
14
+ row(row_idx).concat cells_array.to_a
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module MergeExcel
2
+ module FormatDetector
3
+ def detect_format(filepath)
4
+ case File.extname(filepath)
5
+ when ".xls"
6
+ :xls
7
+ when ".xlsx"
8
+ :xlsx
9
+ else
10
+ raise "Invalid format"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module MergeExcel
2
+ class Header
3
+ def initialize(array, extra_data_hash)
4
+
5
+ if extra_data_hash
6
+ extra_array = extra_data_hash.data.map{|e| e.label}
7
+ case extra_data_hash.position
8
+ when :beginning
9
+ @array = extra_array + array
10
+ else
11
+ @array = array + extra_array
12
+ end
13
+ else
14
+ @array = array
15
+ end
16
+ end
17
+
18
+ def to_a
19
+ @array.to_a
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ module MergeExcel
2
+ module Settings
3
+ class DataRow
4
+ FIRST_COLUMN = 0
5
+ LAST_COLUMN = -1
6
+ DEFAULT_DATA_ROW_CONFIG = {
7
+ header_row: 0,
8
+ data_starting_row: 1,
9
+ data_first_column: FIRST_COLUMN,
10
+ data_last_column: LAST_COLUMN
11
+ }
12
+
13
+ attr_reader :header_row, :data_starting_row, :data_first_column, :data_last_column
14
+ def initialize(hash)
15
+ @header_row = hash.fetch(:header_row){ 0 }
16
+ @data_starting_row = hash.fetch(:data_starting_row){ 1 }
17
+ @data_first_column = hash.fetch(:data_first_column){ FIRST_COLUMN }
18
+ @data_last_column = hash.fetch(:data_last_column){ LAST_COLUMN }
19
+ @data_last_column = LAST_COLUMN if @data_last_column==:last
20
+ validate_rows
21
+ validate_columns
22
+ end
23
+
24
+ def import_header?
25
+ @header_row!=:missing
26
+ end
27
+
28
+ # def self.with_defaults
29
+ # new(DEFAULT_HEADER_ROW_CONFIG)
30
+ # end
31
+
32
+ private
33
+ def validate_rows
34
+ # header_row
35
+ if (@header_row.is_a?(Symbol) && @header_row!=:missing) || (@header_row.is_a?(Integer) && @header_row<0)
36
+ raise "Problem with 'data_rows' settings: header_row must be be an integer >=0 or :missing symbol" if @header_row<0
37
+ end
38
+ # data_starting_row
39
+ if @data_starting_row<0
40
+ raise "Problem with 'data_rows' settings: data_starting_row must be be an integer >=0" if @data_starting_row<0
41
+ end
42
+ # header_row vs data_starting_row
43
+ if @header_row!=:missing && (@header_row >= @data_starting_row)
44
+ raise "Problem with 'data_rows' settings: header_row must be < data_starting_row" if @header_row<0
45
+ end
46
+ end
47
+ def validate_columns
48
+ raise "Problem with 'data_rows' settings: data_first_column must be >=0" if @data_first_column<0
49
+ raise "Problem with 'data_rows' settings: data_last_column must be >=0" if @data_last_column>=0 && @data_first_column>@data_last_column
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ module MergeExcel
2
+ module Settings
3
+ class DataRows
4
+ def initialize
5
+ @hash = {}
6
+ end
7
+
8
+ def insert(sheet_idx, h)
9
+ validate_sheet_idx sheet_idx
10
+ @hash[sheet_idx] = DataRow.new(h)
11
+ end
12
+
13
+ def get(sheet_idx) # a number>=0 or :any
14
+ validate_sheet_idx sheet_idx
15
+ @hash.fetch(sheet_idx) do
16
+ @hash.fetch(:any) do
17
+ @hash[:any] = DataRow.with_defaults
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+ def validate_sheet_idx(sheet_idx)
24
+ if sheet_idx.is_a?(Integer)
25
+ raise "Problem with 'data_rows' settings: sheet indexes must be >=0" if sheet_idx<0
26
+ elsif sheet_idx!=:any
27
+ raise "Problem with 'data_rows' settings: sheet indexes must be an integer or :any symbol"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,59 @@
1
+ module MergeExcel
2
+ module Settings
3
+ class ExtraDataRow
4
+ # DEFAULT_EXTRA_DATA_ROW_CONFIG = {position: :beginning, data: [{ type: :filename, label: "Filename" }]}
5
+ DEFAULT_EXTRA_DATA_ROW_CONFIG = {}
6
+
7
+ class FilenameExtraDataRow
8
+ TYPE = :filename
9
+ attr_reader :label, :type
10
+ def initialize(label)
11
+ @label = label
12
+ @type = :filename
13
+ end
14
+ end
15
+
16
+ class CellValueExtraDataRow
17
+ TYPE = :cell_value
18
+ attr_reader :label, :type, :sheet_idx, :row_idx, :col_idx
19
+ def initialize(label, coordinates_hash)
20
+ @label = label
21
+ @sheet_idx = coordinates_hash.fetch(:sheet_idx)
22
+ @row_idx = coordinates_hash.fetch(:row_idx)
23
+ @col_idx = coordinates_hash.fetch(:col_idx)
24
+ @type = :cell_value
25
+ end
26
+ end
27
+
28
+
29
+ attr_reader :position, :data
30
+ def initialize(hash)
31
+ # hash: {:position=>:beginning, :data=>[{:type=>:filename, :heading_text=>"Filename"}]}
32
+ @position = hash.fetch(:position){ :beginning }
33
+ @data = []
34
+ read_data_array hash.fetch(:data){ [] }
35
+ validate_position
36
+ end
37
+
38
+ def read_data_array(array)
39
+ array.each do |h|
40
+ if h[:type]==:filename
41
+ @data << FilenameExtraDataRow.new(h.fetch(:label){"Filename"})
42
+ elsif h[:type]==:cell_value
43
+ @data << CellValueExtraDataRow.new(h.fetch(:label){"Field"}, h)
44
+ end
45
+ end
46
+ end
47
+
48
+
49
+ def self.with_defaults
50
+ new(DEFAULT_EXTRA_DATA_ROW_CONFIG)
51
+ end
52
+
53
+ private
54
+ def validate_position
55
+ raise "Problem with 'extra_data' settings: 'position' must be :beginning or :end" unless [:beginning, :end].include?(@position)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ module MergeExcel
2
+ module Settings
3
+ class ExtraDataRows
4
+ def initialize
5
+ @hash = {}
6
+ end
7
+
8
+ def insert(sheet_idx, h)
9
+ # h: {:any=>{:position=>:beginning, :data=>{:type=>:filename, :heading_text=>"Filename"}}}
10
+ validate_sheet_idx sheet_idx
11
+ @hash[sheet_idx] = ExtraDataRow.new(h)
12
+ end
13
+
14
+ def get(sheet_idx) # a number>=0 or :any
15
+ validate_sheet_idx sheet_idx
16
+ @hash.fetch(sheet_idx) do
17
+ @hash.fetch(:any) do
18
+ @hash[:any] = ExtraDataRow.with_defaults
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+ def validate_sheet_idx(sheet_idx)
25
+ if sheet_idx.is_a?(Integer)
26
+ raise "Problem with 'extra_data_rows' settings: sheet indexes must be >=0" if sheet_idx<0
27
+ elsif sheet_idx!=:any
28
+ raise "Problem with 'extra_data_rows' settings: sheet indexes must be an integer or :any symbol"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,48 @@
1
+ module MergeExcel
2
+ module Settings
3
+ class Parser
4
+ attr_reader :input_dir, :output_file_path, :sheets_idxs, :data_rows, :extra_data_rows
5
+ def initialize(hash)
6
+ @input_dir = hash.fetch(:input_dir)
7
+ @output_file_path = hash.fetch(:output_file_path)
8
+ read_sheets_index hash.fetch(:sheets_idxs){ :all }
9
+ read_data_rows hash.fetch(:data_rows){ {any: DataRow::DEFAULT_DATA_ROW_CONFIG } }
10
+ read_extra_data_rows hash.fetch(:extra_data_rows){ {any: ExtraDataRow::DEFAULT_EXTRA_DATA_ROW_CONFIG } }
11
+ end
12
+
13
+ def all_sheets?
14
+ @sheets_idxs == :all
15
+ end
16
+
17
+ def read_sheet?(sheet_idx)
18
+ all_sheets? || @sheets_idxs.include?(sheet_idx)
19
+ end
20
+
21
+ private
22
+
23
+ def read_sheets_index(obj)
24
+ if obj==:all || obj==[:all]
25
+ @sheets_idxs = :all
26
+ else
27
+ obj = [obj] if !(obj.is_a? Array)
28
+ @sheets_idxs = obj.select{|e| e.is_a? Integer}.sort
29
+ end
30
+ end
31
+
32
+ def read_data_rows(h)
33
+ @data_rows = DataRows.new
34
+ h.each_pair do |k,v|
35
+ @data_rows.insert(k, v)
36
+ end
37
+ end
38
+
39
+ def read_extra_data_rows(h)
40
+ # {:any=>{:position=>:beginning, :data=>[{:type=>:filename, :heading_text=>"Filename"}}]}
41
+ @extra_data_rows = ExtraDataRows.new
42
+ h.each_pair do |k,v|
43
+ @extra_data_rows.insert(k, v)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module MergeExcel
2
+ class Sheet
3
+ attr_reader :source_sheet, :source_sheet_idx, :sheet_idx, :name, :book, :header, :data_rows
4
+ def initialize(source_sheet, source_sheet_idx)
5
+ @source_sheet = source_sheet
6
+ @source_sheet_idx = source_sheet_idx
7
+ @name = @source_sheet.sheet_name
8
+ @book = @source_sheet.workbook
9
+
10
+ @header = nil
11
+ @data_rows = []
12
+ end
13
+
14
+ def add_header(array, extra_data)
15
+ @header = Header.new(array, extra_data)
16
+ end
17
+
18
+ def add_data_row(array, extra_data, book)
19
+ @data_rows << DataRow.new(array || [], extra_data, book).array
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module MergeExcel
2
+ class Sheets
3
+ attr_reader :array
4
+ def initialize
5
+ @array = []
6
+ end
7
+
8
+ def <<(element)
9
+ @array << element
10
+ end
11
+
12
+ def each
13
+ @array.each{|e| yield e}
14
+ end
15
+
16
+ def each_with_index
17
+ @array.each_with_index{|e, i| yield(e,i)}
18
+ end
19
+
20
+ def get_sheet(original_idx: )
21
+ @array.find{|s| s.source_sheet_idx==original_idx}
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module MergeExcel
2
+ class SpreadsheetParser
3
+ extend FormatDetector
4
+
5
+ def self.open(filepath)
6
+ extname = detect_format(filepath)
7
+ case extname
8
+ when :xls
9
+ book = Spreadsheet.open(filepath)
10
+ when :xlsx
11
+ book = RubyXL::Parser.parse(filepath)
12
+ end
13
+ book.instance_variable_set(:@extname, extname)
14
+ book.instance_variable_set(:@filename, File.basename(filepath))
15
+
16
+ def book.xls?
17
+ @extname==:xls ? true : false
18
+ end
19
+ def book.xlsx?
20
+ !book.xls?
21
+ end
22
+ def book.filename
23
+ @filename
24
+ end
25
+ book
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module MergeExcel
2
+ class SpreadsheetWriter
3
+ extend FormatDetector
4
+
5
+ def self.create(filepath)
6
+ extname = detect_format(filepath)
7
+ wb = case extname
8
+ when :xls
9
+ Spreadsheet::Workbook.new
10
+ when :xlsx
11
+ RubyXL::Workbook.new
12
+ else
13
+ raise "Invalid format"
14
+ end
15
+
16
+ yield(wb)
17
+ wb.write filepath
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module MergeExcel
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,65 @@
1
+ module MergeExcel
2
+ class WBook
3
+ attr_reader :sheets, :settings
4
+
5
+ def initialize(settings)
6
+ @sheets = Sheets.new
7
+ @settings = settings
8
+ end
9
+
10
+ def import_data(filepath)
11
+ book = SpreadsheetParser.open(filepath)
12
+ filename = File.basename(filepath)
13
+
14
+ book.worksheets.each_with_index do |sheet, idx|
15
+ if @settings.read_sheet? idx
16
+ sh_stt = @settings.data_rows.get(idx)
17
+ sh = select_sheet(idx) || create_sheet(sheet, idx, sh_stt)
18
+ row_idx = sh_stt.data_starting_row
19
+ loop do
20
+ break unless cells = sheet.row_values(row_idx, sh_stt.data_first_column, sh_stt.data_last_column)
21
+ sh.add_data_row(cells, @settings.extra_data_rows.get(idx), book)
22
+ row_idx+=1
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def export(filepath)
29
+ print "Exporting data..."
30
+ SpreadsheetWriter.create(filepath) do |wb|
31
+ @sheets.each_with_index do |sheet, sheet_idx|
32
+ s = wb.add_worksheet_at(sheet_idx, sheet.name)
33
+ if sheet.header
34
+ s.add_row_at(0, sheet.header)
35
+ offset = 1
36
+ else
37
+ offset = 0
38
+ end
39
+ sheet.data_rows.each_with_index do |data_row, row_idx|
40
+ s.add_row_at(row_idx+offset, data_row)
41
+ end
42
+ end
43
+ end
44
+ puts "finished!"
45
+ end
46
+
47
+
48
+ private
49
+
50
+ def select_sheet(idx)
51
+ @sheets.get_sheet(original_idx: idx)
52
+ end
53
+
54
+ def create_sheet(sheet, idx, sh_stt)
55
+ sh = Sheet.new(sheet, idx)
56
+ if sh_stt.import_header?
57
+ arr = sheet.row_values(sh_stt.header_row, sh_stt.data_first_column, sh_stt.data_last_column)
58
+ sh.add_header(arr, @settings.extra_data_rows.get(idx))
59
+ end
60
+ @sheets << sh
61
+ sh
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ require 'spreadsheet' # https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md
2
+ require 'rubyXL' # https://github.com/weshatheleopard/rubyXL
3
+ require 'fileutils'
4
+ require 'date'
5
+ require 'byebug'
6
+
7
+ require "merge_excel/wbook"
8
+ require "merge_excel/sheets"
9
+ require "merge_excel/sheet"
10
+ require "merge_excel/header"
11
+ require "merge_excel/data_row"
12
+ require "merge_excel/version"
13
+ require "merge_excel/excel"
14
+ require "merge_excel/format_detector"
15
+ require "merge_excel/spreadsheet_parser"
16
+ require "merge_excel/spreadsheet_writer"
17
+ require "merge_excel/extensions/spreadsheet/worksheet"
18
+ require "merge_excel/extensions/spreadsheet/workbook"
19
+ require "merge_excel/extensions/rubyXL/worksheet"
20
+ require "merge_excel/extensions/rubyXL/workbook"
21
+
22
+ require "merge_excel/settings/parser"
23
+ require "merge_excel/settings/data_row"
24
+ require "merge_excel/settings/data_rows"
25
+ require "merge_excel/settings/extra_data_row"
26
+ require "merge_excel/settings/extra_data_rows"
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'merge_excel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "merge_excel"
8
+ spec.version = MergeExcel::VERSION
9
+ spec.authors = ["Iwan Buetti"]
10
+ spec.email = ["iwan.buetti@gmail.com"]
11
+
12
+ spec.summary = "Library used to merge same-structure Excel files."
13
+ spec.description = "Allow to simply merge multiple Excel files with the same structure."
14
+ spec.homepage = "https://github.com/AEEGSI/merge_excel"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org/"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "byebug"
34
+
35
+ spec.add_dependency "spreadsheet"
36
+ spec.add_dependency "rubyXL"
37
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merge_excel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Iwan Buetti
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: spreadsheet
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubyXL
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Allow to simply merge multiple Excel files with the same structure.
98
+ email:
99
+ - iwan.buetti@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-version"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/merge_excel.rb
116
+ - lib/merge_excel/data_row.rb
117
+ - lib/merge_excel/excel.rb
118
+ - lib/merge_excel/extensions/rubyXL/workbook.rb
119
+ - lib/merge_excel/extensions/rubyXL/worksheet.rb
120
+ - lib/merge_excel/extensions/spreadsheet/workbook.rb
121
+ - lib/merge_excel/extensions/spreadsheet/worksheet.rb
122
+ - lib/merge_excel/format_detector.rb
123
+ - lib/merge_excel/header.rb
124
+ - lib/merge_excel/settings/data_row.rb
125
+ - lib/merge_excel/settings/data_rows.rb
126
+ - lib/merge_excel/settings/extra_data_row.rb
127
+ - lib/merge_excel/settings/extra_data_rows.rb
128
+ - lib/merge_excel/settings/parser.rb
129
+ - lib/merge_excel/sheet.rb
130
+ - lib/merge_excel/sheets.rb
131
+ - lib/merge_excel/spreadsheet_parser.rb
132
+ - lib/merge_excel/spreadsheet_writer.rb
133
+ - lib/merge_excel/version.rb
134
+ - lib/merge_excel/wbook.rb
135
+ - merge_excel.gemspec
136
+ homepage: https://github.com/AEEGSI/merge_excel
137
+ licenses:
138
+ - MIT
139
+ metadata:
140
+ allowed_push_host: https://rubygems.org/
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.6.11
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Library used to merge same-structure Excel files.
161
+ test_files: []