robust_excel_ole 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.rdoc +69 -56
  2. data/README_detail.rdoc +44 -34
  3. data/TodoList.md +8 -6
  4. data/examples/edit_sheets/example_access_sheets_and_cells.rb +6 -6
  5. data/examples/edit_sheets/example_adding_sheets.rb +2 -2
  6. data/examples/edit_sheets/example_ranges.rb +3 -3
  7. data/examples/open_save_close/example_control_to_excel.rb +2 -2
  8. data/examples/open_save_close/example_default_excel.rb +2 -2
  9. data/examples/open_save_close/example_force_excel.rb +1 -1
  10. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +4 -4
  11. data/examples/open_save_close/example_if_obstructed_forget.rb +2 -2
  12. data/examples/open_save_close/example_if_obstructed_save.rb +2 -2
  13. data/examples/open_save_close/example_if_unsaved_accept.rb +1 -1
  14. data/examples/open_save_close/example_if_unsaved_forget.rb +1 -1
  15. data/examples/open_save_close/example_if_unsaved_forget_more.rb +1 -1
  16. data/examples/open_save_close/example_read_only.rb +2 -2
  17. data/examples/open_save_close/example_rename_cells.rb +1 -1
  18. data/examples/open_save_close/example_reuse.rb +3 -3
  19. data/examples/open_save_close/example_simple.rb +2 -2
  20. data/examples/open_save_close/example_unobtrusively.rb +1 -1
  21. data/lib/robust_excel_ole/book.rb +97 -128
  22. data/lib/robust_excel_ole/book_store.rb +23 -23
  23. data/lib/robust_excel_ole/excel.rb +12 -14
  24. data/lib/robust_excel_ole/version.rb +1 -1
  25. data/spec/book_spec.rb +462 -289
  26. data/spec/book_store_spec.rb +36 -55
  27. data/spec/data/book_with_blank.xls +0 -0
  28. data/spec/data/{different_simple.xls → different_workbook.xls} +0 -0
  29. data/spec/data/merge_cells.xls +0 -0
  30. data/spec/data/{more_simple.xls → more_workbook.xls} +0 -0
  31. data/spec/data/protected_sheet.xls +0 -0
  32. data/spec/data/{simple.xls → workbook.xls} +0 -0
  33. data/spec/data/{simple.xlsm → workbook.xlsm} +0 -0
  34. data/spec/data/{simple.xlsx → workbook.xlsx} +0 -0
  35. data/spec/excel_spec.rb +40 -0
  36. metadata +9 -9
data/spec/book_spec.rb CHANGED
@@ -19,11 +19,11 @@ describe Book do
19
19
 
20
20
  before do
21
21
  @dir = create_tmpdir
22
- @simple_file = @dir + '/simple.xls'
23
- @simple_save_file = @dir + '/simple_save.xls'
24
- @different_file = @dir + '/different_simple.xls'
25
- @simple_file_other_path = @dir + '/more_data/simple.xls'
26
- @more_simple_file = @dir + '/more_simple.xls'
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
+ @more_simple_file = @dir + '/more_workbook.xls'
27
27
  end
28
28
 
29
29
  after do
@@ -34,17 +34,25 @@ describe Book do
34
34
 
35
35
  describe "create file" do
36
36
  context "with standard" do
37
- it "simple file with default" do
37
+ it "open an existing file" do
38
38
  expect {
39
39
  @book = Book.new(@simple_file)
40
40
  }.to_not raise_error
41
41
  @book.should be_a Book
42
42
  @book.close
43
43
  end
44
+
45
+ it "create a new file" do
46
+ File.delete @simple_save_file rescue nil
47
+ Book.create(@simple_save_file)
48
+ book = Book.open(@simple_save_file, :if_absent => :raise)
49
+ book.should be_a Book
50
+ book.close
51
+ File.exist?(@simple_save_file).should be_true
52
+ end
44
53
  end
45
54
  end
46
55
 
47
-
48
56
  describe "open" do
49
57
 
50
58
  context "standard use cases" do
@@ -116,7 +124,7 @@ describe Book do
116
124
  end
117
125
  end
118
126
 
119
- context "with transperency identity" do
127
+ context "with identity transperence" do
120
128
 
121
129
  before do
122
130
  @book = Book.open(@simple_file)
@@ -128,19 +136,19 @@ describe Book do
128
136
 
129
137
  it "should yield identical Book objects for identical Excel books" do
130
138
  book2 = Book.open(@simple_file)
131
- book2.should == @book
139
+ book2.should === @book
132
140
  book2.close
133
141
  end
134
142
 
135
143
  it "should yield different Book objects for different Excel books" do
136
144
  book2 = Book.open(@different_file)
137
- book2.should_not == @book
145
+ book2.should_not === @book
138
146
  book2.close
139
147
  end
140
148
 
141
149
  it "should yield different Book objects when opened the same file in different Excel instances" do
142
150
  book2 = Book.open(@simple_file, :force_excel => :new)
143
- book2.should_not == @book
151
+ book2.should_not === @book
144
152
  book2.close
145
153
  end
146
154
 
@@ -148,7 +156,7 @@ describe Book do
148
156
  @book.should be_alive
149
157
  @book.close
150
158
  book2 = Book.open(@simple_file)
151
- book2.should == @book
159
+ book2.should === @book
152
160
  book2.should be_alive
153
161
  book2.close
154
162
  end
@@ -159,7 +167,7 @@ describe Book do
159
167
  Excel.close_all
160
168
  book2 = Book.open(@simple_file)
161
169
  book2.should be_alive
162
- book2.should == @book
170
+ book2.should === @book
163
171
  book2.close
164
172
  end
165
173
 
@@ -169,7 +177,7 @@ describe Book do
169
177
  @book.close
