Class: ExcelValidator::ValidateFile

Inherits:
Object
  • Object
show all
Defined in:
lib/excel_validator.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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’

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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