robust_excel_ole 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +6 -0
  3. data/README.rdoc +2 -2
  4. data/docs/README_excel.rdoc +2 -3
  5. data/docs/README_open.rdoc +8 -12
  6. data/docs/README_ranges.rdoc +21 -9
  7. data/docs/README_sheet.rdoc +1 -1
  8. data/lib/robust_excel_ole/book.rb +3 -23
  9. data/lib/robust_excel_ole/excel.rb +2 -15
  10. data/lib/robust_excel_ole/reo_common.rb +11 -7
  11. data/lib/robust_excel_ole/sheet.rb +36 -15
  12. data/lib/robust_excel_ole/version.rb +1 -1
  13. data/robust_excel_ole.gemspec +1 -1
  14. data/spec/book_spec.rb +1 -1
  15. data/spec/book_specs/book_close_spec.rb +3 -3
  16. data/spec/book_specs/book_misc_spec.rb +25 -1
  17. data/spec/book_specs/book_open_spec.rb +299 -5
  18. data/spec/book_specs/book_sheet_spec.rb +1 -1
  19. data/spec/book_specs/book_unobtr_spec.rb +275 -35
  20. data/spec/data/another_workbook.xls +0 -0
  21. data/spec/data/different_workbook.xls +0 -0
  22. data/spec/data/workbook.xls +0 -0
  23. data/spec/excel_spec.rb +118 -5
  24. data/spec/helpers/key_sender.rb +2 -2
  25. data/spec/reo_common_spec.rb +0 -4
  26. data/spec/sheet_spec.rb +32 -0
  27. data/spec/spec_helper.rb +3 -0
  28. metadata +3 -21
  29. data/spec/ruby1.8.6_rspec2.14/book_spec.rb +0 -1421
  30. data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +0 -22
  31. data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +0 -252
  32. data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +0 -1070
  33. data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +0 -1855
  34. data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +0 -514
  35. data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +0 -395
  36. data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +0 -51
  37. data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +0 -1737
  38. data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +0 -495
  39. data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +0 -76
  40. data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +0 -42
  41. data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +0 -1820
  42. data/spec/ruby1.8.6_rspec2.14/general_spec.rb +0 -212
  43. data/spec/ruby1.8.6_rspec2.14/range_spec.rb +0 -131
  44. data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +0 -130
  45. data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +0 -663
  46. data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +0 -35
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
 
12
12
  s.summary = "RobustExcelOle automates processing Excel workbooks in Windows by using the win32ole library."
13
13
  s.description = "RobustExcelOle automates reading and writing Excel workbooks in Windows by using the win32ole library.
14
- It is disigned to cope with several kinds of concurrency of both simultaneously running
14
+ It is designed to cope with several kinds of concurrency of both simultaneously running
15
15
  Excel instances and simultanously happening user interactions.
16
16
  RobustExcelOle deals with various cases of Excel (and user) behaviour,
17
17
  supplies workarounds for some Excel bugs, and supports referenced libraries."
data/spec/book_spec.rb CHANGED
@@ -34,7 +34,7 @@ describe Book do
34
34
 
35
35
  after do
36
36
  Excel.kill_all
37
- #rm_tmp(@dir)
37
+ rm_tmp(@dir)
38
38
  end
39
39
 
40
40
  describe "create file" do
@@ -228,9 +228,9 @@ describe Book do
228
228
  expect {
229
229
  @book.close(:if_unsaved => :excel)
230
230
  }.to_not raise_error
231
- @book.ole_workbook.Saved.should be false
232
- @book.ole_workbook.should_not == nil
233
- @book.should be_alive
231
+ #@book.ole_workbook.Saved.should be false
232
+ #@book.ole_workbook.should_not == nil
233
+ #@book.should be_alive
234
234
  else
235
235
  @book.excel.Workbooks.Count.should == 1
236
236
  @book.close(:if_unsaved => :excel)
@@ -32,7 +32,7 @@ describe Book do
32
32
 
33
33
  after do
34
34
  Excel.kill_all
35
- #rm_tmp(@dir)
35
+ rm_tmp(@dir)
36
36
  end
