-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_object.rb
396 lines (351 loc) · 9.26 KB
/
excel_object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
98
99
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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
class ExcelObject
class AbstractExcelObject
def initialize(workbook)
# new instance of an excel object attached to the workbook
@workbook = workbook
@sheet = ""
@row = 0
@col = 0
@line = 0
end
def each_sheet
@workbook.Worksheets.each do |sht|
@sheet = sht.Name
yield sht if @sheet.match(Options.sheets_matching)
end
end
def look_in
raise NotImplementedError
end
def find_first_row(sht)
1 # sht.Cells.Find("*", sht.Range("IV65536"), look_in, 2, 1, 1).Row
end
def find_first_column(sht)
1 # sht.Cells.Find("*", sht.Range("IV65536"), look_in, 2, 2, 1).Column
end
def find_last_row(sht)
50 # sht.Cells.Find("*", sht.Range("A1"), look_in, 2, 1, 2).Row
end
def find_last_column(sht)
cell = sht.Cells.Find("*", sht.Range("A1"), look_in, 2, 2, 2)
cell == nil ? 1 : cell.Column
end
def used_range(sht)
first_row = find_first_row(sht)
first_column = find_first_column(sht)
last_row = find_last_row(sht)
last_column = find_last_column(sht)
sht.Range(sht.Cells(first_row, first_column), sht.Cells(last_row, last_column))
end
def each_cell
@workbook.Worksheets.each do |sht|
@sheet = sht.Name
if @sheet.match(Options.sheets_matching)
rng = used_range(sht)
$stderr.puts "Scanning sheet #{@sheet}!#{rng.Address}" if Options.verbose
rng.Rows.each do |row|
@row = row.Row
row.Columns.each do |col|
@col = col.Column
yield col
end
end
end
end
end
def each
raise NotImplementedError
end
def where_r1c1
"#{@sheet}!R#{@row}C#{@col}"
end
def where_a1
col = @col > 26 ? ((@col - 1)/26 + 64).chr + (((@col - 1) % 26)+25).chr : (@col+64).chr
"#{@sheet}!#{col}#{@row}"
end
def where_line
"#{@line}"
end
end
class CellExcelObject < AbstractExcelObject
def where
where_a1
end
end
class FindCellExcelObject < CellExcelObject
def each_cell
@workbook.Worksheets.each do |sht|
@sheet = sht.Name
if @sheet.match(Options.sheets_matching)
rng = sht.UsedRange
$stderr.puts "Scanning sheet #{@sheet}!#{rng.Address}" if Options.verbose
cell = sht.UsedRange.Find("*", nil, look_in, xlPart=2, xlByColumns=2, xlNext=1)
if !cell.nil?
first_address = cell.Address
begin
@row = cell.Row
@col = cell.Column
yield cell
cell = sht.UsedRange.FindNext(cell)
end until cell.nil? || cell.Address == first_address
end
end
end
end
end
class ValueExcelObject < FindCellExcelObject
def look_in
-4163 # xlValues
end
def each
each_cell do |cell|
val = cell.Value.to_s
yield val if val != ""
end
end
end
class CommentsExcelObject < FindCellExcelObject
def look_in
-4144 # xlComments
end
def each
each_cell do |cell|
val = cell.Comment.Text.to_s
yield val if val != ""
end
end
end
class FormulaExcelObject < FindCellExcelObject
def look_in
-4123 # xlFormulas
end
def each
each_cell do |cell|
@cell = cell
yield cell.Formula if cell.HasFormula
end
end
def replace(new_formula)
@cell.Formula = new_formula
end
end
class ConditionalFormattingExcelObject < CellExcelObject
def used_range(sht)
sht.UsedRange
end
def each
each_cell do |cell|
cell.FormatConditions.each do |fc|
begin
op = fc.Operator
rescue
op = 0
end
begin
op1 = fc.Formula1
rescue
op1 = ""
end
begin
op2 = fc.Formula2
rescue
op2 = ""
end
formula = case op
when 0
op1
when 1
"between(#{op1}, #{op2})"
when 2
"not(between(#{op1}, #{op2}))"
when 3
"#{op1} == #{op2}"
when 4
"#{op1} != #{op2}"
when 5
"> #{op1}"
when 6
"< #{op1}"
when 7
">= #{op1}"
when 8
"<= #{op1}"
end
yield formula
end
end
end
end
class ControlExcelObject < AbstractExcelObject
def where
"#{@sheet}.#{@name}"
end
def each
each_sheet do |sht|
sht.OLEObjects.each do |obj|
obj.ole_methods.each do |prop|
Options.controls.each do |c|
@name = "#{obj.Name}.#{prop}"
if @name =~ /#{c}/
@object = obj
@property = prop.to_s
val = nil
begin
val = @object[@property]
rescue
val = nil
end
yield val unless val.nil?
end
end
end
end
end
end
def replace(new_value)
@object[@property] = new_value
end
end
class LineExcelObject < AbstractExcelObject
def where
where_line
end
def iterate(objects, &block)
@line = 0
objects.each do |obj|
@line += 1
begin
val = obj.Value.to_s
rescue # in case COM fails us
val = "?"
end
yield obj.Name + ": " + val
end
end
end
class NameExcelObject < LineExcelObject
def each(&block)
iterate @workbook.Names, &block
end
def replace(line)
@workbook.Names.Item(@line).Value = line.sub(/^(\w+!)?\w+: =/, '=')
end
end
class PropertyExcelObject < LineExcelObject
def each(&block)
iterate @workbook.BuiltinDocumentProperties, &block
end
end
class AddInExcelObject < LineExcelObject
def each
@line = 0
@workbook.Application.AddIns.each do |addin|
@line += 1
yield "#{addin.FullName} (installed: #{addin.Installed})"
end
@workbook.Application.COMAddIns.each do |addin|
@line += 1
yield "COM: #{addin.progID} (#{addin.Description})"
end
end
end
class ReferencesExcelObject < LineExcelObject
def each
@line = 0
@workbook.Application.VBE.ActiveVBProject.References.each do |ref|
@line += 1
yield "#{ref.Name} -- #{ref.Description rescue "<no description>"} (path: #{ref.FullPath})"
end
end
end
class WorkbookCommentExcelObject < LineExcelObject
def each(&block)
@line = 0
@workbook.Comments.each do |comment|
@line += 1
yield comment
end
each_sheet do |sht|
iterate sht.Comments, &block
end
end
end
class MacroExcelObject < LineExcelObject
def where_line
"#{@comp}!##{@line}"
end
def replace(line)
comp = @workbook.VBProject.VBComponents(@comp)
comp.CodeModule.ReplaceLine(@line, line)
end
def delete_current_line
comp = @workbook.VBProject.VBComponents(@comp)
comp.CodeModule.DeleteLines(@line, 1)
end
def each
@workbook.VBProject.VBComponents.each do |comp|
@comp = comp.Name
code = comp.CodeModule.Lines(1, 65536)
@line = 0
code.split(/\r?\n/).each do |line|
@line += 1
yield line
end
end
end
end
class ProcExcelObject < MacroExcelObject
attr_accessor :proc
def where_line
"#{@comp}.#{@proc}!##{@line}"
end
def each
@workbook.VBProject.VBComponents.each do |comp|
@comp = comp.Name
begin
@line = comp.CodeModule.ProcBodyLine(@proc, 0)
cnt = comp.CodeModule.ProcCountLines(@proc, 0)
code = comp.CodeModule.Lines(@line, cnt)
code.split(/\r?\n/).each do |line|
yield line
@line += 1
end
rescue
@line = 0
end
end
end
end
class << self
def new(object_type, workbook)
klass =
case object_type.downcase
when /^macro/
MacroExcelObject
when /^proc/
ProcExcelObject
when /^formula/
FormulaExcelObject
when /^value/
ValueExcelObject
when /^name/
NameExcelObject
when /^conditional/
ConditionalFormattingExcelObject
when /^propert/
PropertyExcelObject
when /^(wb|workbook)comment/
WorkbookCommentExcelObject
when /^comment/
CommentsExcelObject
when /^addin/
AddInExcelObject
when /^reference/
ReferencesExcelObject
when /^control/
ControlExcelObject
end
klass::new(workbook)
end
end
end