merge_excel 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -0
- data/lib/merge_excel.rb +2 -2
- data/lib/merge_excel/data_row.rb +5 -5
- data/lib/merge_excel/header.rb +4 -4
- data/lib/merge_excel/settings/{extra_data_row.rb → extra_column.rb} +8 -9
- data/lib/merge_excel/settings/{extra_data_rows.rb → extra_columns.rb} +5 -5
- data/lib/merge_excel/settings/parser.rb +10 -5
- data/lib/merge_excel/sheet.rb +4 -4
- data/lib/merge_excel/version.rb +1 -1
- data/lib/merge_excel/wbook.rb +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fd22531f7b258fb03b65e156d56e444b8e8dcab
|
4
|
+
data.tar.gz: f8beda50851348cda57c4eebbc33183101e2dab8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 927b150ba12e3f7cb492b1f97f072d4301600fe4a7be8aebd5dc72eb4bc17b56dae6443121071c3f51e527548fb146c94a60053eb525b0c6ead9a23615d5d052
|
7
|
+
data.tar.gz: 527e7f72007805c5e35c5df65f33877d50d864b7761d5b192cd35407d7da8f67be990596e3e635531fbc582fa2dbd0dacfa739b12e4d9e92b9281d624b23f390
|
data/README.md
CHANGED
@@ -61,7 +61,66 @@ me.merge merged_file_path
|
|
61
61
|
|
62
62
|
In this example will be merged only '.xls' files, of which only the columns from 0 to 5 of the second sheet.
|
63
63
|
|
64
|
+
#### Options explanation
|
64
65
|
|
66
|
+
Note that all indexes (sheets, rows and columns) are 0-based.
|
67
|
+
|
68
|
+
- `selector_pattern` is used by `Dir.glob` method for filtering files inside the source directory. Default: `"*.{xls|xlsx}"`.
|
69
|
+
|
70
|
+
- `sheets_idxs` permit to select the sheets to import/merge. Pass an array with indexes (0-based) or `:all`. Default: `:all`.
|
71
|
+
|
72
|
+
- pass an `{ sheet_index => rules }` hash to `data_rows` in order to specify which rows and column you want to import. You can pass `:any` in place of the `sheet_index` for default rules.
|
73
|
+
Rules:
|
74
|
+
- `header_row` set the header row index. Put `:missing` to omit the header. Default is `0`
|
75
|
+
- `data_starting_row` set the index of the **first row** to import, default is `1`
|
76
|
+
- `data_first_column` set the index of the **first column** to import, default is `0`
|
77
|
+
- `data_last_column` set the index of the **last column** to import, default is `:last`
|
78
|
+
|
79
|
+
- pass an `{ sheet_index => rules }` hash to `extra_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:
|
80
|
+
- `position` define the column position of extra data, you can put at `:beginning` or `:end`
|
81
|
+
- `data` define an array of desired extra data
|
82
|
+
- `type` can be `:filename` if you want to add filename info or `:cell_value` if you want to pick extra info from a cell
|
83
|
+
- `label` allow you to set extra data header name
|
84
|
+
- when you select `:cell_value` you have to specify `sheet_idx`, `row_idx` and `col_idx`.
|
85
|
+
|
86
|
+
Another example:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
options = {
|
90
|
+
sheets_idxs: :all,
|
91
|
+
data_rows: {
|
92
|
+
1 => {
|
93
|
+
header_row: 0,
|
94
|
+
data_starting_row: 1,
|
95
|
+
data_first_column: 0 # all columns
|
96
|
+
},
|
97
|
+
:any => {
|
98
|
+
header_row: :missing,
|
99
|
+
data_starting_row: 1,
|
100
|
+
data_first_column: 0,
|
101
|
+
data_last_column: 5
|
102
|
+
}
|
103
|
+
},
|
104
|
+
extra_columns: {
|
105
|
+
any: {
|
106
|
+
position: :beginning,
|
107
|
+
data: [
|
108
|
+
{
|
109
|
+
type: :filename,
|
110
|
+
label: "Filename"
|
111
|
+
},
|
112
|
+
{
|
113
|
+
type: :cell_value,
|
114
|
+
label: "Company",
|
115
|
+
sheet_idx: 0,
|
116
|
+
row_idx: 10,
|
117
|
+
col_idx: 3,
|
118
|
+
}
|
119
|
+
]
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
```
|
65
124
|
|
66
125
|
|
67
126
|
## Development
|
data/lib/merge_excel.rb
CHANGED
@@ -22,5 +22,5 @@ require "merge_excel/extensions/rubyXL/workbook"
|
|
22
22
|
require "merge_excel/settings/parser"
|
23
23
|
require "merge_excel/settings/data_row"
|
24
24
|
require "merge_excel/settings/data_rows"
|
25
|
-
require "merge_excel/settings/
|
26
|
-
require "merge_excel/settings/
|
25
|
+
require "merge_excel/settings/extra_column"
|
26
|
+
require "merge_excel/settings/extra_columns"
|
data/lib/merge_excel/data_row.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module MergeExcel
|
2
2
|
class DataRow
|
3
3
|
attr_reader :array
|
4
|
-
|
5
|
-
def initialize(array,
|
6
|
-
if
|
7
|
-
extra_array =
|
4
|
+
|
5
|
+
def initialize(array, extra_col_hash, book)
|
6
|
+
if extra_col_hash
|
7
|
+
extra_array = extra_col_hash.data.map do |e|
|
8
8
|
case e.type
|
9
9
|
when :filename
|
10
10
|
book.filename
|
@@ -14,7 +14,7 @@ module MergeExcel
|
|
14
14
|
# error?
|
15
15
|
end
|
16
16
|
end
|
17
|
-
case
|
17
|
+
case extra_col_hash.position
|
18
18
|
when :beginning
|
19
19
|
@array = extra_array + array
|
20
20
|
else
|
data/lib/merge_excel/header.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module MergeExcel
|
2
2
|
class Header
|
3
|
-
def initialize(array,
|
3
|
+
def initialize(array, extra_col_hash)
|
4
4
|
|
5
|
-
if
|
6
|
-
extra_array =
|
7
|
-
case
|
5
|
+
if extra_col_hash
|
6
|
+
extra_array = extra_col_hash.data.map{|e| e.label}
|
7
|
+
case extra_col_hash.position
|
8
8
|
when :beginning
|
9
9
|
@array = extra_array + array
|
10
10
|
else
|
@@ -1,10 +1,9 @@
|
|
1
1
|
module MergeExcel
|
2
2
|
module Settings
|
3
|
-
class
|
4
|
-
|
5
|
-
DEFAULT_EXTRA_DATA_ROW_CONFIG = {}
|
3
|
+
class ExtraColumn
|
4
|
+
DEFAULT_EXTRA_COLUMN_CONFIG = {}
|
6
5
|
|
7
|
-
class
|
6
|
+
class FilenameExtraColumn
|
8
7
|
TYPE = :filename
|
9
8
|
attr_reader :label, :type
|
10
9
|
def initialize(label)
|
@@ -13,7 +12,7 @@ module MergeExcel
|
|
13
12
|
end
|
14
13
|
end
|
15
14
|
|
16
|
-
class
|
15
|
+
class CellValueExtraColumn
|
17
16
|
TYPE = :cell_value
|
18
17
|
attr_reader :label, :type, :sheet_idx, :row_idx, :col_idx
|
19
18
|
def initialize(label, coordinates_hash)
|
@@ -38,21 +37,21 @@ module MergeExcel
|
|
38
37
|
def read_data_array(array)
|
39
38
|
array.each do |h|
|
40
39
|
if h[:type]==:filename
|
41
|
-
@data <<
|
40
|
+
@data << FilenameExtraColumn.new(h.fetch(:label){"Filename"})
|
42
41
|
elsif h[:type]==:cell_value
|
43
|
-
@data <<
|
42
|
+
@data << CellValueExtraColumn.new(h.fetch(:label){"Field"}, h)
|
44
43
|
end
|
45
44
|
end
|
46
45
|
end
|
47
46
|
|
48
47
|
|
49
48
|
def self.with_defaults
|
50
|
-
new(
|
49
|
+
new(DEFAULT_EXTRA_COLUMN_CONFIG)
|
51
50
|
end
|
52
51
|
|
53
52
|
private
|
54
53
|
def validate_position
|
55
|
-
raise "Problem with '
|
54
|
+
raise "Problem with 'extra_column' settings: 'position' must be :beginning or :end" unless [:beginning, :end].include?(@position)
|
56
55
|
end
|
57
56
|
end
|
58
57
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module MergeExcel
|
2
2
|
module Settings
|
3
|
-
class
|
3
|
+
class ExtraColumns
|
4
4
|
def initialize
|
5
5
|
@hash = {}
|
6
6
|
end
|
@@ -8,14 +8,14 @@ module MergeExcel
|
|
8
8
|
def insert(sheet_idx, h)
|
9
9
|
# h: {:any=>{:position=>:beginning, :data=>{:type=>:filename, :heading_text=>"Filename"}}}
|
10
10
|
validate_sheet_idx sheet_idx
|
11
|
-
@hash[sheet_idx] =
|
11
|
+
@hash[sheet_idx] = ExtraColumn.new(h)
|
12
12
|
end
|
13
13
|
|
14
14
|
def get(sheet_idx) # a number>=0 or :any
|
15
15
|
validate_sheet_idx sheet_idx
|
16
16
|
@hash.fetch(sheet_idx) do
|
17
17
|
@hash.fetch(:any) do
|
18
|
-
@hash[:any] =
|
18
|
+
@hash[:any] = ExtraColumn.with_defaults
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -23,9 +23,9 @@ module MergeExcel
|
|
23
23
|
private
|
24
24
|
def validate_sheet_idx(sheet_idx)
|
25
25
|
if sheet_idx.is_a?(Integer)
|
26
|
-
raise "Problem with '
|
26
|
+
raise "Problem with 'extra_columns' settings: sheet indexes must be >=0" if sheet_idx<0
|
27
27
|
elsif sheet_idx!=:any
|
28
|
-
raise "Problem with '
|
28
|
+
raise "Problem with 'extra_columns' settings: sheet indexes must be an integer or :any symbol"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -1,12 +1,17 @@
|
|
1
1
|
module MergeExcel
|
2
2
|
module Settings
|
3
3
|
class Parser
|
4
|
-
attr_reader :selector_pattern, :sheets_idxs, :data_rows, :
|
4
|
+
attr_reader :selector_pattern, :sheets_idxs, :data_rows, :extra_columns
|
5
5
|
def initialize(hash)
|
6
6
|
@selector_pattern = hash.fetch(:selector_pattern){ "*.{xls,xlsx}" }
|
7
7
|
read_sheets_index hash.fetch(:sheets_idxs){ :all }
|
8
8
|
read_data_rows hash.fetch(:data_rows){ {any: DataRow::DEFAULT_DATA_ROW_CONFIG } }
|
9
|
-
|
9
|
+
|
10
|
+
if hash.has_key?(:extra_data_rows) # legacy setting
|
11
|
+
read_extra_columns hash.fetch(:extra_data_rows){ {any: ExtraColumn::DEFAULT_EXTRA_COLUMN_CONFIG } }
|
12
|
+
else
|
13
|
+
read_extra_columns hash.fetch(:extra_columns){ {any: ExtraColumn::DEFAULT_EXTRA_COLUMN_CONFIG } }
|
14
|
+
end
|
10
15
|
end
|
11
16
|
|
12
17
|
def all_sheets?
|
@@ -35,11 +40,11 @@ module MergeExcel
|
|
35
40
|
end
|
36
41
|
end
|
37
42
|
|
38
|
-
def
|
43
|
+
def read_extra_columns(h)
|
39
44
|
# {:any=>{:position=>:beginning, :data=>[{:type=>:filename, :heading_text=>"Filename"}}]}
|
40
|
-
@
|
45
|
+
@extra_columns = ExtraColumns.new
|
41
46
|
h.each_pair do |k,v|
|
42
|
-
@
|
47
|
+
@extra_columns.insert(k, v)
|
43
48
|
end
|
44
49
|
end
|
45
50
|
end
|
data/lib/merge_excel/sheet.rb
CHANGED
@@ -11,12 +11,12 @@ module MergeExcel
|
|
11
11
|
@data_rows = []
|
12
12
|
end
|
13
13
|
|
14
|
-
def add_header(array,
|
15
|
-
@header = Header.new(array,
|
14
|
+
def add_header(array, extra_columns)
|
15
|
+
@header = Header.new(array, extra_columns)
|
16
16
|
end
|
17
17
|
|
18
|
-
def add_data_row(array,
|
19
|
-
@data_rows << DataRow.new(array || [],
|
18
|
+
def add_data_row(array, extra_columns, book)
|
19
|
+
@data_rows << DataRow.new(array || [], extra_columns, book).array
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/merge_excel/version.rb
CHANGED
data/lib/merge_excel/wbook.rb
CHANGED
@@ -18,7 +18,7 @@ module MergeExcel
|
|
18
18
|
row_idx = sh_stt.data_starting_row
|
19
19
|
loop do
|
20
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.
|
21
|
+
sh.add_data_row(cells, @settings.extra_columns.get(idx), book)
|
22
22
|
row_idx+=1
|
23
23
|
end
|
24
24
|
end
|
@@ -55,7 +55,7 @@ module MergeExcel
|
|
55
55
|
sh = Sheet.new(sheet, idx)
|
56
56
|
if sh_stt.import_header?
|
57
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.
|
58
|
+
sh.add_header(arr, @settings.extra_columns.get(idx))
|
59
59
|
end
|
60
60
|
@sheets << sh
|
61
61
|
sh
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merge_excel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iwan Buetti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -123,8 +123,8 @@ files:
|
|
123
123
|
- lib/merge_excel/header.rb
|
124
124
|
- lib/merge_excel/settings/data_row.rb
|
125
125
|
- lib/merge_excel/settings/data_rows.rb
|
126
|
-
- lib/merge_excel/settings/
|
127
|
-
- lib/merge_excel/settings/
|
126
|
+
- lib/merge_excel/settings/extra_column.rb
|
127
|
+
- lib/merge_excel/settings/extra_columns.rb
|
128
128
|
- lib/merge_excel/settings/parser.rb
|
129
129
|
- lib/merge_excel/sheet.rb
|
130
130
|
- lib/merge_excel/sheets.rb
|