robust_excel_ole 0.6.2 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +140 -175
- data/lib/robust_excel_ole/book.rb +3 -2
- data/lib/robust_excel_ole/excel.rb +162 -142
- data/lib/robust_excel_ole/reo_common.rb +2 -2
- data/lib/robust_excel_ole/version.rb +1 -1
- data/robust_excel_ole.gemspec +7 -2
- data/spec/book_spec.rb +2 -2
- data/spec/book_specs/book_open_spec.rb +5 -2
- data/spec/book_specs/book_save_spec.rb +5 -24
- data/spec/book_specs/book_sheet_spec.rb +1 -1
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/different_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +232 -272
- data/spec/reo_common_spec.rb +1 -1
- metadata +12 -10
- data/README_detail.rdoc +0 -810
data/README.rdoc
CHANGED
@@ -1,19 +1,14 @@
|
|
1
1
|
= RobustExcelOle
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
This ruby gem automates modifying, reading and writing Excel files.
|
4
|
+
It supports simultaneously running Excel instances and user interactions.
|
5
|
+
RobustExcelOle deals with various cases of Excel and user behaviour,
|
6
|
+
and implements workarounds for some Excel bugs.
|
7
|
+
The gem provides convenient methods for common tasks, and facilitates referenced libraries.
|
8
|
+
It supports Excel 2010 and Excel 2007.
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
- robust Excel AUtomation: be able to deal with all cases of Excel (and user) behaviour, encapsulate errors
|
10
|
-
- provide convenient methods for frequent (common) tasks
|
11
|
-
- support the use of simultaneously running Excel instances
|
12
|
-
- comprehensive wrapping of Excel using Win32OLE
|
13
|
-
- allow the presence of referenced libraries and provide some support for that
|
14
|
-
- support EXCEL 2010, EXCEL 2007
|
15
|
-
|
16
|
-
This is work in progress.
|
10
|
+
RobustExcelOle works by sending VBA methods via Win32OLE.
|
11
|
+
Moreover, it implements a management system and keeps track of Excel files and Excel instances.
|
17
12
|
|
18
13
|
== Requirements
|
19
14
|
|
@@ -25,77 +20,89 @@ This is work in progress.
|
|
25
20
|
|
26
21
|
== Usage
|
27
22
|
|
23
|
+
require 'robust_excel_ole'
|
24
|
+
include RobustExcelOle
|
25
|
+
|
28
26
|
=== Opening a workbook.
|
29
27
|
|
30
28
|
Example:
|
31
|
-
|
32
|
-
include RobustExcelOle
|
29
|
+
|
33
30
|
book = Book.open('workbook.xls')
|
34
31
|
|
35
|
-
|
32
|
+
You can also open a workbook with a block.
|
36
33
|
The semantics is similar to, e.g., +File.open+.
|
37
34
|
|
38
35
|
Book.open('workbook.xls') do |book|
|
39
36
|
# do something
|
40
37
|
end
|
41
38
|
|
42
|
-
|
39
|
+
The options of +open+ are:
|
40
|
+
|
41
|
+
+:default_excel+:: open the workbook in the Excel instance where it was opened before most recently,
|
42
|
+
if the book was opened before and this Excel instance is alive. Otherwise open it in the current (+:current+, or +:active+, or +:reuse+), a new (+:new+) or a given Excel instance (default: +:current+)
|
43
|
+
+:force_excel+:: open the workbook in the current (+:current+, +:active+, or +:reuse+), a new (+:new+) or given Excel instance
|
44
|
+
+:if_absent+:: if the workbook with the given file name does not exist, then raise an error or create a file
|
45
|
+
+:if_unsaved+:: specify behaviour it the workbook was unsaved (+:raise+, +:forget+, +:save+, +:alert+, +:new_excel+)
|
46
|
+
+:if_obstructed+:: specidy behaviour if the workbook is blocked by another book (default: +new_excel+)
|
47
|
+
+:read_only+:: open in read-only mode (default: +false+)
|
48
|
+
+:check_compatibility:: check compatibility when saving (default: +false+)
|
49
|
+
+:update_links+ : user is never (default) or always being asked how to update links
|
50
|
+
+:visible+:: make the window of the workbook visible (default: +true) or invisible
|
43
51
|
|
44
|
-
|
45
|
-
+:read_only+, +:visible+, +:check_compatibility, and +:update_links+.
|
46
|
-
|
47
|
-
Valid values for +:default_excel+ are +:current+ (or +:active+, or +:reuse+), +:new+ or some Excel instance, for +:force_excel+ : +:current+ (or +:active+, or +:reuse+), +:new+ or some Excel instance, for +:if_unsaved+ : +:raise+, +:accept+, +:forget+, +:alert+ and +:new_excel+, for +:if_obstructed+ : +:raise+, +:save+, +:close_if_saved+, +:forget+, +:alert+ and +:new_excel+ , for +:if_absent+ : +:raise+ and +:create+, for +:update_links+ : +:never+, +:always+, +:alert+. The others are Boolean.
|
48
|
-
|
49
|
-
Remark: Doing updating links seems to be dependent on calculation mode: updates happen, if the calcultion mode is automatic, does not happen, if calculation mode is manual.
|
50
|
-
|
51
|
-
Here are a few examples:
|
52
|
+
Here are a few examples:
|
52
53
|
|
53
|
-
|
54
|
+
When you want to open a workbook that was not opened before, or reopen a workbook that was open in an Excel instance that is now closed, in the current (active) Excel instance, then use
|
54
55
|
|
55
|
-
|
56
|
+
Book.open('workbook.xls')
|
56
57
|
|
57
|
-
|
58
|
+
or
|
59
|
+
|
60
|
+
Book.open('workbook.xls', :default_excel => :current)
|
58
61
|
|
59
|
-
|
62
|
+
In case you want to open such a workbook in a new Excel instance, then use
|
60
63
|
|
61
|
-
|
64
|
+
Book.open('workbook.xls', :default_excel => :new)
|
62
65
|
|
63
|
-
|
66
|
+
When you want to open a workbook in a new Excel instance, no matter if it was opened before, you can write
|
64
67
|
|
65
|
-
|
68
|
+
Bool.open('workbook.xls', :force_excel => :new)
|
66
69
|
|
67
|
-
|
70
|
+
You can also specify an Excel instance
|
68
71
|
|
69
|
-
|
72
|
+
excel1 = Excel.create
|
73
|
+
# something
|
74
|
+
Book.open('workbook.xls', :force_excek => excel1)
|
70
75
|
|
71
|
-
|
76
|
+
If you want to open the workbook and make its window visible, then use
|
72
77
|
|
73
|
-
book = Book.open('workbook.xls', :
|
78
|
+
book = Book.open('workbook.xls', :visible => true)
|
74
79
|
|
75
|
-
If a workbook contains unsaved changes and a workbook with the same filename shall be opened, then Excel
|
76
|
-
|
80
|
+
If a workbook contains unsaved changes and a workbook with the same filename shall be opened, then Excel stops with an alert message.
|
81
|
+
Here the option +:if_unsaved+ manages this case. The default value +:raise+ causes an error message. The value +:forget+ would cause to discard the unsaved workbook. The value +:accept+ indicates that the workbook remains open, but no error is raised, i.e. the program can continue.
|
77
82
|
|
78
83
|
book = Book.open('workbook.xls', :if_unsaved => :accept)
|
79
84
|
|
80
|
-
If a workbook is open and a workbook with the same name but in different path shall be opened, then the first workbook blocks opening the other workbook. The option +:if_obstructed+ handles this situation.
|
85
|
+
If a workbook is open and a workbook with the same name but in different path shall be opened, then the first workbook blocks opening the other workbook, and Excel would stop with an alert message. The option +:if_obstructed+ handles this situation. Similar as described above, the values +:raise+, +:forget+ and +:accept+ would cause an error message, and discarding or accepting the old workbook.
|
81
86
|
|
82
87
|
book = Book.open('path/workbook.xls', :if_obstructed => :forget)
|
83
88
|
|
84
|
-
|
89
|
+
Remarks:
|
90
|
+
|
91
|
+
Opening linked workbooks is supported.
|
92
|
+
|
93
|
+
Doing updating links seems to be dependent on calculation mode: updates happen, if the calcultion mode is automatic, and does not happen, if calculation mode is manual.
|
85
94
|
|
86
95
|
=== Closing a workbook.
|
87
96
|
|
88
97
|
book.close
|
89
98
|
|
90
|
-
|
91
|
-
|
92
|
-
Closing the workbook and saving it before if it has unsaved changes.
|
99
|
+
This method has the option +:if_unsaved+. Valid values for this option are +:raise+, +:save+, +:forget+, +:alert+ and +:keep_open+. For example, if you want to close the workbook and save it before if it has unsaved changes:
|
93
100
|
|
94
101
|
book.close(:if_unsaved => :save)
|
95
102
|
|
96
103
|
=== Reopening a workbook.
|
97
104
|
|
98
|
-
A special feature of
|
105
|
+
A special feature of RobustExcelOle is that it allows to reopen workbooks after closing them.
|
99
106
|
|
100
107
|
book = Book.open('workbook.xls')
|
101
108
|
book.close
|
@@ -120,31 +127,33 @@ A Book object can be created when giving an Excel workbook.
|
|
120
127
|
|
121
128
|
book.save
|
122
129
|
|
123
|
-
|
130
|
+
When you want to save a workbook with a file name, then use
|
124
131
|
|
125
132
|
book.save_as('another_workbook.xls')
|
126
133
|
|
127
134
|
The options are +:if_exists+ and +if_obstructed+.
|
128
135
|
|
129
|
-
|
136
|
+
For example, if you want to overwrite a workbook, then use
|
130
137
|
|
131
138
|
book.save_as('another_workbook.xls', :if_exists => :overwrite)
|
132
139
|
|
133
|
-
If a workbook is
|
140
|
+
If a workbook is blocks the workbook that should be saved, then the former one can be saved and closed before.
|
134
141
|
|
135
142
|
book = Book.open('workbook.xls')
|
136
143
|
book2 = Book.open('another_workbook.xls')
|
137
144
|
book2.save_as('dir/workbook.xls', :if_exists => :overwrite, :if_obstructed => :save)
|
138
145
|
|
146
|
+
Values of +:if:obstructed+ are +:raise+, +:forget+, +:save+ and +:close_if_saved+.
|
147
|
+
|
139
148
|
=== Unobtrusively opening a workbook
|
140
149
|
|
141
150
|
The method +unobtrusively+ enables the user to read or modify a workbook, no matter if it is open in some Excel instance, if it is saved or unsaved, and if it is writable or not. When opening a workbook unobtrusively, its status remains unchanged. This status includes, whether the workbook is opened or closed, saved or unsaved, readonly or writable.
|
142
151
|
|
143
152
|
Some options determine the Excel instance in which a closed workbook is opened. The option +:current+ (or +:active+, or +:reuse+) (default) indicates that the closed workbook is opened in the Excel instance where the workbooks is opened, if such an Excel instance exists, otherwise that another Excel instance is reused. The option +:hidden+ provokes that the closed workbook is opened in a separate Excel instance that is not visible and has no DisplayAlerts. Any following closed workbook would be opened in this Excel instance as well when using this option. Moreover, an Excel instance can be given directly where to open the closed workbook.
|
144
153
|
|
145
|
-
Further options are +:read_only+, +:readonly_excel+, and +:keep_open. The option +:readonly_excel+ chooses whether a book that is opened in read only mode. If the workbook is opened as read only, then the option +:readonly_excel+ determines whether to close the workbook and open it as writable in the Excel instance where it was open so far, or to open it as writable in another running Excel instance, if such an instance exists, or to open it in a new Excel instance. Moreover, there are the options +:visible+, and +:check_compatiblity
|
154
|
+
Further options are +:read_only+, +:readonly_excel+, and +:keep_open. The option +:readonly_excel+ chooses whether a book that is opened in read only mode. If the workbook is opened as read only, then the option +:readonly_excel+ determines whether to close the workbook and open it as writable in the Excel instance where it was open so far, or to open it as writable in another running Excel instance, if such an instance exists, or to open it in a new Excel instance. Moreover, there are the options +:visible+, and +:check_compatiblity as for the method +open+.
|
146
155
|
|
147
|
-
Book.unobtrusively('workbook.xls', :current, :read_only => false, :keep_open => false) do |book|
|
156
|
+
Book.unobtrusively('workbook.xls', :if_closed => :current, :read_only => false, :keep_open => false) do |book|
|
148
157
|
# some modification
|
149
158
|
sheet = book[0]
|
150
159
|
sheet[1,1] = "c"
|
@@ -186,22 +195,15 @@ or
|
|
186
195
|
|
187
196
|
=== Bringing a workbook to the focus.
|
188
197
|
|
189
|
-
|
198
|
+
When you want to make the workbook visible and available for keyboard inputs, use
|
190
199
|
|
191
200
|
book.focus
|
192
201
|
|
193
202
|
=== Making the window of the workbook visible
|
194
203
|
|
195
|
-
|
196
|
-
|
197
|
-
book = Book.open('workbook.xls', :visivble => true)
|
198
|
-
|
199
|
-
Make the window of the workbook invisible.
|
204
|
+
You can make the window of the workbook invisible or visible
|
200
205
|
|
201
206
|
book.visible = false
|
202
|
-
|
203
|
-
Make the window of the workbook visible.
|
204
|
-
|
205
207
|
book.visible = true
|
206
208
|
|
207
209
|
=== Making an Excel visible or invisible, and enable and disable DisplayAlerts.
|
@@ -217,23 +219,23 @@ Enable DisplayAlerts.
|
|
217
219
|
|
218
220
|
=== Accessing a sheet.
|
219
221
|
|
220
|
-
|
222
|
+
You can access a sheet by giving the number
|
221
223
|
|
222
224
|
sheet = book.sheet(1)
|
223
225
|
|
224
|
-
or
|
226
|
+
or its name
|
225
227
|
|
226
|
-
sheet = book.
|
228
|
+
sheet = book.sheet('Sheet1')
|
227
229
|
|
228
|
-
|
230
|
+
You can get the first and last sheet with
|
229
231
|
|
230
|
-
sheet = book.
|
232
|
+
sheet = book.first_sheet
|
231
233
|
|
232
|
-
|
234
|
+
and
|
233
235
|
|
234
|
-
sheet = book.
|
236
|
+
sheet = book.last_sheet
|
235
237
|
|
236
|
-
|
238
|
+
You can access all sheet objects by using the methods Book#each.
|
237
239
|
|
238
240
|
book.each do |sheet|
|
239
241
|
# do something with sheet
|
@@ -258,11 +260,11 @@ A sheet object is enumerable. Use the methods Sheet#each_column, Sheet#each_row
|
|
258
260
|
|
259
261
|
=== Accessing a cell.
|
260
262
|
|
261
|
-
Reading a cell from a sheet object
|
263
|
+
Reading a cell from a sheet object
|
262
264
|
|
263
265
|
sheet[1, 1] => first cell (first row, first column).
|
264
266
|
|
265
|
-
Reading a cell from a range object
|
267
|
+
Reading a cell from a range object
|
266
268
|
|
267
269
|
row_range[1] => first cell in row_range
|
268
270
|
column_range[2] => second cell in column_range
|
@@ -280,7 +282,7 @@ Writing a cell
|
|
280
282
|
|
281
283
|
=== Accessing a range of a row or column.
|
282
284
|
|
283
|
-
|
285
|
+
You access a range of a row by giving the number of the row, and optionally, the range of the cell numbers.
|
284
286
|
|
285
287
|
sheet.row_range(1) => first row
|
286
288
|
sheet.row_range(1, 1..3 ) => first three cells of the first row
|
@@ -292,13 +294,13 @@ Accessing a range of a column.
|
|
292
294
|
|
293
295
|
=== Naming a cell
|
294
296
|
|
295
|
-
|
297
|
+
You can (re-) name a cell range.
|
296
298
|
|
297
299
|
book.set_name(1,1,"name")
|
298
300
|
|
299
301
|
=== Getting and setting the contents of a named range in a worksheet
|
300
302
|
|
301
|
-
|
303
|
+
You get the value of a range by
|
302
304
|
|
303
305
|
sheet[name]
|
304
306
|
|
@@ -354,7 +356,7 @@ Copying a sheet, if a sheet is given, adding an empty sheet, if no sheet is give
|
|
354
356
|
|
355
357
|
=== Creating and reusing an Excel instance.
|
356
358
|
|
357
|
-
|
359
|
+
When you want to start a new Excel, use
|
358
360
|
|
359
361
|
excel1 = Excel.create
|
360
362
|
|
@@ -362,7 +364,7 @@ or
|
|
362
364
|
|
363
365
|
excel1 = Excel.new(:reuse => false)
|
364
366
|
|
365
|
-
|
367
|
+
When you want to reuse an already running Excel instance, write
|
366
368
|
|
367
369
|
excel2 = Excel.current
|
368
370
|
|
@@ -372,7 +374,7 @@ or
|
|
372
374
|
|
373
375
|
Options are +:reuse+ (+true+, +false+), +:visible+ (+true+, +false+) and +:displayalerts+ (+true+, +false+, +:if_visible+).
|
374
376
|
|
375
|
-
|
377
|
+
You can also promote an Excel instance represented as WIN32OLE object to an Excel object.
|
376
378
|
|
377
379
|
excel = Excel.new(win32ole_object)
|
378
380
|
|
@@ -386,35 +388,37 @@ or
|
|
386
388
|
|
387
389
|
excel1 = Excel.new(:reuse => false, :visible => true)
|
388
390
|
|
389
|
-
Reusing a Excel and making it invisible.
|
390
|
-
|
391
|
-
excel2 = Excel.current(:reuse => false, :visible => false)
|
392
|
-
|
393
391
|
or
|
394
392
|
|
395
|
-
|
393
|
+
excel1 = Excel.create
|
394
|
+
excel1.visible = true
|
395
|
+
|
396
396
|
|
397
397
|
=== Enabling or disabling DisplayAlerts
|
398
398
|
|
399
|
-
|
399
|
+
You can enable DisplayAlerts with, e.g.
|
400
400
|
|
401
|
-
|
401
|
+
excel1 = Excel.new(:reuse => true, :displayalerts => true)
|
402
402
|
|
403
|
-
|
404
|
-
|
405
|
-
excel3 = Excel.current(:reuse => true, :displayalerts => false)
|
403
|
+
or
|
406
404
|
|
407
|
-
|
405
|
+
excel1 = Excel.current
|
406
|
+
excel1.displayalerts = true
|
408
407
|
|
409
|
-
|
408
|
+
and turn DisplayAlerts off with
|
410
409
|
|
411
|
-
|
410
|
+
excel1.displayalerts = false
|
412
411
|
|
413
|
-
|
412
|
+
You can turn off and off DisplayAlerts in a block.
|
414
413
|
|
415
|
-
|
414
|
+
excel = Excel.create
|
415
|
+
excel.with_displayalerts false do
|
416
|
+
# do something
|
417
|
+
end
|
416
418
|
|
417
|
-
Making all workbooks invisible
|
419
|
+
=== Making all workbooks visible or invisible
|
420
|
+
|
421
|
+
excel1.workbooks_visible true
|
418
422
|
|
419
423
|
excel1.workbooks_visible false
|
420
424
|
|
@@ -422,34 +426,36 @@ Making all workbooks invisible.
|
|
422
426
|
|
423
427
|
Set the window of an Excel instance to the foreground.
|
424
428
|
|
429
|
+
excel1.focus
|
430
|
+
|
425
431
|
=== Closing an Excel
|
426
432
|
|
427
433
|
excel = Excel.current
|
428
434
|
excel.close
|
429
435
|
|
430
|
-
The
|
436
|
+
The method +close has the option +:if_unsaved+ with the values +:raise+, +:save+, +:forget+ and +:alert+.
|
431
437
|
|
432
|
-
|
438
|
+
For example, if you want to close an Excel instance and save unsaved workbooks, use
|
433
439
|
|
434
|
-
excel.close(:if_unsaved => :save
|
440
|
+
excel.close(:if_unsaved => :save)
|
435
441
|
|
436
|
-
=== Closing Excel instances
|
442
|
+
=== Closing all Excel instances
|
437
443
|
|
438
444
|
Excel.close_all
|
439
445
|
|
440
|
-
|
441
|
-
|
442
|
-
Closing all Excel instances, saving unsaved workbooks and terminating the Excel processes softly.
|
446
|
+
This method has the option +:if_unsaved+ as described above. For example, if you want to close all Excel instances containing saved workbooks and raise an error for Excel instances with unsaved workbooks, use
|
443
447
|
|
444
|
-
Excel.close_all(:if_unsaved => :
|
448
|
+
Excel.close_all(:if_unsaved => :raise)
|
445
449
|
|
446
450
|
=== Terminating all Excel processes
|
447
451
|
|
448
452
|
Excel.kill_all
|
449
453
|
|
454
|
+
This method kills all Excel instances no matter whether they contain unsaved workbooks.
|
455
|
+
|
450
456
|
=== Recreating an Excel instance
|
451
457
|
|
452
|
-
|
458
|
+
Closed Excel instances can also be reopened. This includes reopening all workbooks that were open in that Excel instance.
|
453
459
|
|
454
460
|
excel.close
|
455
461
|
excel.recreate
|
@@ -462,36 +468,7 @@ The options are :reopen_workbooks, :visible and :displayalerts.
|
|
462
468
|
|
463
469
|
Providing all Excel instances (opened via RobustExcelOle) as objects of the class Excel
|
464
470
|
|
465
|
-
Excel.
|
466
|
-
|
467
|
-
=== Making Excel visible or invisible
|
468
|
-
|
469
|
-
Making Excel visible.
|
470
|
-
|
471
|
-
excel = Excel.create
|
472
|
-
excel.visible = true
|
473
|
-
|
474
|
-
Making Excel invisible
|
475
|
-
|
476
|
-
excel.visible = false
|
477
|
-
|
478
|
-
=== Turning on or off Displayalerts.
|
479
|
-
|
480
|
-
Turning DisplayAlerts on.
|
481
|
-
|
482
|
-
excel = Excel.create
|
483
|
-
excel.displayalerts = true
|
484
|
-
|
485
|
-
Turning DisplayAlerts off.
|
486
|
-
|
487
|
-
excel.displayalerts = false
|
488
|
-
|
489
|
-
Turning on and off in a block.
|
490
|
-
|
491
|
-
excel = Excel.create
|
492
|
-
excel.with_displayalerts true do
|
493
|
-
book = Book.open('workbook.xls')
|
494
|
-
end
|
471
|
+
Excel.excel_processes
|
495
472
|
|
496
473
|
=== Setting Calculation mode.
|
497
474
|
|
@@ -540,51 +517,51 @@ Setting the value of a range.
|
|
540
517
|
|
541
518
|
=== Example 1
|
542
519
|
|
543
|
-
|
520
|
+
We open a workbook.
|
544
521
|
|
545
522
|
book = Book.open('workbook.xls')
|
546
523
|
|
547
|
-
|
524
|
+
Then we access a sheet via its name.
|
548
525
|
|
549
526
|
sheet = book['Sheet1']
|
550
527
|
|
551
|
-
|
528
|
+
Now we can modify the first cell.
|
552
529
|
|
553
530
|
sheet[0,0] = "new"
|
554
531
|
|
555
|
-
|
532
|
+
Then we save the workbook.
|
556
533
|
|
557
534
|
book.save
|
558
535
|
|
559
|
-
|
536
|
+
We can also save the workbook with a different name, and overwrite if a file with this name already exists.
|
560
537
|
|
561
538
|
book.save_as('different_workbook.xls', :if_exists => :overwrite)
|
562
539
|
|
563
|
-
|
540
|
+
Finally we close the workbook.
|
564
541
|
|
565
542
|
book.close
|
566
543
|
|
567
544
|
=== Example 2
|
568
545
|
|
569
|
-
|
546
|
+
We open a workbook.
|
570
547
|
|
571
548
|
book = Book.open('workbook.xls')
|
572
549
|
|
573
|
-
|
550
|
+
open it also in a new Excel instance and make it visible.
|
574
551
|
|
575
552
|
new_book = Book.open('workbook.xls', :force_excel => :new, :visible => true)
|
576
553
|
|
577
|
-
|
554
|
+
and open another workbook in a the first Excel instance.
|
578
555
|
|
579
|
-
another_book = Book.open('
|
556
|
+
another_book = Book.open('another_workbook.xls', :force_excel => book.excel)
|
580
557
|
|
581
|
-
|
558
|
+
Then we close the workbooks.
|
582
559
|
|
583
560
|
book.close
|
584
561
|
new_book.close
|
585
562
|
another_book.close
|
586
563
|
|
587
|
-
|
564
|
+
We reopen the first workbook.
|
588
565
|
|
589
566
|
reopened_book = book.reopen
|
590
567
|
|
@@ -593,88 +570,88 @@ The writable workbook is being prefered.
|
|
593
570
|
reopened_book === book
|
594
571
|
=> true
|
595
572
|
|
596
|
-
|
597
|
-
Since the workbook was not open before, reopening the workbook fails and the :default_excel option applies.
|
598
|
-
According to :default_excel => :new a new Excel is created, and the workbook is opened there.
|
573
|
+
We want to open yet another workbook. Since the workbook was not open before, reopening the workbook fails and the :default_excel option applies. According to :default_excel => :new a new Excel is created, and the workbook is opened there.
|
599
574
|
|
600
575
|
different_book = Book.open('different.xls', :default_excel => :new)
|
601
576
|
|
602
577
|
|
603
578
|
=== Example 3
|
604
579
|
|
605
|
-
|
580
|
+
Open a workbook.
|
606
581
|
|
607
582
|
book = Book.open('workbook.xls')
|
608
583
|
|
609
|
-
|
584
|
+
We want to add a copy of the first sheet and insert it after the second sheet.
|
610
585
|
|
611
586
|
book.add_sheet(book[0], :as => 'Sheet1_copy', :after => book[1])
|
612
587
|
|
613
|
-
|
588
|
+
Then we open a new workbook with the same name in a new Excel, and leave the workbook that contains unsaved changes in the old Excel.
|
614
589
|
|
615
590
|
new_book = Book.open('workbook.xls', :if_unsaved => :new_excel)
|
616
591
|
|
617
|
-
|
592
|
+
We acccess the first sheet and change the first cell.
|
618
593
|
|
619
594
|
sheet = new_book[0]
|
620
595
|
sheet[1,1] = "another"
|
621
596
|
|
622
|
-
|
597
|
+
Then we open a workbook with the same name in the running Excel while discarding the workbook that contained unsaved changes.
|
623
598
|
|
624
599
|
third_book = Book.open('workbook.xls', :if_unsaved => :forget)
|
625
600
|
|
626
|
-
|
601
|
+
Now we add a sheet.
|
627
602
|
|
628
603
|
third_book.add_sheet
|
629
604
|
|
630
|
-
|
605
|
+
and close the workbook without saving it.
|
631
606
|
|
632
607
|
third_book.close(:if_unsaved => :forget)
|
633
608
|
|
634
|
-
|
609
|
+
We close the first workbook and save it before.
|
635
610
|
|
636
611
|
book.close(:if_unsaved => :save)
|
637
612
|
|
638
613
|
=== Example 4
|
639
614
|
|
640
|
-
|
615
|
+
We open a workbook.
|
641
616
|
|
642
617
|
book1 = Book.open('workbook.xls')
|
643
618
|
|
644
|
-
|
619
|
+
Then we open a workbook with the same name in a different path, while closing the workbook opened before.
|
645
620
|
|
646
621
|
book2 = Book.open('more/workbook.xls', :if_obstructed => :forget)
|
647
622
|
|
648
|
-
|
623
|
+
We change its first cell.
|
649
624
|
|
650
625
|
sheet = book2[0]
|
651
626
|
sheet[1,1] = "new"
|
652
627
|
|
653
|
-
|
628
|
+
Now we open a workbook with the same name again in a different path. while saving and closing the other workbook.
|
654
629
|
|
655
630
|
book3 = Book.open('workbook.xls', :if_obstructed => :save)
|
656
631
|
|
657
|
-
|
632
|
+
Then we open a workbook with the same name in a different path.
|
633
|
+
The other workbook will be closed, because it does not contain unsaved changes.
|
658
634
|
|
659
635
|
book4 = Book.open('more/workbook.xls', :if_obstructed => :close_if_unsaved)
|
660
636
|
|
661
|
-
|
637
|
+
|
638
|
+
Finally we close this workbook.
|
662
639
|
|
663
640
|
book4.close
|
664
641
|
|
665
642
|
|
666
643
|
=== Example 5
|
667
644
|
|
668
|
-
|
645
|
+
We open a workbook
|
669
646
|
|
670
647
|
book = Book.open('workbook.xls')
|
671
648
|
|
672
|
-
|
649
|
+
and print its first cell.
|
673
650
|
|
674
651
|
sheet = book[0]
|
675
652
|
p "1st cell: #{sheet[1,1].Value}"
|
676
653
|
|
677
|
-
|
654
|
+
Then we unobtrusively modify the workbook.
|
678
655
|
|
679
656
|
Book.unobtrusively('workbook.xls') do |book|
|
680
657
|
sheet = book[0]
|
@@ -683,27 +660,15 @@ Unobtrusively modify the workbook.
|
|
683
660
|
|
684
661
|
The workbook is modified, but its status is unchanged.
|
685
662
|
|
686
|
-
new_sheet = book[0]
|
687
|
-
not (new_sheet[1,1].Value == sheet[1,1].Value)
|
688
|
-
=> true
|
689
|
-
|
690
|
-
book.saved
|
691
|
-
=> true
|
692
|
-
|
693
|
-
|
694
|
-
=== More Details
|
695
|
-
|
696
|
-
For more details about usage: see link:https://github.com/Thomas008/robust_excel_ole/blob/master/README_detail.rdoc
|
697
|
-
|
698
663
|
=== Development
|
699
664
|
|
700
|
-
|
665
|
+
RobustExcelOle started as a simple fork from tomiacannondale's wrap_excel adapted to Ruby 1.8.6.
|
701
666
|
The functionality of wrap_excel is optimised and extended by new features.
|
702
667
|
Most notable extensions include:
|
703
668
|
* workbooks can be opened in already running Excel instances (instead of opening a new Excel whenever a book is opened)
|
704
669
|
* a workbook management system stores all workbooks that have been open. This workbook store is being used, e.g., for reopening a workbook that has been closed before. It provides transparency identity, i.e., equal Excel workbooks correspond to equal Book objects of RobustExcelOle.
|
705
670
|
|
706
|
-
Some features in
|
671
|
+
Some features in RobustExcelOle that are not compatible with wrap_excel:
|
707
672
|
* +open+ uses by default a running Excel instance instead of creating a new one,
|
708
673
|
and opens a workbook by default in writable mode instead of read_only
|
709
674
|
* +close+ closes the workbook instead of closing all workbooks and the Excel instance.
|