flex_excel 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/flex_excel.rb +87 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b363bd990addac21fddfb8aa967729d0abfdc5f
4
+ data.tar.gz: 9e124104f7e983829d6f3f0a536cd85ce8509e9f
5
+ SHA512:
6
+ metadata.gz: cc9b82040b83b622b309b9f743ddab28a3bf4b0ad807a358d5226457e2b63c462a7b1b5db237ec7ee087bdd3b0838546268a6cfd356aa0e3aa0967c094127392
7
+ data.tar.gz: 098875ca1e6ee64e6a7ffa0a9b8e8d7ec99382137aa35285adcb030daf95aa66ea993f9b1ea283a1b95c8e145cc238a57e6e19a19fc9802e6a52740943a82e3d
data/lib/flex_excel.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'simple_xlsx_reader'
2
+
3
+ class FlexExcel
4
+
5
+ class << self
6
+
7
+ def mass_cast (hashes, scheme)
8
+ keys = hashes.first.keys
9
+ hashes.each { |hash|
10
+ hash.values.each_with_index { |value, index|
11
+ if scheme[index].is_a?(Array)
12
+ new_value = value
13
+ scheme[index].each { |meth|
14
+ new_value = new_value.send(meth)
15
+ }
16
+ hash.store(keys[index], new_value)
17
+ elsif scheme[index] != 0
18
+ hash.store(keys[index], value.send(scheme[index]))
19
+ end
20
+ }
21
+ }
22
+ hashes
23
+ end
24
+
25
+ end
26
+
27
+ def initialize (path, sheet)
28
+ @sheet = SimpleXlsxReader.open(path).sheets[sheet]
29
+ @rows = @sheet.rows
30
+ end
31
+
32
+ def to_hash (mode = nil, labels = [], convert = {}, filter = {})
33
+ hdr = headers
34
+ raise ArgumentError('Headers must be unique') if hdr.compact.size != hdr.compact.uniq.size
35
+ output = []
36
+ @rows[1..-1].each { |row|
37
+ hash = {}
38
+ hdr.each_with_index { |label, index|
39
+
40
+ if filter.keys.include?(label.downcase) && filter[label.downcase] ==
41
+ row[index]
42
+ hash = nil
43
+ break
44
+ else
45
+
46
+ if convert.empty?
47
+ key = label
48
+ else
49
+ key = convert[label.downcase] unless label.nil?
50
+ end
51
+
52
+ if mode.nil?
53
+ hash.store(key, row[index]) unless label.nil?
54
+ elsif mode == 'trim'
55
+ hash.store(key, row[index]) unless label.nil? || labels.include?(label.downcase)
56
+ elsif mode == 'keep'
57
+ hash.store(key, row[index]) if key != nil && labels.include?(label.downcase)
58
+ end
59
+ end
60
+
61
+ }
62
+ output << hash
63
+ }
64
+ output.compact!
65
+ end
66
+
67
+ def add_headers! (headers)
68
+ @rows.unshift(headers)
69
+ end
70
+
71
+ def chop! (index)
72
+ @rows.map { |row| row[0..index]}
73
+ end
74
+
75
+ def method_missing (method, *args)
76
+ @rows.send(method, *args)
77
+ end
78
+
79
+ def rows
80
+ @sheet.rows
81
+ end
82
+
83
+ def headers
84
+ @sheet.rows.first
85
+ end
86
+
87
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flex_excel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Lai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simple_xlsx_reader
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: To do
28
+ email: ejt.lai@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/flex_excel.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.5.2
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A powerful Excel utility
57
+ test_files: []