robust_excel_ole 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -29,7 +29,7 @@ describe Book do
29
29
  end
30
30
 
31
31
  after do
32
- Excel.close_all
32
+ Excel.kill_all
33
33
  rm_tmp(@dir)
34
34
  end
35
35
 
@@ -89,6 +89,15 @@ describe Book do
89
89
  }.to raise_error(ExcelErrorClose, /workbook is unsaved: "workbook.xls"/)
90
90
  end
91
91
 
92
+ it "should keep the book open" do
93
+ ole_workbook = @book.workbook
94
+ excel = @book.excel
95
+ excel.Workbooks.Count.should == 1
96
+ @book.close(:if_unsaved => :keep_open)
97
+ excel.Workbooks.Count.should == 1
98
+ @book.should be_alive
99
+ end
100
+
92
101
  it "should close the book and leave its file untouched with option :forget" do
93
102
  ole_workbook = @book.workbook
94
103
  excel = @book.excel
@@ -29,7 +29,7 @@ describe Book do
29
29
  end
30
30
 
31
31
  after do
32
- Excel.close_all
32
+ Excel.kill_all
33
33
  rm_tmp(@dir)
34
34
  end
35
35
 
@@ -13,8 +13,7 @@ describe Book do
13
13
  excel = Excel.new(:reuse => true)
14
14
  open_books = excel == nil ? 0 : excel.Workbooks.Count
15
15
  puts "*** open books *** : #{open_books}" if open_books > 0
16
- #Excel.close_all
17
- Excel.kill_all
16
+ Excel.close_all
18
17
  end
19
18
 
20
19
  before do
@@ -31,12 +30,87 @@ describe Book do
31
30
 
32
31
  after do
33
32
  Excel.kill_all
34
- #Excel.close_all
35
33
  rm_tmp(@dir)
36
34
  end
37
35
 
38
36
  describe "open" do
39
37
 
38
+ context "with class identifier 'Workbook'" do
39
+
40
+ before do
41
+ @book = Workbook.open(@simple_file)
42
+ end
43
+
44
+ after do
45
+ @book.close rescue nil
46
+ end
47
+
48
+ it "should open in a new Excel" do
49
+ book2 = Workbook.open(@simple_file, :force_excel => :new)
50
+ book2.should be_alive
51
+ book2.should be_a Book
52
+ book2.excel.should_not == @book.excel
53
+ book2.should_not == @book
54
+ @book.Readonly.should be_false
55
+ book2.Readonly.should be_true
56
+ book2.close
57
+ end
58
+ end
59
+
60
+ context "lift a workbook to a Book object" do
61
+
62
+ before do
63
+ @book = Book.open(@simple_file)
64
+ end
65
+
66
+ after do
67
+ @book.close
68
+ end
69
+
70
+ it "should fetch the workbook" do
71
+ workbook = @book.workbook
72
+ new_book = Book.new(workbook)
73
+ new_book.should be_a Book
74
+ new_book.should be_alive
75
+ new_book.should == @book
76
+ new_book.filename.should == @book.filename
77
+ new_book.excel.should == @book.excel
78
+ new_book.excel.Visible.should be_false
79
+ new_book.excel.DisplayAlerts.should be_false
80
+ new_book.should === @book
81
+ new_book.close
82
+ end
83
+
84
+ it "should fetch the workbook" do
85
+ workbook = @book.workbook
86
+ new_book = Book.new(workbook, :visible => true)
87
+ new_book.should be_a Book
88
+ new_book.should be_alive
89
+ new_book.should == @book
90
+ new_book.filename.should == @book.filename
91
+ new_book.excel.should == @book.excel
92
+ new_book.excel.Visible.should be_true
93
+ new_book.excel.DisplayAlerts.should be_false
94
+ new_book.should === @book
95
+ new_book.close
96
+ end
97
+
98
+ it "should yield an identical Book and set visible and displayalerts values" do
99
+ workbook = @book.workbook
100
+ new_book = Book.new(workbook, :visible => true, :displayalerts => true)
101
+ new_book.should be_a Book
102
+ new_book.should be_alive
103
+ new_book.should == @book
104
+ new_book.filename.should == @book.filename
105
+ new_book.excel.should == @book.excel
106
+ new_book.should === @book
107
+ new_book.excel.visible.should be_true
108
+ new_book.excel.displayalerts.should be_true
109
+ new_book.close
110
+ end
111
+
112
+ end
113
+
40
114
  context "with standard options" do