37
37
 
38
38
  describe "create file" do
@@ -753,6 +753,30 @@ describe Book do
753
753
  @book1.Worksheets.Item(1).Delete
754
754
  @book1.nameval("localname").should == "simple"
755
755
  end
756
+
757
+ it "should color the cell" do
758
+ @book1.set_nameval("new", "bar")
759
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == -4142
760
+ @book1.set_nameval("new", "bar", :color => 4)
761
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
762
+ @book1["new"].should == "bar"
763
+ @book1["new"] = "bar"
764
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 42
765
+ @book1.save
766
+ @book1.close
767
+ #book2 = Book.open(@simple_file1, :visible => true)
768
+ #book2.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 42
769
+ end
770
+
771
+ it "should save without color" do
772
+ @book1.set_nameval("new", "bar", :color => 4)
773
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
774
+ @book1.save(:discoloring => true)
775
+ @book1.close
776
+ #book2 = Book.open(@simple_file1, :visible => true)
777
+ #book2.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 0
778
+ end
779
+
756
780
  end
757
781
 
758
782
  describe "rename_range" do
@@ -34,7 +34,146 @@ describe Book do
34
34
 
35
35
  after do
36
36
  Excel.kill_all
37
- #rm_tmp(@dir)
37
+ rm_tmp(@dir)
38
+ end
39
+
40
+ describe "with already open Excel instances and an open unsaved workbook" do
41
+
42
+ before do
43
+ @ole_excel1 = WIN32OLE.new('Excel.Application')
44
+ @ole_excel2 = WIN32OLE.new('Excel.Application')
45
+ @ole_workbook1 = @ole_excel1.Workbooks.Open(@simple_file1, { 'ReadOnly' => false })
46
+ @ole_workbook1.Worksheets.Add
47
+ end
48
+
49
+ context "with simple general situations" do
50
+
51
+ it "should simply open" do
52
+ book = Workbook.open(@simple_file1, :if_unsaved => :accept)
53
+ book.should be_alive
54
+ book.should be_a Book
55
+ end
56
+
57
+ it "should open in a new Excel" do
58
+ book2 = Workbook.open(@simple_file1, :force => {:excel => :new})
59
+ book2.should be_alive
60
+ book2.should be_a Book
61
+ book2.excel.should_not == @ole_excel1
62
+ book2.Readonly.should be true
63
+ end
64
+
65
+ it "should fetch the workbook" do
66
+ new_book = Book.new(@ole_workbook1)
67
+ new_book.should be_a Book
68
+ new_book.should be_alive
69
+ new_book.ole_workbook.should == @ole_workbook1
70
+ new_book.excel.ole_excel.Hwnd.should == @ole_excel1.Hwnd
71
+ end
72
+
73
+ it "should fetch a closed workbook" do
74
+ new_book = Book.new(@ole_workbook1)
75
+ new_book.close(:if_unsaved => :forget)
76
+ new_book.should_not be_alive
77
+ book2 = Book.open(@simple_file1)
78
+ book2.should === new_book
79
+ book2.should be_alive
80
+ book2.close
81
+ end
82
+
83
+ it "should force_excel with :reuse" do
84
+ book2 = Book.open(@different_file, :force => {:excel => :current})
85
+ book2.should be_alive
86
+ book2.should be_a Book
87
+ book2.excel.ole_excel.Hwnd.should == @ole_excel1.Hwnd
88
+ end
89
+
90
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
91
+ book1 = Book.open(@simple_file1, :if_unsaved => :forget)
92
+ excel2 = Excel.new(:reuse => false)
93
+ book1.excel.close(:if_unsaved => :forget)
94
+ sleep 1
95
+ book2 = Book.open(@simple_file1, :force => {:excel => :current}, :default => {:excel => :new})
96
+ book2.should be_alive
97
+ book2.should be_a Book
98
+ book2.excel.ole_excel.Hwnd.should == @ole_excel2.Hwnd
99
+ end
100
+
101
+ it "should reopen the closed book" do
102
+ book1 = Book.open(@simple_file1, :if_unsaved => :accept)
103
+ book1.should be_alive
104
+ book2 = book1
105
+ book1.close(:if_unsaved => :forget)
106
+ book1.should_not be_alive
107
+ book1.reopen
108
+ book1.should be_a Book
109
+ book1.should be_alive
110
+ book1.should === book2
111
+ end
112
+ end
113
+
114
+ context "with :if_unsaved" do
115
+
116
+ before do
117
+ @book = Book.open(@simple_file1, :if_unsaved => :accept)
118
+ sheet = @book.sheet(1)
119
+ @old_value = sheet[1,1].Value
120
+ sheet[1,1] = (sheet[1,1].value == "foo" ? "bar" : "foo")
121
+ @new_value = sheet[1,1].Value
122
+ @book.Saved.should be false
123
+ end
124
+
125
+ after do
126
+ @book.close(:if_unsaved => :forget)
127
+ end
128
+
129
+ it "should let the book open, if :if_unsaved is :accept" do
130
+ new_book = Book.open(@simple_file1, :if_unsaved => :accept)
131
+ @book.should be_alive
132
+ new_book.should be_alive
133
+ new_book.Saved.should be false
134
+ @book.Saved.should be false
135
+ new_book.sheet(1)[1,1].Value.should == @new_value
136
+ new_book.should == @book
137
+ end
138
+
139
+ it "should open book and close old book, if :if_unsaved is :forget" do
140
+ new_book = Book.open(@simple_file1, :if_unsaved => :forget)
141
+ @book.should_not be_alive
142
+ new_book.should be_alive
143
+ new_book.Saved.should be true
144
+ end
145
+ end
146
+
147
+ context "with :if_obstructed" do
148
+
149
+ it "should raise an error, if :if_obstructed is :raise" do
150
+ expect {
151
+ new_book = Book.open(@simple_file_other_path1)
152
+ }.to raise_error(WorkbookBlocked, /blocked by a workbook with the same name in a different path/)
153
+ end
154
+
155
+ it "should close the other book and open the new book, if :if_obstructed is :forget" do
156
+ new_book = Book.open(@simple_file_other_path1, :if_obstructed => :forget)
157
+ expect{
158
+ @ole_workbook1.Name
159
+ }.to raise_error
160
+ new_book.should be_alive
161
+ end
162
+
163
+ end
164
+
165
+ context "with :force => {:excel}" do
166
+
167
+ it "should open in a provided Excel" do
168
+ book1 = Book.open(@simple_file1, :force => {:excel => :new})
169
+ book2 = Book.open(@simple_file1, :force => {:excel => :new})
170
+ book3 = Book.open(@simple_file1, :force => {:excel => book2.excel})
171
+ book3.should be_alive
172
+ book3.should be_a Book
173
+ book3.excel.should == book2.excel
174
+ book3.Readonly.should be true
175
+ end
176
+ end
38
177
  end