170
178
  @book.should_not be_alive
171
179
  book2 = Book.open(@simple_file, :force_excel => :new)
172
- book2.should_not == @book
180
+ book2.should_not === @book
173
181
  book2.should be_alive
174
182
  book2.excel.should_not == old_excel
175
183
  book2.close
@@ -181,7 +189,7 @@ describe Book do
181
189
  @book.close
182
190
  @book.should_not be_alive
183
191
  book2 = Book.open(@simple_file, :force_excel => new_excel)
184
- book2.should_not == @book
192
+ book2.should_not === @book
185
193
  book2.should be_alive
186
194
  book2.excel.should == new_excel
187
195
  book2.excel.should_not == old_excel
@@ -194,7 +202,7 @@ describe Book do
194
202
  @book.close
195
203
  @book.should_not be_alive
196
204
  book2 = Book.open(@simple_file, :force_excel => old_excel)
197
- book2.should == @book
205
+ book2.should === @book
198
206
  book2.should be_alive
199
207
  book2.excel.should == old_excel
200
208
  @book.should be_alive
@@ -270,6 +278,14 @@ describe Book do
270
278
  end
271
279
  end
272
280
 
281
+ context "with another :force_excel" do
282
+ it "should do force_excel even if both force_ and default_excel is given" do
283
+ book2 = Book.open(@simple_file, :force_excel => nil)
284
+ book2.should be_alive
285
+ book2.should be_a Book
286
+ end
287
+ end
288
+
273
289
  context "with :default_excel" do
274
290
 
275
291
  before do
@@ -327,9 +343,9 @@ describe Book do
327
343
  book2 = Book.open(@simple_file, :default_excel => :reuse)
328
344
  book2.should be_alive
329
345
  book2.should be_a Book
330
- #book2.excel.should_not == excel
331
- #book2.excel.should_not == new_excel2
332
- #book2.excel.should == new_excel
346
+ book2.excel.should_not == excel
347
+ book2.excel.should_not == new_excel2
348
+ book2.excel.should == new_excel
333
349
  @book.should be_alive
334
350
  book2.should == @book
335
351
  book2.close
@@ -388,152 +404,6 @@ describe Book do
388
404
  book2.excel.should == @book.excel
389
405
  book2.should == @book
390
406
  end
391
-
392
- end
393
-
394
- context "with :if_locked" do
395
-
396
- context "with an writable book first" do
397
-
398
- before do
399
- @book = Book.open(@simple_file)
400
- end
401
-
402
- after do
403
- @book.close
404
- end
405
-
406
- it "should use the already open Excel and book" do
407
- @book.ReadOnly.should be_false
408
- new_book1 = Book.open(@simple_file, :if_locked => :readonly)
409
- new_book1.ReadOnly.should be_true
410
- new_book1.excel.should_not == @book.excel
411
- new_book1.should_not == @book
412
- new_book1.close
413
- new_book2 = Book.open(@simple_file, :if_locked => :take_writable)
414
- new_book2.ReadOnly.should be_false
415
- new_book2.excel == @book.excel
416
- new_book2.should == @book
417
- new_book2.close
418
- new_book3 = Book.open(@simple_file)
419
- new_book3.ReadOnly.should be_false
420
- new_book3.excel == @book.excel
421
- new_book3.should == @book
422
- new_book3.close
423
- end
424
-
425
- it "should open in a new Excel as read_only" do
426
- new_book = Book.open(@simple_file, :force_excel => :new, :if_locked => :readonly)
427
- @book.ReadOnly.should be_false
428
- new_book.ReadOnly.should be_true
429
- new_book.excel.should_not == @book.excel
430
- new_book.close
431
- new_excel = Excel.new(:reuse => false)
432
- new_book2 = Book.open(@simple_file, :force_excel => new_excel, :if_locked => :readonly)
433
- new_book2.ReadOnly.should be_true
434
- new_book2.excel.should_not == @book.excel
435
- new_book2.close
436
- end
437
-
438
- it "should open in the Excel where the book is writable" do
439
- new_book = Book.open(@simple_file, :force_excel => :new, :if_locked => :take_writable)
440
- @book.ReadOnly.should be_false
441
- new_book.ReadOnly.should be_false
442
- new_book.excel.should == @book.excel
443
- new_book.should == @book
444
- new_book.close
445
- new_excel = Excel.new(:reuse => false)
446
- new_book2 = Book.open(@simple_file, :force_excel => new_excel, :if_locked => :take_writable)
447
- new_book2.ReadOnly.should be_false
448
- new_book2.excel.should == @book.excel
449
- new_book2.close
450
- end
451
-
452
- it "should open in a new Excel as writable" do
453
- new_book = Book.open(@simple_file, :force_excel => :new, :if_locked => :force_writability)
454
- @book.ReadOnly.should be_false
455
- new_book.ReadOnly.should be_false
456
- new_book.excel.should_not == @book.excel
457
- new_book.close
458
- new_excel = Excel.new(:reuse => false)
459
- new_book2 = Book.open(@simple_file, :force_excel => new_excel, :if_locked => :force_writability)
460
- new_book2.ReadOnly.should be_false
461
- new_book2.excel.should_not == @book.excel
462
- new_book2.close
463
- end
464
-
465
- it "should open in a new Excel as read_only by default" do
466
- new_book = Book.open(@simple_file, :force_excel => :new)
467
- @book.ReadOnly.should be_false
468
- new_book.ReadOnly.should be_true
469
- new_book.excel.should_not == @book.excel
470
- new_book.close
471
- new_excel = Excel.new(:reuse => false)
472
- new_book2 = Book.open(@simple_file, :force_excel => new_excel)
473
- new_book2.ReadOnly.should be_true
474
- new_book2.excel.should_not == @book.excel
475
- new_book2.close
476
- end
477
- end
478
-
479
- context "with read_only book first" do
480
-
481
- before do
482
- @book = Book.open(@simple_file, :read_only => true)
483
- end
484
-
485
- after do
486
- @book.close
487
- end
488
-
489
- it "should use the already open Excel and book" do
490
- @book.ReadOnly.should be_true
491
- new_book1 = Book.open(@simple_file, :if_locked => :readonly)
492
- new_book1.ReadOnly.should be_true
493
- new_book1.excel.should == @book.excel
494
- new_book1.should == @book
495
- new_book1.close
496
- new_book2 = Book.open(@simple_file, :if_locked => :take_writable)
497
- new_book2.ReadOnly.should be_true
498
- new_book2.excel.should == @book.excel
499
- new_book2.should == @book
500
- new_book2.close
501
- new_book3 = Book.open(@simple_file)
502
- new_book3.ReadOnly.should be_true
503
- new_book3.excel.should == @book.excel
504
- new_book3.should == @book
505
- new_book3.close
506
- end
507
-
508
- it "should open in the already open Excel" do
509
- new_book = Book.open(@simple_file, :if_locked => :force_writability)
510
- @book.ReadOnly.should be_true
511
- new_book.ReadOnly.should be_false
512
- new_book.excel.should_not == @book.excel
513
- new_book.should_not == @book
514
- new_book.close
515
- end
516
-
517
- it "should open in a new Excel as writable" do
518
- @book.ReadOnly.should be_true
519
- new_book1 = Book.open(@simple_file, :force_excel => :new, :if_locked => :readonly)
520
- new_book1.ReadOnly.should be_false
521
- new_book1.excel.should_not == @book.excel
522
- new_book1.close
523
- new_book2 = Book.open(@simple_file, :force_excel => :new, :if_locked => :take_writable)
524
- new_book2.ReadOnly.should be_false
525
- new_book2.excel.should_not == @book.excel
526
- new_book2.close
527
- new_book3 = Book.open(@simple_file, :force_excel => :new, :if_locked => :force_writability)
528
- new_book3.ReadOnly.should be_false
529
- new_book3.excel.should_not == @book.excel
530
- new_book3.close
531
- new_book4 = Book.open(@simple_file, :force_excel => :new)
532
- new_book4.ReadOnly.should be_false
533
- new_book4.excel.should_not == @book.excel
534
- new_book4.close
535
- end
536
- end
537
407
  end
