excel_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in excel_validator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aaditi Jain
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # ExcelValidator
2
+
3
+ Validates the format of your imported excel (xls, xlsx) file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+ `gem 'excel_validator'`
9
+
10
+ And then execute:
11
+ `$ bundle`
12
+
13
+ Or install it yourself as:
14
+ `$ gem install excel_validator`
15
+
16
+ ## Usage
17
+ **require 'excel_validator'**
18
+
19
+ **f = ExcelValidator::ValidateFile.new(file_name, file_path)**
20
+
21
+ #returns true if the number of sheets in the file is 5.
22
+
23
+ **f.number_of_sheets_is?(5)**
24
+
25
+ #returns true if the names of the sheets at each position is as specified. If the names are case sensitive , do f.names_of_sheets_are?({1: 'Banks', 2: 'Countries', 3: 'Metrics'}, true)
26
+
27
+ **f.names_of_sheets_are?({1: 'Banks', 2: 'Countries', 3: 'Metrics'})**
28
+
29
+
30
+ #returns true if the first non-empty row of 2nd sheet in the file is 12
31
+
32
+ **f.first_row_is?(2, 5)**
33
+
34
+
35
+ #returns true if the first non-empty row of 2nd sheet in the file is 100
36
+
37
+ **f.last_row_is?(2,100)**
38
+
39
+
40
+ #returns true if the first non-empty row of 2nd sheet in the file is 'B'
41
+
42
+ **f.first_column_is?(2, 'B')**
43
+
44
+
45
+ #returns true if the first non-empty row of 2nd sheet in the file is 'CK'
46
+
47
+ **f.last_column_is?(2, 'CK')**
48
+
49
+
50
+ #returns true if the 'B' column of 2nd sheet in the file is completely empty
51
+
52
+ **f.column_is_empty?(2, 'B')**
53
+
54
+
55
+ #returns true if the 12th row of 2nd sheet in the file is completely empty
56
+
57
+ **f.row_is_empty?(2, 12)**
58
+
59
+
60
+ #returns true if at 2nd sheet in the file Row3, Column C, content is 'Location'. If the content is case sensitive , do
61
+
62
+ **f.content_of_cell_is?(2, 3, 'C', 'Location', true)**
63
+
64
+
65
+ #returns true if there are empty sheets present in the file.
66
+
67
+ **f.empty_sheets_present?**
68
+
69
+
70
+ #returns true if keyword 'ClassA' is present the 2nd sheet of the file. If the keyword is case sensitive, do f.keyword_present?(2, 'ClassA', true)
71
+
72
+ **f.keyword_present?(2, 'ClassA')**
73
+
74
+
75
+
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+ task test: :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'excel_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "excel_validator"
8
+ spec.version = ExcelValidator::VERSION
9
+ spec.authors = ["Aaditi Jain"]
10
+ spec.email = ["aaditi2290@gmail.com"]
11
+ spec.description = %q{provides methods to validates the content of excel file}
12
+ spec.summary = %q{provides methods to validates the content of excel file}
13
+ spec.homepage = "https://github.com/Aaditi/excel_validator.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency "roo", "~> 1.13.2"
26
+ end
@@ -0,0 +1,3 @@
1
+ module ExcelValidator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,107 @@
1
+ require "excel_validator/version"
2
+ module ExcelValidator
3
+ class ValidateFile
4
+ require "roo"
5
+
6
+ def initialize(file_name,file_path)
7
+ case File.extname(file_name)
8
+ when ".xls"
9
+ @file = Roo::Excel.new(file_path)
10
+ when ".xlsx"
11
+ @file = Roo::Excelx.new(file_path)
12
+ else raise "Not a Excel / Excelx File"
13
+ end
14
+ end
15
+
16
+ #counts the number of sheets in the file.
17
+ #sheet_number: should be an integer
18
+ def number_of_sheets_is?(sheet_number)
19
+ @file.sheets.count == sheet_number ? true : false
20
+ end
21
+
22
+ #checks the names of the sheets in the file
23
+ #sheets_hash: should be a hash with key, value as position, name respectively. Example: {1: 'Bank', 2: 'Metric', 7: 'Country'}
24
+ def names_of_sheets_are?(sheets_hash, case_sensitive = false)
25
+ sheets_hash.each do |key, value|
26
+ if ( case_sensitive ? (@file.sheets[(key.to_i - 1)] != value) : (@file.sheets[(key.to_i - 1)].to_s.downcase != value.to_s.downcase ))
27
+ return false
28
+ end
29
+ end
30
+ true
31
+ end
32
+
33
+ #checks that the first non_empty row in the sheet is as specified
34
+ def first_row_is?(sheet_number, row_number)
35
+ set_default_sheet(sheet_number)
36
+ @file.first_row == row_number ? true : false
37
+ end
38
+
39
+ #checks that the last non_empty row in the sheet is as specified
40
+ def last_row_is?(sheet_number, row_number)
41
+ set_default_sheet(sheet_number)
42
+ @file.last_row == row_number ? true : false
43
+ end
44
+
45
+ #checks that the first non_empty column in the sheet is as specified
46
+ def first_column_is?(sheet_number, column_number)
47
+ set_default_sheet(sheet_number)
48
+ @file.first_column_as_letter == column_number ? true : false
49
+ end
50
+
51
+ #checks that the last non_empty column in the sheet is as specified
52
+ def last_column_is?(sheet_number, column_number)
53
+ set_default_sheet(sheet_number)
54
+ @file.last_column_as_letter == column_number ? true : false
55
+ end
56
+
57
+ #checks if the content at the specified row and column is as required
58
+ def content_of_cell_is?(sheet_number, row, column, content, case_sensitive=false)
59
+ set_default_sheet(sheet_number)
60
+ return ( (case_sensitive) ? (@file.cell(row,column).to_s == content.to_s) : (@file.cell(row,column).to_s.downcase == content.to_s.downcase))
61
+ end
62
+
63
+
64
+ #checks if the specified column is empty
65
+ def column_is_empty?(sheet_number, column)
66
+ set_default_sheet(sheet_number)
67
+ @file.first_row.upto @file.last_row do |row|
68
+ return false if @file.cell(row,column).nil? != true
69
+ end
70
+ true
71
+ end
72
+
73
+ #checks if the specified row is empty
74
+ def row_is_empty?(sheet_number, row)
75
+ @file.first_column.upto @file.last_column do |column|
76
+ return false if @file.cell(row,column).nil? != true
77
+ end
78
+ true
79
+ end
80
+
81
+ #Returns true if there is an empty sheet in the file
82
+ def empty_sheets_present?
83
+ @file.sheets.count.times do |sheet_number|
84
+ set_default_sheet(sheet_number + 1)
85
+ @file.first_row ? next : (return true)
86
+ end
87
+ false
88
+ end
89
+
90
+ #Checks if a keyword is present in the sheet
91
+ def keyword_present?(sheet_number, keyword, case_sensitive=false )
92
+ set_default_sheet(sheet_number)
93
+ @file.first_row.upto @file.last_row do |row|
94
+ @file.first_column.upto @file.last_column do |column|
95
+ return true if ( (case_sensitive) ? @file.cell(row, column) == keyword : @file.cell(row, column).to_s.downcase == keyword.to_s.downcase )
96
+ end
97
+ end
98
+ false
99
+ end
100
+
101
+ private
102
+ def set_default_sheet(sheet_number)
103
+ @file.default_sheet = @file.sheets[sheet_number - 1]
104
+ end
105
+
106
+ end #class end
107
+ end #module end
Binary file
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+
3
+ describe ExcelValidator do
4
+ before (:all) do
5
+ @input_file = ExcelValidator::ValidateFile.new("test_excel.xlsx", "spec/fixtures/test_excel.xlsx")
6
+ end
7
+
8
+ describe '#number_of_sheets_is?' do
9
+ it 'should return true if input integer is equal to number sheets in the file' do
10
+ @input_file.number_of_sheets_is?(3).should eq(true)
11
+ end
12
+
13
+ it 'should return false if input integer is not equal to number sheets in the file' do
14
+ @input_file.number_of_sheets_is?(2).should eq(false)
15
+ end
16
+ end
17
+
18
+ describe '#first_row_is?' do
19
+ it 'should return true if start row of the specified sheet is as in the arguments' do
20
+ @input_file.first_row_is?(1, 2).should eq(true)
21
+ end
22
+
23
+ it 'should return false if start row of the specified sheet is not as in the arguments' do
24
+ @input_file.first_row_is?(1, 6).should eq(false)
25
+ end
26
+ end
27
+
28
+ describe '#last_row_is?' do
29
+ it 'should return true if last row of the specified sheet is as in the arguments' do
30
+ @input_file.last_row_is?(1, 5).should eq(true)
31
+ end
32
+
33
+ it 'should return false if last row of the specified sheet is not as in the arguments' do
34
+ @input_file.last_row_is?(1, 2).should eq(false)
35
+ end
36
+ end
37
+
38
+ describe '#first_column_is?' do
39
+ it 'should return true if start column of the specified sheet is as in the arguments' do
40
+ @input_file.first_column_is?(1, 'B').should eq(true)
41
+ end
42
+
43
+ it 'should return false if start column of the specified sheet is not as in the arguments' do
44
+ @input_file.first_column_is?(1, 'C').should eq(false)
45
+ end
46
+ end
47
+
48
+ describe '#last_column_is?' do
49
+ it 'should return true if last column of the specified sheet is as in the arguments' do
50
+ @input_file.last_column_is?(1, 'E').should eq(true)
51
+ end
52
+
53
+ it 'should return false if last column of the specified sheet is not as in the arguments' do
54
+ @input_file.last_column_is?(1, 'G').should eq(false)
55
+ end
56
+ end
57
+
58
+ describe '#column_is_empty?' do
59
+ it 'should return true if specified column in the specified sheet is empty' do
60
+ @input_file.column_is_empty?(1, 'A').should eq(true)
61
+ end
62
+
63
+ it 'should return false if specified column in the specified sheet is not empty' do
64
+ @input_file.column_is_empty?(1, 'B').should eq(false)
65
+ end
66
+ end
67
+
68
+ describe '#row_is_empty?' do
69
+ it 'should return true if specified row in the specified sheet is empty' do
70
+ @input_file.row_is_empty?(1, 6).should eq(true)
71
+ end
72
+
73
+ it 'should return false if specified row in the specified sheet is not empty' do
74
+ @input_file.row_is_empty?(1, 2).should eq(false)
75
+ end
76
+ end
77
+
78
+ describe '#empty_sheets_present?' do
79
+ it 'should return true if there are empty sheets present in the file' do
80
+ @input_file.empty_sheets_present?.should eq(true)
81
+ end
82
+ end
83
+
84
+ describe '#keyword_present?' do
85
+ it 'should return true if keyword (case insensitve) is present in sheet ' do
86
+ @input_file.keyword_present?(1,"Name").should eq(true)
87
+ end
88
+
89
+ it 'should return false if keyword (case insensitve) is not present in sheet ' do
90
+ @input_file.keyword_present?(1, "Absent").should eq(false)
91
+ end
92
+
93
+ it 'should return true if keyword(case sensitve) is present in sheet ' do
94
+ @input_file.keyword_present?(1,"Name", true).should eq(true)
95
+ end
96
+
97
+ it 'should return false if keyword (case sensitve) is not present in sheet' do
98
+ @input_file.keyword_present?(1, "name", true).should eq(false)
99
+ end
100
+ end
101
+
102
+ describe "#names_of_sheets_are?" do
103
+ it 'should return true if names of sheets in file are as specified in the input hash(case insensitve)' do
104
+ @input_file.names_of_sheets_are?({1 => 'users', 2 => 'country'}).should eq(true)
105
+ end
106
+
107
+ it 'should return false if names of sheets in file are not as specified in the input hash(case insensitve)' do
108
+ @input_file.names_of_sheets_are?({1 => 'Users', 2 => 'Metric'}).should eq(false)
109
+ end
110
+
111
+ it 'should return true if names of sheets in file are as specified in the input hash(case sensitve)' do
112
+ @input_file.names_of_sheets_are?({1 => 'Users', 2 => 'Country'}, true).should eq(true)
113
+ end
114
+
115
+ it 'should return false if names of sheets in file are not as specified in the input hash(case sensitve)' do
116
+ @input_file.names_of_sheets_are?({1 => 'users', 2 => 'country'}, true).should eq(false)
117
+ end
118
+ end
119
+
120
+ describe "#content_of_cell_is?" do
121
+ it 'should return true if content of the cell is as specified(case insensitve)' do
122
+ @input_file.content_of_cell_is?(2, 3, 'B', 'India').should eq(true)
123
+ end
124
+
125
+ it 'should return false if content of the cell is not as specified(case insensitve)' do
126
+ @input_file.content_of_cell_is?(2, 3, 'B', 'Countries').should eq(false)
127
+ end
128
+
129
+ it 'should return true if content of the cell is as specified(case sensitve)' do
130
+ @input_file.content_of_cell_is?(2, 3, 'B', 'India', true).should eq(true)
131
+ end
132
+
133
+ it 'should return false if content of the cell is not as specified(case sensitve)' do
134
+ @input_file.content_of_cell_is?(2, 3, 'B', 'india', true).should eq(false)
135
+ end
136
+ end
137
+
138
+ end
@@ -0,0 +1 @@
1
+ require 'excel_validator'
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excel_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaditi Jain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: roo
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.13.2
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.13.2
78
+ description: provides methods to validates the content of excel file
79
+ email:
80
+ - aaditi2290@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - excel_validator.gemspec
91
+ - lib/excel_validator.rb
92
+ - lib/excel_validator/version.rb
93
+ - spec/fixtures/test_excel.xlsx
94
+ - spec/lib/excel_validator_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: https://github.com/Aaditi/excel_validator.git
97
+ licenses:
98
+ - MIT
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.28
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: provides methods to validates the content of excel file
121
+ test_files:
122
+ - spec/fixtures/test_excel.xlsx
123
+ - spec/lib/excel_validator_spec.rb
124
+ - spec/spec_helper.rb