39
178
 
40
179
  describe "simple open" do
@@ -120,12 +259,11 @@ describe Book do
120
259
  context "with causing warning dead excel without window handle" do
121
260
 
122
261
  it "combined" do
123
- Excel.kill_all
124
- book1 = Book.open(@simple_file, :visible => true)
262
+ book1 = Book.open(@simple_file1)
125
263
  book2 = Book.open(@different_file)
126
264
  Excel.kill_all
127
- #sleep 1
128
- book3 = Book.open(@another_simple_file)
265
+ #sleep 1 #then no warning
266
+ Excel.current # or book3 = Book.open(@another_simple_file)
129
267
  end
130
268
 
131
269
  end
@@ -551,6 +689,162 @@ describe Book do
551
689
 
552
690
  end
553
691
 
692
+ context "with leaving out :force => {:excel}" do
693
+
694
+ before do
695
+ @book = Book.open(@simple_file1)
696
+ end
697
+
698
+ after do
699
+ @book.close rescue nil
700
+ end
701
+
702
+ it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
703
+ book2 = Book.open(@another_simple_file)
704
+ book3 = Book.open(@different_file)
705
+ book3 = Book.open(@simple_file1, :excel => book2.excel)
706
+ book3.excel.should === book2.excel
707
+ book4 = Book.open(@simple_file1, :excel => @book)
708
+ book4.excel.should === @book.excel
709
+ book3.close
710
+ book4.close
711
+ book5 = Book.open(@simple_file1, :excel => book2.ole_workbook)
712
+ book5.excel.should === book2.excel
713
+ win32ole_excel1 = WIN32OLE.connect(@book.ole_workbook.Fullname).Application
714
+ book6 = Book.open(@simple_file1, :excel => win32ole_excel1)
715
+ book6.excel.should === @book.excel
716
+ end
717
+
718
+ it "should open in a new Excel" do
719
+ book2 = Book.open(@simple_file1, :excel => :new)
720
+ book2.should be_alive
721
+ book2.should be_a Book
722
+ book2.excel.should_not == @book.excel
723
+ book2.should_not == @book
724
+ @book.Readonly.should be false
725
+ book2.Readonly.should be true
726
+ book2.close
727
+ end
728
+
729
+ it "should open in a given Excel, not provide identity transparency, because old book readonly, new book writable" do
730
+ book2 = Book.open(@simple_file1, :excel => :new)
731
+ book2.excel.should_not == @book.excel
732
+ book3 = Book.open(@simple_file1, :excel => :new)
733
+ book3.excel.should_not == book2.excel
734
+ book3.excel.should_not == @book.excel
735
+ book2.close
736
+ book4 = Book.open(@simple_file1, :excel => book2.excel)
737
+ book4.should be_alive
738
+ book4.should be_a Book
739
+ book4.excel.should == book2.excel
740
+ #book4.Readonly.should == true
741
+ book4.should_not == book2
742
+ book4.close
743
+ book5 = Book.open(@simple_file1, :excel => book2)
744
+ book5.should be_alive
745
+ book5.should be_a Book
746
+ book5.excel.should == book2.excel
747
+ #book5.Readonly.should == true
748
+ book5.should_not == book2
749
+ book5.close
750
+ book3.close
751
+ end
752
+
753
+ 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
754
+ book2 = Book.open(@simple_file1, :excel => :new)
755
+ book2.excel.should_not == @book.excel
756
+ book3 = Book.open(@simple_file1, :excel => :new)
757
+ book3.excel.should_not == book2.excel
758
+ book3.excel.should_not == @book.excel
759
+ book2.close
760
+ book3.close
761
+ @book.close
762
+ book4 = Book.open(@simple_file1, :excel => book2.excel, :read_only => true)
763
+ book4.should be_alive
764
+ book4.should be_a Book
765
+ book4.excel.should == book2.excel
766
+ book4.ReadOnly.should be true
767
+ book4.should == book2
768
+ book4.close
769
+ book5 = Book.open(@simple_file1, :excel => book2, :read_only => true)
770
+ book5.should be_alive
771
+ book5.should be_a Book
772
+ book5.excel.should == book2.excel
773
+ book5.ReadOnly.should be true
774
+ book5.should == book2
775
+ book5.close
776
+ book3.close
777
+ end
778
+
779
+ 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
780
+ book2 = Book.open(@simple_file1, :excel => :new)
781
+ book2.excel.should_not == @book.excel
782
+ book2.close
783
+ @book.close
784
+ book4 = Book.open(@simple_file1, :excel => book2, :read_only => true)
785
+ book4.should be_alive
786
+ book4.should be_a Book
787
+ book4.excel.should == book2.excel
788
+ book4.ReadOnly.should be true
789
+ book4.should == book2
790
+ book4.close
791
+ end
792
+
793
+ it "should raise an error if no Excel or Book is given" do
794
+ expect{
795
+ Book.open(@simple_file1, :excel => :b)
796
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
797
+ end
798
+
799
+ it "should do force_excel even if both force_ and default_excel is given" do
800
+ book2 = Book.open(@simple_file1, :default => {:excel => @book.excel}, :force => {:excel => :new})
801
+ book2.should be_alive
802
+ book2.should be_a Book
803
+ book2.excel.should_not == @book.excel
804
+ book2.should_not == @book
805
+ end
806
+
807
+ it "should do default_excel if force_excel is nil" do
808
+ book2 = Book.open(@another_simple_file, :excel => nil)
809
+ book2.should be_alive
810
+ book2.should be_a Book
811
+ book2.excel.should == @book.excel
812
+ end
813
+
814
+ it "should force_excel with :reuse" do
815
+ book2 = Book.open(@different_file, :excel => :current)
816
+ book2.should be_alive
817
+ book2.should be_a Book
818
+ book2.excel.should == @book.excel
819
+ end
820
+
821
+ it "should force_excel with :reuse even if :default_excel says sth. else" do
822
+ book2 = Book.open(@different_file, :excel => :current, :default => {:excel => :new})
823
+ book2.should be_alive
824
+ book2.should be_a Book
825
+ book2.excel.should == @book.excel
826
+ end
827
+
828
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
829
+ excel2 = Excel.new(:reuse => false)
830
+ @book.excel.close
831
+ book2 = Book.open(@simple_file1, :excel => :current, :default => {:excel => :new})
832
+ book2.should be_alive
833
+ book2.should be_a Book
834
+ book2.excel.should === excel2
835
+ end
836
+
837
+ it "should force_excel with :reuse when reopening and the Excel is not alive even if :default_excel says sth. else" do
838
+ book2 = Book.open(@different_file1, :excel => :new)
839
+ book2.excel.close
840
+ book3 = Book.open(@different_file1, :excel => :current, :default => {:excel => :new})
841
+ book3.should be_alive
842
+ book3.should be_a Book
843
+ book3.excel.should == @book.excel
844
+ end
845
+
846
+ end
847
+
554
848
 
555
849
  context "with :force_excel" do
556
850
 
@@ -31,7 +31,7 @@ describe Book do
31
31
 
32
32
  after do
33
33
  Excel.kill_all
34
- #rm_tmp(@dir)
34
+ rm_tmp(@dir)
35
35
  end
36
36
 
37
37
  describe 'access sheet' do
@@ -32,7 +32,7 @@ describe Book do
32
32
 
33
33
  after do
34
34
  Excel.kill_all
35
- #rm_tmp(@dir)
35
+ rm_tmp(@dir)
36
36
  end
37
37
 
38
38
 
@@ -52,6 +52,249 @@ describe Book do
52
52
  expect{unobtrusively_ok?}.to_not raise_error
53
53
  end
54
54
 
55
+ =begin
56
+ describe "with already open Excel instances and workbooks" do
57
+
58
+ before do
59
+ @ole_excel1 = WIN32OLE.new('Excel.Application')
60
+ @ole_excel2 = WIN32OLE.new('Excel.Application')
61
+ end
62
+
63
+ context "with open unsaved workbook" do
64
+
65
+ before do
66
+ @book1 = Book.open(@simple_file1)
67
+ @book1.sheet(1)[1,1] = "foo"
68
+ #@ole_workbook1 = @ole_excel1.Workbooks.Open(@simple_file1, { 'ReadOnly' => false })
69
+ #@old_value = @ole_workbook1.Worksheets.Item(1).Cells.Item(1, 1).Value
70
+ #@ole_workbook1.Worksheets.Item(1).Cells.Item(1, 1).Value =
71
+ # @old_value == "foo" ? "bar" : "foo"
72
+ end
73
+
74
+ context "with writability" do
75
+
76
+ it "should open as read-write by default" do
77
+ Book.unobtrusively(@simple_file1) do |book|
78
+ book.Readonly.should be false
79
+ #book.filename.should == @ole_workbook1.Fullname.tr('\\','/')
80
+ #book.excel.ole_excel.Hwnd.should == @ole_excel1.Hwnd
81
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
82
+ end
83
+ #@ole_workbook1.Saved.should be false
84
+ Excel.kill_all
85
+ sleep 1
86
+ book2 = Book.open(@simple_file1)
87
+ #book2.sheet(1)[1,1].Value.should == @old_value
88
+ end
89
+
90
+ it "should open not writable" do
91
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
92
+ book.ReadOnly.should be false
93
+ book.filename.should == @ole_workbook1.Fullname.tr('\\','/')
94
+ book.excel.ole_excel.Hwnd.should == @ole_excel1.Hwnd
95
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
96
+ end
97
+ @book.Saved.should be false
98
+ @book.sheet(1)[1,1].Value.should_not == @old_value
99
+ Excel.kill_all
100
+ sleep 1
101
+ book2 = Book.open(@simple_file1)
102
+ book2.sheet(1)[1,1].Value.should_not == @old_value
103
+ end
104
+ end
105
+
106
+ context "further tests" do
107
+
108
+ it "should write in the outer and not in the inner block" do
109
+ Book.unobtrusively(@simple_file1) do |book|
110
+ @old_value = book.sheet(1)[1,1].Value
111
+ book.ReadOnly.should be false
112
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
113
+ book.Saved.should be false
114
+ book.sheet(1)[1,1].Value.should_not == @old_value
115
+ Book.unobtrusively(@simple_file1, :writable => false) do |book2|
116
+ book2.should == book
117
+ book2.ReadOnly.should be false
118
+ book2.Saved.should be false
119
+ book2.sheet(1)[1,1].Value.should_not == @old_value
120
+ book2.sheet(1)[1,1] = book2.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
121
+ book2.sheet(1)[1,1].Value.should == @old_value
122
+ end
123
+ book.should be_alive
124
+ book.Saved.should be false
125
+ book.sheet(1)[1,1].Value.should == @old_value
126
+ end
127
+ book = Book.open(@simple_file1)
128
+ book.sheet(1)[1,1].Value.should == @old_value
129
+ end
130
+
131
+ it "should use the excel of the book and keep open the book" do
132
+ @book = Book.open(@simple_file1)
133
+ excel = Excel.new(:reuse => false)
134
+ sheet = @book.sheet(1)
135
+ old_cell_value = sheet[1,1].value
136
+ @book.close
137
+ @book.should_not be_alive
138
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
139
+ book.should be_a Book
140
+ book.excel.should == @book.excel
141
+ book.excel.should_not == excel
142
+ sheet = book.sheet(1)
143
+ cell = sheet[1,1]
144
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
145
+ book.Saved.should be false
146
+ end
147
+ @book.should be_alive
148
+ @book.close
149
+ new_book = Book.open(@simple_file1)
150
+ sheet = new_book.sheet(1)
151
+ sheet[1,1].value.should_not == old_cell_value
152
+ end
153
+
154
+ it "should use the excel of the book and keep open the book" do
155
+ @book = Book.open(@simple_file1)
156
+ excel = Excel.new(:reuse => false)
157
+ sheet = @book.sheet(1)
158
+ old_cell_value = sheet[1,1].value
159
+ @book.close
160
+ @book.should_not be_alive
161
+ Book.unobtrusively(@simple_file, :if_closed => :new) do |book|
162
+ book.should be_a Book
163
+ book.should be_alive
164
+ book.excel.should_not == @book.excel
165
+ book.excel.should_not == excel
166
+ sheet = book.sheet(1)
167
+ cell = sheet[1,1]
168
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
169
+ book.Saved.should be false
170
+ end
171
+ @book.should_not be_alive
172
+ new_book = Book.open(@simple_file1)
173
+ sheet = new_book.sheet(1)
174
+ sheet[1,1].value.should_not == old_cell_value
175
+ end
176
+
177
+ it "should use a given Excel" do
178
+ @book = Book.open(@simple_file1)
179
+ @book.close
180
+ new_excel = Excel.new(:reuse => false)
181
+ another_excel = Excel.new(:reuse => false)
182
+ Book.unobtrusively(@simple_file1, :if_closed => another_excel) do |book|
183
+ book.excel.should_not == @book.excel
184
+ book.excel.should_not == new_excel
185
+ book.excel.should == another_excel
186
+ end
187
+ end
188
+
189
+ it "should reuse Excel" do
190
+ new_excel = Excel.new(:reuse => false)
191
+ Book.unobtrusively(@simple_file1, :if_closed => :reuse) do |book|
192
+ book.excel.should == @book.excel
193
+ book.excel.should_not == new_excel
194
+ end
195
+
196
+ it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
197
+ @book1 = Book.open(@simple_file1)
198
+ @book2 = Book.open(@simple_file1, :force_excel => :new)
199
+ @book1.Readonly.should == false
200
+ @book2.Readonly.should == true
201
+ old_sheet = @book1.sheet(1)
202
+ @old_cell_value = old_sheet[1,1].value
203
+ @book1.close
204
+ @book2.close
205
+ @book1.should_not be_alive
206
+ @book2.should_not be_alive
207
+ Book.unobtrusively(@simple_file1) do |book|
208
+ book.excel.should_not == @book2.excel
209
+ book.excel.should == @book1.excel
210
+ book.ReadOnly.should == false
211
+ sheet = book.sheet(1)
212
+ cell = sheet[1,1]
213
+ sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
214
+ book.Saved.should be false
215
+ end
216
+ new_book = Book.open(@simple_file1)
217
+ sheet = new_book.sheet(1)
218
+ sheet[1,1].value.should_not == @old_cell_value
219
+ end
220
+ end
221
+ end
222
+
223
+ context "with open unsaved read-only workbook" do
224
+
225
+ before do
226
+ @ole_workbook1 = @ole_excel1.Workbooks.Open(@simple_file1, { 'ReadOnly' => true })
227
+ @old_value = @ole_workbook1.Worksheets.Item(1).Cells.Item(1, 1).Value
228
+ @ole_workbook1.Worksheets.Item(1).Cells.Item(1, 1).Value =
229
+ @old_value == "foo" ? "bar" : "foo"
230
+ end
231
+
232
+ context "with writability" do
233
+
234
+ it "should open as read-only by default" do
235
+ Book.unobtrusively(@simple_file1) do |book|
236
+ book.Readonly.should be true
237
+ book.filename.should == @ole_workbook1.Fullname.tr('\\','/')
238
+ book.excel.ole_excel.Hwnd.should == @ole_excel1.Hwnd
239
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
240
+ end
241
+ @ole_workbook1.Saved.should be false
242
+ @ole_workbook1.ReadOnly.should be true
243
+ @ole_workbook1.Worksheets.Item(1).Cells.Item(1, 1).Value.should_not == @old_value
244
+ Excel.kill_all
245
+ sleep 1
246
+ book2 = Book.open(@simple_file1)
247
+ book2.sheet(1)[1,1].Value.should_not == @old_value
248
+ end
249
+
250
+ it "should open not writable" do
251
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
252
+ book.ReadOnly.should be true
253
+ book.filename.should == @ole_workbook1.Fullname.tr('\\','/')
254
+ book.excel.ole_excel.Hwnd.should == @ole_excel1.Hwnd
255
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
256
+ end
257
+ @ole_workbook1.Saved.should be false
258
+ @ole_workbook1.ReadOnly.should be true
259
+ @ole_workbook1.Worksheets.Item(1).Cells.Item(1, 1).Value.should_not == @old_value
260
+ Excel.kill_all
261
+ sleep 1
262
+ book2 = Book.open(@simple_file1)
263
+ book2.sheet(1)[1,1].Value.should == @old_value
264
+ end
265
+ end
266
+ end
267
+ end
268
+ end
269
+ =end
270
+
271
+ describe "block transparency" do
272
+
273
+ it "should return correct value of the block" do
274
+ (Book.unobtrusively(@simple_file1) do |book|
275
+ 22
276
+ end).should == 22
277
+ end
278
+
279
+ it "should return value of the last block" do
280
+ (Book.unobtrusively(@simple_file1) do |book|
281
+ Book.unobtrusively(@different_file) do |book2|
282
+ 11
283
+ end
284
+ 22
285
+ end).should == 22
286
+ end
287
+
288
+ it "should return correct value in several blocks" do
289
+ (Book.unobtrusively(@simple_file1) do |book|
290
+ Book.unobtrusively(@different_file) do |book2|
291
+ 22
292
+ end
293
+ end).should == 22
294
+ end
295
+
296
+ end
297
+
55
298
  describe "writability" do
56
299
 
57
300
  context "with no book" do
@@ -278,6 +521,21 @@ describe Book do
278
521
  @old_value = @book.sheet(1)[1,1].Value
279
522
  end
280
523
 
524
+ it "should not write" do
525
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
526
+ book.Readonly.should be true
527
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
528
+ book.sheet(1)[1,1].Value.should_not == @old_value
529
+ end
530
+ @book.close
531
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
532
+ book.sheet(1)[1,1].Value.should == @old_value
533
+ end
534
+ #@book.close
535
+ #book2 = Book.open(@simple_file1)
536
+ #book2.sheet(1)[1,1].Value.should == @old_value
537
+ end
538
+
281
539
  it "should not change the read_only mode" do
282
540
  Book.unobtrusively(@simple_file1) do |book|
283
541
  book.Readonly.should be true
@@ -490,7 +748,7 @@ describe Book do
490
748
  @book.sheet(1)[1,1].Value.should_not == @old_value
491
749
  end
492
750
 
493
- it "should open as read-write" do
751
+ it "should force to read-write" do
494
752
  Book.unobtrusively(@simple_file1, :writable => true, :read_only => false) do |book|
495
753
  book.Readonly.should be false
496
754
  book.should == @book
@@ -514,36 +772,6 @@ describe Book do
514
772
  }.to raise_error(NotImplementedREOError)
