robust_excel_ole 1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +14 -0
- data/README.rdoc +94 -351
- data/README_detail.rdoc +791 -0
- data/examples/edit_sheets/example_saving.rb +1 -1
- data/examples/open_save_close/example_default_excel.rb +5 -5
- data/examples/open_save_close/example_force_excel.rb +2 -2
- data/lib/robust_excel_ole/book.rb +237 -161
- data/lib/robust_excel_ole/excel.rb +96 -68
- data/lib/robust_excel_ole/reo_common.rb +19 -1
- data/lib/robust_excel_ole/sheet.rb +2 -3
- data/lib/robust_excel_ole/version.rb +1 -1
- data/robust_excel_ole.gemspec +2 -2
- data/spec/book_spec.rb +71 -16
- data/spec/book_specs/book_misc_spec.rb +244 -7
- data/spec/book_specs/book_open_spec.rb +472 -33
- data/spec/book_specs/book_save_spec.rb +19 -0
- data/spec/book_specs/book_unobtr_spec.rb +46 -4
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/book_with_blank.xls +0 -0
- data/spec/data/different_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +134 -31
- metadata +7 -6
- data/spec/data/refed_wb.xls +0 -0
data/Changelog
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
4
|
|
5
|
+
## [1.0.1] - 2017-01-04
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Book#open : options :calculation
|
10
|
+
- Excel#new : options :calculation, :screen_updating
|
11
|
+
- Excel#calculation, calculation=, screen_updating, screenupdating=
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
|
15
|
+
- Book#open : options: two levels: :default and :force
|
16
|
+
example:
|
17
|
+
Book.open(:default => {:excel => :current, :visible => true}, {:force => {:excel => :new})
|
18
|
+
|
5
19
|
## [0.6.2] - 2016-04-11
|
6
20
|
|
7
21
|
### Changed
|
data/README.rdoc
CHANGED
@@ -10,6 +10,32 @@ It supports Excel 2010 and Excel 2007.
|
|
10
10
|
RobustExcelOle works by sending VBA methods via Win32OLE.
|
11
11
|
Moreover, it implements a management system and keeps track of Excel files and Excel instances.
|
12
12
|
|
13
|
+
In the following, some features of RobustExcelOle are depicted.
|
14
|
+
|
15
|
+
RobustExcelOle allows a "script mode" and an "interactive mode": Commands enable to open Excel files (or workbooks) in various Excel instances, enable to close, reopen, modify and save the Excel files, without the need of the user's interaction, and even without the user noticing. While running this script, the user can open and mofify any Excel files at any time. RobustExcelOle manages the complex cases of conflicts that might occur such that the user does not need to interfere and the script can continue.
|
16
|
+
|
17
|
+
For example, suppose you want to process a list of workbooks (Excel files). RobustExcelOle allows to rapidly open, manipulate, close and save these workbooks (script mode). Now assume, the workbook "workbook.xls" is being processed, while the user has opened this workbook, has manipulated but not saved it yet. Excel would prompt a message and ask the user what to do. RobustExcelOle solves this conflict by using several options that state whether the changes of the user should be saved (accepted) or discarded before opening the workbook.
|
18
|
+
|
19
|
+
book1 = Book.open('workbook.xls') # user
|
20
|
+
...
|
21
|
+
book2 = Book.open('workbook.xls', :if_unsaved => accept) # script
|
22
|
+
|
23
|
+
Similarly, if the user has opened a workbook that has the same name but a different path, the conflict is solved via options.
|
24
|
+
|
25
|
+
book1 = Book.open('path1/workbook.xls')
|
26
|
+
...
|
27
|
+
book2 = Book.open('workbook.xls', :if_obstructed => :forget)
|
28
|
+
|
29
|
+
Another feature that RobustExcelOle povides is reopening workbooks after closing them. A workbook is opened by default in the Excel instance where it was open before most recently. If this Excel instance is damaged or closed, then RobustExcelIle controls via options whether the workbook is opened in the current (active) Excel instance, a new or a given Excel instance.
|
30
|
+
|
31
|
+
book1 = Book.open('workbook.xls', :default => {:excel => :new})
|
32
|
+
|
33
|
+
Moreover, RobustExcelOle allows unobtrusively reading and modifying workbooks, i.e. accessing workbooks without changing their "status". The status comprises whether the workbook is open in some Excel instance , saved and writable.
|
34
|
+
|
35
|
+
Book.for_modifying('workbook.xls') do |book|
|
36
|
+
...
|
37
|
+
end
|
38
|
+
|
13
39
|
== Requirements
|
14
40
|
|
15
41
|
* Ruby 1.8.6 or higher
|
@@ -25,8 +51,6 @@ Moreover, it implements a management system and keeps track of Excel files and E
|
|
25
51
|
|
26
52
|
=== Opening a workbook.
|
27
53
|
|
28
|
-
Example:
|
29
|
-
|
30
54
|
book = Book.open('workbook.xls')
|
31
55
|
|
32
56
|
You can also open a workbook with a block.
|
@@ -36,67 +60,37 @@ The semantics is similar to, e.g., +File.open+.
|
|
36
60
|
# do something
|
37
61
|
end
|
38
62
|
|
39
|
-
|
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
|
51
|
-
|
52
|
-
Here are a few examples:
|
63
|
+
There are some options that determine the Excel instance in which the workbook is opened, the visibility, calculation mode, and solving conflicts when the workbook is unsaved or blocked. Here are a few examples:
|
53
64
|
|
54
|
-
|
65
|
+
If 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
|
55
66
|
|
56
|
-
Book.open('workbook.xls')
|
67
|
+
Book.open('workbook.xls', :default => {:excel => :current})
|
57
68
|
|
58
|
-
or
|
69
|
+
or simply
|
59
70
|
|
60
|
-
Book.open('workbook.xls'
|
61
|
-
|
62
|
-
In case you want to open such a workbook in a new Excel instance, then use
|
63
|
-
|
64
|
-
Book.open('workbook.xls', :default_excel => :new)
|
65
|
-
|
66
|
-
When you want to open a workbook in a new Excel instance, no matter if it was opened before, you can write
|
67
|
-
|
68
|
-
Bool.open('workbook.xls', :force_excel => :new)
|
71
|
+
Book.open('workbook.xls')
|
69
72
|
|
70
|
-
|
73
|
+
If you want to open a workbook in a new Excel instance, no matter if it was opened before, you can write
|
71
74
|
|
72
|
-
|
73
|
-
# something
|
74
|
-
Book.open('workbook.xls', :force_excek => excel1)
|
75
|
+
Bool.open('workbook.xls', :force => {:excel => :new})
|
75
76
|
|
76
|
-
If you want to open the workbook and make its window visible, then use
|
77
|
+
If you want to open the workbook and make its window visible, then you can use
|
77
78
|
|
78
79
|
book = Book.open('workbook.xls', :visible => true)
|
79
80
|
|
80
|
-
If a workbook contains unsaved changes
|
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.
|
81
|
+
If a workbook contains unsaved changes, a workbook with the same filename shall be opened and the former one shall remain open, you apply
|
82
82
|
|
83
83
|
book = Book.open('workbook.xls', :if_unsaved => :accept)
|
84
84
|
|
85
|
-
If a workbook is open and a workbook with the same name but in different path shall be opened,
|
85
|
+
If a workbook is open and a workbook with the same name but in different path shall be opened, i.e. the first workbook blocks opening the other workbook, then the option +:if_obstructed+ handles this situation, e.g.
|
86
86
|
|
87
87
|
book = Book.open('path/workbook.xls', :if_obstructed => :forget)
|
88
88
|
|
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.
|
94
|
-
|
95
89
|
=== Closing a workbook.
|
96
90
|
|
97
91
|
book.close
|
98
92
|
|
99
|
-
This method has the option +:if_unsaved+.
|
93
|
+
This method has the option +:if_unsaved+. For example, if you want to close the workbook and save its changes before, you can use
|
100
94
|
|
101
95
|
book.close(:if_unsaved => :save)
|
102
96
|
|
@@ -108,9 +102,6 @@ A special feature of RobustExcelOle is that it allows to reopen workbooks after
|
|
108
102
|
book.close
|
109
103
|
book.reopen
|
110
104
|
|
111
|
-
The closed workbook is now alive again, i.e. is open and responds to Excel methods.
|
112
|
-
This feature is a result of providing identity transparence and storing the file name.
|
113
|
-
|
114
105
|
=== The Book objects and transperence identity
|
115
106
|
|
116
107
|
An Excel file (or workbook) is represented by a Book object. A Book object is defined by the full name of the workbook and the Excel instance in which it is opened. RobustExcelOle aims to ensure identity transperency.
|
@@ -127,55 +118,46 @@ A Book object can be created when giving an Excel workbook.
|
|
127
118
|
|
128
119
|
book.save
|
129
120
|
|
130
|
-
|
121
|
+
Saving a workbook with a file name, is done by
|
131
122
|
|
132
123
|
book.save_as('another_workbook.xls')
|
133
124
|
|
134
|
-
|
135
|
-
|
136
|
-
For example, if you want to overwrite a workbook, then use
|
125
|
+
When you want to overwrite a workbook, then use
|
137
126
|
|
138
127
|
book.save_as('another_workbook.xls', :if_exists => :overwrite)
|
139
128
|
|
140
|
-
If a workbook
|
129
|
+
If a workbook blocks the workbook that should be saved, then the former one can be saved and closed before.
|
141
130
|
|
142
|
-
book
|
143
|
-
book2 = Book.open('another_workbook.xls')
|
144
|
-
book2.save_as('dir/workbook.xls', :if_exists => :overwrite, :if_obstructed => :save)
|
145
|
-
|
146
|
-
Values of +:if:obstructed+ are +:raise+, +:forget+, +:save+ and +:close_if_saved+.
|
131
|
+
book.save_as('workbook.xls', :if_exists => :overwrite, :if_obstructed => :save)
|
147
132
|
|
148
133
|
=== Unobtrusively opening a workbook
|
149
134
|
|
150
|
-
The method +unobtrusively+ enables
|
151
|
-
|
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.
|
135
|
+
The method +unobtrusively+ enables reading and modifying a workbook, without changing its "status". This status comprises whether the workbook is open in some Excel instance or closed, whether it is saved or unsaved, and whether it is writable or not. Some options determine the Excel instance in which a closed workbook is opened.
|
153
136
|
|
154
|
-
|
155
|
-
|
156
|
-
Book.unobtrusively('workbook.xls', :if_closed => :current, :read_only => false, :keep_open => false) do |book|
|
137
|
+
Book.unobtrusively('workbook.xls') do |book|
|
157
138
|
# some modification
|
158
|
-
sheet = book[0]
|
159
|
-
sheet[1,1] = "c"
|
160
139
|
end
|
161
140
|
|
162
|
-
|
141
|
+
Likewise, the methods +for_reading+ and +for_modifying+ are methods for unobtrusively reading or modifying.
|
163
142
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
143
|
+
=== Retaining the saved-status
|
144
|
+
|
145
|
+
This method ensures keeping the save status of the workbook
|
146
|
+
|
147
|
+
book = Book.open('workbook.xls')
|
148
|
+
book.retain_saved do
|
149
|
+
# some reading or modifying
|
168
150
|
end
|
169
151
|
|
170
152
|
=== Checking whether the workbook is alive.
|
171
153
|
|
172
|
-
This method finds out whether the
|
154
|
+
This method finds out whether the workbook that is referenced by the Book object responds to methods.
|
173
155
|
|
174
156
|
if book.alive? then sheet = book[0] end
|
175
157
|
|
176
158
|
=== Getting and setting the contents of a range in a workbook.
|
177
159
|
|
178
|
-
|
160
|
+
You can get the contents of a range with
|
179
161
|
|
180
162
|
book["name"]
|
181
163
|
=> "value"
|
@@ -185,7 +167,7 @@ or
|
|
185
167
|
book.nameval("name")
|
186
168
|
=> "value"
|
187
169
|
|
188
|
-
|
170
|
+
You can set the contents of a range with
|
189
171
|
|
190
172
|
book["name"] = "value"
|
191
173
|
|
@@ -195,31 +177,31 @@ or
|
|
195
177
|
|
196
178
|
=== Bringing a workbook to the focus.
|
197
179
|
|
198
|
-
|
180
|
+
If you want to make the workbook visible and available for keyboard inputs, use
|
199
181
|
|
200
182
|
book.focus
|
201
183
|
|
202
184
|
=== Making the window of the workbook visible
|
203
185
|
|
204
|
-
You can make the window of the workbook invisible or visible
|
186
|
+
You can make the window of the workbook invisible or visible:
|
205
187
|
|
206
188
|
book.visible = false
|
207
189
|
book.visible = true
|
208
190
|
|
209
191
|
=== Making an Excel visible or invisible, and enable and disable DisplayAlerts.
|
210
192
|
|
211
|
-
|
193
|
+
You can make an Excel instance visible with
|
212
194
|
|
213
195
|
book.excel.visible = true
|
214
196
|
|
215
|
-
|
197
|
+
and enable DisplayAlerts using
|
216
198
|
|
217
199
|
book.excel.displayalerts = true
|
218
200
|
|
219
201
|
|
220
|
-
=== Accessing a
|
202
|
+
=== Accessing a worksheet.
|
221
203
|
|
222
|
-
You can access a
|
204
|
+
You can access a worksheet by providing its number
|
223
205
|
|
224
206
|
sheet = book.sheet(1)
|
225
207
|
|
@@ -227,7 +209,7 @@ or its name
|
|
227
209
|
|
228
210
|
sheet = book.sheet('Sheet1')
|
229
211
|
|
230
|
-
You can get the first and last
|
212
|
+
You can get the first and last worksheet using
|
231
213
|
|
232
214
|
sheet = book.first_sheet
|
233
215
|
|
@@ -235,19 +217,18 @@ and
|
|
235
217
|
|
236
218
|
sheet = book.last_sheet
|
237
219
|
|
238
|
-
You can access all
|
220
|
+
You can access all worksheet objects by using the methods Book#each.
|
239
221
|
|
240
222
|
book.each do |sheet|
|
241
223
|
# do something with sheet
|
242
224
|
end
|
243
225
|
|
244
|
-
=== Accessing
|
226
|
+
=== Accessing cells, rows and columns.
|
245
227
|
|
246
|
-
|
228
|
+
You can use Sheet#each, each_column, and each_row to access cells, rows and columns.
|
247
229
|
|
248
230
|
sheet.each do |cell|
|
249
231
|
# do something with cell
|
250
|
-
# read every row every column
|
251
232
|
end
|
252
233
|
|
253
234
|
sheet.each_row do |row|
|
@@ -260,24 +241,19 @@ A sheet object is enumerable. Use the methods Sheet#each_column, Sheet#each_row
|
|
260
241
|
|
261
242
|
=== Accessing a cell.
|
262
243
|
|
263
|
-
|
244
|
+
You can read a cell from a sheet object
|
264
245
|
|
265
246
|
sheet[1, 1] => first cell (first row, first column).
|
266
247
|
|
267
|
-
|
248
|
+
or a range object
|
268
249
|
|
269
250
|
row_range[1] => first cell in row_range
|
270
251
|
column_range[2] => second cell in column_range
|
271
252
|
|
272
|
-
|
273
|
-
|
274
|
-
Reading the value of a cell.
|
253
|
+
You can read and write the value of a cell
|
275
254
|
|
276
255
|
cell = sheet[1,1]
|
277
256
|
cell.Value => value of the cell.
|
278
|
-
|
279
|
-
Writing a cell
|
280
|
-
|
281
257
|
sheet[1,1] = "new_value"
|
282
258
|
|
283
259
|
=== Accessing a range of a row or column.
|
@@ -287,7 +263,7 @@ You access a range of a row by giving the number of the row, and optionally, the
|
|
287
263
|
sheet.row_range(1) => first row
|
288
264
|
sheet.row_range(1, 1..3 ) => first three cells of the first row
|
289
265
|
|
290
|
-
|
266
|
+
Simarly you can access a range of a column.
|
291
267
|
|
292
268
|
sheet.col_range(3) => third column
|
293
269
|
sheet.col_range(3, 1..2) => first two cells of the third column
|
@@ -308,7 +284,7 @@ or
|
|
308
284
|
|
309
285
|
sheet.nameval(name)
|
310
286
|
|
311
|
-
|
287
|
+
You set the value if a range using
|
312
288
|
|
313
289
|
book[name] = value
|
314
290
|
|
@@ -318,45 +294,31 @@ or
|
|
318
294
|
|
319
295
|
=== Getting and setting the contents of a named range in a Worksheet directly
|
320
296
|
|
321
|
-
|
297
|
+
You get the value of a range with
|
322
298
|
|
323
299
|
sheet.rangeval(name)
|
324
300
|
|
325
|
-
|
301
|
+
and set the value of a range.
|
326
302
|
|
327
303
|
book.set_rangeval(name,value)
|
328
304
|
|
329
|
-
===
|
330
|
-
|
331
|
-
Adding (appending) an empty sheet.
|
332
|
-
|
333
|
-
book.add_empty_sheet
|
305
|
+
=== Adding and copying a worksheet.
|
334
306
|
|
335
|
-
|
336
|
-
|
337
|
-
book.add_empty_sheet(:as => 'sheet_name')
|
338
|
-
|
339
|
-
Adding an empty sheet with a name before another sheet.
|
307
|
+
You can add (append) an empty worksheet using, name it and specify its position.
|
340
308
|
|
341
309
|
book.add_empty_sheet(:as => 'new_name', :before => another_sheet)
|
342
310
|
|
343
|
-
|
344
|
-
|
345
|
-
book.copy_sheet sheet
|
346
|
-
|
347
|
-
Copying a sheet after a another sheet and naming it.
|
311
|
+
You can copy a worksheet, add it, and specify its name and position.
|
348
312
|
|
349
313
|
book.copy_sheet(sheet, :as => 'new_name', :after => another_sheet)
|
350
314
|
|
351
|
-
|
315
|
+
If you want to copy a worksheet, if a sheet is given, and add an empty worksheet, if no worksheet is given, then use
|
352
316
|
|
353
317
|
book.add_or_copy_sheet
|
354
318
|
|
355
|
-
book.add_or_copy_sheet(sheet, :as => 'new_name', :after => another_sheet)
|
356
|
-
|
357
319
|
=== Creating and reusing an Excel instance.
|
358
320
|
|
359
|
-
|
321
|
+
If you want to start a new Excel, use
|
360
322
|
|
361
323
|
excel1 = Excel.create
|
362
324
|
|
@@ -364,7 +326,7 @@ or
|
|
364
326
|
|
365
327
|
excel1 = Excel.new(:reuse => false)
|
366
328
|
|
367
|
-
|
329
|
+
In case you want to reuse an already running Excel instance, write
|
368
330
|
|
369
331
|
excel2 = Excel.current
|
370
332
|
|
@@ -372,80 +334,31 @@ or
|
|
372
334
|
|
373
335
|
excel2 = Excel.new(:reuse => true)
|
374
336
|
|
375
|
-
|
337
|
+
Further options specify the visibility, displayalerts, screen updating and calculation mode, e.g.
|
338
|
+
|
339
|
+
excel1 = Excel.new(:reuse => false, :visible => true, :displayalerts => true, :calculation => :manual)
|
376
340
|
|
377
341
|
You can also promote an Excel instance represented as WIN32OLE object to an Excel object.
|
378
342
|
|
379
343
|
excel = Excel.new(win32ole_object)
|
380
344
|
|
381
|
-
=== Making a Excel visible or invisible
|
382
|
-
|
383
|
-
Creating a new Excel and making it visible.
|
384
|
-
|
385
|
-
excel1 = Excel.create(:visible => true)
|
386
|
-
|
387
|
-
or
|
388
|
-
|
389
|
-
excel1 = Excel.new(:reuse => false, :visible => true)
|
390
|
-
|
391
|
-
or
|
392
|
-
|
393
|
-
excel1 = Excel.create
|
394
|
-
excel1.visible = true
|
395
|
-
|
396
|
-
|
397
|
-
=== Enabling or disabling DisplayAlerts
|
398
|
-
|
399
|
-
You can enable DisplayAlerts with, e.g.
|
400
|
-
|
401
|
-
excel1 = Excel.new(:reuse => true, :displayalerts => true)
|
402
|
-
|
403
|
-
or
|
404
|
-
|
405
|
-
excel1 = Excel.current
|
406
|
-
excel1.displayalerts = true
|
407
|
-
|
408
|
-
and turn DisplayAlerts off with
|
409
|
-
|
410
|
-
excel1.displayalerts = false
|
411
|
-
|
412
|
-
You can turn off and off DisplayAlerts in a block.
|
413
|
-
|
414
|
-
excel = Excel.create
|
415
|
-
excel.with_displayalerts false do
|
416
|
-
# do something
|
417
|
-
end
|
418
|
-
|
419
345
|
=== Making all workbooks visible or invisible
|
420
346
|
|
421
347
|
excel1.workbooks_visible true
|
422
348
|
|
423
|
-
|
424
|
-
|
425
|
-
=== Bringing an Excel instance to the focus
|
426
|
-
|
427
|
-
Set the window of an Excel instance to the foreground.
|
349
|
+
=== Bringing an Excel instance to the foreground
|
428
350
|
|
429
351
|
excel1.focus
|
430
352
|
|
431
353
|
=== Closing an Excel
|
432
354
|
|
433
|
-
excel = Excel.current
|
434
355
|
excel.close
|
435
356
|
|
436
|
-
The method +close has the option +:if_unsaved+ with the values +:raise+, +:save+, +:forget+ and +:alert+.
|
437
|
-
|
438
|
-
For example, if you want to close an Excel instance and save unsaved workbooks, use
|
439
|
-
|
440
|
-
excel.close(:if_unsaved => :save)
|
441
|
-
|
442
357
|
=== Closing all Excel instances
|
443
358
|
|
444
359
|
Excel.close_all
|
445
360
|
|
446
|
-
|
447
|
-
|
448
|
-
Excel.close_all(:if_unsaved => :raise)
|
361
|
+
The option +:if_unsaved+ manages unsaved workbooks.
|
449
362
|
|
450
363
|
=== Terminating all Excel processes
|
451
364
|
|
@@ -455,12 +368,7 @@ This method kills all Excel instances no matter whether they contain unsaved wor
|
|
455
368
|
|
456
369
|
=== Recreating an Excel instance
|
457
370
|
|
458
|
-
Closed Excel instances can
|
459
|
-
|
460
|
-
excel.close
|
461
|
-
excel.recreate
|
462
|
-
|
463
|
-
The options are :reopen_workbooks, :visible and :displayalerts.
|
371
|
+
Closed Excel instances can be reopened.
|
464
372
|
|
465
373
|
excel.recreate(:reopen_workbooks => true, :visible => true, :displayalerts => true)
|
466
374
|
|
@@ -472,21 +380,19 @@ Providing all Excel instances (opened via RobustExcelOle) as objects of the clas
|
|
472
380
|
|
473
381
|
=== Setting Calculation mode.
|
474
382
|
|
475
|
-
|
476
|
-
|
383
|
+
The calculation mode of an Excel instance can be set to manual or automatic.
|
384
|
+
|
385
|
+
excel.calculation = :manual
|
386
|
+
|
387
|
+
You can do it in a block:
|
477
388
|
|
478
|
-
excel = Excel.create
|
479
|
-
book = Book.open('workbook.xls')
|
480
389
|
excel.with_calculation(:manual) do
|
481
390
|
# do something
|
482
391
|
end
|
483
392
|
|
484
393
|
=== Getting and setting the contents of a named range in an Excel application
|
485
394
|
|
486
|
-
|
487
|
-
book = Book.open('another_workbook.xls')
|
488
|
-
|
489
|
-
Getting the value of a range.
|
395
|
+
You can get the value of a range by using
|
490
396
|
|
491
397
|
excel[name]
|
492
398
|
|
@@ -494,7 +400,7 @@ or
|
|
494
400
|
|
495
401
|
excel.nameval(name)
|
496
402
|
|
497
|
-
|
403
|
+
and set the value of a range by using
|
498
404
|
|
499
405
|
excel[name] = value
|
500
406
|
|
@@ -502,178 +408,15 @@ or
|
|
502
408
|
|
503
409
|
excel.set_nameval(name,value)
|
504
410
|
|
505
|
-
=== Getting and setting the contents of a named range in an Excel
|
506
|
-
|
507
|
-
Getting the value of a range.
|
411
|
+
=== Getting and setting the contents of a named range in an Excel instance
|
508
412
|
|
509
413
|
excel.rangeval(name)
|
510
414
|
|
511
|
-
Setting the value of a range.
|
512
|
-
|
513
415
|
excel.set_rangeval(name,value)
|
514
416
|
|
417
|
+
=== More details
|
515
418
|
|
516
|
-
|
517
|
-
|
518
|
-
=== Example 1
|
519
|
-
|
520
|
-
We open a workbook.
|
521
|
-
|
522
|
-
book = Book.open('workbook.xls')
|
523
|
-
|
524
|
-
Then we access a sheet via its name.
|
525
|
-
|
526
|
-
sheet = book['Sheet1']
|
527
|
-
|
528
|
-
Now we can modify the first cell.
|
529
|
-
|
530
|
-
sheet[0,0] = "new"
|
531
|
-
|
532
|
-
Then we save the workbook.
|
533
|
-
|
534
|
-
book.save
|
535
|
-
|
536
|
-
We can also save the workbook with a different name, and overwrite if a file with this name already exists.
|
537
|
-
|
538
|
-
book.save_as('different_workbook.xls', :if_exists => :overwrite)
|
539
|
-
|
540
|
-
Finally we close the workbook.
|
541
|
-
|
542
|
-
book.close
|
543
|
-
|
544
|
-
=== Example 2
|
545
|
-
|
546
|
-
We open a workbook.
|
547
|
-
|
548
|
-
book = Book.open('workbook.xls')
|
549
|
-
|
550
|
-
open it also in a new Excel instance and make it visible.
|
551
|
-
|
552
|
-
new_book = Book.open('workbook.xls', :force_excel => :new, :visible => true)
|
553
|
-
|
554
|
-
and open another workbook in a the first Excel instance.
|
555
|
-
|
556
|
-
another_book = Book.open('another_workbook.xls', :force_excel => book.excel)
|
557
|
-
|
558
|
-
Then we close the workbooks.
|
559
|
-
|
560
|
-
book.close
|
561
|
-
new_book.close
|
562
|
-
another_book.close
|
563
|
-
|
564
|
-
We reopen the first workbook.
|
565
|
-
|
566
|
-
reopened_book = book.reopen
|
567
|
-
|
568
|
-
The writable workbook is being prefered.
|
569
|
-
|
570
|
-
reopened_book === book
|
571
|
-
=> true
|
572
|
-
|
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.
|
574
|
-
|
575
|
-
different_book = Book.open('different.xls', :default_excel => :new)
|
576
|
-
|
577
|
-
|
578
|
-
=== Example 3
|
579
|
-
|
580
|
-
Open a workbook.
|
581
|
-
|
582
|
-
book = Book.open('workbook.xls')
|
583
|
-
|
584
|
-
We want to add a copy of the first sheet and insert it after the second sheet.
|
585
|
-
|
586
|
-
book.add_sheet(book[0], :as => 'Sheet1_copy', :after => book[1])
|
587
|
-
|
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.
|
589
|
-
|
590
|
-
new_book = Book.open('workbook.xls', :if_unsaved => :new_excel)
|
591
|
-
|
592
|
-
We acccess the first sheet and change the first cell.
|
593
|
-
|
594
|
-
sheet = new_book[0]
|
595
|
-
sheet[1,1] = "another"
|
596
|
-
|
597
|
-
Then we open a workbook with the same name in the running Excel while discarding the workbook that contained unsaved changes.
|
598
|
-
|
599
|
-
third_book = Book.open('workbook.xls', :if_unsaved => :forget)
|
600
|
-
|
601
|
-
Now we add a sheet.
|
602
|
-
|
603
|
-
third_book.add_sheet
|
604
|
-
|
605
|
-
and close the workbook without saving it.
|
606
|
-
|
607
|
-
third_book.close(:if_unsaved => :forget)
|
608
|
-
|
609
|
-
We close the first workbook and save it before.
|
610
|
-
|
611
|
-
book.close(:if_unsaved => :save)
|
612
|
-
|
613
|
-
=== Example 4
|
614
|
-
|
615
|
-
We open a workbook.
|
616
|
-
|
617
|
-
book1 = Book.open('workbook.xls')
|
618
|
-
|
619
|
-
Then we open a workbook with the same name in a different path, while closing the workbook opened before.
|
620
|
-
|
621
|
-
book2 = Book.open('more/workbook.xls', :if_obstructed => :forget)
|
622
|
-
|
623
|
-
We change its first cell.
|
624
|
-
|
625
|
-
sheet = book2[0]
|
626
|
-
sheet[1,1] = "new"
|
627
|
-
|
628
|
-
Now we open a workbook with the same name again in a different path. while saving and closing the other workbook.
|
629
|
-
|
630
|
-
book3 = Book.open('workbook.xls', :if_obstructed => :save)
|
631
|
-
|
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.
|
634
|
-
|
635
|
-
book4 = Book.open('more/workbook.xls', :if_obstructed => :close_if_unsaved)
|
636
|
-
|
637
|
-
|
638
|
-
Finally we close this workbook.
|
639
|
-
|
640
|
-
book4.close
|
641
|
-
|
642
|
-
|
643
|
-
=== Example 5
|
644
|
-
|
645
|
-
We open a workbook
|
646
|
-
|
647
|
-
book = Book.open('workbook.xls')
|
648
|
-
|
649
|
-
and print its first cell.
|
650
|
-
|
651
|
-
sheet = book[0]
|
652
|
-
p "1st cell: #{sheet[1,1].Value}"
|
653
|
-
|
654
|
-
Then we unobtrusively modify the workbook.
|
655
|
-
|
656
|
-
Book.unobtrusively('workbook.xls') do |book|
|
657
|
-
sheet = book[0]
|
658
|
-
sheet[1,1] = 'simple'
|
659
|
-
end
|
660
|
-
|
661
|
-
The workbook is modified, but its status is unchanged.
|
662
|
-
|
663
|
-
=== Development
|
664
|
-
|
665
|
-
RobustExcelOle started as a simple fork from tomiacannondale's wrap_excel adapted to Ruby 1.8.6.
|
666
|
-
The functionality of wrap_excel is optimised and extended by new features.
|
667
|
-
Most notable extensions include:
|
668
|
-
* workbooks can be opened in already running Excel instances (instead of opening a new Excel whenever a book is opened)
|
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.
|
670
|
-
|
671
|
-
Some features in RobustExcelOle that are not compatible with wrap_excel:
|
672
|
-
* +open+ uses by default a running Excel instance instead of creating a new one,
|
673
|
-
and opens a workbook by default in writable mode instead of read_only
|
674
|
-
* +close+ closes the workbook instead of closing all workbooks and the Excel instance.
|
675
|
-
* +save_as+ instead of +save+.
|
676
|
-
|
419
|
+
{Readme_detail.rdoc}[file:../Readme_detail.rdoc]
|
677
420
|
|
678
421
|
=== Want to do more things
|
679
422
|
|
@@ -681,7 +424,7 @@ If you want to do something that not provide a function, you can use win32ole me
|
|
681
424
|
|
682
425
|
== Support
|
683
426
|
|
684
|
-
|
427
|
+
Please contact us and to report issues and feature requests to github Issues.
|
685
428
|
https://github.com/Thomas008/robust_excel_ole/issues
|
686
429
|
|
687
430
|
== Collaborate
|