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.
- checksums.yaml +4 -4
- data/LICENSE.md +9 -9
- data/README.md +782 -779
- data/lib/rubyexcel.rb +264 -260
- data/lib/rubyexcel/address.rb +187 -187
- data/lib/rubyexcel/data.rb +450 -450
- data/lib/rubyexcel/element.rb +226 -226
- data/lib/rubyexcel/excel_tools.rb +287 -261
- data/lib/rubyexcel/rubyexcel_components.rb +70 -70
- data/lib/rubyexcel/section.rb +376 -376
- data/lib/rubyexcel/sheet.rb +720 -720
- metadata +6 -7
data/lib/rubyexcel.rb
CHANGED
@@ -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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
#
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
self
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
# @
|
89
|
-
#
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
s
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
#
|
117
|
-
#
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
when
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
#
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
#
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
#
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
#
|
206
|
-
#
|
207
|
-
#
|
208
|
-
#
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
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
|