Class: ExcelValidator::ValidateFile
- Inherits:
-
Object
- Object
- ExcelValidator::ValidateFile
- Defined in:
- lib/excel_validator.rb
Instance Method Summary collapse
-
#column_is_empty?(sheet_number, column) ⇒ Boolean
checks if the specified column is empty.
-
#content_of_cell_is?(sheet_number, row, column, content, case_sensitive = false) ⇒ Boolean
checks if the content at the specified row and column is as required.
-
#empty_sheets_present? ⇒ Boolean
Returns true if there is an empty sheet in the file.
-
#first_column_is?(sheet_number, column_number) ⇒ Boolean
checks that the first non_empty column in the sheet is as specified.
-
#first_row_is?(sheet_number, row_number) ⇒ Boolean
checks that the first non_empty row in the sheet is as specified.
-
#initialize(file_name, file_path) ⇒ ValidateFile
constructor
A new instance of ValidateFile.
-
#keyword_present?(sheet_number, keyword, case_sensitive = false) ⇒ Boolean
Checks if a keyword is present in the sheet.
-
#last_column_is?(sheet_number, column_number) ⇒ Boolean
checks that the last non_empty column in the sheet is as specified.
-
#last_row_is?(sheet_number, row_number) ⇒ Boolean
checks that the last non_empty row in the sheet is as specified.
-
#names_of_sheets_are?(sheets_hash, case_sensitive = false) ⇒ Boolean
checks the names of the sheets in the file sheets_hash: should be a hash with key, value as position, name respectively.
-
#number_of_sheets_is?(sheet_number) ⇒ Boolean
counts the number of sheets in the file.
-
#row_is_empty?(sheet_number, row) ⇒ Boolean
checks if the specified row is empty.
Constructor Details
#initialize(file_name, file_path) ⇒ ValidateFile
Returns a new instance of ValidateFile.
6 7 8 9 10 11 12 13 14 |
# File 'lib/excel_validator.rb', line 6 def initialize(file_name,file_path) case File.extname(file_name) when ".xls" @file = Roo::Excel.new(file_path) when ".xlsx" @file = Roo::Excelx.new(file_path) else raise "Not a Excel / Excelx File" end end |
Instance Method Details
#column_is_empty?(sheet_number, column) ⇒ Boolean
checks if the specified column is empty
65 66 67 68 69 70 71 |
# File 'lib/excel_validator.rb', line 65 def column_is_empty?(sheet_number, column) set_default_sheet(sheet_number) @file.first_row.upto @file.last_row do |row| return false if @file.cell(row,column).nil? != true end true end |
#content_of_cell_is?(sheet_number, row, column, content, case_sensitive = false) ⇒ Boolean
checks if the content at the specified row and column is as required
58 59 60 61 |
# File 'lib/excel_validator.rb', line 58 def content_of_cell_is?(sheet_number, row, column, content, case_sensitive=false) set_default_sheet(sheet_number) return ( (case_sensitive) ? (@file.cell(row,column).to_s == content.to_s) : (@file.cell(row,column).to_s.downcase == content.to_s.downcase)) end |
#empty_sheets_present? ⇒ Boolean
Returns true if there is an empty sheet in the file
82 83 84 85 86 87 88 |
# File 'lib/excel_validator.rb', line 82 def empty_sheets_present? @file.sheets.count.times do |sheet_number| set_default_sheet(sheet_number + 1) @file.first_row ? next : (return true) end false end |
#first_column_is?(sheet_number, column_number) ⇒ Boolean
checks that the first non_empty column in the sheet is as specified
46 47 48 49 |
# File 'lib/excel_validator.rb', line 46 def first_column_is?(sheet_number, column_number) set_default_sheet(sheet_number) @file.first_column_as_letter == column_number ? true : false end |
#first_row_is?(sheet_number, row_number) ⇒ Boolean
checks that the first non_empty row in the sheet is as specified
34 35 36 37 |
# File 'lib/excel_validator.rb', line 34 def first_row_is?(sheet_number, row_number) set_default_sheet(sheet_number) @file.first_row == row_number ? true : false end |
#keyword_present?(sheet_number, keyword, case_sensitive = false) ⇒ Boolean
Checks if a keyword is present in the sheet
91 92 93 94 95 96 97 98 99 |
# File 'lib/excel_validator.rb', line 91 def keyword_present?(sheet_number, keyword, case_sensitive=false ) set_default_sheet(sheet_number) @file.first_row.upto @file.last_row do |row| @file.first_column.upto @file.last_column do |column| return true if ( (case_sensitive) ? @file.cell(row, column) == keyword : @file.cell(row, column).to_s.downcase == keyword.to_s.downcase ) end end false end |
#last_column_is?(sheet_number, column_number) ⇒ Boolean
checks that the last non_empty column in the sheet is as specified
52 53 54 55 |
# File 'lib/excel_validator.rb', line 52 def last_column_is?(sheet_number, column_number) set_default_sheet(sheet_number) @file.last_column_as_letter == column_number ? true : false end |
#last_row_is?(sheet_number, row_number) ⇒ Boolean
checks that the last non_empty row in the sheet is as specified
40 41 42 43 |
# File 'lib/excel_validator.rb', line 40 def last_row_is?(sheet_number, row_number) set_default_sheet(sheet_number) @file.last_row == row_number ? true : false end |
#names_of_sheets_are?(sheets_hash, case_sensitive = false) ⇒ Boolean
checks the names of the sheets in the file sheets_hash: should be a hash with key, value as position, name respectively. Example: ‘Bank’, 2: ‘Metric’, 7: ‘Country’
24 25 26 27 28 29 30 31 |
# File 'lib/excel_validator.rb', line 24 def names_of_sheets_are?(sheets_hash, case_sensitive = false) sheets_hash.each do |key, value| if ( case_sensitive ? (@file.sheets[(key.to_i - 1)] != value) : (@file.sheets[(key.to_i - 1)].to_s.downcase != value.to_s.downcase )) return false end end true end |
#number_of_sheets_is?(sheet_number) ⇒ Boolean
counts the number of sheets in the file. sheet_number: should be an integer
18 19 20 |
# File 'lib/excel_validator.rb', line 18 def number_of_sheets_is?(sheet_number) @file.sheets.count == sheet_number ? true : false end |
#row_is_empty?(sheet_number, row) ⇒ Boolean
checks if the specified row is empty
74 75 76 77 78 79 |
# File 'lib/excel_validator.rb', line 74 def row_is_empty?(sheet_number, row) @file.first_column.upto @file.last_column do |column| return false if @file.cell(row,column).nil? != true end true end |