41
115
  before do
42
116
  @book = Book.open(@simple_file)
@@ -149,6 +223,24 @@ describe Book do
149
223
  @book.close rescue nil
150
224
  end
151
225
 
226
+ it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
227
+ book2 = Book.open(@another_simple_file)
228
+ book3 = Book.open(@different_file)
229
+ book3 = Book.open(@simple_file, :force_excel => book2.excel)
230
+ book3.excel.should === book2.excel
231
+ book4 = Book.open(@simple_file, :force_excel => @book)
232
+ book4.excel.should === @book.excel
233
+ book3.close
234
+ book4.close
235
+ book5 = Book.open(@simple_file, :force_excel => book2.workbook)
236
+ book5.excel.should === book2.excel
237
+ win32ole_excel1 = WIN32OLE.connect(@book.workbook.Fullname).Application
238
+ puts "win32ole_excel1: #{win32ole_excel1}"
239
+ book6 = Book.open(@simple_file, :force_excel => win32ole_excel1)
240
+ book6.excel.should === @book.excel
241
+ end
242
+
243
+
152
244
  it "should open in a new Excel" do
153
245
  book2 = Book.open(@simple_file, :force_excel => :new)
154
246
  book2.should be_alive
@@ -225,12 +317,8 @@ describe Book do
225
317
  end
226
318
 
227
319
  it "should raise an error if no Excel or Book is given" do
228
- book2 = Book.open(@simple_file, :force_excel => :new)
229
- book2.excel.should_not == @book.excel
230
- book2.close
231
- @book.close
232
320
  expect{
233
- Book.open(@simple_file, :force_excel => :book)
321
+ Book.open(@simple_file, :force_excel => :b)
234
322
  }.to raise_error(ExcelError, "receiver instance is neither an Excel nor a Book")
235
323
  end
236
324
 
@@ -29,7 +29,7 @@ describe Book do
29
29
  end
30
30
 
31
31
  after do
32
- Excel.close_all
32
+ Excel.kill_all
33
33
  rm_tmp(@dir)
34
34
  end
35
35
 
@@ -29,7 +29,7 @@ describe Book do
29
29
  end
30
30
 
31
31
  after do
32
- Excel.close_all
32
+ Excel.kill_all
33
33
  rm_tmp(@dir)
34
34
  end
35
35
 
@@ -29,7 +29,7 @@ describe Book do
29
29
  end
30
30
 
31
31
  after do
32
- Excel.close_all
32
+ Excel.kill_all
33
33
  rm_tmp(@dir)
34
34
  end
35
35
 
@@ -47,6 +47,37 @@ describe Book do
47
47
 
48
48
  describe "open" do
49
49
 
50
+ context "with various file formats" do
51
+
52
+ it "should open linked workbook" do
53
+ book = Book.open(@linked_file, :visible => true)
54
+ book.close
55
+ end
56
+ end
57
+
58
+
59
+ context "with class identifier 'Workbook'" do
60
+
61
+ before do
62
+ @book = Workbook.open(@simple_file)
63
+ end
64
+
65
+ after do
66
+ @book.close rescue nil
67
+ end
68
+
69
+ it "should open in a new Excel" do
70
+ book2 = Workbook.open(@simple_file, :force_excel => :new)
71
+ book2.should be_alive
72
+ book2.should be_a Book
73
+ book2.excel.should_not == @book.excel
74
+ book2.should_not == @book
75
+ @book.Readonly.should be_false
76
+ book2.Readonly.should be_true
77
+ book2.close
78
+ end
79
+ end
80
+
50
81
  context "lift a workbook to a Book object" do
51
82
 
52
83
  before do
@@ -57,27 +88,23 @@ describe Book do
57
88
  @book.close
58
89
  end
59
90
 
60
- it "should yield an identical Book" do
91
+ it "should yield an identical Book and set visible and displayalerts values" do
61
92
  workbook = @book.workbook
62
- new_book = Book.new(workbook)
93
+ new_book = Book.new(workbook, :visible => true, :displayalerts => true)
63
94
  new_book.should be_a Book
64
95
  new_book.should be_alive
65
96
  new_book.should == @book
