robust_excel_ole 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +13 -0
  3. data/README.rdoc +107 -73
  4. data/{README_excel.rdoc → docs/README_excel.rdoc} +2 -0
  5. data/{README_open.rdoc → docs/README_open.rdoc} +12 -16
  6. data/{README_ranges.rdoc → docs/README_ranges.rdoc} +1 -1
  7. data/{README_save_close.rdoc → docs/README_save_close.rdoc} +0 -0
  8. data/{README_sheet.rdoc → docs/README_sheet.rdoc} +0 -0
  9. data/examples/open_save_close/example_control_to_excel.rb +3 -3
  10. data/examples/open_save_close/example_default_excel.rb +2 -2
  11. data/examples/open_save_close/example_force_excel.rb +2 -2
  12. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +2 -2
  13. data/examples/open_save_close/example_if_obstructed_forget.rb +2 -2
  14. data/examples/open_save_close/example_if_obstructed_save.rb +2 -2
  15. data/examples/open_save_close/example_if_unsaved_accept.rb +3 -3
  16. data/examples/open_save_close/example_if_unsaved_forget.rb +7 -3
  17. data/examples/open_save_close/example_if_unsaved_forget_more.rb +1 -1
  18. data/examples/open_save_close/example_read_only.rb +2 -2
  19. data/examples/open_save_close/example_rename_cells.rb +2 -2
  20. data/examples/open_save_close/example_reuse.rb +2 -2
  21. data/examples/open_save_close/example_simple.rb +1 -1
  22. data/examples/open_save_close/example_unobtrusively.rb +2 -2
  23. data/lib/robust_excel_ole/book.rb +75 -76
  24. data/lib/robust_excel_ole/bookstore.rb +9 -9
  25. data/lib/robust_excel_ole/excel.rb +5 -5
  26. data/lib/robust_excel_ole/general.rb +11 -4
  27. data/lib/robust_excel_ole/reo_common.rb +3 -0
  28. data/lib/robust_excel_ole/version.rb +1 -1
  29. data/robust_excel_ole.gemspec +2 -2
  30. data/spec/book_spec.rb +67 -63
  31. data/spec/book_specs/book_close_spec.rb +12 -10
  32. data/spec/book_specs/book_misc_spec.rb +279 -279
  33. data/spec/book_specs/book_open_spec.rb +258 -105
  34. data/spec/book_specs/book_save_spec.rb +28 -28
  35. data/spec/book_specs/book_unobtr_spec.rb +862 -164
  36. data/spec/bookstore_spec.rb +22 -22
  37. data/spec/data/another_workbook.xls +0 -0
  38. data/spec/data/different_workbook.xls +0 -0
  39. data/spec/data/workbook.xls +0 -0
  40. data/spec/excel_spec.rb +256 -254
  41. data/spec/general_spec.rb +7 -7
  42. data/spec/ruby1.8.6_rspec2.14/book_spec.rb +1421 -0
  43. data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +22 -0
  44. data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +252 -0
  45. data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +1070 -0
  46. data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +1855 -0
  47. data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +514 -0
  48. data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +395 -0
  49. data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +51 -0
  50. data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +1737 -0
  51. data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +495 -0
  52. data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +76 -0
  53. data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +42 -0
  54. data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +1820 -0
  55. data/spec/ruby1.8.6_rspec2.14/general_spec.rb +212 -0
  56. data/spec/ruby1.8.6_rspec2.14/range_spec.rb +131 -0
  57. data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +130 -0
  58. data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +663 -0
  59. data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +35 -0
  60. data/spec/sheet_spec.rb +2 -2
  61. metadata +28 -12
  62. data/README_detail.rdoc +0 -852
  63. data/README_development.rdoc +0 -5
