Class: RooTools::Grep
- Inherits:
-
Object
- Object
- RooTools::Grep
- Defined in:
- lib/roo-tools/grep.rb
Constant Summary collapse
- FilenameFormat =
"%-76s"
- MatchFormat =
"%-31s %-8s %-35s "
- @@matches =
0
- @@matching_files =
0
Instance Method Summary collapse
- #check_sheet_contents ⇒ Object
- #check_sheet_name ⇒ Object
- #create_regular_expression ⇒ Object
- #execute(file) ⇒ Object
-
#initialize(opts) ⇒ Grep
constructor
A new instance of Grep.
- #matches ⇒ Object
- #matching_files ⇒ Object
- #open_spreadsheet(filename) ⇒ Object
- #process_files ⇒ Object
- #report_match(desc = nil, cellname = nil) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Grep
Returns a new instance of Grep.
15 16 17 18 19 |
# File 'lib/roo-tools/grep.rb', line 15 def initialize(opts) @options = opts @regex = create_regular_expression @seen = {} end |
Instance Method Details
#check_sheet_contents ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/roo-tools/grep.rb', line 73 def check_sheet_contents return unless @options[:cells] return unless @oo.last_row && @oo.last_column (1..@oo.last_row).each do |row| (1..@oo.last_column).each do |col| cell = @oo.cell(row,col) cellname = GenericSpreadsheet.number_to_letter(col) + row.to_s if cell =~ @regex case @options[:font] when 'ignore' report_match cell, cellname when 'normal' if !@oo.font(row,col).bold? && !@oo.font(row,col).bold? && !@oo.font(row,col).italic? report_match cell, cellname end else if @oo.font(row,col).send "#{@options[:font]}?" report_match cell, cellname end end return if @options[:list_only] && @seen[@file] end end end end |
#check_sheet_name ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/roo-tools/grep.rb', line 65 def check_sheet_name return unless @options[:tabs] return unless @options[:font] == 'normal' || @options[:font] == 'ignore' if @oo.default_sheet =~ @regex report_match end end |
#create_regular_expression ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/roo-tools/grep.rb', line 121 def create_regular_expression str = @options[:searchstring] if @options[:exact_match] str = "\\s*\\A#{Regexp.escape(str)}\\Z\\s*" end if @options[:case_insensitive] /#{str}/i else /#{str}/ end end |
#execute(file) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/roo-tools/grep.rb', line 35 def execute(file) begin @file = file @oo = open_spreadsheet(@file) @oo.sheets.each do |sheet| @oo.default_sheet = sheet check_sheet_name check_sheet_contents return if @options[:list_only] && @seen[@file] end rescue Exception =>e puts "Unable to open file: #{file}" end end |
#matches ⇒ Object
8 9 10 |
# File 'lib/roo-tools/grep.rb', line 8 def matches @@matches end |
#matching_files ⇒ Object
11 12 13 |
# File 'lib/roo-tools/grep.rb', line 11 def matching_files @@matching_files end |
#open_spreadsheet(filename) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/roo-tools/grep.rb', line 50 def open_spreadsheet(filename) case File.extname(filename) when '.xls' Excel.new(filename) when '.xlsx' Excelx.new(filename) when '.ods' Openoffice.new(filename) when '' Google.new(filename) else raise ArgumentError, "Don't know how to handle spreadsheet #{filename}" end end |
#process_files ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/roo-tools/grep.rb', line 21 def process_files @options[:files].each do |file| if File.directory?(file) if @options[:recurse] Dir.glob(File.join(file.gsub('\\','/'),"**","*.{xls, xlsx, ods}")).each { |file| execute(file)} else Dir.glob(File.join(file.gsub('\\','/'),"*.{xls, xlsx, ods}")).each { |file| execute(file)} end else execute(file) end end end |
#report_match(desc = nil, cellname = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/roo-tools/grep.rb', line 99 def report_match(desc=nil, cellname=nil ) if desc && desc.length >= 35 desc = desc[0..32] + '...' end unless @seen[@file] puts "\n" unless @options[:list_only] puts sprintf(FilenameFormat, @file[0..74]) @seen[@file] = true @@matching_files += 1 end @@matches +=1 unless @options[:list_only] if cellname # This is a matching tab puts sprintf(MatchFormat, @oo.default_sheet, cellname, desc) else # This is a matching tab puts sprintf(MatchFormat, @oo.default_sheet, 'tabname', nil) end end end |