66
97
  new_book.filename.should == @book.filename
67
98
  new_book.excel.should == @book.excel
68
99
  new_book.should === @book
100
+ new_book.excel.visible.should be_true
101
+ new_book.excel.displayalerts.should be_true
69
102
  new_book.close
70
103
  end
71
- end
72
104
 
73
- context "with various file formats" do
74
-
75
- it "should open linked workbook" do
76
- book = Book.open(@linked_file, :visible => true)
77
- book.close
78
- end
79
105
  end
80
106
 
107
+
81
108
  context "with standard options" do
82
109
  before do
83
110
  @book = Book.open(@simple_file)
@@ -208,6 +235,25 @@ describe Book do
208
235
  book2.excel.should_not == @book.excel
209
236
  book2.close
210
237
  end
238
+
239
+ it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
240
+ book2 = Book.open(@another_simple_file)
241
+ book3 = Book.open(@different_file, :default_excel => book2.excel)
242
+ book3.excel.should === book2.excel
243
+ book3.close
244
+ book4 = Book.open(@different_file, :default_excel => book2)
245
+ book4.excel.should === book2.excel
246
+ book4.close
247
+ book5 = Book.open(@different_file, :default_excel => book2.workbook)
248
+ book5.excel.should === book2.excel
249
+ book5.close
250
+ win32ole_excel1 = WIN32OLE.connect(book2.workbook.Fullname).Application
251
+ book6 = Book.open(@different_file, :default_excel => win32ole_excel1)
252
+ book6.excel.should === book2.excel
253
+ book6.close
254
+ end
255
+
256
+
211
257
  end
212
258
 
213
259
  context "with :if_unsaved" do
@@ -26,7 +26,7 @@ describe "subclassed Book" do
26
26
  end
27
27
 
28
28
  after do
29
- RobustExcelOle::Excel.close_all
29
+ RobustExcelOle::Excel.kill_all
30
30
  rm_tmp(@dir)
31
31
  end
32
32
 
@@ -29,7 +29,7 @@ describe Book do
29
29
  end
30
30
 
31
31
  after do
32
- Excel.close_all
32
+ Excel.kill_all
33
33
  rm_tmp(@dir)
34
34
  end
35
35
 
@@ -55,7 +55,7 @@ describe Bookstore do
55
55
 
56
56
  after do
57
57
  begin
58
- Excel.close_all
58
+ Excel.kill_all
59
59
  rescue WeakRef::RefError => msg
60
60
  puts "#{msg.message}"
61
61
  Excel.kill_all
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -11,8 +11,7 @@ module RobustExcelOle
11
11
  describe Excel do
12
12
 
13
13
  before(:all) do
14
- Excel.kill_all
15
- #Excel.close_all
14
+ Excel.close_all
16
15
  end
17
16
 
18
17
  before do
@@ -25,7 +24,6 @@ module RobustExcelOle
25
24
  end
26
25
 
27
26
  after do
28
- #Excel.close_all
29
27
  Excel.kill_all
30
28
  rm_tmp(@dir)
31
29
  end
@@ -75,20 +73,37 @@ module RobustExcelOle
75
73
  @excel.should_not == excel
76
74
  end
77
75
 
78
- it "should work with reusing (uplifting) an Excel given as WIN32Ole object" do
79
- excel0 = Excel.create
80
- book = Book.open(@simple_file)
81
- excel = book.excel
82
- win32ole_excel = WIN32OLE.connect(book.workbook.Fullname).Application
83
- @excel = Excel.new(:reuse => win32ole_excel)
84
- creation_ok?
85
- @excel.should be_a Excel
86
- @excel.should be_alive
87
- @excel.should == excel
76
+ context "lifting an Excel instance given as WIN32Ole object" do
77
+
78
+ before do
79
+ @book = Book.open(@simple_file)
80
+ @excel = @book.excel
81
+ end
82
+
83
+ it "lifts an Excel instance given as WIN32Ole object" do
84
+ win32ole_excel = WIN32OLE.connect(@book.workbook.Fullname).Application
85
+ excel = Excel.new(win32ole_excel)
86
+ excel.should be_a Excel
87
+ excel.should be_alive
88
+ excel.should === @excel
89
+ end
90
+
91
+ it "lifts an Excel instance given as WIN32Ole object with options" do
92
+ @excel.Visible = true
93
+ @excel.DisplayAlerts = true
94
+ win32ole_excel = WIN32OLE.connect(@book.workbook.Fullname).Application
95
+ excel = Excel.new(win32ole_excel)
96
+ excel.should be_a Excel
97
+ excel.should be_alive
98
+ excel.should === @excel
99
+ excel.Visible.should be_true
100
+ excel.DisplayAlerts.should be_true
101
+ end
102
+
88
103
  end
