rubyexcel 0.3.9 → 0.4.0

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.
@@ -1,261 +1,265 @@
1
- require_relative 'rubyexcel/rubyexcel_components.rb'
2
- require 'cgi'
3
- require 'csv'
4
-
5
- #
6
- # Ruby's standard Regexp class.
7
- #
8
-
9
- class Regexp
10
-
11
- #
12
- # A bit of "syntactic sugar" which allows shorthand Regexp blocks
13
- #
14
- # @example
15
- # sheet.filter!( 'Part', &/Type[13]/ )
16
- #
17
-
18
- def to_proc
19
- proc { |string| self =~ string.to_s }
20
- end
21
- end
22
-
23
- #
24
- # Namespace for all RubyExcel Classes and Modules
25
- #
26
-
27
- module RubyExcel
28
-
29
- #
30
- # Don't require Windows-specific libraries unless the relevant methods are called
31
- #
32
-
33
- def self.method_missing( method, *args, &block )
34
- if method == :documents_path
35
- require_relative 'rubyexcel/excel_tools.rb'
36
- send( method, *args, &block )
37
- else
38
- super
39
- end
40
- end
41
-
42
- #
43
- # A Workbook which can hold multiple Sheets
44
- #
45
-
46
- class Workbook
47
- include Enumerable
48
-
49
- # Names of methods which require win32ole
50
- ExcelToolsMethods = [ :disable_formulas!, :documents_path, :dump_to_sheet, :get_excel, :get_workbook, :import, :make_sheet_pretty, :save_excel, :to_excel, :to_safe_format, :to_safe_format! ]
51
-
52
- # Get and set the Workbook name
53
- attr_accessor :name
54
-
55
- #
56
- # Creates a RubyExcel::Workbook instance.
57
- #
58
-
59
- def initialize( name = 'Output' )
60
- @name = name
61
- @sheets = []
62
- end
63
-
64
- #
65
- # Appends an object to the Workbook
66
- #
67
- # @param [RubyExcel::Workbook, RubyExcel::Sheet, Array<Array>] other the object to append to the Workbook
68
- #
69
-
70
- def <<( other )
71
- case other
72
- when Workbook ; other.each { |sht| sht.workbook = self; @sheets << sht }
73
- when Sheet ; @sheets << other; other.workbook = self
74
- when Array ; load( other )
75
- else ; fail TypeError, "Unsupported Type: #{ other.class }"
76
- end
77
- self
78
- end
79
-
80
- #
81
- # Adds a Sheet to the Workbook.
82
- # If no argument is given, names the Sheet 'Sheet' + total number of Sheets
83
- #
84
- # @example
85
- # sheet = workbook.add
86
- # #=> RubyExcel::Sheet:0x2b3a0b8: Sheet1
87
- #
88
- # @param [nil, RubyExcel::Sheet, String] ref the identifier or Sheet to add
89
- # @return [RubyExcel::Sheet] the Sheet which was added
90
-
91
- def add( ref = false )
92
- case ref
93
- when false ; s = Sheet.new( 'Sheet' + ( @sheets.count + 1 ).to_s, self )
94
- when Sheet ; ( s = ref ).workbook = self
95
- when String ; s = Sheet.new( ref, self )
96
- else ; fail TypeError, "Unsupported Type: #{ ref.class }"
97
- end
98
- @sheets << s
99
- s
100
- end
101
- alias add_sheet add
102
-
103
- #
104
- # Removes all Sheets from the Workbook
105
- #
106
-
107
- def clear_all
108
- @sheets = []; self
109
- end
110
- alias delete_all clear_all
111
-
112
- #
113
- # Removes Sheet(s) from the Workbook
114
- #
115
- # @param [Fixnum, String, Regexp, RubyExcel::Sheet, NilClass] ref the reference or object to remove, or nil if passing a block
116
- # @yield [RubyExcel::Sheet] yields each sheet, if there is no argument and a block is given
117
- #
118
-
119
- def delete( ref=nil, &block )
120
-
121
- fail ArgumentError, 'Requires either an argument OR a block' if ref && block_given?
122
-
123
- case ref
124
- when nil ; @sheets.reject! { |sht| yield sht }
125
- when Fixnum ; @sheets.delete_at( ref - 1 )
126
- when String ; @sheets.reject! { |s| s.name == ref }
127
- when Regexp ; @sheets.reject! { |s| s.name =~ ref }
128
- when Sheet ; @sheets.reject! { |s| s == ref }
129
- else ; fail ArgumentError, 'Unrecognised Argument Type: ' + ref.class.to_s
130
- end
131
- self
132
- end
133
-
134
- #
135
- # Return a copy of self
136
- #
137
- # @return [RubyExcel::Workbook]
138
- #
139
-
140
- def dup
141
- wb = Workbook.new
142
- self.each { |s| wb.add s.dup }
143
- wb
144
- end
145
-
146
- #
147
- # Yields each Sheet.
148
- #
149
-
150
- def each
151
- return to_enum( :each ) unless block_given?
152
- @sheets.each { |s| yield s }
153
- end
154
-
155
- #
156
- # Check whether the workbook has Sheets
157
- #
158
- # @return [Boolean] if there are any Sheets in the Workbook
159
- #
160
-
161
- def empty?
162
- @sheets.empty?
163
- end
164
-
165
- # @overload load( input_data, header_rows=1 )
166
- # Shortcut to create a Sheet and fill it with data
167
- # @param [Array<Array>, Hash<Hash>] input_data the data to fill the Sheet with
168
- # @param Fixnum] header_rows the number of Rows to be treated as headers
169
- #
170
-
171
- def load( *args )
172
- add.load( *args )
173
- end
174
-
175
- #
176
- # Don't require Windows-specific libraries unless the relevant methods are called
177
- #
178
-
179
- def method_missing(method, *args, &block)
180
- if ExcelToolsMethods.include?( method )
181
- require_relative 'rubyexcel/excel_tools.rb'
182
- send( method, *args, &block )
183
- else
184
- super
185
- end
186
- end
187
-
188
- #
189
- # Allow for certain method_missing calls
190
- #
191
-
192
- def respond_to?( method, include_private = false )
193
- if ExcelToolsMethods.include?( method )
194
- true
195
- else
196
- super
197
- end
198
-
199
- end
200
-
201
- #
202
- # Select a Sheet or iterate through them
203
- #
204
- # @param [Fixnum, String, Regexp, nil] ref the reference to select a Sheet by
205
- # @return [RubyExcel::Sheet] if a search term was given
206
- # @return [Enumerator] if nil or no argument given
207
- # @yield [RubyExcel::Sheet] yields each sheet, if there is no argument and a block is given
208
- #
209
-
210
- def sheets( ref=nil )
211
- if ref.nil?
212
- return to_enum (:each) unless block_given?
213
- each { |s| yield s }
214
- else
215
- case ref
216
- when Fixnum ; @sheets[ ref - 1 ]
217
- when String ; @sheets.find { |s| s.name =~ /^#{ ref }$/i }
218
- when Regexp ; @sheets.find { |s| s.name =~ ref }
219
- end
220
- end
221
- end
222
-
223
- # {Workbook#sort!}
224
-
225
- def sort( &block )
226
- dup.sort!( &block )
227
- end
228
-
229
- #
230
- # Sort Sheets according to a block
231
- #
232
-
233
- def sort!( &block )
234
- @sheets = @sheets.sort( &block )
235
- end
236
-
237
- # {Workbook#sort_by!}
238
-
239
- def sort_by( &block )
240
- dup.sort_by!( &block )
241
- end
242
-
243
- #
244
- # Sort Sheets by an attribute given in a block
245
- #
246
-
247
- def sort_by!( &block )
248
- @sheets = @sheets.sort_by( &block )
249
- end
250
-
251
- #
252
- # The Workbook as a group of HTML Tables
253
- #
254
-
255
- def to_html
256
- map(&:to_html).join('</br>')
257
- end
258
-
259
- end # Workbook
260
-
1
+ require_relative 'rubyexcel/rubyexcel_components.rb'
2
+ require 'cgi'
3
+ require 'csv'
4
+
5
+ #
6
+ # Ruby's standard Regexp class.
7
+ #
8
+
9
+ class Regexp
10
+
11
+ #
12
+ # A bit of "syntactic sugar" which allows shorthand Regexp blocks
13
+ #
14
+ # @example
15
+ # sheet.filter!( 'Part', &/Type[13]/ )
16
+ #
17
+
18
+ def to_proc
19
+ proc { |string| self =~ string.to_s }
20
+ end
21
+ end
22
+
23
+ #
24
+ # Namespace for all RubyExcel Classes and Modules
25
+ #
26
+
27
+ module RubyExcel
28
+
29
+ #
30
+ # Don't require Windows-specific libraries unless the relevant methods are called
31
+ #
32
+
33
+ def self.method_missing( method, *args, &block )
34
+ if method == :documents_path
35
+ require_relative 'rubyexcel/excel_tools.rb'
36
+ send( method, *args, &block )
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ #
43
+ # A Workbook which can hold multiple Sheets
44
+ #
45
+
46
+ class Workbook
47
+ include Enumerable
48
+
49
+ # Names of methods which require win32ole
50
+ ExcelToolsMethods = [ :disable_formulas!, :documents_path, :dump_to_sheet, :get_excel, :get_workbook, :import, :make_sheet_pretty, :save_excel, :to_excel, :to_safe_format, :to_safe_format! ]
51
+
52
+ # Get and set the Workbook name
53
+ attr_accessor :name
54
+
55
+ # Set to true to cause the workbook to always create a new instance of Excel when exporting
56
+ attr_accessor :standalone
57
+
58
+ #
59
+ # Creates a RubyExcel::Workbook instance.
60
+ #
61
+
62
+ def initialize( name = 'Output' )
63
+ self.name = name
64
+ @sheets = []
65
+ self.standalone = false
66
+ end
67
+
68
+ #
69
+ # Appends an object to the Workbook
70
+ #
71
+ # @param [RubyExcel::Workbook, RubyExcel::Sheet, Array<Array>] other the object to append to the Workbook
72
+ #
73
+
74
+ def <<( other )
75
+ case other
76
+ when Workbook ; other.each { |sht| sht.workbook = self; @sheets << sht }
77
+ when Sheet ; @sheets << other; other.workbook = self
78
+ when Array ; load( other )
79
+ else ; fail TypeError, "Unsupported Type: #{ other.class }"
80
+ end
81
+ self
82
+ end
83
+
84
+ #
85
+ # Adds a Sheet to the Workbook.
86
+ # If no argument is given, names the Sheet 'Sheet' + total number of Sheets
87
+ #
88
+ # @example
89
+ # sheet = workbook.add
90
+ # #=> RubyExcel::Sheet:0x2b3a0b8: Sheet1
91
+ #
92
+ # @param [nil, RubyExcel::Sheet, String] ref the identifier or Sheet to add
93
+ # @return [RubyExcel::Sheet] the Sheet which was added
94
+
95
+ def add( ref = false )
96
+ case ref
97
+ when false ; s = Sheet.new( 'Sheet' + ( @sheets.count + 1 ).to_s, self )
98
+ when Sheet ; ( s = ref ).workbook = self
99
+ when String ; s = Sheet.new( ref, self )
100
+ else ; fail TypeError, "Unsupported Type: #{ ref.class }"
101
+ end
102
+ @sheets << s
103
+ s
104
+ end
105
+ alias add_sheet add
106
+
107
+ #
108
+ # Removes all Sheets from the Workbook
109
+ #
110
+
111
+ def clear_all
112
+ @sheets = []; self
113
+ end
114
+ alias delete_all clear_all
115
+
116
+ #
117
+ # Removes Sheet(s) from the Workbook
118
+ #
119
+ # @param [Fixnum, String, Regexp, RubyExcel::Sheet, NilClass] ref the reference or object to remove, or nil if passing a block
120
+ # @yield [RubyExcel::Sheet] yields each sheet, if there is no argument and a block is given
121
+ #
122
+
123
+ def delete( ref=nil, &block )
124
+
125
+ fail ArgumentError, 'Requires either an argument OR a block' if ref && block_given?
126
+
127
+ case ref
128
+ when nil ; @sheets.reject! { |sht| yield sht }
129
+ when Fixnum ; @sheets.delete_at( ref - 1 )
130
+ when String ; @sheets.reject! { |s| s.name == ref }
131
+ when Regexp ; @sheets.reject! { |s| s.name =~ ref }
132
+ when Sheet ; @sheets.reject! { |s| s == ref }
133
+ else ; fail ArgumentError, 'Unrecognised Argument Type: ' + ref.class.to_s
134
+ end
135
+ self
136
+ end
137
+
138
+ #
139
+ # Return a copy of self
140
+ #
141
+ # @return [RubyExcel::Workbook]
142
+ #
143
+
144
+ def dup
145
+ wb = Workbook.new
146
+ self.each { |s| wb.add s.dup }
147
+ wb
148
+ end
149
+
150
+ #
151
+ # Yields each Sheet.
152
+ #
153
+
154
+ def each
155
+ return to_enum( :each ) unless block_given?
156
+ @sheets.each { |s| yield s }
157
+ end
158
+
159
+ #
160
+ # Check whether the workbook has Sheets
161
+ #
162
+ # @return [Boolean] if there are any Sheets in the Workbook
163
+ #
164
+
165
+ def empty?
166
+ @sheets.empty?
167
+ end
168
+
169
+ # @overload load( input_data, header_rows=1 )
170
+ # Shortcut to create a Sheet and fill it with data
171
+ # @param [Array<Array>, Hash<Hash>] input_data the data to fill the Sheet with
172
+ # @param Fixnum] header_rows the number of Rows to be treated as headers
173
+ #
174
+
175
+ def load( *args )
176
+ add.load( *args )
177
+ end
178
+
179
+ #
180
+ # Don't require Windows-specific libraries unless the relevant methods are called
181
+ #
182
+
183
+ def method_missing(method, *args, &block)
184
+ if ExcelToolsMethods.include?( method )
185
+ require_relative 'rubyexcel/excel_tools.rb'
186
+ send( method, *args, &block )
187
+ else
188
+ super
189
+ end
190
+ end
191
+
192
+ #
193
+ # Allow for certain method_missing calls
194
+ #
195
+
196
+ def respond_to?( method, include_private = false )
197
+ if ExcelToolsMethods.include?( method )
198
+ true
199
+ else
200
+ super
201
+ end
202
+
203
+ end
204
+
205
+ #
206
+ # Select a Sheet or iterate through them
207
+ #
208
+ # @param [Fixnum, String, Regexp, nil] ref the reference to select a Sheet by
209
+ # @return [RubyExcel::Sheet] if a search term was given
210
+ # @return [Enumerator] if nil or no argument given
211
+ # @yield [RubyExcel::Sheet] yields each sheet, if there is no argument and a block is given
212
+ #
213
+
214
+ def sheets( ref=nil )
215
+ if ref.nil?
216
+ return to_enum (:each) unless block_given?
217
+ each { |s| yield s }
218
+ else
219
+ case ref
220
+ when Fixnum ; @sheets[ ref - 1 ]
221
+ when String ; @sheets.find { |s| s.name =~ /^#{ ref }$/i }
222
+ when Regexp ; @sheets.find { |s| s.name =~ ref }
223
+ end
224
+ end
225
+ end
226
+
227
+ # {Workbook#sort!}
228
+
229
+ def sort( &block )
230
+ dup.sort!( &block )
231
+ end
232
+
233
+ #
234
+ # Sort Sheets according to a block
235
+ #
236
+
237
+ def sort!( &block )
238
+ @sheets = @sheets.sort( &block )
239
+ end
240
+
241
+ # {Workbook#sort_by!}
242
+
243
+ def sort_by( &block )
244
+ dup.sort_by!( &block )
245
+ end
246
+
247
+ #
248
+ # Sort Sheets by an attribute given in a block
249
+ #
250
+
251
+ def sort_by!( &block )
252
+ @sheets = @sheets.sort_by( &block )
253
+ end
254
+
255
+ #
256
+ # The Workbook as a group of HTML Tables
257
+ #
258
+
259
+ def to_html
260
+ map(&:to_html).join('</br>')
261
+ end
262
+
263
+ end # Workbook
264
+
261
265
  end # RubyExcel