robust_excel_ole 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README.rdoc +73 -26
  2. data/README_detail.rdoc +92 -27
  3. data/examples/edit_sheets/example_access_sheets_and_cells.rb +3 -3
  4. data/examples/edit_sheets/example_concating.rb +12 -12
  5. data/examples/edit_sheets/example_copying.rb +47 -0
  6. data/examples/edit_sheets/example_expanding.rb +17 -26
  7. data/examples/edit_sheets/example_naming.rb +13 -10
  8. data/examples/edit_sheets/example_ranges.rb +2 -2
  9. data/examples/edit_sheets/example_saving.rb +8 -14
  10. data/examples/open_save_close/example_control_to_excel.rb +1 -1
  11. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +3 -3
  12. data/examples/open_save_close/example_if_obstructed_save.rb +3 -3
  13. data/examples/open_save_close/example_if_unsaved_accept.rb +1 -1
  14. data/examples/open_save_close/example_if_unsaved_forget.rb +4 -4
  15. data/examples/open_save_close/example_if_unsaved_forget_more.rb +5 -5
  16. data/examples/open_save_close/example_read_only.rb +1 -1
  17. data/examples/open_save_close/example_rename_cells.rb +1 -13
  18. data/examples/open_save_close/example_simple.rb +1 -1
  19. data/examples/open_save_close/example_unobtrusively.rb +3 -3
  20. data/lib/robust_excel_ole.rb +81 -2
  21. data/lib/robust_excel_ole/book.rb +171 -118
  22. data/lib/robust_excel_ole/{book_store.rb → bookstore.rb} +2 -2
  23. data/lib/robust_excel_ole/excel.rb +153 -24
  24. data/lib/robust_excel_ole/range.rb +2 -2
  25. data/lib/robust_excel_ole/sheet.rb +84 -35
  26. data/lib/robust_excel_ole/version.rb +1 -1
  27. data/reo.bat +3 -0
  28. data/spec/book_close_spec.rb +179 -0
  29. data/spec/book_misc_spec.rb +365 -0
  30. data/spec/book_open_spec.rb +793 -0
  31. data/spec/book_save_spec.rb +257 -0
  32. data/spec/book_sheet_spec.rb +160 -0
  33. data/spec/book_spec.rb +145 -1533
  34. data/spec/book_subclass_spec.rb +50 -0
  35. data/spec/book_unobtr_spec.rb +950 -0
  36. data/spec/{book_store_spec.rb → bookstore_spec.rb} +5 -5
  37. data/spec/cell_spec.rb +6 -6
  38. data/spec/data/{more_workbook.xls → another_workbook.xls} +0 -0
  39. data/spec/data/different_workbook.xls +0 -0
  40. data/spec/data/workbook.xls +0 -0
  41. data/spec/data/workbook.xlsm +0 -0
  42. data/spec/data/workbook.xlsx +0 -0
  43. data/spec/data/workbook_linked.xlsm +0 -0
  44. data/spec/data/workbook_linked_sub.xlsm +0 -0
  45. data/spec/excel_spec.rb +204 -5
  46. data/spec/range_spec.rb +6 -6
  47. data/spec/sheet_spec.rb +122 -34
  48. metadata +18 -8
  49. data/spec/data/workbook_connected_sub.xlsm +0 -0
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './spec_helper')
4
+
5
+
6
+ module My
7
+ class Excel < RobustExcelOle::Excel
8
+ end
9
+
10
+ class Book < RobustExcelOle::Book
11
+ end
12
+ end
13
+
14
+ describe "subclassed Book" do
15
+
16
+ before(:all) do
17
+ excel = RobustExcelOle::Excel.new(:reuse => true)
18
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
19
+ puts "*** open books *** : #{open_books}" if open_books > 0
20
+ RobustExcelOle::Excel.close_all
21
+ end
22
+
23
+ before do
24
+ @dir = create_tmpdir
25
+ @simple_file = @dir + '/workbook.xls'
26
+ end
27
+
28
+ after do
29
+ RobustExcelOle::Excel.close_all
30
+ rm_tmp(@dir)
31
+ end
32
+
33
+
34
+ describe "open" do
35
+
36
+ it "should use the subclassed Excel" do
37
+ #REO::Book.open(@simple_file) do |book|
38
+ My::Book.open(@simple_file) do |book|
39
+ book.should be_a RobustExcelOle::Book
40
+ book.should be_a My::Book
41
+ book.excel.should be_a RobustExcelOle::Excel
42
+ book.excel.class.should == My::Excel
43
+ book.excel.should be_a My::Excel
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+
50
+ end
@@ -0,0 +1,950 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+
10
+ describe Book do
11
+
12
+ before(:all) do
13
+ excel = Excel.new(:reuse => true)
14
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
15
+ puts "*** open books *** : #{open_books}" if open_books > 0
16
+ Excel.close_all
17
+ end
18
+
19
+ before do
20
+ @dir = create_tmpdir
21
+ @simple_file = @dir + '/workbook.xls'
22
+ @simple_save_file = @dir + '/workbook_save.xls'
23
+ @different_file = @dir + '/different_workbook.xls'
24
+ @simple_file_other_path = @dir + '/more_data/workbook.xls'
25
+ @another_simple_file = @dir + '/another_workbook.xls'
26
+ @linked_file = @dir + '/workbook_linked.xlsm'
27
+ @simple_file_xlsm = @dir + '/workbook.xls'
28
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
29
+ end
30
+
31
+ after do
32
+ Excel.close_all
33
+ rm_tmp(@dir)
34
+ end
35
+
36
+
37
+ describe "unobtrusively" do
38
+
39
+ def unobtrusively_ok? # :nodoc: #
40
+ Book.unobtrusively(@simple_file) do |book|
41
+ book.should be_a Book
42
+ sheet = book[0]
43
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
44
+ book.should be_alive
45
+ book.Saved.should be_false
46
+ end
47
+ end
48
+
49
+ context "with no open book" do
50
+
51
+ it "should open unobtrusively if no Excel is open" do
52
+ Excel.close_all
53
+ Book.unobtrusively(@simple_file) do |book|
54
+ book.should be_a Book
55
+ end
56
+ end
57
+
58
+ it "should open unobtrusively in a new Excel" do
59
+ expect{ unobtrusively_ok? }.to_not raise_error
60
+ end
61
+
62
+ context "with two running excel instances" do
63
+ before :all do
64
+ Excel.close_all
65
+ end
66
+
67
+ before do
68
+ @excel1 = Excel.new(:reuse => false)
69
+ @excel2 = Excel.new(:reuse => false)
70
+ end
71
+
72
+ after do
73
+ #Excel.close_all
74
+ begin
75
+ @excel1.close
76
+ @excel2.close
77
+ rescue ExcelErrorOpen => msg
78
+ puts "ExcelError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
79
+ end
80
+ end
81
+
82
+ it "should open unobtrusively in the first opened Excel" do
83
+ Book.unobtrusively(@simple_file) do |book|
84
+ book.should be_a Book
85
+ book.should be_alive
86
+ book.excel.should == @excel1
87
+ book.excel.should_not == @excel2
88
+ end
89
+ end
90
+
91
+ it "should open unobtrusively in a new Excel" do
92
+ Book.unobtrusively(@simple_file, :hidden) do |book|
93
+ book.should be_a Book
94
+ book.should be_alive
95
+ book.excel.should_not == @excel1
96
+ book.excel.should_not == @excel2
97
+ end
98
+ end
99
+
100
+ it "should open unobtrusively in a given Excel" do
101
+ Book.unobtrusively(@simple_file, @excel2) do |book|
102
+ book.should be_a Book
103
+ book.should be_alive
104
+ book.excel.should_not == @excel1
105
+ book.excel.should == @excel2
106
+ end
107
+ end
108
+
109
+ it "should raise an error if the excel instance is not alive" do
110
+ Excel.close_all
111
+ expect{
112
+ Book.unobtrusively(@simple_file, @excel2) do |book|
113
+ end
114
+ }.to raise_error(ExcelErrorOpen, "Excel instance not alive or damaged")
115
+ end
116
+ end
117
+
118
+ it "should raise an error if the option is invalid" do
119
+ expect{
120
+ Book.unobtrusively(@simple_file, :invalid_option) do |book|
121
+ end
122
+ }.to raise_error(ExcelErrorOpen, "provided instance is neither an Excel nor a Book")
123
+ end
124
+
125
+ end
126
+
127
+ context "with an open book" do
128
+
129
+ before do
130
+ @book = Book.open(@simple_file)
131
+ end
132
+
133
+ after do
134
+ @book.close(:if_unsaved => :forget)
135
+ @book2.close(:if_unsaved => :forget) rescue nil
136
+ end
137
+
138
+ it "should open in the Excel of the given Book" do
139
+ #book1 = Book.open(@different_file)
140
+ @book2 = Book.open(@another_simple_file, :force_excel => :new)
141
+ Book.unobtrusively(@different_file, @book2) do |book|
142
+ book.should be_a Book
143
+ book.should be_alive
144
+ book.excel.should_not == @book.excel
145
+ book.excel.should == @book2.excel
146
+ end
147
+ end
148
+
149
+ it "should let a saved book saved" do
150
+ @book.Saved.should be_true
151
+ @book.should be_alive
152
+ sheet = @book[0]
153
+ old_cell_value = sheet[1,1].value
154
+ unobtrusively_ok?
155
+ @book.Saved.should be_true
156
+ @book.should be_alive
157
+ sheet = @book[0]
158
+ sheet[1,1].value.should_not == old_cell_value
159
+ end
160
+
161
+ it "should let the unsaved book unsaved" do
162
+ sheet = @book[0]
163
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
164
+ old_cell_value = sheet[1,1].value
165
+ @book.Saved.should be_false
166
+ unobtrusively_ok?
167
+ @book.should be_alive
168
+ @book.Saved.should be_false
169
+ @book.close(:if_unsaved => :forget)
170
+ @book2 = Book.open(@simple_file)
171
+ sheet2 = @book2[0]
172
+ sheet2[1,1].value.should_not == old_cell_value
173
+ end
174
+
175
+ it "should modify unobtrusively the second, writable book" do
176
+ @book2 = Book.open(@simple_file, :force_excel => :new)
177
+ @book.ReadOnly.should be_false
178
+ @book2.ReadOnly.should be_true
179
+ sheet = @book2[0]
180
+ old_cell_value = sheet[1,1].value
181
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
182
+ unobtrusively_ok?
183
+ @book2.should be_alive
184
+ @book2.Saved.should be_false
185
+ @book2.close(:if_unsaved => :forget)
186
+ @book.close
187
+ @book = Book.open(@simple_file)
188
+ sheet2 = @book[0]
189
+ sheet2[1,1].value.should_not == old_cell_value
190
+ end
191
+ end
192
+
193
+ context "with a closed book" do
194
+
195
+ before do
196
+ @book = Book.open(@simple_file)
197
+ end
198
+
199
+ after do
200
+ @book.close(:if_unsaved => :forget)
201
+ end
202
+
203
+ it "should let the closed book closed by default" do
204
+ sheet = @book[0]
205
+ old_cell_value = sheet[1,1].value
206
+ @book.close
207
+ @book.should_not be_alive
208
+ unobtrusively_ok?
209
+ @book.should_not be_alive
210
+ @book = Book.open(@simple_file)
211
+ sheet = @book[0]
212
+ sheet[1,1].Value.should_not == old_cell_value
213
+ end
214
+
215
+ # The bold reanimation of the @book
216
+ it "should use the excel of the book and keep open the book" do
217
+ excel = Excel.new(:reuse => false)
218
+ sheet = @book[0]
219
+ old_cell_value = sheet[1,1].value
220
+ @book.close
221
+ @book.should_not be_alive
222
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
223
+ book.should be_a Book
224
+ book.excel.should == @book.excel
225
+ book.excel.should_not == excel
226
+ sheet = book[0]
227
+ cell = sheet[1,1]
228
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
229
+ book.Saved.should be_false
230
+ end
231
+ @book.should be_alive
232
+ @book.close
233
+ new_book = Book.open(@simple_file)
234
+ sheet = new_book[0]
235
+ sheet[1,1].value.should_not == old_cell_value
236
+ end
237
+
238
+ # book shall be reanimated even with :hidden
239
+ it "should use the excel of the book and keep open the book" do
240
+ excel = Excel.new(:reuse => false)
241
+ sheet = @book[0]
242
+ old_cell_value = sheet[1,1].value
243
+ @book.close
244
+ @book.should_not be_alive
245
+ Book.unobtrusively(@simple_file, :hidden) do |book|
246
+ book.should be_a Book
247
+ book.should be_alive
248
+ book.excel.should_not == @book.excel
249
+ book.excel.should_not == excel
250
+ sheet = book[0]
251
+ cell = sheet[1,1]
252
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
253
+ book.Saved.should be_false
254
+ end
255
+ @book.should_not be_alive
256
+ new_book = Book.open(@simple_file)
257
+ sheet = new_book[0]
258
+ sheet[1,1].value.should_not == old_cell_value
259
+ end
260
+
261
+ it "should use another excel if the Excels are closed" do
262
+ excel = Excel.new(:reuse => false)
263
+ sheet = @book[0]
264
+ old_cell_value = sheet[1,1].value
265
+ @book.close
266
+ @book.should_not be_alive
267
+ Excel.close_all
268
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
269
+ book.should be_a Book
270
+ book.excel.should == @book.excel
271
+ book.excel.should_not == excel
272
+ sheet = book[0]
273
+ cell = sheet[1,1]
274
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
275
+ book.Saved.should be_false
276
+ end
277
+ @book.should be_alive
278
+ @book.close
279
+ new_book = Book.open(@simple_file)
280
+ sheet = new_book[0]
281
+ sheet[1,1].value.should_not == old_cell_value
282
+ end
283
+
284
+ it "should use another excel if the Excels are closed" do
285
+ excel = Excel.new(:reuse => false)
286
+ sheet = @book[0]
287
+ old_cell_value = sheet[1,1].value
288
+ @book.close
289
+ @book.should_not be_alive
290
+ Excel.close_all
291
+ Book.unobtrusively(@simple_file, :hidden, :keep_open => true) do |book|
292
+ book.should be_a Book
293
+ book.excel.should_not == @book.excel
294
+ book.excel.should_not == excel
295
+ sheet = book[0]
296
+ cell = sheet[1,1]
297
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
298
+ book.Saved.should be_false
299
+ end
300
+ @book.should_not be_alive
301
+ new_book = Book.open(@simple_file)
302
+ sheet = new_book[0]
303
+ sheet[1,1].value.should_not == old_cell_value
304
+ end
305
+
306
+ it "should modify unobtrusively the copied file" do
307
+ sheet = @book[0]
308
+ old_cell_value = sheet[1,1].value
309
+ File.delete simple_save_file rescue nil
310
+ @book.save_as(@simple_save_file)
311
+ @book.close
312
+ Book.unobtrusively(@simple_save_file) do |book|
313
+ sheet = book[0]
314
+ cell = sheet[1,1]
315
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
316
+ end
317
+ old_book = Book.open(@simple_file)
318
+ old_sheet = old_book[0]
319
+ old_sheet[1,1].Value.should == old_cell_value
320
+ old_book.close
321
+ new_book = Book.open(@simple_save_file)
322
+ new_sheet = new_book[0]
323
+ new_sheet[1,1].Value.should_not == old_cell_value
324
+ new_book.close
325
+ end
326
+
327
+
328
+ end
329
+
330
+ context "with various options for an Excel instance in which to open a closed book" do
331
+
332
+ before do
333
+ @book = Book.open(@simple_file)
334
+ @book.close
335
+ end
336
+
337
+ it "should use a given Excel" do
338
+ new_excel = Excel.new(:reuse => false)
339
+ another_excel = Excel.new(:reuse => false)
340
+ Book.unobtrusively(@simple_file, another_excel) do |book|
341
+ book.excel.should_not == @book.excel
342
+ book.excel.should_not == new_excel
343
+ book.excel.should == another_excel
344
+ end
345
+ end
346
+
347
+ it "should use the hidden Excel" do
348
+ new_excel = Excel.new(:reuse => false)
349
+ Book.unobtrusively(@simple_file, :hidden) do |book|
350
+ book.excel.should_not == @book.excel
351
+ book.excel.should_not == new_excel
352
+ book.excel.visible.should be_false
353
+ book.excel.displayalerts.should be_false
354
+ @hidden_excel = book.excel
355
+ end
356
+ Book.unobtrusively(@simple_file, :hidden) do |book|
357
+ book.excel.should_not == @book.excel
358
+ book.excel.should_not == new_excel
359
+ book.excel.visible.should be_false
360
+ book.excel.displayalerts.should be_false
361
+ book.excel.should == @hidden_excel
362
+ end
363
+ end
364
+
365
+ it "should reuse Excel" do
366
+ new_excel = Excel.new(:reuse => false)
367
+ Book.unobtrusively(@simple_file, :reuse) do |book|
368
+ book.excel.should == @book.excel
369
+ book.excel.should_not == new_excel
370
+ end
371
+ end
372
+
373
+ it "should reuse Excel by default" do
374
+ new_excel = Excel.new(:reuse => false)
375
+ Book.unobtrusively(@simple_file) do |book|
376
+ book.excel.should == @book.excel
377
+ book.excel.should_not == new_excel
378
+ end
379
+ end
380
+
381
+ end
382
+
383
+ context "with a read_only book" do
384
+
385
+ before do
386
+ @book = Book.open(@simple_file, :read_only => true)
387
+ end
388
+
389
+ after do
390
+ @book.close
391
+ end
392
+
393
+ it "should let the saved book saved" do
394
+ @book.ReadOnly.should be_true
395
+ @book.Saved.should be_true
396
+ sheet = @book[0]
397
+ old_cell_value = sheet[1,1].value
398
+ unobtrusively_ok?
399
+ @book.should be_alive
400
+ @book.Saved.should be_true
401
+ @book.ReadOnly.should be_true
402
+ @book.close
403
+ book2 = Book.open(@simple_file)
404
+ sheet2 = book2[0]
405
+ sheet2[1,1].value.should_not == old_cell_value
406
+ end
407
+
408
+ it "should let the unsaved book unsaved" do
409
+ @book.ReadOnly.should be_true
410
+ sheet = @book[0]
411
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
412
+ @book.Saved.should be_false
413
+ @book.should be_alive
414
+ Book.unobtrusively(@simple_file) do |book|
415
+ book.should be_a Book
416
+ sheet = book[0]
417
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
418
+ @cell_value = sheet[1,1].Value
419
+ book.should be_alive
420
+ book.Saved.should be_false
421
+ end
422
+ @book.should be_alive
423
+ @book.Saved.should be_false
424
+ @book.ReadOnly.should be_true
425
+ @book.close
426
+ book2 = Book.open(@simple_file)
427
+ sheet2 = book2[0]
428
+ # modifies unobtrusively the saved version, not the unsaved version
429
+ sheet2[1,1].value.should == @cell_value
430
+ end
431
+
432
+ it "should open unobtrusively by default the writable book" do
433
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => false)
434
+ @book.ReadOnly.should be_true
435
+ book2.Readonly.should be_false
436
+ sheet = @book[0]
437
+ cell_value = sheet[1,1].value
438
+ Book.unobtrusively(@simple_file, :hidden) do |book|
439
+ book.should be_a Book
440
+ book.excel.should == book2.excel
441
+ book.excel.should_not == @book.excel
442
+ sheet = book[0]
443
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
444
+ book.should be_alive
445
+ book.Saved.should be_false
446
+ end
447
+ @book.Saved.should be_true
448
+ @book.ReadOnly.should be_true
449
+ @book.close
450
+ book2.close
451
+ book3 = Book.open(@simple_file)
452
+ new_sheet = book3[0]
453
+ new_sheet[1,1].value.should_not == cell_value
454
+ book3.close
455
+ end
456
+
457
+ it "should open unobtrusively by default the book in a new Excel such that the book is writable" do
458
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
459
+ @book.ReadOnly.should be_true
460
+ book2.Readonly.should be_true
461
+ sheet = @book[0]
462
+ cell_value = sheet[1,1].value
463
+ Book.unobtrusively(@simple_file, :hidden) do |book|
464
+ book.should be_a Book
465
+ book.excel.should_not == book2.excel
466
+ book.excel.should_not == @book.excel
467
+ sheet = book[0]
468
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
469
+ book.should be_alive
470
+ book.Saved.should be_false
471
+ end
472
+ @book.Saved.should be_true
473
+ @book.ReadOnly.should be_true
474
+ @book.close
475
+ book2.close
476
+ book3 = Book.open(@simple_file)
477
+ new_sheet = book3[0]
478
+ new_sheet[1,1].value.should_not == cell_value
479
+ book3.close
480
+ end
481
+
482
+ it "should open unobtrusively the book in a new Excel such that the book is writable" do
483
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
484
+ @book.ReadOnly.should be_true
485
+ book2.Readonly.should be_true
486
+ sheet = @book[0]
487
+ cell_value = sheet[1,1].value
488
+ Book.unobtrusively(@simple_file, :hidden) do |book|
489
+ book.should be_a Book
490
+ book.excel.should_not == book2.excel
491
+ book.excel.should_not == @book.excel
492
+ sheet = book[0]
493
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
494
+ book.should be_alive
495
+ book.Saved.should be_false
496
+ end
497
+ @book.Saved.should be_true
498
+ @book.ReadOnly.should be_true
499
+ @book.close
500
+ book2.close
501
+ book3 = Book.open(@simple_file)
502
+ new_sheet = book3[0]
503
+ new_sheet[1,1].value.should_not == cell_value
504
+ book3.close
505
+ end
506
+
507
+ it "should open unobtrusively the book in a new Excel to open the book writable" do
508
+ excel1 = Excel.new(:reuse => false)
509
+ excel2 = Excel.new(:reuse => false)
510
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
511
+ @book.ReadOnly.should be_true
512
+ book2.Readonly.should be_true
513
+ sheet = @book[0]
514
+ cell_value = sheet[1,1].value
515
+ Book.unobtrusively(@simple_file, :hidden, :readonly_excel => false) do |book|
516
+ book.should be_a Book
517
+ book.ReadOnly.should be_false
518
+ book.excel.should_not == book2.excel
519
+ book.excel.should_not == @book.excel
520
+ book.excel.should_not == excel1
521
+ book.excel.should_not == excel2
522
+ sheet = book[0]
523
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
524
+ book.should be_alive
525
+ book.Saved.should be_false
526
+ end
527
+ @book.Saved.should be_true
528
+ @book.ReadOnly.should be_true
529
+ @book.close
530
+ book2.close
531
+ book3 = Book.open(@simple_file)
532
+ new_sheet = book3[0]
533
+ new_sheet[1,1].value.should_not == cell_value
534
+ book3.close
535
+ end
536
+
537
+ it "should open unobtrusively the book in the same Excel to open the book writable" do
538
+ excel1 = Excel.new(:reuse => false)
539
+ excel2 = Excel.new(:reuse => false)
540
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
541
+ @book.ReadOnly.should be_true
542
+ book2.Readonly.should be_true
543
+ sheet = @book[0]
544
+ cell_value = sheet[1,1].value
545
+ Book.unobtrusively(@simple_file, :hidden, :readonly_excel => true) do |book|
546
+ book.should be_a Book
547
+ book.excel.should == book2.excel
548
+ book.ReadOnly.should be_false
549
+ sheet = book[0]
550
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
551
+ book.should be_alive
552
+ book.Saved.should be_false
553
+ end
554
+ book2.Saved.should be_true
555
+ book2.ReadOnly.should be_false
556
+ @book.close
557
+ book2.close
558
+ book3 = Book.open(@simple_file)
559
+ new_sheet = book3[0]
560
+ new_sheet[1,1].value.should_not == cell_value
561
+ book3.close
562
+ end
563
+
564
+ it "should open unobtrusively the book in the Excel where it was opened most recently" do
565
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
566
+ @book.ReadOnly.should be_true
567
+ book2.Readonly.should be_true
568
+ sheet = @book[0]
569
+ cell_value = sheet[1,1].value
570
+ Book.unobtrusively(@simple_file, :hidden, :read_only => true) do |book|
571
+ book.should be_a Book
572
+ book.excel.should == book2.excel
573
+ book.excel.should_not == @book.excel
574
+ book.should be_alive
575
+ book.Saved.should be_true
576
+ end
577
+ @book.Saved.should be_true
578
+ @book.ReadOnly.should be_true
579
+ @book.close
580
+ book2.close
581
+ end
582
+
583
+ end
584
+
585
+ context "with a virgin Book class" do
586
+ before do
587
+ class Book
588
+ @@bookstore = nil
589
+ end
590
+ end
591
+ it "should work" do
592
+ expect{ unobtrusively_ok? }.to_not raise_error
593
+ end
594
+ end
595
+
596
+ context "with a book never opened before" do
597
+ before do
598
+ class Book
599
+ @@bookstore = nil
600
+ end
601
+ other_book = Book.open(@different_file)
602
+ end
603
+ it "should open the book" do
604
+ expect{ unobtrusively_ok? }.to_not raise_error
605
+ end
606
+ end
607
+
608
+ context "with a saved book" do
609
+
610
+ before do
611
+ @book1 = Book.open(@simple_file)
612
+ end
613
+
614
+ after do
615
+ @book1.close(:if_unsaved => :forget)
616
+ end
617
+
618
+ it "should save if the book was modified during unobtrusively" do
619
+ m_time = File.mtime(@book1.stored_filename)
620
+ Book.unobtrusively(@simple_file, :hidden) do |book|
621
+ @book1.Saved.should be_true
622
+ book.Saved.should be_true
623
+ sheet = book[0]
624
+ cell = sheet[1,1]
625
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
626
+ @book1.Saved.should be_false
627
+ book.Saved.should be_false
628
+ sleep 1
629
+ end
630
+ @book1.Saved.should be_true
631
+ m_time2 = File.mtime(@book1.stored_filename)
632
+ m_time2.should_not == m_time
633
+ end
634
+
635
+ it "should not save the book if it was not modified during unobtrusively" do
636
+ m_time = File.mtime(@book1.stored_filename)
637
+ Book.unobtrusively(@simple_file) do |book|
638
+ @book1.Saved.should be_true
639
+ book.Saved.should be_true
640
+ sleep 1
641
+ end
642
+ m_time2 = File.mtime(@book1.stored_filename)
643
+ m_time2.should == m_time
644
+ end
645
+ end
646
+
647
+ context "with block result" do
648
+ before do
649
+ @book1 = Book.open(@simple_file)
650
+ end
651
+
652
+ after do
653
+ @book1.close(:if_unsaved => :forget)
654
+ end
655
+
656
+ it "should yield the block result true" do
657
+ result =
658
+ Book.unobtrusively(@simple_file) do |book|
659
+ @book1.Saved.should be_true
660
+ end
661
+ result.should == true
662
+ end
663
+
664
+ it "should yield the block result nil" do
665
+ result =
666
+ Book.unobtrusively(@simple_file) do |book|
667
+ end
668
+ result.should == nil
669
+ end
670
+
671
+ it "should yield the block result with an unmodified book" do
672
+ sheet1 = @book1[0]
673
+ cell1 = sheet1[1,1].value
674
+ result =
675
+ Book.unobtrusively(@simple_file) do |book|
676
+ sheet = book[0]
677
+ cell = sheet[1,1].value
678
+ end
679
+ result.should == cell1
680
+ end
681
+
682
+ it "should yield the block result even if the book gets saved" do
683
+ sheet1 = @book1[0]
684
+ @book1.save
685
+ result =
686
+ Book.unobtrusively(@simple_file) do |book|
687
+ sheet = book[0]
688
+ sheet[1,1] = 22
689
+ @book1.Saved.should be_false
690
+ 42
691
+ end
692
+ result.should == 42
693
+ @book1.Saved.should be_true
694
+ end
695
+ end
696
+
697
+ context "with several Excel instances" do
698
+
699
+ before do
700
+ @book1 = Book.open(@simple_file)
701
+ @book2 = Book.open(@simple_file, :force_excel => :new)
702
+ @book1.Readonly.should == false
703
+ @book2.Readonly.should == true
704
+ old_sheet = @book1[0]
705
+ @old_cell_value = old_sheet[1,1].value
706
+ @book1.close
707
+ @book2.close
708
+ @book1.should_not be_alive
709
+ @book2.should_not be_alive
710
+ end
711
+
712
+ it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
713
+ Book.unobtrusively(@simple_file) do |book|
714
+ book.excel.should == @book2.excel
715
+ book.excel.should_not == @book1.excel
716
+ book.ReadOnly.should == false
717
+ sheet = book[0]
718
+ cell = sheet[1,1]
719
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
720
+ book.Saved.should be_false
721
+ end
722
+ new_book = Book.open(@simple_file)
723
+ sheet = new_book[0]
724
+ sheet[1,1].value.should_not == @old_cell_value
725
+ end
726
+
727
+ it "should open unobtrusively the closed book in the new hidden Excel" do
728
+ Book.unobtrusively(@simple_file, :hidden) do |book|
729
+ book.excel.should_not == @book2.excel
730
+ book.excel.should_not == @book1.excel
731
+ book.ReadOnly.should == false
732
+ sheet = book[0]
733
+ cell = sheet[1,1]
734
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
735
+ book.Saved.should be_false
736
+ end
737
+ new_book = Book.open(@simple_file)
738
+ sheet = new_book[0]
739
+ sheet[1,1].Value.should_not == @old_cell_value
740
+ end
741
+
742
+ it "should open unobtrusively the closed book in a new Excel if the Excel is not alive anymore" do
743
+ Excel.close_all
744
+ Book.unobtrusively(@simple_file, :hidden) do |book|
745
+ book.ReadOnly.should == false
746
+ book.excel.should_not == @book1.excel
747
+ book.excel.should_not == @book2.excel
748
+ sheet = book[0]
749
+ cell = sheet[1,1]
750
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
751
+ book.Saved.should be_false
752
+ end
753
+ new_book = Book.open(@simple_file)
754
+ sheet = new_book[0]
755
+ sheet[1,1].Value.should_not == @old_cell_value
756
+ end
757
+ end
758
+
759
+ context "with :hidden" do
760
+
761
+ before do
762
+ @book1 = Book.open(@simple_file)
763
+ @book1.close
764
+ end
765
+
766
+ it "should create a new hidden Excel instance" do
767
+ Book.unobtrusively(@simple_file, :hidden) do |book|
768
+ book.should be_a Book
769
+ book.should be_alive
770
+ book.excel.Visible.should be_false
771
+ book.excel.DisplayAlerts.should be_false
772
+ end
773
+ end
774
+
775
+ it "should create a new hidden Excel instance and use this afterwards" do
776
+ hidden_excel = nil
777
+ Book.unobtrusively(@simple_file, :hidden) do |book|
778
+ book.should be_a Book
779
+ book.should be_alive
780
+ book.excel.Visible.should be_false
781
+ book.excel.DisplayAlerts.should be_false
782
+ hidden_excel = book.excel
783
+ end
784
+ Book.unobtrusively(@different_file, :hidden) do |book|
785
+ book.should be_a Book
786
+ book.should be_alive
787
+ book.excel.Visible.should be_false
788
+ book.excel.DisplayAlerts.should be_false
789
+ book.excel.should == hidden_excel
790
+ end
791
+ end
792
+
793
+ it "should create a new hidden Excel instance if the Excel is closed" do
794
+ Excel.close_all
795
+ Book.unobtrusively(@simple_file, :hidden) do |book|
796
+ book.should be_a Book
797
+ book.should be_alive
798
+ book.excel.Visible.should be_false
799
+ book.excel.DisplayAlerts.should be_false
800
+ book.excel.should_not == @book1.excel
801
+ end
802
+ end
803
+
804
+ it "should exclude hidden Excel when reuse in unobtrusively" do
805
+ hidden_excel = nil
806
+ Book.unobtrusively(@simple_file, :hidden) do |book|
807
+ book.should be_a Book
808
+ book.should be_alive
809
+ book.excel.Visible.should be_false
810
+ book.excel.DisplayAlerts.should be_false
811
+ book.excel.should_not == @book1.excel
812
+ hidden_excel = book.excel
813
+ end
814
+ Book.unobtrusively(@simple_file) do |book|
815
+ book.should be_a Book
816
+ book.should be_alive
817
+ book.excel.Visible.should be_false
818
+ book.excel.DisplayAlerts.should be_false
819
+ book.excel.should_not == hidden_excel
820
+ end
821
+ end
822
+
823
+ it "should exclude hidden Excel when reuse in open" do
824
+ hidden_excel = nil
825
+ Book.unobtrusively(@simple_file, :hidden) do |book|
826
+ book.should be_a Book
827
+ book.should be_alive
828
+ book.excel.Visible.should be_false
829
+ book.excel.DisplayAlerts.should be_false
830
+ book.excel.should_not == @book1.excel
831
+ hidden_excel = book.excel
832
+ end
833
+ book2 = Book.open(@simple_file, :default_excel => :reuse)
834
+ book2.excel.should_not == hidden_excel
835
+ end
836
+
837
+ it "should exclude hidden Excel when reuse in open" do
838
+ book1 = Book.open(@simple_file)
839
+ book1.close
840
+ book2 = Book.open(@simple_file, :default_excel => :reuse)
841
+ book2.excel.should == book1.excel
842
+ book1.should be_alive
843
+ book2.close
844
+ end
845
+ end
846
+ end
847
+
848
+ describe "for_reading, for_modifying" do
849
+
850
+ context "open unobtrusively for reading and modifying" do
851
+
852
+ before do
853
+ @book = Book.open(@simple_file)
854
+ sheet = @book[0]
855
+ @old_cell_value = sheet[1,1].value
856
+ @book.close
857
+ end
858
+
859
+ it "should not change the value" do
860
+ Book.for_reading(@simple_file) do |book|
861
+ book.should be_a Book
862
+ book.should be_alive
863
+ book.Saved.should be_true
864
+ sheet = book[0]
865
+ cell = sheet[1,1]
866
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
867
+ book.Saved.should be_false
868
+ book.excel.should == @book.excel
869
+ end
870
+ new_book = Book.open(@simple_file, :visible => true)
871
+ sheet = new_book[0]
872
+ sheet[1,1].Value.should == @old_cell_value
873
+ end
874
+
875
+ it "should not change the value and use a given Excel" do
876
+ new_excel = Excel.new(:reuse => false)
877
+ another_excel = Excel.new(:reuse => false)
878
+ Book.for_reading(@simple_file, another_excel) do |book|
879
+ sheet = book[0]
880
+ cell = sheet[1,1]
881
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
882
+ book.excel.should == another_excel
883
+ end
884
+ new_book = Book.open(@simple_file, :visible => true)
885
+ sheet = new_book[0]
886
+ sheet[1,1].Value.should == @old_cell_value
887
+ end
888
+
889
+ it "should not change the value and use the hidden Excel instance" do
890
+ new_excel = Excel.new(:reuse => false)
891
+ Book.for_reading(@simple_file, :hidden) do |book|
892
+ sheet = book[0]
893
+ cell = sheet[1,1]
894
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
895
+ book.excel.should_not == @book.excel
896
+ book.excel.should_not == new_excel
897
+ book.excel.visible.should be_false
898
+ book.excel.displayalerts.should be_false
899
+ end
900
+ new_book = Book.open(@simple_file, :visible => true)
901
+ sheet = new_book[0]
902
+ sheet[1,1].Value.should == @old_cell_value
903
+ end
904
+
905
+ it "should change the value" do
906
+ Book.for_modifying(@simple_file) do |book|
907
+ sheet = book[0]
908
+ cell = sheet[1,1]
909
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
910
+ book.excel.should == @book.excel
911
+ end
912
+ new_book = Book.open(@simple_file, :visible => true)
913
+ sheet = new_book[0]
914
+ sheet[1,1].Value.should_not == @old_cell_value
915
+ end
916
+
917
+ it "should change the value and use a given Excel" do
918
+ new_excel = Excel.new(:reuse => false)
919
+ another_excel = Excel.new(:reuse => false)
920
+ Book.for_modifying(@simple_file, another_excel) do |book|
921
+ sheet = book[0]
922
+ cell = sheet[1,1]
923
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
924
+ book.excel.should == another_excel
925
+ end
926
+ new_book = Book.open(@simple_file, :visible => true)
927
+ sheet = new_book[0]
928
+ sheet[1,1].Value.should_not == @old_cell_value
929
+ end
930
+
931
+ it "should change the value and use the hidden Excel instance" do
932
+ new_excel = Excel.new(:reuse => false)
933
+ Book.for_modifying(@simple_file, :hidden) do |book|
934
+ sheet = book[0]
935
+ cell = sheet[1,1]
936
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
937
+ book.excel.should_not == @book.excel
938
+ book.excel.should_not == new_excel
939
+ book.excel.visible.should be_false
940
+ book.excel.displayalerts.should be_false
941
+ end
942
+ new_book = Book.open(@simple_file, :visible => true)
943
+ sheet = new_book[0]
944
+ sheet[1,1].Value.should_not == @old_cell_value
945
+ end
946
+ end
947
+ end
948
+
949
+
950
+ end