538
408
 
539
409
  context "with :if_unsaved" do
@@ -612,13 +482,10 @@ describe Book do
612
482
  @new_book.close
613
483
  end
614
484
 
615
- it "should open the book in a new excel instance, if :if_unsaved is default" do
616
- @new_book = Book.open(@simple_file)
617
- @book.should be_alive
618
- @new_book.should be_alive
619
- @new_book.filename.should == @book.filename
620
- @new_book.excel.should_not == @book.excel
621
- @new_book.close
485
+ it "should raise an error, if :if_unsaved is default" do
486
+ expect {
487
+ @new_book = Book.open(@simple_file, :if_unsaved => :raise)
488
+ }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
622
489
  end
623
490
 
624
491
  it "should raise an error, if :if_unsaved is invalid option" do
@@ -696,14 +563,11 @@ describe Book do
696
563
  @new_book.excel.should_not == @book.excel
697
564
  end
698
565
 
699
- it "should open the book in a new excel instance, if :if_obstructed is default" do
700
- @new_book = Book.open(@simple_file)
701
- @book.should be_alive
702
- @new_book.should be_alive
703
- @new_book.filename.should_not == @book.filename
704
- @new_book.excel.should_not == @book.excel
705
- end
706
-
566
+ it "should raise an error, if :if_obstructed is default" do
567
+ expect {
568
+ @new_book = Book.open(@simple_file)
569
+ }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
570
+ end
707
571
 
708
572
  it "should raise an error, if :if_obstructed is invalid option" do