515
773
  end
516
774
 
517
- #it "should force to read-only (not implemented)" do
518
- # expect{
519
- # Book.unobtrusively(@simple_file1, :read_only => true) do |book|
520
- # book.ReadOnly.should be true
521
- # book.should == @book
522
- # book.filename.should == @book.filename
523
- # book.excel.should == @book.excel
524
- # book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
525
- # end
526
- # @book.Saved.should be false
527
- # @book.sheet(1)[1,1].Value.should == @old_value
528
- # end
529
- # }.to raise_error(NotImplementedREOError)
530
- #end
531
-
532
- #it "should force to read-only" do
533
- # expect{
534
- # Book.unobtrusively(@simple_file1, :read_only => true, :writable => false) do |book|
535
- # book.ReadOnly.should be true
536
- # book.should == @book
537
- # book.filename.should == @book.filename
538
- # book.excel.should == @book.excel
539
- # book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
540
- # end
541
- # @book.Saved.should be false
542
- # @book.sheet(1)[1,1].Value.should == @old_value
543
- # end
544
- # }.to raise_error(NotImplementedREOError)
545
- #end
546
-
547
775
  it "should open not writable" do
548
776
  Book.unobtrusively(@simple_file1, :writable => false) do |book|
549
777
  book.ReadOnly.should be false