@@ -0,0 +1,1855 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+ include General
10
+
11
+ describe Book do
12
+
13
+ before(:all) do
14
+ excel = Excel.new(:reuse => true)
15
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
16
+ puts "*** open books *** : #{open_books}" if open_books > 0
17
+ Excel.kill_all
18
+ end
19
+
20
+ before do
21
+ @dir = create_tmpdir
22
+ @simple_file = @dir + '/workbook.xls'
23
+ @simple_save_file = @dir + '/workbook_save.xls'
24
+ @different_file = @dir + '/different_workbook.xls'
25
+ @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
+ @another_simple_file = @dir + '/another_workbook.xls'
27
+ @linked_file = @dir + '/workbook_linked.xlsm'
28
+ @simple_file_xlsm = @dir + '/workbook.xls'
29
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
30
+ @simple_file1 = @simple_file
31
+ @different_file1 = @different_file
32
+ @simple_file_other_path1 = @simple_file_other_path
33
+ end
34
+
35
+ after do
36
+ Excel.kill_all
37
+ #rm_tmp(@dir)
38
+ end
39
+
40
+ describe "simple open" do
41
+
42
+ it "should simply open" do
43
+ book = Workbook.open(@simple_file, :v => true, :f => {:e => :new})
44
+ end
45
+
46
+ end
47
+
48
+ describe "new" do
49
+
50
+ it "should simply create a new one" do
51
+ book = Workbook.new(@simple_file)
52
+ book.should be_alive
53
+ book.should be_a Book
54
+ end
55
+
56
+ it "should set options" do
57
+ book = Workbook.new(@simple_file, :visible => true, :read_only => true, :force => {:excel => :new})
58
+ book.should be_alive
59
+ book.should be_a Book
60
+ book.excel.Visible.should be_true
61
+ book.Windows(book.Name).Visible.should be_true
62
+ book.ReadOnly.should be_true
63
+ book2 = Workbook.new(@different_file, :force => {:excel => :new}, :v => true)
64
+ book2.should be_alive
65
+ book2.should be_a Book
66
+ book2.excel.Visible.should be_true
67
+ book2.Windows(book2.Name).Visible.should be_true
68
+ book2.ReadOnly.should be_false
69
+ book2.excel.should_not == book.excel
70
+ end
71
+
72
+ end
73
+
74
+ describe "open" do
75
+
76
+ context "with calculation mode" do
77
+
78
+ it "should set calculation mode" do
79
+ book1 = Book.open(@simple_file1, :visible => true)
80
+ book1.excel.calculation = :manual
81
+ book1.excel.Calculation.should == -4135
82
+ book1.save
83
+ book1.excel.close
84
+ book2 = Book.open(@simple_file1, :visible => true)
85
+ book2.excel.calculation = :automatic
86
+ book2.excel.Calculation.should == -4105
87
+ book2.save
88
+ book2.excel.close
89
+ end
90
+
91
+ it "should not set the default value" do
92
+ book1 = Book.open(@simple_file)
93
+ book1.excel.calculation.should == nil
94
+ end
95
+
96
+ it "should set the calculation mode to automatic" do
97
+ book1 = Book.open(@simple_file)
98
+ book1.excel.calculation = :automatic
99
+ book1.excel.calculation.should == :automatic
100
+ book1.excel.Calculation.should == -4105
101
+ end
102
+
103
+ it "should set the calculation mode to manual" do
104
+ book1 = Book.open(@simple_file)
105
+ book1.excel.calculation = :manual
106
+ book1.excel.calculation.should == :manual
107
+ book1.excel.Calculation.should == -4135
108
+ end
109
+
110
+ it "should change the calculation mode from manual to automatic" do
111
+ book1 = Book.open(@simple_file, :visible => true)
112
+ excel1 = Excel.current(:calculation => :automatic)
113
+ book2 = Book.open(@different_file, :visible => true)
114
+ book2.excel.Calculation.should == -4105
115
+ book1.excel.Calculation.should == -4105
116
+ end
117
+ end
118
+
119
+
120
+ context "with causing warning dead excel without window handle" do
121
+
122
+ it "combined" do
123
+ Excel.kill_all
124
+ book1 = Book.open(@simple_file, :visible => true)
125
+ book2 = Book.open(@different_file)
126
+ Excel.kill_all
127
+ #sleep 1
128
+ book3 = Book.open(@another_simple_file)
129
+ end
130
+
131
+ end
132
+
133
+ context "with class identifier 'Workbook'" do
134
+
135
+ before do
136
+ @book = Workbook.open(@simple_file)
137
+ end
138
+
139
+ after do
140
+ @book.close rescue nil
141
+ end
142
+
143
+ it "should open in a new Excel" do
144
+ book2 = Workbook.open(@simple_file, :force => {:excel => :new})
145
+ book2.should be_alive
146
+ book2.should be_a Book
147
+ book2.excel.should_not == @book.excel
148
+ book2.should_not == @book
149
+ @book.Readonly.should be_false
150
+ book2.Readonly.should be_true
151
+ book2.close
152
+ end
153
+ end
154
+
155
+ context "lift a workbook to a Book object" do
156
+
157
+ before do
158
+ @book = Book.open(@simple_file)
159
+ end
160
+
161
+ after do
162
+ @book.close
163
+ end
164
+
165
+ it "should fetch the workbook" do
166
+ workbook = @book.ole_workbook
167
+ new_book = Book.new(workbook)
168
+ new_book.should be_a Book
169
+ new_book.should be_alive
170
+ new_book.should == @book
171
+ new_book.filename.should == @book.filename
172
+ new_book.excel.should == @book.excel
173
+ new_book.excel.Visible.should be_false
174
+ new_book.excel.DisplayAlerts.should be_false
175
+ new_book.should === @book
176
+ new_book.close
177
+ end
178
+
179
+ it "should fetch the workbook" do
180
+ workbook = @book.ole_workbook
181
+ new_book = Book.new(workbook, :visible => true)
182
+ new_book.should be_a Book
183
+ new_book.should be_alive
184
+ new_book.should == @book
185
+ new_book.filename.should == @book.filename
186
+ new_book.excel.should == @book.excel
187
+ new_book.excel.Visible.should be_true
188
+ new_book.excel.DisplayAlerts.should be_true
189
+ new_book.should === @book
190
+ new_book.close
191
+ end
192
+
193
+ it "should yield an identical Book and set visible value" do
194
+ workbook = @book.ole_workbook
195
+ new_book = Book.new(workbook, :visible => true)
196
+ new_book.excel.displayalerts = true
197
+ new_book.should be_a Book
198
+ new_book.should be_alive
199
+ new_book.should == @book
200
+ new_book.filename.should == @book.filename
201
+ new_book.excel.should == @book.excel
202
+ new_book.should === @book
203
+ new_book.excel.Visible.should be_true
204
+ new_book.excel.DisplayAlerts.should be_true
205
+ new_book.close
206
+ end
207
+
208
+ end
209
+
210
+ context "with standard options" do
211
+ before do
212
+ @book = Book.open(@simple_file)
213
+ end
214
+
215
+ after do
216
+ @book.close
217
+ end
218
+
219
+ it "should say that it lives" do
220
+ @book.should be_alive
221
+ end
222
+ end
223
+
224
+ context "with identity transperence" do
225
+
226
+ before do
227
+ @book = Book.open(@simple_file1)
228
+ end
229
+
230
+ after do
231
+ @book.close
232
+ end
233
+
234
+ it "should yield identical Book objects for identical Excel books" do
235
+ book2 = Book.open(@simple_file1)
236
+ book2.should === @book
237
+ book2.close
238
+ end
239
+
240
+ it "should yield different Book objects for different Excel books" do
241
+ book2 = Book.open(@different_file)
242
+ book2.should_not === @book
243
+ book2.close
244
+ end
245
+
246
+ it "should yield different Book objects when opened the same file in different Excel instances" do
247
+ book2 = Book.open(@simple_file, :force => {:excel => :new})
248
+ book2.should_not === @book
249
+ book2.close
250
+ end
251
+
252
+ it "should yield identical Book objects for identical Excel books when reopening" do
253
+ @book.should be_alive
254
+ @book.close
255
+ @book.should_not be_alive
256
+ book2 = Book.open(@simple_file1)
257
+ book2.should === @book
258
+ book2.should be_alive
259
+ book2.close
260
+ end
261
+
262
+ it "should yield identical Book objects when reopening and the Excel is closed" do
263
+ @book.should be_alive
264
+ @book.close
265
+ Excel.close_all
266
+ book2 = Book.open(@simple_file1)
267
+ book2.should be_alive
268
+ book2.should === @book
269
+ book2.close
270
+ end
271
+
272
+ it "should yield different Book objects when reopening in a new Excel" do
273
+ @book.should be_alive
274
+ old_excel = @book.excel
275
+ @book.close
276
+ @book.should_not be_alive
277
+ book2 = Book.open(@simple_file1, :force => {:excel => :new})
278
+ book2.should_not === @book
279
+ book2.should be_alive
280
+ book2.excel.should_not == old_excel
281
+ book2.close
282
+ end
283
+
284
+ it "should yield different Book objects when reopening in a new given Excel instance" do
285
+ old_excel = @book.excel
286
+ new_excel = Excel.new(:reuse => false)
287
+ @book.close
288
+ @book.should_not be_alive
289
+ book2 = Book.open(@simple_file1, :force => {:excel => new_excel})
290
+ #@book.should be_alive
291
+ #book2.should === @book
292
+ book2.should be_alive
293
+ book2.excel.should == new_excel
294
+ book2.excel.should_not == old_excel
295
+ book2.close
296
+ @book.reopen
297
+ @book.should be_alive
298
+ end
299
+
300
+ it "should yield identical Book objects when reopening in the old excel" do
301
+ old_excel = @book.excel
302
+ new_excel = Excel.new(:reuse => false)
303
+ @book.close
304
+ @book.should_not be_alive
305
+ book2 = Book.open(@simple_file1, :force => {:excel => old_excel})
306
+ book2.should === @book
307
+ book2.should be_alive
308
+ book2.excel.should == old_excel
309
+ @book.should be_alive
310
+ book2.close
311
+ end
312
+
313
+ end
314
+
315
+ context "with abbrevations" do
316
+
317
+ before do
318
+ @book = Book.open(@simple_file1)
319
+ end
320
+
321
+ after do
322
+ @book.close rescue nil
323
+ end
324
+
325
+ it "should work as force" do
326
+ book2 = Book.open(@another_simple_file, :excel => :new)
327
+ book2.excel.should_not == @book.excel
328
+ book3 = Book.open(@different_file, :excel => book2.excel)
329
+ book3.excel.should == book2.excel
330
+ end
331
+
332
+ it "should work with abbrevation of force and excel" do
333
+ book2 = Book.open(@another_simple_file, :f => {:e => :new})
334
+ book2.excel.should_not == @book.excel
335
+ book3 = Book.open(@different_file, :f => {:e => book2.excel})
336
+ book3.excel.should == book2.excel
337
+ end
338
+
339
+ it "should work with abbrevation of force" do
340
+ book2 = Book.open(@another_simple_file, :f => {:excel => :new})
341
+ book2.excel.should_not == @book.excel
342
+ book3 = Book.open(@different_file, :f => {:excel => book2.excel})
343
+ book3.excel.should == book2.excel
344
+ end
345
+
346
+ it "should work with abbrevation of force" do
347
+ book2 = Book.open(@another_simple_file, :force => {:e => :new})
348
+ book2.excel.should_not == @book.excel
349
+ book3 = Book.open(@different_file, :force => {:e => book2.excel})
350
+ book3.excel.should == book2.excel
351
+ end
352
+
353
+ it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
354
+ book2 = Book.open(@another_simple_file)
355
+ book3 = Book.open(@different_file)
356
+ book3 = Book.open(@simple_file1, :excel => book2.excel)
357
+ book3.excel.should === book2.excel
358
+ book4 = Book.open(@simple_file1, :excel => @book)
359
+ book4.excel.should === @book.excel
360
+ book3.close
361
+ book4.close
362
+ book5 = Book.open(@simple_file1, :excel => book2.ole_workbook)
363
+ book5.excel.should === book2.excel
364
+ win32ole_excel1 = WIN32OLE.connect(@book.ole_workbook.Fullname).Application
365
+ book6 = Book.open(@simple_file1, :excel => win32ole_excel1)
366
+ book6.excel.should === @book.excel
367
+ end
368
+
369
+ it "should use abbreviations of default" do
370
+ book2 = Book.open(@simple_file1, :d => {:excel => :current})
371
+ book2.excel.should == @book.excel
372
+ end
373
+
374
+ it "should use abbreviations of default" do
375
+ book2 = Book.open(@simple_file1, :d => {:e => :current})
376
+ book2.excel.should == @book.excel
377
+ end
378
+
379
+ it "should use abbreviations of default" do
380
+ book2 = Book.open(@simple_file1, :default => {:e => :current})
381
+ book2.excel.should == @book.excel
382
+ end
383
+
384
+ it "should reopen the book in the Excel where it was opened most recently" do
385
+ excel1 = @book.excel
386
+ excel2 = Excel.new(:reuse => false)
387
+ @book.close
388
+ book2 = Book.open(@simple_file1, :d => {:e => :current})
389
+ book2.excel.should == excel1
390
+ book2.close
391
+ book3 = Book.open(@simple_file1, :e => excel2)
392
+ book3.close
393
+ book3 = Book.open(@simple_file1, :d => {:e => :current})
394
+ book3.excel.should == excel2
395
+ book3.close
396
+ end
397
+
398
+ end
399
+
400
+ context "with :force => {:excel}" do
401
+
402
+ before do
403
+ @book = Book.open(@simple_file1)
404
+ end
405
+
406
+ after do
407
+ @book.close rescue nil
408
+ end
409
+
410
+ it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
411
+ book2 = Book.open(@another_simple_file)
412
+ book3 = Book.open(@different_file)
413
+ book3 = Book.open(@simple_file1, :force => {:excel => book2.excel})
414
+ book3.excel.should === book2.excel
415
+ book4 = Book.open(@simple_file1, :force => {:excel => @book})
416
+ book4.excel.should === @book.excel
417
+ book3.close
418
+ book4.close
419
+ book5 = Book.open(@simple_file1, :force => {:excel => book2.ole_workbook})
420
+ book5.excel.should === book2.excel
421
+ win32ole_excel1 = WIN32OLE.connect(@book.ole_workbook.Fullname).Application
422
+ book6 = Book.open(@simple_file1, :force => {:excel => win32ole_excel1})
423
+ book6.excel.should === @book.excel
424
+ end
425
+
426
+ it "should open in a new Excel" do
427
+ book2 = Book.open(@simple_file1, :force => {:excel => :new})
428
+ book2.should be_alive
429
+ book2.should be_a Book
430
+ book2.excel.should_not == @book.excel
431
+ book2.should_not == @book
432
+ @book.Readonly.should be_false
433
+ book2.Readonly.should be_true
434
+ book2.close
435
+ end
436
+
437
+ it "should open in a given Excel, not provide identity transparency, because old book readonly, new book writable" do
438
+ book2 = Book.open(@simple_file1, :force => {:excel => :new})
439
+ book2.excel.should_not == @book.excel
440
+ book3 = Book.open(@simple_file1, :force => {:excel => :new})
441
+ book3.excel.should_not == book2.excel
442
+ book3.excel.should_not == @book.excel
443
+ book2.close
444
+ book4 = Book.open(@simple_file1, :force => {:excel => book2.excel})
445
+ book4.should be_alive
446
+ book4.should be_a Book
447
+ book4.excel.should == book2.excel
448
+ #book4.Readonly.should == true
449
+ book4.should_not == book2
450
+ book4.close
451
+ book5 = Book.open(@simple_file1, :force => {:excel => book2})
452
+ book5.should be_alive
453
+ book5.should be_a Book
454
+ book5.excel.should == book2.excel
455
+ #book5.Readonly.should == true
456
+ book5.should_not == book2
457
+ book5.close
458
+ book3.close
459
+ end
460
+
461
+ it "should open in a given Excel, provide identity transparency, because book can be readonly, such that the old and the new book are readonly" do
462
+ book2 = Book.open(@simple_file1, :force => {:excel => :new})
463
+ book2.excel.should_not == @book.excel
464
+ book3 = Book.open(@simple_file1, :force => {:excel => :new})
465
+ book3.excel.should_not == book2.excel
466
+ book3.excel.should_not == @book.excel
467
+ book2.close
468
+ book3.close
469
+ @book.close
470
+ book4 = Book.open(@simple_file1, :force => {:excel => book2.excel}, :read_only => true)
471
+ book4.should be_alive
472
+ book4.should be_a Book
473
+ book4.excel.should == book2.excel
474
+ book4.ReadOnly.should be_true
475
+ book4.should == book2
476
+ book4.close
477
+ book5 = Book.open(@simple_file1, :force => {:excel => book2}, :read_only => true)
478
+ book5.should be_alive
479
+ book5.should be_a Book
480
+ book5.excel.should == book2.excel
481
+ book5.ReadOnly.should be_true
482
+ book5.should == book2
483
+ book5.close
484
+ book3.close
485
+ end
486
+
487
+ it "should open in a given Excel, provide identity transparency, because book can be readonly, such that the old and the new book are readonly" do
488
+ book2 = Book.open(@simple_file1, :force => {:excel => :new})
489
+ book2.excel.should_not == @book.excel
490
+ book2.close
491
+ @book.close
492
+ book4 = Book.open(@simple_file1, :force => {:excel => book2}, :read_only => true)
493
+ book4.should be_alive
494
+ book4.should be_a Book
495
+ book4.excel.should == book2.excel
496
+ book4.ReadOnly.should be_true
497
+ book4.should == book2
498
+ book4.close
499
+ end
500
+
501
+ it "should raise an error if no Excel or Book is given" do
502
+ expect{
503
+ Book.open(@simple_file1, :force => {:excel => :b})
504
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
505
+ end
506
+
507
+ it "should do force_excel even if both force_ and default_excel is given" do
508
+ book2 = Book.open(@simple_file1, :default => {:excel => @book.excel}, :force => {:excel => :new})
509
+ book2.should be_alive
510
+ book2.should be_a Book
511
+ book2.excel.should_not == @book.excel
512
+ book2.should_not == @book
513
+ end
514
+
515
+ it "should do default_excel if force_excel is nil" do
516
+ book2 = Book.open(@another_simple_file, :force => {:excel => nil})
517
+ book2.should be_alive
518
+ book2.should be_a Book
519
+ book2.excel.should == @book.excel
520
+ end
521
+
522
+ it "should force_excel with :reuse" do
523
+ book2 = Book.open(@different_file, :force => {:excel => :current})
524
+ book2.should be_alive
525
+ book2.should be_a Book
526
+ book2.excel.should == @book.excel
527
+ end
528
+
529
+ it "should force_excel with :reuse even if :default_excel says sth. else" do
530
+ book2 = Book.open(@different_file, :force => {:excel => :current}, :default => {:excel => :new})
531
+ book2.should be_alive
532
+ book2.should be_a Book
533
+ book2.excel.should == @book.excel
534
+ end
535
+
536
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
537
+ excel2 = Excel.new(:reuse => false)
538
+ @book.excel.close
539
+ book2 = Book.open(@simple_file1, :force => {:excel => :current}, :default => {:excel => :new})
540
+ book2.should be_alive
541
+ book2.should be_a Book
542
+ book2.excel.should === excel2
543
+ end
544
+
545
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
546
+ book2 = Book.open(@different_file1, :force => {:excel => :new})
547
+ book2.excel.close
548
+ book3 = Book.open(@different_file1, :force => {:excel => :current}, :default => {:excel => :new})
549
+ book3.should be_alive
550
+ book3.should be_a Book
551
+ book3.excel.should == @book.excel
552
+ end
553
+
554
+ end
555
+
556
+
557
+ context "with :force_excel" do
558
+
559
+ before do
560
+ @book = Book.open(@simple_file1)
561
+ end
562
+
563
+ after do
564
+ @book.close rescue nil
565
+ end
566
+
567
+ it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
568
+ book2 = Book.open(@another_simple_file)
569
+ book3 = Book.open(@different_file)
570
+ book3 = Book.open(@simple_file1, :force_excel => book2.excel)
571
+ book3.excel.should === book2.excel
572
+ book4 = Book.open(@simple_file1, :force_excel => @book)
573
+ book4.excel.should === @book.excel
574
+ book3.close
575
+ book4.close
576
+ book5 = Book.open(@simple_file1, :force_excel => book2.ole_workbook)
577
+ book5.excel.should === book2.excel
578
+ win32ole_excel1 = WIN32OLE.connect(@book.ole_workbook.Fullname).Application
579
+ book6 = Book.open(@simple_file1, :force_excel => win32ole_excel1)
580
+ book6.excel.should === @book.excel
581
+ end
582
+
583
+
584
+ it "should open in a new Excel" do
585
+ book2 = Book.open(@simple_file1, :force_excel => :new)
586
+ book2.should be_alive
587
+ book2.should be_a Book
588
+ book2.excel.should_not == @book.excel
589
+ book2.should_not == @book
590
+ @book.Readonly.should be_false
591
+ book2.Readonly.should be_true
592
+ book2.close
593
+ end
594
+
595
+ it "should open in a given Excel, not provide identity transparency, because old book readonly, new book writable" do
596
+ book2 = Book.open(@simple_file1, :force_excel => :new)
597
+ book2.excel.should_not == @book.excel
598
+ book3 = Book.open(@simple_file1, :force_excel => :new)
599
+ book3.excel.should_not == book2.excel
600
+ book3.excel.should_not == @book.excel
601
+ book2.close
602
+ book4 = Book.open(@simple_file1, :force_excel => book2.excel)
603
+ book4.should be_alive
604
+ book4.should be_a Book
605
+ book4.excel.should == book2.excel
606
+ #book4.Readonly.should == false
607
+ book4.should_not == book2
608
+ book4.close
609
+ book5 = Book.open(@simple_file1, :force_excel => book2)
610
+ book5.should be_alive
611
+ book5.should be_a Book
612
+ book5.excel.should == book2.excel
613
+ #book5.Readonly.should == false
614
+ book5.should_not == book2
615
+ book5.close
616
+ book3.close
617
+ end
618
+
619
+ it "should open in a given Excel, provide identity transparency, because book can be readonly, such that the old and the new book are readonly" do
620
+ book2 = Book.open(@simple_file1, :force_excel => :new)
621
+ book2.excel.should_not == @book.excel
622
+ book3 = Book.open(@simple_file1, :force_excel => :new)
623
+ book3.excel.should_not == book2.excel
624
+ book3.excel.should_not == @book.excel
625
+ book2.close
626
+ book3.close
627
+ @book.close
628
+ book4 = Book.open(@simple_file1, :force_excel => book2.excel, :read_only => true)
629
+ book4.should be_alive
630
+ book4.should be_a Book
631
+ book4.excel.should == book2.excel
632
+ book4.ReadOnly.should be_true
633
+ book4.should == book2
634
+ book4.close
635
+ book5 = Book.open(@simple_file1, :force_excel => book2, :read_only => true)
636
+ book5.should be_alive
637
+ book5.should be_a Book
638
+ book5.excel.should == book2.excel
639
+ book5.ReadOnly.should be_true
640
+ book5.should == book2
641
+ book5.close
642
+ book3.close
643
+ end
644
+
645
+ it "should open in a given Excel, provide identity transparency, because book can be readonly, such that the old and the new book are readonly" do
646
+ book2 = Book.open(@simple_file1, :force_excel => :new)
647
+ book2.excel.should_not == @book.excel
648
+ book2.close
649
+ @book.close
650
+ book4 = Book.open(@simple_file1, :force_excel => book2, :read_only => true)
651
+ book4.should be_alive
652
+ book4.should be_a Book
653
+ book4.excel.should == book2.excel
654
+ book4.ReadOnly.should be_true
655
+ book4.should == book2
656
+ book4.close
657
+ end
658
+
659
+ it "should raise an error if no Excel or Book is given" do
660
+ expect{
661
+ Book.open(@simple_file1, :force_excel => :b)
662
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
663
+ end
664
+
665
+ it "should do force_excel even if both force_ and default_excel is given" do
666
+ book2 = Book.open(@simple_file1, :default_excel => @book.excel, :force_excel => :new)
667
+ book2.should be_alive
668
+ book2.should be_a Book
669
+ book2.excel.should_not == @book.excel
670
+ book2.should_not == @book
671
+ end
672
+
673
+ it "should do default_excel if force_excel is nil" do
674
+ book2 = Book.open(@another_simple_file, :force_excel => nil)
675
+ book2.should be_alive
676
+ book2.should be_a Book
677
+ book2.excel.should == @book.excel
678
+ end
679
+
680
+ it "should force_excel with :reuse" do
681
+ book2 = Book.open(@different_file, :force_excel => :current)
682
+ book2.should be_alive
683
+ book2.should be_a Book
684
+ book2.excel.should == @book.excel
685
+ end
686
+
687
+ it "should force_excel with :reuse even if :default_excel says sth. else" do
688
+ book2 = Book.open(@different_file, :force_excel => :current, :default_excel => :new)
689
+ book2.should be_alive
690
+ book2.should be_a Book
691
+ book2.excel.should == @book.excel
692
+ end
693
+
694
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
695
+ excel2 = Excel.new(:reuse => false)
696
+ @book.excel.close
697
+ book2 = Book.open(@simple_file1, :force_excel => :current, :default_excel => :new)
698
+ book2.should be_alive
699
+ book2.should be_a Book
700
+ book2.excel.should === excel2
701
+ end
702
+
703
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
704
+ book2 = Book.open(@different_file1, :force_excel => :new)
705
+ book2.excel.close
706
+ book3 = Book.open(@different_file1, :force_excel => :current, :default_excel => :new)
707
+ book3.should be_alive
708
+ book3.should be_a Book
709
+ book3.excel.should == @book.excel
710
+ end
711
+
712
+ end
713
+
714
+ context "with :default => {:excel}" do
715
+
716
+ before do
717
+ @book = Book.open(@simple_file1, :visible => true)
718
+ end
719
+
720
+ after do
721
+ @book.close rescue nil
722
+ end
723
+
724
+ it "should use the open book" do
725
+ book2 = Book.open(@simple_file1, :default => {:excel => :current})
726
+ book2.excel.should == @book.excel
727
+ book2.should be_alive
728
+ book2.should be_a Book
729
+ book2.should == @book
730
+ book2.close
731
+ end
732
+
733
+ it "should reopen the book in the excel instance where it was opened before" do
734
+ excel = Excel.new(:reuse => false)
735
+ @book.close
736
+ book2 = Book.open(@simple_file1)
737
+ book2.should be_alive
738
+ book2.should be_a Book
739
+ book2.excel.should == @book.excel
740
+ book2.excel.should_not == excel
741
+ book2.filename.should == @book.filename
742
+ @book.should be_alive
743
+ book2.should == @book
744
+ book2.close
745
+ end
746
+
747
+ it "should reopen a book in a new Excel if all Excel instances are closed" do
748
+ excel = Excel.new(:reuse => false)
749
+ excel2 = @book.excel
750
+ fn = @book.filename
751
+ @book.close
752
+ Excel.close_all
753
+ book2 = Book.open(@simple_file1, :default => {:excel => :current})
754
+ book2.should be_alive
755
+ book2.should be_a Book
756
+ book2.filename.should == fn
757
+ @book.should be_alive
758
+ book2.should == @book
759
+ book2.close
760
+ end
761
+
762
+ it "should reopen a book in the first opened Excel if the old Excel is closed" do
763
+ excel = @book.excel
764
+ Excel.close_all
765
+ new_excel = Excel.new(:reuse => false)
766
+ new_excel2 = Excel.new(:reuse => false)
767
+ book2 = Book.open(@simple_file1, :default => {:excel => :current})
768
+ book2.should be_alive
769
+ book2.should be_a Book
770
+ book2.excel.should_not == excel
771
+ book2.excel.should_not == new_excel2
772
+ book2.excel.should == new_excel
773
+ @book.should be_alive
774
+ book2.should == @book
775
+ book2.close
776
+ end
777
+
778
+ it "should reopen a book in the first opened excel, if the book cannot be reopened" do
779
+ @book.close
780
+ Excel.close_all
781
+ excel1 = Excel.new(:reuse => false)
782
+ excel2 = Excel.new(:reuse => false)
783
+ book2 = Book.open(@different_file, :default => {:excel => :current})
784
+ book2.should be_alive
785
+ book2.should be_a Book
786
+ book2.excel.should == excel1
787
+ book2.excel.should_not == excel2
788
+ book2.close
789
+ end
790
+
791
+ it "should reopen the book in the Excel where it was opened most recently" do
792
+ excel1 = @book.excel
793
+ excel2 = Excel.new(:reuse => false)
794
+ @book.close
795
+ book2 = Book.open(@simple_file1, :default => {:excel => :current})
796
+ book2.excel.should == excel1
797
+ book2.close
798
+ book3 = Book.open(@simple_file1, :force => {:excel => excel2})
799
+ book3.close
800
+ book3 = Book.open(@simple_file1, :default => {:excel => :current})
801
+ book3.excel.should == excel2
802
+ book3.close
803
+ end
804
+
805
+ it "should reopen a book in the excel instance where it was opened most recently" do
806
+ book2 = Book.open(@simple_file, :force => {:excel => :new})
807
+ @book.close
808
+ book2.close
809
+ book3 = Book.open(@simple_file1)
810
+ book2.should be_alive
811
+ book2.should be_a Book
812
+ book3.excel.should == book2.excel
813
+ book3.excel.should_not == @book.excel
814
+ book3.should == book2
815
+ book3.should_not == @book
816
+ end
817
+
818
+ it "should open the book in a new excel if the book was not opened before" do
819
+ book2 = Book.open(@different_file, :default => {:excel => :current})
820
+ book2.excel.should == @book.excel
821
+ book3 = Book.open(@another_simple_file, :default => {:excel => :new})
822
+ book3.excel.should_not == @book.excel
823
+ end
824
+
825
+ it "should open the book in a new excel if the book was opened before but the excel has been closed" do
826
+ excel = @book.excel
827
+ excel2 = Excel.new(:reuse => false)
828
+ excel.close
829
+ book2 = Book.open(@simple_file1, :default => {:excel => :new})
830
+ book2.excel.should_not == excel2
831
+ book2.close
832
+ end
833
+
834
+ it "should open the book in a given excel if the book was not opened before" do
835
+ book2 = Book.open(@different_file, :default => {:excel => :current})
836
+ book2.excel.should == @book.excel
837
+ excel = Excel.new(:reuse => false)
838
+ book3 = Book.open(@another_simple_file, :default => {:excel => excel})
839
+ book3.excel.should == excel
840
+ end
841
+
842
+ it "should open the book in a given excel if the book was opened before but the excel has been closed" do
843
+ excel2 = Excel.new(:reuse => false, :visible => true)
844
+ @book.excel.close
845
+ book2 = Book.open(@simple_file1, :default => {:excel => excel2, :visible => true})
846
+ book2.excel.should == excel2
847
+ end
848
+
849
+ it "should open a new excel, if the book cannot be reopened" do
850
+ @book.close
851
+ new_excel = Excel.new(:reuse => false)
852
+ book2 = Book.open(@different_file, :default => {:excel => :new})
853
+ book2.should be_alive
854
+ book2.should be_a Book
855
+ book2.excel.should_not == new_excel
856
+ book2.excel.should_not == @book.excel
857
+ book2.close
858
+ end
859
+
860
+ it "should open a given excel, if the book cannot be reopened" do
861
+ @book.close
862
+ new_excel = Excel.new(:reuse => false)
863
+ book2 = Book.open(@different_file, :default => {:excel => @book.excel})
864
+ book2.should be_alive
865
+ book2.should be_a Book
866
+ book2.excel.should_not == new_excel
867
+ book2.excel.should == @book.excel
868
+ book2.close
869
+ end
870
+
871
+ it "should open a given excel, if the book cannot be reopened" do
872
+ @book.close
873
+ new_excel = Excel.new(:reuse => false)
874
+ book2 = Book.open(@different_file, :default => {:excel => @book})
875
+ book2.should be_alive
876
+ book2.should be_a Book
877
+ book2.excel.should_not == new_excel
878
+ book2.excel.should == @book.excel
879
+ book2.close
880
+ end
881
+
882
+ it "should reuse an open book by default" do
883
+ book2 = Book.open(@simple_file1)
884
+ book2.excel.should == @book.excel
885
+ book2.should == @book
886
+ end
887
+
888
+ it "should raise an error if no Excel or Book is given" do
889
+ expect{
890
+ Book.open(@different_file, :default => {:excel => :a})
891
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
892
+ end
893
+
894
+ end
895
+
896
+ context "with :default_excel" do
897
+
898
+ before do
899
+ @book = Book.open(@simple_file1, :visible => true)
900
+ end
901
+
902
+ after do
903
+ @book.close rescue nil
904
+ end
905
+
906
+ it "should use the open book" do
907
+ book2 = Book.open(@simple_file1, :default_excel => :current)
908
+ book2.excel.should == @book.excel
909
+ book2.should be_alive
910
+ book2.should be_a Book
911
+ book2.should == @book
912
+ book2.close
913
+ end
914
+
915
+ it "should reopen the book in the excel instance where it was opened before" do
916
+ excel = Excel.new(:reuse => false)
917
+ @book.close
918
+ book2 = Book.open(@simple_file1)
919
+ book2.should be_alive
920
+ book2.should be_a Book
921
+ book2.excel.should == @book.excel
922
+ book2.excel.should_not == excel
923
+ book2.filename.should == @book.filename
924
+ @book.should be_alive
925
+ book2.should == @book
926
+ book2.close
927
+ end
928
+
929
+ it "should reopen a book in a new Excel if all Excel instances are closed" do
930
+ excel = Excel.new(:reuse => false)
931
+ excel2 = @book.excel
932
+ fn = @book.filename
933
+ @book.close
934
+ Excel.close_all
935
+ book2 = Book.open(@simple_file1, :default_excel => :current)
936
+ book2.should be_alive
937
+ book2.should be_a Book
938
+ book2.filename.should == fn
939
+ @book.should be_alive
940
+ book2.should == @book
941
+ book2.close
942
+ end
943
+
944
+ it "should reopen a book in the first opened Excel if the old Excel is closed" do
945
+ excel = @book.excel
946
+ Excel.close_all
947
+ new_excel = Excel.new(:reuse => false)
948
+ new_excel2 = Excel.new(:reuse => false)
949
+ book2 = Book.open(@simple_file1, :default_excel => :current)
950
+ book2.should be_alive
951
+ book2.should be_a Book
952
+ book2.excel.should_not == excel
953
+ book2.excel.should_not == new_excel2
954
+ book2.excel.should == new_excel
955
+ @book.should be_alive
956
+ book2.should == @book
957
+ book2.close
958
+ end
959
+
960
+ it "should reopen a book in the first opened excel, if the book cannot be reopened" do
961
+ @book.close
962
+ Excel.close_all
963
+ excel1 = Excel.new(:reuse => false)
964
+ excel2 = Excel.new(:reuse => false)
965
+ book2 = Book.open(@different_file, :default_excel => :current)
966
+ book2.should be_alive
967
+ book2.should be_a Book
968
+ book2.excel.should == excel1
969
+ book2.excel.should_not == excel2
970
+ book2.close
971
+ end
972
+
973
+ it "should reopen the book in the Excel where it was opened most recently" do
974
+ excel1 = @book.excel
975
+ excel2 = Excel.new(:reuse => false)
976
+ @book.close
977
+ book2 = Book.open(@simple_file1, :default_excel => :current)
978
+ book2.excel.should == excel1
979
+ book2.close
980
+ book3 = Book.open(@simple_file1, :force_excel => excel2)
981
+ book3.close
982
+ book3 = Book.open(@simple_file1, :default_excel => :current)
983
+ book3.excel.should == excel2
984
+ book3.close
985
+ end
986
+
987
+ it "should reopen a book in the excel instance where it was opened most recently" do
988
+ book2 = Book.open(@simple_file, :force_excel => :new)
989
+ @book.close
990
+ book2.close
991
+ book3 = Book.open(@simple_file1)
992
+ book2.should be_alive
993
+ book2.should be_a Book
994
+ book3.excel.should == book2.excel
995
+ book3.excel.should_not == @book.excel
996
+ book3.should == book2
997
+ book3.should_not == @book
998
+ end
999
+
1000
+ it "should open the book in a new excel if the book was not opened before" do
1001
+ book2 = Book.open(@different_file, :default_excel => :current)
1002
+ book2.excel.should == @book.excel
1003
+ book3 = Book.open(@another_simple_file, :default_excel => :new)
1004
+ book3.excel.should_not == @book.excel
1005
+ end
1006
+
1007
+ it "should open the book in a new excel if the book was opened before but the excel has been closed" do
1008
+ excel = @book.excel
1009
+ excel2 = Excel.new(:reuse => false)
1010
+ excel.close
1011
+ book2 = Book.open(@simple_file1, :default_excel => :new)
1012
+ book2.excel.should_not == excel2
1013
+ book2.close
1014
+ end
1015
+
1016
+ it "should open the book in a given excel if the book was not opened before" do
1017
+ book2 = Book.open(@different_file, :default_excel => :current)
1018
+ book2.excel.should == @book.excel
1019
+ excel = Excel.new(:reuse => false)
1020
+ book3 = Book.open(@another_simple_file, :default_excel => excel)
1021
+ book3.excel.should == excel
1022
+ end
1023
+
1024
+ it "should open the book in a given excel if the book was opened before but the excel has been closed" do
1025
+ excel2 = Excel.new(:reuse => false, :visible => true)
1026
+ @book.excel.close
1027
+ book2 = Book.open(@simple_file1, :visible => true, :default_excel => excel2)
1028
+ book2.excel.should == excel2
1029
+ end
1030
+
1031
+ it "should open a new excel, if the book cannot be reopened" do
1032
+ @book.close
1033
+ new_excel = Excel.new(:reuse => false)
1034
+ book2 = Book.open(@different_file, :default_excel => :new)
1035
+ book2.should be_alive
1036
+ book2.should be_a Book
1037
+ book2.excel.should_not == new_excel
1038
+ book2.excel.should_not == @book.excel
1039
+ book2.close
1040
+ end
1041
+
1042
+ it "should open a given excel, if the book cannot be reopened" do
1043
+ @book.close
1044
+ new_excel = Excel.new(:reuse => false)
1045
+ book2 = Book.open(@different_file, :default_excel => @book.excel)
1046
+ book2.should be_alive
1047
+ book2.should be_a Book
1048
+ book2.excel.should_not == new_excel
1049
+ book2.excel.should == @book.excel
1050
+ book2.close
1051
+ end
1052
+
1053
+ it "should open a given excel, if the book cannot be reopened" do
1054
+ @book.close
1055
+ new_excel = Excel.new(:reuse => false)
1056
+ book2 = Book.open(@different_file, :default_excel => @book)
1057
+ book2.should be_alive
1058
+ book2.should be_a Book
1059
+ book2.excel.should_not == new_excel
1060
+ book2.excel.should == @book.excel
1061
+ book2.close
1062
+ end
1063
+
1064
+ it "should reuse an open book by default" do
1065
+ book2 = Book.open(@simple_file1)
1066
+ book2.excel.should == @book.excel
1067
+ book2.should == @book
1068
+ end
1069
+
1070
+ it "should raise an error if no Excel or Book is given" do
1071
+ expect{
1072
+ Book.open(@different_file, :default_excel => :a)
1073
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
1074
+ end
1075
+
1076
+ end
1077
+
1078
+ context "with :active instead of :current" do
1079
+
1080
+ before do
1081
+ @book = Book.open(@simple_file1)
1082
+ end
1083
+
1084
+ after do
1085
+ @book.close rescue nil
1086
+ end
1087
+
1088
+ it "should force_excel with :active" do
1089
+ book2 = Book.open(@different_file, :force => {:excel => :active})
1090
+ book2.should be_alive
1091
+ book2.should be_a Book
1092
+ book2.excel.should == @book.excel
1093
+ end
1094
+
1095
+ it "should force_excel with :reuse even if :default_excel says sth. else" do
1096
+ book2 = Book.open(@different_file, :force => {:excel => :active}, :default => {:excel => :new})
1097
+ book2.should be_alive
1098
+ book2.should be_a Book
1099
+ book2.excel.should == @book.excel
1100
+ end
1101
+
1102
+ it "should open force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
1103
+ excel2 = Excel.new(:reuse => false)
1104
+ @book.excel.close
1105
+ book2 = Book.open(@simple_file1, :force => {:excel => :active}, :default => {:excel => :new})
1106
+ book2.should be_alive
1107
+ book2.should be_a Book
1108
+ book2.excel.should === excel2
1109
+ end
1110
+
1111
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
1112
+ book2 = Book.open(@different_file1, :force => {:excel => :new})
1113
+ book2.excel.close
1114
+ book3 = Book.open(@different_file1, :force => {:excel => :active}, :default => {:excel => :new})
1115
+ book3.should be_alive
1116
+ book3.should be_a Book
1117
+ book3.excel.should == @book.excel
1118
+ end
1119
+
1120
+ it "should use the open book" do
1121
+ book2 = Book.open(@simple_file1, :default => {:excel => :active})
1122
+ book2.excel.should == @book.excel
1123
+ book2.should be_alive
1124
+ book2.should be_a Book
1125
+ book2.should == @book
1126
+ book2.close
1127
+ end
1128
+
1129
+ it "should reopen a book in a new Excel if all Excel instances are closed" do
1130
+ excel = Excel.new(:reuse => false)
1131
+ excel2 = @book.excel
1132
+ fn = @book.filename
1133
+ @book.close
1134
+ Excel.close_all
1135
+ book2 = Book.open(@simple_file1, :default => {:excel => :active})
1136
+ book2.should be_alive
1137
+ book2.should be_a Book
1138
+ book2.filename.should == fn
1139
+ @book.should be_alive
1140
+ book2.should == @book
1141
+ book2.close
1142
+ end
1143
+
1144
+ it "should reopen a book in the first opened Excel if the old Excel is closed" do
1145
+ excel = @book.excel
1146
+ Excel.close_all
1147
+ new_excel = Excel.new(:reuse => false)
1148
+ new_excel2 = Excel.new(:reuse => false)
1149
+ book2 = Book.open(@simple_file1, :default => {:excel => :active})
1150
+ book2.should be_alive
1151
+ book2.should be_a Book
1152
+ book2.excel.should_not == excel
1153
+ book2.excel.should_not == new_excel2
1154
+ book2.excel.should == new_excel
1155
+ @book.should be_alive
1156
+ book2.should == @book
1157
+ book2.close
1158
+ end
1159
+
1160
+ it "should reopen a book in the first opened excel, if the book cannot be reopened" do
1161
+ @book.close
1162
+ Excel.close_all
1163
+ excel1 = Excel.new(:reuse => false)
1164
+ excel2 = Excel.new(:reuse => false)
1165
+ book2 = Book.open(@different_file, :default => {:excel => :active})
1166
+ book2.should be_alive
1167
+ book2.should be_a Book
1168
+ book2.excel.should == excel1
1169
+ book2.excel.should_not == excel2
1170
+ book2.close
1171
+ end
1172
+
1173
+ it "should reopen the book in the Excel where it was opened most recently" do
1174
+ excel1 = @book.excel
1175
+ excel2 = Excel.new(:reuse => false)
1176
+ @book.close
1177
+ book2 = Book.open(@simple_file1, :default => {:excel => :active})
1178
+ book2.excel.should == excel1
1179
+ book2.close
1180
+ book3 = Book.open(@simple_file1, :force => {:excel => excel2})
1181
+ book3.close
1182
+ book3 = Book.open(@simple_file1, :default => {:excel => :active})
1183
+ book3.excel.should == excel2
1184
+ book3.close
1185
+ end
1186
+
1187
+ end
1188
+
1189
+ context "with :reuse instead of :current" do
1190
+
1191
+ before do
1192
+ @book = Book.open(@simple_file1)
1193
+ end
1194
+
1195
+ after do
1196
+ @book.close rescue nil
1197
+ end
1198
+
1199
+ it "should force_excel with :reuse" do
1200
+ book2 = Book.open(@different_file, :force => {:excel => :reuse})
1201
+ book2.should be_alive
1202
+ book2.should be_a Book
1203
+ book2.excel.should == @book.excel
1204
+ end
1205
+
1206
+ it "should force_excel with :reuse even if :default_excel says sth. else" do
1207
+ book2 = Book.open(@different_file, :force => {:excel => :reuse}, :default => {:excel => :new})
1208
+ book2.should be_alive
1209
+ book2.should be_a Book
1210
+ book2.excel.should == @book.excel
1211
+ end
1212
+
1213
+ it "should open force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
1214
+ excel2 = Excel.new(:reuse => false)
1215
+ @book.excel.close
1216
+ book2 = Book.open(@simple_file1, :force => {:excel => :reuse}, :default => {:excel => :new})
1217
+ book2.should be_alive
1218
+ book2.should be_a Book
1219
+ book2.excel.should === excel2
1220
+ end
1221
+
1222
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
1223
+ book2 = Book.open(@different_file1, :force => {:excel => :new})
1224
+ book2.excel.close
1225
+ book3 = Book.open(@different_file1, :force => {:excel => :reuse}, :default => {:excel => :new})
1226
+ book3.should be_alive
1227
+ book3.should be_a Book
1228
+ book3.excel.should == @book.excel
1229
+ end
1230
+
1231
+ it "should use the open book" do
1232
+ book2 = Book.open(@simple_file1, :default => {:excel => :reuse})
1233
+ book2.excel.should == @book.excel
1234
+ book2.should be_alive
1235
+ book2.should be_a Book
1236
+ book2.should == @book
1237
+ book2.close
1238
+ end
1239
+
1240
+ it "should reopen a book in a new Excel if all Excel instances are closed" do
1241
+ excel = Excel.new(:reuse => false)
1242
+ excel2 = @book.excel
1243
+ fn = @book.filename
1244
+ @book.close
1245
+ Excel.close_all
1246
+ book2 = Book.open(@simple_file1, :default => {:excel => :reuse})
1247
+ book2.should be_alive
1248
+ book2.should be_a Book
1249
+ book2.filename.should == fn
1250
+ @book.should be_alive
1251
+ book2.should == @book
1252
+ book2.close
1253
+ end
1254
+
1255
+ it "should reopen a book in the first opened Excel if the old Excel is closed" do
1256
+ excel = @book.excel
1257
+ Excel.close_all
1258
+ new_excel = Excel.new(:reuse => false)
1259
+ new_excel2 = Excel.new(:reuse => false)
1260
+ book2 = Book.open(@simple_file1, :default => {:excel => :reuse})
1261
+ book2.should be_alive
1262
+ book2.should be_a Book
1263
+ book2.excel.should_not == excel
1264
+ book2.excel.should_not == new_excel2
1265
+ book2.excel.should == new_excel
1266
+ @book.should be_alive
1267
+ book2.should == @book
1268
+ book2.close
1269
+ end
1270
+
1271
+ it "should reopen a book in the first opened excel, if the book cannot be reopened" do
1272
+ @book.close
1273
+ Excel.close_all
1274
+ excel1 = Excel.new(:reuse => false)
1275
+ excel2 = Excel.new(:reuse => false)
1276
+ book2 = Book.open(@different_file, :default => {:excel => :reuse})
1277
+ book2.should be_alive
1278
+ book2.should be_a Book
1279
+ book2.excel.should == excel1
1280
+ book2.excel.should_not == excel2
1281
+ book2.close
1282
+ end
1283
+
1284
+ it "should reopen the book in the Excel where it was opened most recently" do
1285
+ excel1 = @book.excel
1286
+ excel2 = Excel.new(:reuse => false)
1287
+ @book.close
1288
+ book2 = Book.open(@simple_file1, :default => {:excel => :reuse})
1289
+ book2.excel.should == excel1
1290
+ book2.close
1291
+ book3 = Book.open(@simple_file1, :force => {:excel => excel2})
1292
+ book3.close
1293
+ book3 = Book.open(@simple_file1, :default => {:excel => :reuse})
1294
+ book3.excel.should == excel2
1295
+ book3.close
1296
+ end
1297
+
1298
+ end
1299
+
1300
+ it "should new_excel" do
1301
+ book = Book.open(@simple_file1)
1302
+ puts "book:#{book.inspect}"
1303
+ book.sheet(1)[1,1].Value = "f"
1304
+ book.Saved.should be_false
1305
+ book2 = Book.open(@simple_file1, :if_unsaved => :new_excel)
1306
+ puts "book2: #{book2.inspect}"
1307
+ end
1308
+
1309
+ context "with :if_unsaved" do
1310
+
1311
+ before do
1312
+ @book = Book.open(@simple_file1)
1313
+ sheet = @book.sheet(1)
1314
+ #@book.add_sheet(@sheet, :as => 'a_name')
1315
+ @old_value = sheet[1,1].Value
1316
+ sheet[1,1] = (sheet[1,1].value == "foo" ? "bar" : "foo")
1317
+ @new_value = sheet[1,1].Value
1318
+ @book.Saved.should be_false
1319
+ end
1320
+
1321
+ after do
1322
+ @book.close(:if_unsaved => :forget)
1323
+ end
1324
+
1325
+ it "should open the book in a new excel instance, if :if_unsaved is :new_excel" do
1326
+ new_book = Book.open(@simple_file1, :if_unsaved => :new_excel)
1327
+ new_book.excel.should_not == @book.excel
1328
+ @book.should be_alive
1329
+ @book.Saved.should be_false
1330
+ new_book.should be_alive
1331
+ new_book.Saved.should be_true
1332
+ new_book.sheet(1)[1,1].Value.should == @old_value
1333
+ #new_book.filename.should == @book.filename
1334
+ new_book.excel.should_not == @book.excel
1335
+ new_book.close
1336
+ end
1337
+
1338
+ it "should raise an error, if :if_unsaved is :raise" do
1339
+ expect {
1340
+ new_book = Book.open(@simple_file1, :if_unsaved => :raise)
1341
+ }.to raise_error(WorkbookNotSaved, /workbook is already open but not saved: "workbook.xls"/)
1342
+ end
1343
+
1344
+ it "should let the book open, if :if_unsaved is :accept" do
1345
+ new_book = Book.open(@simple_file1, :if_unsaved => :accept)
1346
+ @book.should be_alive
1347
+ new_book.should be_alive
1348
+ new_book.Saved.should be_false
1349
+ @book.Saved.should be_false
1350
+ new_book.sheet(1)[1,1].Value.should == @new_value
1351
+ new_book.should == @book
1352
+ end
1353
+
1354
+ it "should open book and close old book, if :if_unsaved is :forget" do
1355
+ new_book = Book.open(@simple_file1, :if_unsaved => :forget)
1356
+ @book.should_not be_alive
1357
+ new_book.should be_alive
1358
+ new_book.Saved.should be_true
1359
+ new_book.sheet(1)[1,1].Value.should == @old_value
1360
+ #@new_book.filename.downcase.should == @simple_file.downcase
1361
+ end
1362
+
1363
+ context "with :if_unsaved => :alert" do
1364
+ before do
1365
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
1366
+ end
1367
+
1368
+ after do
1369
+ @key_sender.close
1370
+ end
1371
+
1372
+ it "should open the new book and close the unsaved book, if user answers 'yes'" do
1373
+ # "Yes" is the default. --> language independent
1374
+ @key_sender.puts "{enter}"
1375
+ new_book = Book.open(@simple_file1, :if_unsaved => :alert)
1376
+ new_book.should be_alive
1377
+ #@book.should_not be_alive
1378
+ #new_book.filename.downcase.should == @simple_file.downcase
1379
+ new_book.Saved.should be_true
1380
+ new_book.sheet(1)[1,1].Value.should == @old_value
1381
+ end
1382
+ end
1383
+ =begin
1384
+ # only for Excel2007:
1385
+ it "should not open the new book and not close the unsaved book, if user answers 'no'" do
1386
+ # "No" is right to "Yes" (the default). --> language independent
1387
+ # strangely, in the "no" case, the question will sometimes be repeated three times
1388
+ #@book.excel.Visible = true
1389
+ @key_sender.puts "{right}{enter}"
1390
+ @key_sender.puts "{right}{enter}"
1391
+ @key_sender.puts "{right}{enter}"
1392
+ expect{
1393
+ Book.open(@simple_file1, :if_unsaved => :alert)
1394
+ }.to raise_error(ExcelREOError, "user canceled or runtime error")
1395
+ @book.should be_alive
1396
+ end
1397
+
1398
+ =end
1399
+
1400
+ context "with :if_unsaved => :excel" do
1401
+ before do
1402
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
1403
+ end
1404
+
1405
+ after do
1406
+ @key_sender.close
1407
+ end
1408
+
1409
+ it "should open the new book and close the unsaved book, if user answers 'yes'" do
1410
+ # "Yes" is the default. --> language independent
1411
+ @key_sender.puts "{enter}"
1412
+ new_book = Book.open(@simple_file1, :if_unsaved => :excel)
1413
+ new_book.should be_alive
1414
+ #@book.should_not be_alive
1415
+ new_book.Saved.should be_true
1416
+ new_book.sheet(1)[1,1].Value.should == @old_value
1417
+ end
1418
+
1419
+ =begin
1420
+ it "should not open the new book and not close the unsaved book, if user answers 'no'" do
1421
+ # "No" is right to "Yes" (the default). --> language independent
1422
+ # strangely, in the "no" case, the question will sometimes be repeated three times
1423
+ #@book.excel.Visible = true
1424
+ @key_sender.puts "{right}{enter}"
1425
+ @key_sender.puts "{right}{enter}"
1426
+ @key_sender.puts "{right}{enter}"
1427
+ expect{
1428
+ Book.open(@simple_file1, :if_unsaved => :excel)
1429
+ }.to raise_error(ExcelREOError, "user canceled or runtime error")
1430
+ @book.should be_alive
1431
+ end
1432
+ =end
1433
+
1434
+ end
1435
+
1436
+ it "should raise an error, if :if_unsaved is default" do
1437
+ expect {
1438
+ new_book = Book.open(@simple_file1, :if_unsaved => :raise)
1439
+ }.to raise_error(WorkbookNotSaved, /workbook is already open but not saved: "workbook.xls"/)
1440
+ end
1441
+
1442
+ it "should raise an error, if :if_unsaved is invalid option" do
1443
+ expect {
1444
+ new_book = Book.open(@simple_file1, :if_unsaved => :invalid_option)
1445
+ }.to raise_error(OptionInvalid, ":if_unsaved: invalid option: :invalid_option")
1446
+ end
1447
+ end
1448
+
1449
+ context "with :if_obstructed" do
1450
+
1451
+ for i in 1..2 do
1452
+
1453
+ context "with and without reopen" do
1454
+
1455
+ before do
1456
+ if i == 1 then
1457
+ book_before = Book.open(@simple_file1)
1458
+ book_before.close
1459
+ end
1460
+ @book = Book.open(@simple_file_other_path1)
1461
+ #@book.Windows(@book.Name).Visible = true
1462
+ #@sheet_count = @book.ole_workbook.Worksheets.Count
1463
+ sheet = @book.sheet(1)
1464
+ #@book.add_sheet(@sheet, :as => 'a_name')
1465
+ @old_value = sheet[1,1].Value
1466
+ sheet[1,1] = (sheet[1,1].value == "foo" ? "bar" : "foo")
1467
+ @new_value = sheet[1,1].Value
1468
+ @book.Saved.should be_false
1469
+ end
1470
+
1471
+ after do
1472
+ @book.close(:if_unsaved => :forget)
1473
+ #@new_book.close rescue nil
1474
+ end
1475
+
1476
+ it "should raise an error, if :if_obstructed is :raise" do
1477
+ expect {
1478
+ new_book = Book.open(@simple_file1, :if_obstructed => :raise)
1479
+ }.to raise_error(WorkbookBlocked, /blocked by a workbook with the same name in a different path/)
1480
+ end
1481
+
1482
+ it "should close the other book and open the new book, if :if_obstructed is :forget" do
1483
+ new_book = Book.open(@simple_file1, :if_obstructed => :forget)
1484
+ @book.should_not be_alive
1485
+ new_book.should be_alive
1486
+ new_book.filename.downcase.should == @simple_file.downcase
1487
+ old_book = Book.open(@simple_file_other_path1, :if_obstructed => :forget)
1488
+ old_book.sheet(1)[1,1].Value.should == @old_value
1489
+ end
1490
+
1491
+ it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
1492
+ new_book = Book.open(@simple_file1, :if_obstructed => :save)
1493
+ @book.should_not be_alive
1494
+ new_book.should be_alive
1495
+ new_book.filename.downcase.should == @simple_file1.downcase
1496
+ old_book = Book.open(@simple_file_other_path1, :if_obstructed => :forget)
1497
+ old_book.sheet(1)[1,1].Value.should == @new_value
1498
+ #old_book.ole_workbook.Worksheets.Count.should == @sheet_count + 1
1499
+ old_book.close
1500
+ end
1501
+
1502
+ it "should raise an error, if the old book is unsaved, and close the old book and open the new book,
1503
+ if :if_obstructed is :close_if_saved" do
1504
+ expect{
1505
+ new_book = Book.open(@simple_file1, :if_obstructed => :close_if_saved)
1506
+ }.to raise_error(WorkbookBlocked, /workbook with the same name in a different path is unsaved/)
1507
+ @book.save
1508
+ new_book = Book.open(@simple_file1, :if_obstructed => :close_if_saved)
1509
+ @book.should_not be_alive
1510
+ new_book.should be_alive
1511
+ new_book.filename.downcase.should == @simple_file1.downcase
1512
+ old_book = Book.open(@simple_file_other_path1, :if_obstructed => :forget)
1513
+ old_book.sheet(1)[1,1].Value.should == @new_value
1514
+ #old_book.ole_workbook.Worksheets.Count.should == @sheet_count + 1
1515
+ old_book.close
1516
+ end
1517
+
1518
+ it "should close the old book and open the new book, if :if_obstructed is :close_if_saved" do
1519
+ @book.close(:if_unsaved => :forget)
1520
+ book = Book.open(@simple_file_other_path)
1521
+ book2 = Book.open(@simple_file1, :if_obstructed => :close_if_saved)
1522
+ end
1523
+
1524
+ it "should open the book in a new excel instance, if :if_obstructed is :new_excel" do
1525
+ new_book = Book.open(@simple_file1, :if_obstructed => :new_excel)
1526
+ @book.should be_alive
1527
+ @book.Saved.should be_false
1528
+ @book.sheet(1)[1,1].Value.should == @new_value
1529
+ new_book.should be_alive
1530
+ new_book.filename.should_not == @book.filename
1531
+ new_book.excel.should_not == @book.excel
1532
+ new_book.sheet(1)[1,1].Value.should == @old_value
1533
+ end
1534
+
1535
+ it "should raise an error, if :if_obstructed is default" do
1536
+ expect {
1537
+ new_book = Book.open(@simple_file1)
1538
+ }.to raise_error(WorkbookBlocked, /blocked by a workbook with the same name in a different path/)
1539
+ end
1540
+
1541
+ it "should raise an error, if :if_obstructed is invalid option" do
1542
+ expect {
1543
+ new_book = Book.open(@simple_file1, :if_obstructed => :invalid_option)
1544
+ }.to raise_error(OptionInvalid, ":if_obstructed: invalid option: :invalid_option")
1545
+ end
1546
+ end
1547
+ end
1548
+ end
1549
+
1550
+ context "with an already saved book" do
1551
+ before do
1552
+ @book = Book.open(@simple_file)
1553
+ end
1554
+
1555
+ after do
1556
+ @book.close
1557
+ end
1558
+
1559
+ possible_options = [:read_only, :raise, :accept, :forget, nil]
1560
+ possible_options.each do |options_value|
1561
+ context "with :if_unsaved => #{options_value} and in the same and different path" do
1562
+ before do
1563
+ @new_book = Book.open(@simple_file1, :reuse => true, :if_unsaved => options_value)
1564
+ @different_book = Book.open(@different_file, :reuse => true, :if_unsaved => options_value)
1565
+ end
1566
+ after do
1567
+ @new_book.close
1568
+ @different_book.close
1569
+ end
1570
+ it "should open without problems " do
1571
+ @new_book.should be_a Book
1572
+ @different_book.should be_a Book
1573
+ end
1574
+ it "should belong to the same Excel instance" do
1575
+ @new_book.excel.should == @book.excel
1576
+ @different_book.excel.should == @book.excel
1577
+ end
1578
+ end
1579
+ end
1580
+ end
1581
+
1582
+ context "with non-existing file" do
1583
+
1584
+ it "should raise error if filename is nil" do
1585
+ expect{
1586
+ Book.open(@nonexisting)
1587
+ }.to raise_error(FileNameNotGiven, "filename is nil")
1588
+ end
1589
+
1590
+ it "should raise error if file is a directory" do
1591
+ expect{
1592
+ Book.open(@dir)
1593
+ }.to raise_error(FileNotFound, "file #{General::absolute_path(@dir).gsub("/","\\").inspect} is a directory")
1594
+ end
1595
+
1596
+ it "should raise error if file does not exist" do
1597
+ File.delete @simple_save_file rescue nil
1598
+ expect {
1599
+ Book.open(@simple_save_file, :if_absent => :raise)
1600
+ }.to raise_error(FileNotFound, "file #{General::absolute_path(@simple_save_file).gsub("/","\\").inspect} not found")
1601
+ end
1602
+
1603
+ it "should create a workbook" do
1604
+ File.delete @simple_save_file rescue nil
1605
+ book = Book.open(@simple_save_file, :if_absent => :create)
1606
+ book.should be_a Book
1607
+ book.close
1608
+ File.exist?(@simple_save_file).should be_true
1609
+ end
1610
+
1611
+ it "should raise an exception by default" do
1612
+ File.delete @simple_save_file rescue nil
1613
+ expect {
1614
+ Book.open(@simple_save_file)
1615
+ }.to raise_error(FileNotFound, "file #{General::absolute_path(@simple_save_file).gsub("/","\\").inspect} not found")
1616
+ end
1617
+
1618
+ end
1619
+
1620
+ context "with attr_reader excel" do
1621
+
1622
+ before do
1623
+ @new_book = Book.open(@simple_file)
1624
+ end
1625
+ after do
1626
+ @new_book.close
1627
+ end
1628
+ it "should provide the excel instance of the book" do
1629
+ excel = @new_book.excel
1630
+ excel.class.should == Excel
1631
+ excel.should be_a Excel
1632
+ end
1633
+ end
1634
+
1635
+ =begin
1636
+ # work in progress
1637
+ context "with :update_links" do
1638
+
1639
+ it "should set update_links to :alert" do
1640
+ book = Book.open(@simple_file, :update_links => :alert)
1641
+ book.UpdateLinks.should == XlUpdateLinksUserSetting
1642
+ book.Saved.should be_true
1643
+ end
1644
+
1645
+ it "should set update_links to :never" do
1646
+ book = Book.open(@simple_file, :update_links => :never)
1647
+ book.UpdateLinks.should == XlUpdateLinksNever
1648
+ book = Book.open(@simple_file, :update_links => :foo)
1649
+ book.UpdateLinks.should == XlUpdateLinksNever
1650
+ end
1651
+
1652
+ it "should set update_links to :always" do
1653
+ book = Book.open(@simple_file, :update_links => :always)
1654
+ book.UpdateLinks.should == XlUpdateLinksAlways
1655
+ end
1656
+
1657
+ it "should set update_links to :never per default" do
1658
+ book = Book.open(@simple_file)
1659
+ book.UpdateLinks.should == XlUpdateLinksNever
1660
+ end
1661
+
1662
+ end
1663
+ =end
1664
+
1665
+ context "with :read_only" do
1666
+
1667
+ it "should reopen the book with writable (unsaved changes from readonly will not be saved)" do
1668
+ book = Book.open(@simple_file1, :read_only => true)
1669
+ book.ReadOnly.should be_true
1670
+ book.should be_alive
1671
+ sheet = book.sheet(1)
1672
+ old_cell_value = sheet[1,1].value
1673
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1674
+ book.Saved.should be_false
1675
+ new_book = Book.open(@simple_file1, :read_only => false, :if_unsaved => :accept)
1676
+ new_book.ReadOnly.should be_false
1677
+ new_book.should be_alive
1678
+ book.should be_alive
1679
+ new_book.should == book
1680
+ new_sheet = new_book.sheet(1)
1681
+ new_cell_value = new_sheet[1,1].value
1682
+ new_cell_value.should == old_cell_value
1683
+ end
1684
+
1685
+ it "should not raise an error when trying to reopen the book as read_only while the writable book had unsaved changes" do
1686
+ book = Book.open(@simple_file1, :read_only => false)
1687
+ book.ReadOnly.should be_false
1688
+ book.should be_alive
1689
+ sheet = book.sheet(1)
1690
+ old_cell_value = sheet[1,1].value
1691
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1692
+ book.Saved.should be_false
1693
+ new_book = Book.open(@simple_file1, :read_only => true, :if_unsaved => :accept)
1694
+ new_book.ReadOnly.should be_true
1695
+ new_book.Saved.should be_true
1696
+ new_book.should == book
1697
+ end
1698
+
1699
+ it "should reopen the book with writable in the same Excel instance (unsaved changes from readonly will not be saved)" do
1700
+ book = Book.open(@simple_file1, :read_only => true)
1701
+ book.ReadOnyl.should be_true
1702
+ book.should be_alive
1703
+ sheet = book.sheet(1)
1704
+ old_cell_value = sheet[1,1].value
1705
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1706
+ book.Saved.should be_false
1707
+ new_book = Book.open(@simple_file1, :if_unsaved => :accept, :force => {:excel => book.excel}, :read_only => false)
1708
+ new_book.ReadOnly.should be_false
1709
+ new_book.should be_alive
1710
+ book.should be_alive
1711
+ new_book.should == book
1712
+ new_sheet = new_book.sheet(1)
1713
+ new_cell_value = new_sheet[1,1].value
1714
+ new_cell_value.should == old_cell_value
1715
+ end
1716
+
1717
+ it "should reopen the book with readonly (unsaved changes of the writable should be saved)" do
1718
+ book = Book.open(@simple_file1, :force => {:excel => :new}, :read_only => false)
1719
+ book.ReadOnly.should be_false
1720
+ book.should be_alive
1721
+ sheet = book.sheet(1)
1722
+ old_cell_value = sheet[1,1].value
1723
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1724
+ book.Saved.should be_false
1725
+ new_book = Book.open(@simple_file1, :force => {:excel => book.excel}, :read_only => true, :if_unsaved => :accept)
1726
+ new_book.ReadOnly.should be_true
1727
+ new_book.Saved.should be_true
1728
+ new_book.should == book
1729
+ new_sheet = new_book.sheet(1)
1730
+ new_cell_value = new_sheet[1,1].value
1731
+ new_cell_value.should_not == old_cell_value
1732
+ end
1733
+
1734
+ it "should open the second book in another Excel as writable" do
1735
+ book = Book.open(@simple_file1, :read_only => true)
1736
+ book.ReadOnly.should be_true
1737
+ new_book = Book.open(@simple_file1, :force => {:excel => :new}, :read_only => false)
1738
+ new_book.ReadOnly.should be_false
1739
+ new_book.close
1740
+ book.close
1741
+ end
1742
+
1743
+ it "should be able to save, if :read_only => false" do
1744
+ book = Book.open(@simple_file1, :read_only => false)
1745
+ book.should be_a Book
1746
+ expect {
1747
+ book.save_as(@simple_save_file, :if_exists => :overwrite)
1748
+ }.to_not raise_error
1749
+ book.close
1750
+ end
1751
+
1752
+ it "should be able to save, if :read_only is default" do
1753
+ book = Book.open(@simple_file1)
1754
+ book.should be_a Book
1755
+ expect {
1756
+ book.save_as(@simple_save_file, :if_exists => :overwrite)
1757
+ }.to_not raise_error
1758
+ book.close
1759
+ end
1760
+
1761
+ it "should raise an error, if :read_only => true" do
1762
+ book = Book.open(@simple_file, :read_only => true)
1763
+ book.should be_a Book
1764
+ expect {
1765
+ book.save_as(@simple_save_file, :if_exists => :overwrite)
1766
+ }.to raise_error
1767
+ book.close
1768
+ end
1769
+ end
1770
+
1771
+ context "with various file formats" do
1772
+
1773
+ it "should open linked workbook" do
1774
+ book = Book.open(@linked_file, :visible => true)
1775
+ book.close
1776
+ end
1777
+
1778
+ it "should open xlsm file" do
1779
+ book = Book.open(@simple_file_xlsm, :visible => true)
1780
+ book.close
1781
+ end
1782
+
1783
+ it "should open xlsx file" do
1784
+ book = Book.open(@simple_file_xlsx, :visible => true)
1785
+ book.close
1786
+ end
1787
+ end
1788
+
1789
+
1790
+ context "with block" do
1791
+ it 'block parameter should be instance of Book' do
1792
+ Book.open(@simple_file) do |book|
1793
+ book.should be_a Book
1794
+ end
1795
+ end
1796
+ end
1797
+
1798
+ context "with WIN32OLE#GetAbsolutePathName" do
1799
+ it "'~' should be HOME directory" do
1800
+ path = '~/Abrakadabra.xlsx'
1801
+ expected_path = Regexp.new(File.expand_path(path).gsub(/\//, "."))
1802
+ expect {
1803
+ Book.open(path)
1804
+ }.to raise_error(FileNotFound, "file #{General::absolute_path(path).gsub("/","\\").inspect} not found")
1805
+ end
1806
+ end
1807
+ end
1808
+
1809
+ describe "reopen" do
1810
+
1811
+ context "with standard" do
1812
+
1813
+ before do
1814
+ @book = Book.open(@simple_file)
1815
+ end
1816
+
1817
+ after do
1818
+ @book.close
1819
+ end
1820
+
1821
+ it "should reopen the closed book" do
1822
+ @book.should be_alive
1823
+ book1 = @book
1824
+ @book.close
1825
+ @book.should_not be_alive
1826
+ @book.reopen
1827
+ @book.should be_a Book
1828
+ @book.should be_alive
1829
+ @book.should === book1
1830
+ end
1831
+ end
1832
+ end
1833
+
1834
+ describe "uplifting" do
1835
+
1836
+ context "with standard" do
1837
+
1838
+ before do
1839
+ @book = Book.open(@simple_file)
1840
+ end
1841
+
1842
+ after do
1843
+ @book.close
1844
+ end
1845
+
1846
+ it "should uplift a workbook to a book with an open book" do
1847
+ workbook = @book.ole_workbook
1848
+ book1 = Book.new(workbook)
1849
+ book1.should be_a Book
1850
+ book1.should be_alive
1851
+ book1.should == @book
1852
+ end
1853
+ end
1854
+ end
1855
+ end