89
104
  end
90
105
 
91
- context "with identity transparence" do
106
+ context "identity transparence" do
92
107
 
93
108
  before do
94
109
  @excel1 = Excel.create
@@ -149,52 +164,113 @@ module RobustExcelOle
149
164
 
150
165
  end
151
166
 
152
- context "with recreating Excel instances" do
167
+ context "recreating Excel instances" do
153
168
 
154
- it "should recreate a single Excel instance" do
155
- book = Book.open(@simple_file)
156
- excel = book.excel
157
- excel.close
158
- excel.should_not be_alive
159
- excel.recreate
160
- excel.should be_a Excel
161
- excel.should be_alive
162
- book.should be_alive
163
- excel.close
164
- excel.should_not be_alive
165
- end
169
+ context "with a single Excel instance" do
166
170
 
167
- it "should recreate several Excel instances" do
168
- book = Book.open(@simple_file)
169
- book2 = Book.open(@another_simple_file, :force_excel => book)
170
- book3 = Book.open(@different_file, :force_excel => :new)
171
- excel = book.excel
172
- excel3 = book3.excel
173
- excel.close
174
- excel3.close
175
- excel.should_not be_alive
176
- excel3.should_not be_alive
177
- excel.recreate(:visible => true)
178
- excel.should be_alive
179
- excel.should be_a Excel
180
- excel.visible.should be_true
181
- excel.displayalerts.should be_false
182
- book.should be_alive
183
- book2.should be_alive
184
- book.excel.should == excel
185
- book2.excel.should == excel
186
- excel3.recreate(:visible => true, :displayalerts => true)
187
- excel3.should be_alive
188
- excel3.should be_a Excel
189
- excel3.visible.should be_true
190
- excel3.displayalerts.should be_true
191
- book3.should be_alive
192
- excel.close
193
- excel.should_not be_alive
194
- excel3.close
195
- excel3.should_not be_alive
171
+ before do
172
+ @book1 = Book.open(@simple_file)
173
+ @excel1 = @book1.excel
174
+ end
175
+
176
+ it "should recreate an Excel instance" do
177
+ @excel1.close
178
+ @excel1.should_not be_alive
179
+ @excel1.recreate
180
+ @excel1.should be_a Excel
181
+ @excel1.should be_alive
182
+ @excel1.Visible.should be_false
183
+ @excel1.DisplayAlerts.should be_false
184
+ @book1.should_not be_alive
185
+ @book1.reopen
186
+ @book1.should be_alive
187
+ @excel1.close
188
+ @excel1.should_not be_alive
189
+ end
190
+
191
+ it "should recreate an Excel instance with old visible and displayalerts values" do
192
+ @excel1.visible = true
193
+ @excel1.displayalerts = true
194
+ @excel1.close
195
+ @excel1.should_not be_alive
196
+ @excel1.recreate
197
+ @excel1.should be_a Excel
198
+ @excel1.should be_alive
199
+ @excel1.Visible.should be_true
200
+ @excel1.DisplayAlerts.should be_true
201
+ @book1.reopen
202
+ @book1.should be_alive
203
+ @excel1.close
204
+ @excel1.should_not be_alive
205
+ end
206
+
207
+ it "should recreate an Excel instance with new visible and displayalerts values" do
208
+ @excel1.close
209
+ @excel1.should_not be_alive
210
+ @excel1.recreate(:visible => true, :displayalerts => true)
211
+ @excel1.should be_a Excel
212
+ @excel1.should be_alive
213
+ @excel1.Visible.should be_true
214
+ @excel1.DisplayAlerts.should be_true
215
+ @book1.reopen
216
+ @book1.should be_alive
217
+ @excel1.close
218
+ @excel1.should_not be_alive
219
+ end
220
+
221
+ it "should recreate an Excel instance and reopen the book" do
222
+ @excel1.close
223
+ @excel1.should_not be_alive
224
+ @excel1.recreate(:reopen_workbooks => true)
225
+ @excel1.should be_a Excel
226
+ @excel1.should be_alive
227
+ @excel1.Visible.should be_false
228
+ @excel1.DisplayAlerts.should be_false
229
+ @book1.should be_alive
230
+ @excel1.close
231
+ @excel1.should_not be_alive
232
+ end
196
233
  end