@@ -696,6 +924,19 @@ describe Book do
696
924
  end
697
925
  end
698
926
 
927
+ it "should not close the book in the outer block with writable false" do
928
+ Book.unobtrusively(@simple_file1, :writable => false) do |book|
929
+ Book.unobtrusively(@simple_file1, :writable => false) do |book2|
930
+ book2.should == book
931
+ end
932
+ book.should be_alive
933
+ #Book.unobtrusively(@simple_file1, :writable => false) do |book3|
934
+ # book3.should == book
935
+ #end
936
+ #book.should be_alive
937
+ end
938
+ end
939
+
699
940
  it "should write in the outer and inner block" do
700
941
  Book.unobtrusively(@simple_file1) do |book|
701
942
  @old_value = book.sheet(1)[1,1].Value
@@ -808,10 +1049,10 @@ describe Book do
808
1049
  it "should remain open" do
809
1050
  book1 = Book.open(@simple_file1)
810
1051
  Book.unobtrusively(@simple_file1) do |book|
811
- #book.should be_a Book
1052
+ book.should be_a Book
812
1053
  book.should be_alive
813
1054
  end
814
- #book1.should be_alive
1055
+ book1.should be_alive
815
1056
  end
816
1057
 
817
1058
  end
@@ -1734,4 +1975,3 @@ describe Book do
1734
1975
  end
1735
1976
 
1736
1977
  end
1737
-