709
573
  expect {
@@ -746,17 +610,35 @@ describe Book do
746
610
  end
747
611
  end
748
612
 
749
-
750
613
  context "with non-existing file" do
614
+
751
615
  it "should raise an exception" do
752
616
  File.delete @simple_save_file rescue nil
753
617
  expect {
754
- Book.open(@simple_save_file)
618
+ Book.open(@simple_save_file, :if_absent => :raise)
755
619
  }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
756
620
  end
621
+
622
+ it "should create a workbook" do
623
+ File.delete @simple_save_file rescue nil
624
+ book = Book.open(@simple_save_file, :if_absent => :create)
625
+ book.should be_a Book
626
+ book.close
627
+ File.exist?(@simple_save_file).should be_true
628
+ end
629
+
630
+ it "should create a workbook by default" do
631
+ File.delete @simple_save_file rescue nil
632
+ book = Book.open(@simple_save_file, :if_absent => :create)
633
+ book.should be_a Book
634
+ book.close
635
+ File.exist?(@simple_save_file).should be_true
636
+ end
637
+
757
638
  end
758
639
 
759
640
  context "with attr_reader excel" do
641
+
760
642
  before do
761
643
  @new_book = Book.open(@simple_file)
762
644
  end
@@ -772,7 +654,7 @@ describe Book do
772
654
 
773
655
  context "with :read_only" do
774
656
 
775
- it "should reopen the book with writable (unsaved changes from readonly are not saved)" do
657
+ it "should reopen the book with writable (unsaved changes from readonly will not be saved)" do
776
658
  book = Book.open(@simple_file, :read_only => true)
777
659
  book.ReadOnly.should be_true
778
660
  book.should be_alive
@@ -790,7 +672,7 @@ describe Book do
790
672
  new_cell_value.should == old_cell_value
791
673
  end
792
674
 
793
- it "should reopen the book with readonly (unsaved changes of the writable should be saved)" do
675
+ it "should not raise an error when trying to reopen the book as read_only while the writable book had unsaved changes" do
794
676
  book = Book.open(@simple_file, :read_only => false)
795
677
  book.ReadOnly.should be_false
796
678
  book.should be_alive
@@ -799,24 +681,20 @@ describe Book do
799
681
  sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
800
682
  book.Saved.should be_false
801
683
  new_book = Book.open(@simple_file, :read_only => true, :if_unsaved => :accept)
802
- new_book.ReadOnly.should be_true
803
- new_book.should be_alive
804
- book.should be_alive
805
- new_book.should == book
806
- new_sheet = new_book[0]
807
- new_cell_value = new_sheet[0,0].value
808
- new_cell_value.should_not == old_cell_value
684
+ new_book.ReadOnly.should be_false
685
+ new_book.Saved.should be_false
686
+ new_book.should == book
809
687
  end
810
688
 
811
- it "should reopen the book with writable (unsaved changes from readonly are not saved)" do
812
- book = Book.open(@simple_file, :force_excel => :new, :read_only => true)
689
+ it "should reopen the book with writable in the same Excel instance (unsaved changes from readonly will not be saved)" do
690
+ book = Book.open(@simple_file, :read_only => true)
813
691
  book.ReadOnly.should be_true
814
692
  book.should be_alive
815
693
  sheet = book[0]
816
694
  old_cell_value = sheet[0,0].value
817
695
  sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
818
696
  book.Saved.should be_false
819
- new_book = Book.open(@simple_file, :force_excel => book.excel, :read_only => false, :if_unsaved => :accept)
697
+ new_book = Book.open(@simple_file, :if_unsaved => :accept, :force_excel => book.excel, :read_only => false)
820
698
  new_book.ReadOnly.should be_false
821
699
  new_book.should be_alive
822
700
  book.should be_alive
@@ -835,16 +713,11 @@ describe Book do
835
713
  sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
836
714
  book.Saved.should be_false
837
715
  new_book = Book.open(@simple_file, :force_excel => book.excel, :read_only => true, :if_unsaved => :accept)
838
- new_book.ReadOnly.should be_true
839
- new_book.should be_alive
840
- book.should be_alive
841
- new_book.should == book
842
- new_sheet = new_book[0]
843
- new_cell_value = new_sheet[0,0].value
844
- new_cell_value.should_not == old_cell_value
716
+ new_book.ReadOnly.should be_false
717
+ new_book.Saved.should be_false
718
+ new_book.should == book
845
719
  end
846
720
 
847
-
848
721
  it "should open the second book in another Excel as writable" do
849
722
  book = Book.open(@simple_file, :read_only => true)
850
723
  book.ReadOnly.should be_true
@@ -895,7 +768,7 @@ describe Book do
895
768
  path = '~/Abrakadabra.xlsx'
896
769
  expected_path = Regexp.new(File.expand_path(path).gsub(/\//, "."))
897
770
  expect {
898
- Book.open(path)
771
+ Book.open(path, :if_absent => :raise)
899
772
  }.to raise_error(ExcelErrorOpen, "file #{path} not found")
900
773
  end
901
774
  end
@@ -922,6 +795,28 @@ describe Book do
922
795
  end
923
796
  end
924
797
 
798
+ describe "hidden_excel" do
799
+
800
+ context "with some open book" do
801
+
802
+ before do
803
+ @book = Book.open(@simple_file)
804
+ end
805
+
806
+ after do
807
+ @book.close
808
+ end
809
+
810
+ it "should create and use a hidden Excel instance" do
811
+ book2 = Book.open(@simple_file, :force_excel => @book.book_store.ensure_hidden_excel)
812
+ book2.excel.should_not == @book.excel
813
+ book2.excel.Visible.should be_false
814
+ book2.excel.DisplayAlerts.should be_false
815
+ book2.close
816
+ end
817
+ end
818
+ end
819
+
925
820
  describe "unobtrusively" do
926
821
 
927
822
  def unobtrusively_ok? # :nodoc: #
@@ -943,13 +838,24 @@ describe Book do
943
838
  it "should open unobtrusively in the first opened Excel" do
944
839
  excel = Excel.new(:reuse => false)
945
840
  new_excel = Excel.new(:reuse => false)
946
- Book.unobtrusively(@simple_file) do |book|
841
+ Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
947
842
  book.should be_a Book
948
843
  book.should be_alive
949
844
  book.excel.should == excel
950
845
  book.excel.should_not == new_excel
951
846
  end
952
847
  end
848
+
849
+ it "should open unobtrusively in a new Excel" do
850
+ excel = Excel.new(:reuse => false)
851
+ new_excel = Excel.new(:reuse => false)
852
+ Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
853
+ book.should be_a Book
854
+ book.should be_alive
855
+ book.excel.should_not == excel
856
+ book.excel.should_not == new_excel
857
+ end
858
+ end
953
859
  end
954
860
 
955
861
  context "with an open book" do
@@ -1030,13 +936,16 @@ describe Book do
1030
936
  end
1031
937
 
1032
938
  # The bold reanimation of the @book
1033
- it "should keep open the book" do
939
+ it "should use the excel of the book and keep open the book" do
940
+ excel = Excel.new(:reuse => false)
1034
941
  sheet = @book[0]
1035
942
  old_cell_value = sheet[0,0].value
1036
943
  @book.close
1037
944
  @book.should_not be_alive
1038
- Book.unobtrusively(@simple_file, :keep_open => true) do |book|
945
+ Book.unobtrusively(@simple_file, :if_closed => :reuse, :keep_open => true) do |book|
1039
946
  book.should be_a Book
947
+ book.excel.should == @book.excel
948
+ book.excel.should_not == excel
1040
949
  sheet = book[0]
1041
950
  cell = sheet[0,0]
1042
951
  sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
@@ -1048,8 +957,77 @@ describe Book do
1048
957
  sheet = new_book[0]
1049
958
  sheet[0,0].value.should_not == old_cell_value
1050
959
  end
960
+
961
+ # book shall be reanimated even with if_closed => hidden
962
+ it "should use the excel of the book and keep open the book" do
963
+ excel = Excel.new(:reuse => false)
964
+ sheet = @book[0]
965
+ old_cell_value = sheet[0,0].value
966
+ @book.close
967
+ @book.should_not be_alive
968
+ Book.unobtrusively(@simple_file) do |book|
969
+ book.should be_a Book
970
+ book.should be_alive
971
+ book.excel.should_not == @book.excel
972
+ book.excel.should_not == excel
973
+ sheet = book[0]
974
+ cell = sheet[0,0]
975
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
976
+ book.Saved.should be_false
977
+ end
978
+ @book.should_not be_alive
979
+ new_book = Book.open(@simple_file)
980
+ sheet = new_book[0]
981
+ sheet[0,0].value.should_not == old_cell_value
982
+ end
983
+
984
+ it "should use another excel if the Excels are closed" do
985
+ excel = Excel.new(:reuse => false)
986
+ sheet = @book[0]
987
+ old_cell_value = sheet[0,0].value
988
+ @book.close
989
+ @book.should_not be_alive
990
+ Excel.close_all
991
+ Book.unobtrusively(@simple_file, :keep_open => true, :if_closed => :reuse) do |book|
992
+ book.should be_a Book
993
+ book.excel.should == @book.excel
994
+ book.excel.should_not == excel
995
+ sheet = book[0]
996
+ cell = sheet[0,0]
997
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
998
+ book.Saved.should be_false
999
+ end
1000
+ @book.should be_alive
1001
+ @book.close
1002
+ new_book = Book.open(@simple_file)
1003
+ sheet = new_book[0]
1004
+ sheet[0,0].value.should_not == old_cell_value
1005
+ end
1006
+
1007
+ it "should use another excel if the Excels are closed" do
1008
+ excel = Excel.new(:reuse => false)
1009
+ sheet = @book[0]
1010
+ old_cell_value = sheet[0,0].value
1011
+ @book.close
1012
+ @book.should_not be_alive
1013
+ Excel.close_all
1014
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
1015
+ book.should be_a Book
1016
+ book.excel.should_not == @book.excel
1017
+ book.excel.should_not == excel
1018
+ sheet = book[0]
1019
+ cell = sheet[0,0]
1020
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1021
+ book.Saved.should be_false
1022
+ end
1023
+ @book.should_not be_alive
1024
+ new_book = Book.open(@simple_file)
1025
+ sheet = new_book[0]
1026
+ sheet[0,0].value.should_not == old_cell_value
1027
+ end
1051
1028
  end
1052
1029
 
1030
+
1053
1031
  context "with a read_only book" do
1054
1032
 
1055
1033
  before do
@@ -1093,7 +1071,32 @@ describe Book do
1093
1071
  sheet2[0,0].value.should == cell_value
1094
1072
  end
1095
1073
 
1096
- it "should modify one of the unobtrusively books" do
1074
+ it "should open unobtrusively by default the writable book" do
1075
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => false)
1076
+ @book.ReadOnly.should be_true
1077
+ book2.Readonly.should be_false
1078
+ sheet = @book[0]
1079
+ cell_value = sheet[0,0].value
1080
+ Book.unobtrusively(@simple_file) do |book|
1081
+ book.should be_a Book
1082
+ book.excel.should == book2.excel
1083
+ book.excel.should_not == @book.excel
1084
+ sheet = book[0]
1085
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1086
+ book.should be_alive
1087
+ book.Saved.should be_false
1088
+ end
1089
+ @book.Saved.should be_true
1090
+ @book.ReadOnly.should be_true
1091
+ @book.close
1092
+ book2.close
1093
+ book3 = Book.open(@simple_file)
1094
+ new_sheet = book3[0]
1095
+ new_sheet[0,0].value.should_not == cell_value
1096
+ book3.close
1097
+ end
1098
+
1099
+ it "should open unobtrusively by default the book in a new Excel such that the book is writable" do
1097
1100
  book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1098
1101
  @book.ReadOnly.should be_true
1099
1102
  book2.Readonly.should be_true
@@ -1101,6 +1104,63 @@ describe Book do
1101
1104
  cell_value = sheet[0,0].value
1102
1105
  Book.unobtrusively(@simple_file) do |book|
1103
1106
  book.should be_a Book
1107
+ book.excel.should_not == book2.excel
1108
+ book.excel.should_not == @book.excel
1109
+ sheet = book[0]
1110
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1111
+ book.should be_alive
1112
+ book.Saved.should be_false
1113
+ end
1114
+ @book.Saved.should be_true
1115
+ @book.ReadOnly.should be_true
1116
+ @book.close
1117
+ book2.close
1118
+ book3 = Book.open(@simple_file)
1119
+ new_sheet = book3[0]
1120
+ new_sheet[0,0].value.should_not == cell_value
1121
+ book3.close
1122
+ end
1123
+
1124
+ it "should open unobtrusively the book in a new Excel such that the book is writable" do
1125
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1126
+ @book.ReadOnly.should be_true
1127
+ book2.Readonly.should be_true
1128
+ sheet = @book[0]
1129
+ cell_value = sheet[0,0].value
1130
+ Book.unobtrusively(@simple_file, :use_this => false) do |book|
1131
+ book.should be_a Book
1132
+ book.excel.should_not == book2.excel
1133
+ book.excel.should_not == @book.excel
1134
+ sheet = book[0]
1135
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1136
+ book.should be_alive
1137
+ book.Saved.should be_false
1138
+ end
1139
+ @book.Saved.should be_true
1140
+ @book.ReadOnly.should be_true
1141
+ @book.close
1142
+ book2.close
1143
+ book3 = Book.open(@simple_file)
1144
+ new_sheet = book3[0]
1145
+ new_sheet[0,0].value.should_not == cell_value
1146
+ book3.close
1147
+ end
1148
+
1149
+ it "should open unobtrusively the book in a new Excel to open the book writable" do
1150
+ excel1 = Excel.new(:reuse => false)
1151
+ excel2 = Excel.new(:reuse => false)
1152
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1153
+ @book.ReadOnly.should be_true
1154
+ book2.Readonly.should be_true
1155
+ sheet = @book[0]
1156
+ cell_value = sheet[0,0].value
1157
+ Book.unobtrusively(@simple_file, :use_readonly_excel => false) do |book|
1158
+ book.should be_a Book
1159
+ book.ReadOnly.should be_false
1160
+ book.excel.should_not == book2.excel
1161
+ book.excel.should_not == @book.excel
1162
+ book.excel.should_not == excel1
1163
+ book.excel.should_not == excel2
1104
1164
  sheet = book[0]
1105
1165
  sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1106
1166
  book.should be_alive
@@ -1115,6 +1175,53 @@ describe Book do
1115
1175
  new_sheet[0,0].value.should_not == cell_value
1116
1176
  book3.close
1117
1177
  end
1178
+
1179
+ it "should open unobtrusively the book in the same Excel to open the book writable" do
1180
+ excel1 = Excel.new(:reuse => false)
1181
+ excel2 = Excel.new(:reuse => false)
1182
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1183
+ @book.ReadOnly.should be_true
1184
+ book2.Readonly.should be_true
1185
+ sheet = @book[0]
1186
+ cell_value = sheet[0,0].value
1187
+ Book.unobtrusively(@simple_file, :use_readonly_excel => true) do |book|
1188
+ book.should be_a Book
1189
+ book.excel.should == book2.excel
1190
+ book.ReadOnly.should be_false
1191
+ sheet = book[0]
1192
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1193
+ book.should be_alive
1194
+ book.Saved.should be_false
1195
+ end
1196
+ book2.Saved.should be_true
1197
+ book2.ReadOnly.should be_false
1198
+ @book.close
1199
+ book2.close
1200
+ book3 = Book.open(@simple_file)
1201
+ new_sheet = book3[0]
1202
+ new_sheet[0,0].value.should_not == cell_value
1203
+ book3.close
1204
+ end
1205
+
1206
+ it "should open unobtrusively the book in the Excel where it was opened most recently" do
1207
+ book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1208
+ @book.ReadOnly.should be_true
1209
+ book2.Readonly.should be_true
1210
+ sheet = @book[0]
1211
+ cell_value = sheet[0,0].value
1212
+ Book.unobtrusively(@simple_file, :read_only => true) do |book|
1213
+ book.should be_a Book
1214
+ book.excel.should == book2.excel
1215
+ book.excel.should_not == @book.excel
1216
+ book.should be_alive
1217
+ book.Saved.should be_true
1218
+ end
1219
+ @book.Saved.should be_true
1220
+ @book.ReadOnly.should be_true
1221
+ @book.close
1222
+ book2.close
1223
+ end
1224
+
1118
1225
  end
1119
1226
 
1120
1227
  context "with a virgin Book class" do
@@ -1162,6 +1269,7 @@ describe Book do
1162
1269
  book.Saved.should be_false
1163
1270
  sleep 1
1164
1271
  end
1272
+ @book1.Saved.should be_true
1165
1273
  m_time2 = File.mtime(@book1.stored_filename)
1166
1274
  m_time2.should_not == m_time
1167
1275
  end
@@ -1229,83 +1337,76 @@ describe Book do
1229
1337
  end
1230
1338
 
1231
1339
  context "with visible" do
1232
- before do
1233
- @book1 = Book.open(@simple_file)
1234
- end
1235
1340
 
1236
1341
  after do
1237
- @book1.close(:if_unsaved => :forget)
1342
+ @book1.close
1238
1343
  end
1239
1344
 
1240
- it "should unobtrusively use a book invisible" do
1345
+ it "should let the book invisible" do
1346
+ @book1 = Book.open(@simple_file)
1347
+ @book1.excel.Visible.should be_false
1348
+ Book.unobtrusively(@simple_file) do |book|
1349
+ book.excel.Visible.should be_false
1350
+ end
1241
1351
  @book1.excel.Visible.should be_false
1242
1352
  Book.unobtrusively(@simple_file, :visible => false) do |book|
1243
- @book1.excel.Visible.should be_false
1244
1353
  book.excel.Visible.should be_false
1245
- sheet = book[0]
1246
- cell = sheet[0,0]
1247
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1248
- sheet = book[0]
1249
1354
  end
1250
1355
  @book1.excel.Visible.should be_false
1251
1356
  end
1252
1357
 
1253
- it "should unobtrusively use a book visible" do
1358
+ it "should let the book visible" do
1359
+ @book1 = Book.open(@simple_file, :visible => true)
1360
+ @book1.excel.Visible.should be_true
1361
+ Book.unobtrusively(@simple_file) do |book|
1362
+ book.excel.Visible.should be_true
1363
+ end
1364
+ @book1.excel.Visible.should be_true
1365
+ Book.unobtrusively(@simple_file, :visible => true) do |book|
1366
+ book.excel.Visible.should be_true
1367
+ end
1368
+ @book1.excel.Visible.should be_true
1369
+ end
1370
+
1371
+ it "should open the book unobtrusively visible" do
1372
+ @book1 = Book.open(@simple_file)
1254
1373
  @book1.excel.Visible.should be_false
1255
1374
  Book.unobtrusively(@simple_file, :visible => true) do |book|
1256
- @book1.excel.Visible.should be_true
1257
1375
  book.excel.Visible.should be_true
1258
- sheet = book[0]
1259
- cell = sheet[0,0]
1260
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1261
- sheet = book[0]
1262
1376
  end
1263
1377
  @book1.excel.Visible.should be_false
1264
1378
  end
1265
1379
 
1266
- it "should unobtrusively use a book invisibe by default" do
1267
- @book1.excel.Visible.should be_false
1268
- Book.unobtrusively(@simple_file) do |book|
1269
- @book1.excel.Visible.should be_false
1380
+ it "should open the book unobtrusively invisible" do
1381
+ @book1 = Book.open(@simple_file, :visible => true)
1382
+ @book1.excel.Visible.should be_true
1383
+ Book.unobtrusively(@simple_file, :visible => false) do |book|
1270
1384
  book.excel.Visible.should be_false
1271
- sheet = book[0]
1272
- cell = sheet[0,0]
1273
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1274
- sheet = book[0]
1275
1385
  end
1276
- @book1.excel.Visible.should be_false
1386
+ @book1.excel.Visible.should be_true
1277
1387
  end
1388
+
1278
1389
  end
1279
1390
 
1280
1391
  context "with several Excel instances" do
1281
1392
 
1282
- it "should open unobtrusively the closed book in the most recent Excel" do
1283
- book1 = Book.open(@simple_file)
1284
- book2 = Book.open(@simple_file, :force_excel => :new)
1285
- book1.Readonly.should == false
1286
- book2.Readonly.should == true
1287
- book1.close
1288
- book2.close
1289
- Book.unobtrusively(@simple_file) do |book|
1290
- book.excel.should == book2.excel
1291
- book.excel.should_not == book1.excel
1292
- book.Readonly.should == false
1293
- end
1393
+ before do
1394
+ @book1 = Book.open(@simple_file)
1395
+ @book2 = Book.open(@simple_file, :force_excel => :new)
1396
+ @book1.Readonly.should == false
1397
+ @book2.Readonly.should == true
1398
+ old_sheet = @book1[0]
1399
+ @old_cell_value = old_sheet[0,0].value
1400
+ @book1.close
1401
+ @book2.close
1402
+ @book1.should_not be_alive
1403
+ @book2.should_not be_alive
1294
1404
  end
1295
1405
 
1296
- it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
1297
- book1 = Book.open(@simple_file)
1298
- book2 = Book.open(@simple_file, :force_excel => :new)
1299
- book2.Readonly.should == true
1300
- old_sheet = book1[0]
1301
- old_cell_value = old_sheet[0,0].value
1302
- book1.close
1303
- book2.close
1304
- book1.should_not be_alive
1305
- book2.should_not be_alive
1306
- Book.unobtrusively(@simple_file) do |book|
1307
- book.excel.should == book2.excel
1308
- book.excel.should_not == book1.excel
1406
+ it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
1407
+ Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
1408
+ book.excel.should == @book2.excel
1409
+ book.excel.should_not == @book1.excel
1309
1410
  book.ReadOnly.should == false
1310
1411
  sheet = book[0]
1311
1412
  cell = sheet[0,0]
@@ -1314,24 +1415,14 @@ describe Book do
1314
1415
  end
1315
1416
  new_book = Book.open(@simple_file)
1316
1417
  sheet = new_book[0]
1317
- sheet[0,0].value.should_not == old_cell_value
1418
+ sheet[0,0].value.should_not == @old_cell_value
1318
1419
  end
1319
1420
 
1320
- it "should open unobtrusively the closed book in a new Excel if the Excel is not alive anymore" do
1321
- book1 = Book.open(@simple_file)
1322
- book2 = Book.open(@simple_file, :force_excel => :new)
1323
- book2.Readonly.should == true
1324
- old_sheet = book1[0]
1325
- old_cell_value = old_sheet[0,0].value
1326
- book1.close
1327
- book2.close
1328
- book1.should_not be_alive
1329
- book2.should_not be_alive
1330
- Excel.close_all
1421
+ it "should open unobtrusively the closed book in the new hidden Excel" do
1331
1422
  Book.unobtrusively(@simple_file) do |book|
1423
+ book.excel.should_not == @book2.excel
1424
+ book.excel.should_not == @book1.excel
1332
1425
  book.ReadOnly.should == false
1333
- #book.excel.should_not == book1.excel
1334
- #book.excel.should_not == book2.excel
1335
1426
  sheet = book[0]
1336
1427
  cell = sheet[0,0]
1337
1428
  sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
@@ -1339,22 +1430,15 @@ describe Book do
1339
1430
  end
1340
1431
  new_book = Book.open(@simple_file)
1341
1432
  sheet = new_book[0]
1342
- sheet[0,0].value.should_not == old_cell_value
1433
+ sheet[0,0].value.should_not == @old_cell_value
1343
1434
  end
1344
1435
 
1345
- it "should open unobtrusively the once opened book in a new Excel if the Excel is not alive anymore" do
1346
- book1 = Book.open(@simple_file)
1347
- book2 = Book.open(@simple_file, :force_excel => :new)
1348
- book2.Readonly.should == true
1349
- old_sheet = book1[0]
1350
- old_cell_value = old_sheet[0,0].value
1351
- book1.should be_alive
1352
- book2.should be_alive
1436
+ it "should open unobtrusively the closed book in a new Excel if the Excel is not alive anymore" do
1353
1437
  Excel.close_all
1354
1438
  Book.unobtrusively(@simple_file) do |book|
1355
1439
  book.ReadOnly.should == false
1356
- #book.excel.should_not == book1.excel
1357
- #book.excel.should_not == book2.excel
1440
+ book.excel.should_not == @book1.excel
1441
+ book.excel.should_not == @book2.excel
1358
1442
  sheet = book[0]
1359
1443
  cell = sheet[0,0]
1360
1444
  sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
@@ -1362,9 +1446,96 @@ describe Book do
1362
1446
  end
1363
1447
  new_book = Book.open(@simple_file)
1364
1448
  sheet = new_book[0]
1365
- sheet[0,0].value.should_not == old_cell_value
1449
+ sheet[0,0].value.should_not == @old_cell_value
1450
+ end
1451
+ end
1452
+
1453
+ context "with :hidden" do
1454
+
1455
+ before do
1456
+ @book1 = Book.open(@simple_file)
1457
+ @book1.close
1458
+ end
1459
+
1460
+ it "should create a new hidden Excel instance" do
1461
+ Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1462
+ book.should be_a Book
1463
+ book.should be_alive
1464
+ book.excel.Visible.should be_false
1465
+ book.excel.DisplayAlerts.should be_false
1466
+ end
1467
+ end
1468
+
1469
+ it "should create a new hidden Excel instance and use this afterwards" do
1470
+ hidden_excel = nil
1471
+ Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1472
+ book.should be_a Book
1473
+ book.should be_alive
1474
+ book.excel.Visible.should be_false
1475
+ book.excel.DisplayAlerts.should be_false
1476
+ hidden_excel = book.excel
1477
+ end
1478
+ Book.unobtrusively(@different_file, :if_closed => :hidden) do |book|
1479
+ book.should be_a Book
1480
+ book.should be_alive
1481
+ book.excel.Visible.should be_false
1482
+ book.excel.DisplayAlerts.should be_false
1483
+ book.excel.should == hidden_excel
1484
+ end
1366
1485
  end
1367
1486
 
1487
+ it "should create a new hidden Excel instance if the Excel is closed" do
1488
+ Excel.close_all
1489
+ Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1490
+ book.should be_a Book
1491
+ book.should be_alive
1492
+ book.excel.Visible.should be_false
1493
+ book.excel.DisplayAlerts.should be_false
1494
+ book.excel.should_not == @book1.excel
1495
+ end
1496
+ end
1497
+
1498
+ it "should exclude hidden Excel when reuse in unobtrusively" do
1499
+ hidden_excel = nil
1500
+ Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1501
+ book.should be_a Book
1502
+ book.should be_alive
1503
+ book.excel.Visible.should be_false
1504
+ book.excel.DisplayAlerts.should be_false
1505
+ book.excel.should_not == @book1.excel
1506
+ hidden_excel = book.excel
1507
+ end
1508
+ Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
1509
+ book.should be_a Book
1510
+ book.should be_alive
1511
+ book.excel.Visible.should be_false
1512
+ book.excel.DisplayAlerts.should be_false
1513
+ book.excel.should_not == hidden_excel
1514
+ end
1515
+ end
1516
+
1517
+ it "should exclude hidden Excel when reuse in open" do
1518
+ hidden_excel = nil
1519
+ Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1520
+ book.should be_a Book
1521
+ book.should be_alive
1522
+ book.excel.Visible.should be_false
1523
+ book.excel.DisplayAlerts.should be_false
1524
+ book.excel.should_not == @book1.excel
1525
+ hidden_excel = book.excel
1526
+ end
1527
+ book2 = Book.open(@simple_file, :default_excel => :reuse)
1528
+ book2.excel.should_not == hidden_excel
1529
+ end
1530
+
1531
+ it "should exclude hidden Excel when reuse in open" do
1532
+ book1 = Book.open(@simple_file)
1533
+ book1.close
1534
+ book2 = Book.open(@simple_file, :default_excel => :reuse)
1535
+ book2.excel.should == book1.excel
1536
+ book1.should be_alive
1537
+ book2.close
1538
+ end
1368
1539
  end
1369
1540
  end
1370
1541
 
@@ -1389,16 +1560,14 @@ describe Book do
1389
1560
  it "should raise an error if name not defined" do
1390
1561
  expect {
1391
1562
  value = @book1.nvalue("foo")
1392
- }.to raise_error(ExcelErrorNValue, "name foo not in more_simple.xls")
1563
+ }.to raise_error(ExcelErrorNValue, "name foo not in more_workbook.xls")
1393
1564
  end
1394
1565
 
1395
1566
  it "should raise an error if name was defined but contents is calcuated" do
1396
1567
  expect {
1397
1568
  value = @book1.nvalue("named_formula")
1398
- }.to raise_error(ExcelErrorNValue, "range error in more_simple.xls")
1569
+ }.to raise_error(ExcelErrorNValue, "range error in more_workbook.xls")
1399
1570
  end
1400
-
1401
-
1402
1571
  end
1403
1572
  end
1404
1573
 
@@ -1453,6 +1622,12 @@ describe Book do
1453
1622
  }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
1454
1623
  end
1455
1624
 
1625
+ it "should raise error by default" do
1626
+ expect{
1627
+ @book.close(:if_unsaved => :raise)
1628
+ }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
1629
+ end
1630
+
1456
1631
  it "should close the book and leave its file untouched with option :forget" do
1457
1632
  ole_workbook = @book.workbook
1458
1633
  excel = @book.excel
@@ -1952,8 +2127,6 @@ describe Book do
1952
2127
  }.to raise_error(ExcelErrorSheet, "sheet name already exists")
1953
2128
  end
1954
2129
  end
1955
-
1956
-
1957
2130
  end
1958
2131
 
1959
2132
  describe 'access sheet' do