robust_excel_ole 1.1.4 → 1.1.5

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