hmcgowan-roo-tools 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/README +43 -0
  2. data/bin/oogrep +4 -0
  3. data/lib/roo-tools.rb +74 -0
  4. data/lib/roo-tools/grep.rb +129 -0
  5. metadata +79 -0
data/README ADDED
@@ -0,0 +1,43 @@
1
+ = OOGrep
2
+
3
+ Search multiple spreadsheets using roo. This means that you can search across Excel, Excelx, OpenOffice and Google Spreadsheets.
4
+
5
+ sage: oogrep.rb [options] searchstring targets
6
+
7
+ Options:
8
+ -l, --list-only List matching files only
9
+ -i, --case-insensitive Case insensitive search
10
+ -e, --exact-match Force exact match on cell contents
11
+ -f, --font STYLE Filter results by cell font style
12
+ --[no-]tabs Show/hide tabs in results
13
+ --[no-]cells Show/hide cells in results
14
+ -?, -h, --help Show this message.
15
+
16
+ == Options
17
+
18
+ * list-only
19
+
20
+ Only show the names of files that have a match
21
+
22
+ * case-insensitive
23
+
24
+ The searches are strict by default but you can make the search ignore case with this option
25
+
26
+ * exact-match
27
+
28
+ This option will force the search to only show results if the cell's text matches the search string verbatim
29
+
30
+ * font-style
31
+
32
+ You can filter the search results based on the font. Valid values include 'bold', 'italic', 'underline','normal' and 'ignore'. By default the results will ignore the font information. Also note
33
+ that tabs will be filtered out unless the font is 'normal' or 'ignore'.
34
+
35
+ Note that this feature requires the git version of roo if/until the patch to roo for fonts is integrated
36
+
37
+ * [no]-tabs
38
+
39
+ Show or hide tabs from the results using --tabs or --no-tabs
40
+
41
+ * [no]-cells
42
+
43
+ Show or hide cells from the results using --cells or --no-cells
data/bin/oogrep ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
3
+ require 'roo-tools'
4
+ exit ::RooTools::RooGrepRunner.new.execute
data/lib/roo-tools.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'roo'
2
+ require 'user-choices'
3
+ require 'roo-tools/grep'
4
+
5
+ module RooTools
6
+
7
+ class RooGrepRunner < UserChoices::Command
8
+ include UserChoices
9
+
10
+ def initialize
11
+ @seen = {}
12
+ @matches = 0
13
+ @matching_files = 0
14
+ super
15
+ end
16
+
17
+ def add_sources(builder)
18
+ builder.add_source(CommandLineSource, :usage, "Usage: oogrep.rb [options] searchstring targets")
19
+ end
20
+
21
+ def add_choices(builder)
22
+ builder.add_choice(:list_only, :type=>:boolean, :default=>false) do | command_line |
23
+ command_line.uses_option("-l", "--list-only", "List matching files only")
24
+ end
25
+ builder.add_choice(:case_insensitive, :type=>:boolean, :default=>false) do | command_line |
26
+ command_line.uses_option("-i", "--case-insensitive", "Case insensitive search")
27
+ end
28
+ builder.add_choice(:exact_match, :type=>:boolean, :default=>false) do | command_line |
29
+ command_line.uses_option("-e", "--exact-match", "Force exact match on cell contents")
30
+ end
31
+ builder.add_choice(:font, :type=>['bold','italic','underline','normal','ignore'], :default=>'ignore') do | command_line |
32
+ command_line.uses_option("-f", "--font STYLE", "Filter results by cell font style")
33
+ end
34
+ builder.add_choice(:tabs, :type=>:boolean, :default=>true) do | command_line |
35
+ command_line.uses_switch("--tabs", "Show/hide tabs in results")
36
+ end
37
+ builder.add_choice(:cells, :type=>:boolean, :default=>true) do | command_line |
38
+ command_line.uses_switch("--cells", "Show/hide cells in results")
39
+ end
40
+ builder.add_choice(:files) { |command_line |
41
+ command_line.uses_arglist
42
+ }
43
+ end
44
+
45
+ def execute
46
+ if @user_choices[:help]
47
+ puts @user_choices[:usage]
48
+ exit 0
49
+ end
50
+ @user_choices[:searchstring] = @user_choices[:files].shift
51
+ puts
52
+ start_interrupt_handler
53
+ oogrep = Grep.new(@user_choices)
54
+ oogrep.process_files
55
+ unless @user_choices[:list_only]
56
+ puts "#{oogrep.matches} matches in #{oogrep.matching_files} file(s)"
57
+ end
58
+ return 0
59
+ end
60
+
61
+ def start_interrupt_handler
62
+ trap "SIGINT", proc { puts "\nUser Interrupt...exiting" ; exit 1}
63
+ end
64
+ private :start_interrupt_handler
65
+
66
+ def exit_gracefully
67
+ exit 0
68
+ end
69
+ private :exit_gracefully
70
+
71
+ end
72
+ end
73
+
74
+
@@ -0,0 +1,129 @@
1
+ module RooTools
2
+
3
+ class Grep
4
+ @@matches = 0
5
+ @@matching_files = 0
6
+ FilenameFormat = "%-76s"
7
+ MatchFormat = "%-31s %-8s %-35s "
8
+ def matches
9
+ @@matches
10
+ end
11
+ def matching_files
12
+ @@matching_files
13
+ end
14
+
15
+ def initialize(opts)
16
+ @options = opts
17
+ @regex = create_regular_expression
18
+ @seen = {}
19
+ end
20
+
21
+ def process_files
22
+ @options[:files].each do |file|
23
+ if File.directory?(file)
24
+ Dir.glob(File.join(file.gsub('\\','/'),"**","*.xls")).each do |file|
25
+ execute(file)
26
+ end
27
+ else
28
+ execute(file)
29
+ end
30
+ end
31
+ end
32
+
33
+ def execute(file)
34
+ @file = file
35
+ @oo = open_spreadsheet(@file)
36
+ @oo.sheets.each do |sheet|
37
+ @oo.default_sheet = sheet
38
+ check_sheet_name
39
+ check_sheet_contents
40
+ return if @options[:list_only] && @seen[@file]
41
+ end
42
+ end
43
+
44
+ def open_spreadsheet(filename)
45
+ case File.extname(filename)
46
+ when '.xls'
47
+ Excel.new(filename)
48
+ when '.xlsx'
49
+ Excelx.new(filename)
50
+ when '.ods'
51
+ Openoffice.new(filename)
52
+ when ''
53
+ Google.new(filename)
54
+ else
55
+ raise ArgumentError, "Don't know how to handle spreadsheet #{filename}"
56
+ end
57
+ end
58
+
59
+ def check_sheet_name
60
+ return unless @options[:tabs]
61
+ return unless @options[:font] == 'normal' || @options[:font] == 'ignore'
62
+ if @oo.default_sheet =~ @regex
63
+ report_match
64
+ end
65
+ end
66
+
67
+ def check_sheet_contents
68
+ return unless @options[:cells]
69
+ return unless @oo.last_row && @oo.last_column
70
+ (1..@oo.last_row).each do |row|
71
+ (1..@oo.last_column).each do |col|
72
+ cell = @oo.cell(row,col)
73
+ cellname = GenericSpreadsheet.number_to_letter(col) + row.to_s
74
+ if cell =~ @regex
75
+ case @options[:font]
76
+ when 'ignore'
77
+ report_match cell, cellname
78
+ when 'normal'
79
+ if !@oo.font(row,col).bold? && !@oo.font(row,col).bold? && !@oo.font(row,col).italic?
80
+ report_match cell, cellname
81
+ end
82
+ else
83
+ if @oo.font(row,col).send "#{@options[:font]}?"
84
+ report_match cell, cellname
85
+ end
86
+ end
87
+ return if @options[:list_only] && @seen[@file]
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ def report_match(desc=nil, cellname=nil )
94
+ if desc && desc.length >= 35
95
+ desc = desc[0..32] + '...'
96
+ end
97
+ unless @seen[@file]
98
+ puts "\n" unless @options[:list_only]
99
+ puts sprintf(FilenameFormat, @file[0..74])
100
+ @seen[@file] = true
101
+ @@matching_files += 1
102
+ end
103
+ @@matches +=1
104
+ unless @options[:list_only]
105
+ if cellname
106
+ # This is a matching tab
107
+ puts sprintf(MatchFormat, @oo.default_sheet, cellname, desc)
108
+ else
109
+ # This is a matching tab
110
+ puts sprintf(MatchFormat, @oo.default_sheet, 'tabname', nil)
111
+ end
112
+ end
113
+ end
114
+
115
+ def create_regular_expression
116
+ str = @options[:searchstring]
117
+ if @options[:exact_match]
118
+ str = "\\s*\\A#{Regexp.escape(str)}\\Z\\s*"
119
+ end
120
+ if @options[:case_insensitive]
121
+ /#{str}/i
122
+ else
123
+ /#{str}/
124
+ end
125
+ end
126
+
127
+
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hmcgowan-roo-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Hugh McGowan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-12 00:00:00 -07:00
13
+ default_executable: oogrep
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: roo
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: user-choices
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.6
34
+ version:
35
+ description: Tools using roo to performa actions on multiple spreadsheets
36
+ email: hugh_mcgowan@yahoo.com
37
+ executables:
38
+ - oogrep
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - lib/roo-tools
45
+ - lib/roo-tools/grep.rb
46
+ - lib/roo-tools.rb
47
+ - bin/oogrep
48
+ - README
49
+ has_rdoc: true
50
+ homepage: ""
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README
55
+ - --inline-source
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: roo-tools
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: roo-tools
78
+ test_files: []
79
+