197
- end
234
+
235
+ context "with several Excel instances" do
236
+
237
+ before do
238
+ @book1 = Book.open(@simple_file)
239
+ @book2 = Book.open(@another_simple_file, :force_excel => @book1)
240
+ @book3 = Book.open(@different_file, :force_excel => :new)
241
+ @excel1 = @book1.excel
242
+ @excel3 = @book3.excel
243
+ @excel1.visible = true
244
+ @excel3.displayalerts = true
245
+ end
246
+
247
+ it "should recreate several Excel instances" do
248
+ @excel1.close
249
+ @excel3.close
250
+ @excel1.should_not be_alive
251
+ @excel3.should_not be_alive
252
+ @excel1.recreate(:reopen_workbooks => true, :displayalerts => true)
253
+ @excel1.should be_alive
254
+ @excel1.should be_a Excel
255
+ @excel1.visible.should be_true
256
+ @excel1.displayalerts.should be_true
257
+ @book1.should be_alive
258
+ @book2.should be_alive
259
+ @excel3.recreate(:visible => true)
260
+ @excel3.should be_alive
261
+ @excel3.should be_a Excel
262
+ @excel3.visible.should be_true
263
+ @excel3.displayalerts.should be_true
264
+ @book3.reopen
265
+ @book3.should be_alive
266
+ @book3.excel.should == @excel3
267
+ @excel1.close
268
+ @excel1.should_not be_alive
269
+ @excel3.close
270
+ @excel3.should_not be_alive
271
+ end
272
+ end
273
+ end
198
274
 
199
275
  context "close excel instances" do
200
276
  def direct_excel_creation_helper # :nodoc: #
@@ -446,6 +522,13 @@ module RobustExcelOle
446
522
  new_book2.close
447
523
  end
448
524
 
525
+ it "should close the Excel with saving the workbook" do
526
+ @excel.should be_alive
527
+ @excel.close(:if_unsaved => :keep_open)
528
+ @excel.should be_alive
529
+ @excel.close(:if_unsaved => :forget)
530
+ end
531
+
449
532
  it "should raise an error for invalid option" do
450
533
  expect {
451
534
  @excel.close(:if_unsaved => :invalid_option)
@@ -706,18 +789,19 @@ module RobustExcelOle
706
789
 
707
790
  before do
708
791
  @book = Book.open(@simple_file)
709
- @book2 = Book.open(@another_simple_file)
710
792
  @book3 = Book.open(@different_file, :read_only => true)
711
793
  sheet = @book[0]
712
794
  sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
713
795
  sheet3 = @book3[0]
714
796
  sheet3[1,1] = sheet3[1,1].value == "foo" ? "bar" : "foo"
797
+ @book2 = Book.open(@another_simple_file, :force_excel => :new)
798
+ sheet2 = @book2[0]
799
+ sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
715
800
  end
716
801
 
717
802
  it "should list unsaved workbooks" do
718
803
  @book.Saved.should be_false
719
- @book2.Save
720
- @book2.Saved.should be_true
804
+ @book2.Saved.should be_false
721
805
  @book3.Saved.should be_false
722
806
  excel = @book.excel
723
807
  # unsaved_workbooks yields different WIN32OLE objects than book.workbook
@@ -726,6 +810,17 @@ module RobustExcelOle
726
810
  uw_names.should == [@book.workbook.Name]
727
811
  end
728
812
 
813
+ it "should list all unsaved workbooks" do
814
+ result = []
815
+ Excel.unsaved_workbooks_all.each do |unsaved_workbooks|
816
+ uw_names = []
817
+ unsaved_workbooks.each {|uw| uw_names << uw.Name}
818
+ result << uw_names
819
+ end
820
+ result.include?([@book.workbook.Name]).should be_true
821
+ result.include?([@book2.workbook.Name]).should be_true
822
+ end
823
+
729
824
  end
730
825
  end
731
826