robust_excel_ole 0.6.2 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -853,7 +853,7 @@ describe Book do
853
853
  @key_sender.puts "{right}{enter}"
854
854
  expect{
855
855
  Book.open(@simple_file1, :if_unsaved => :alert)
856
- }.to raise_error(WorkbookError, "open: user canceled or open error")
856
+ }.to raise_error(ExcelError, "user canceled or runtime error")
857
857
  @book.should be_alive
858
858
  end
859
859
  end
@@ -885,7 +885,7 @@ describe Book do
885
885
  @key_sender.puts "{right}{enter}"
886
886
  expect{
887
887
  Book.open(@simple_file1, :if_unsaved => :excel)
888
- }.to raise_error(WorkbookError, "open: user canceled or open error")
888
+ }.to raise_error(ExcelError, "user canceled or runtime error")
889
889
  @book.should be_alive
890
890
  end
891
891
  end
@@ -1074,6 +1074,8 @@ describe Book do
1074
1074
  end
1075
1075
  end
1076
1076
 
1077
+ =begin
1078
+ # work in progress
1077
1079
  context "with :update_links" do
1078
1080
 
1079
1081
  it "should set update_links to :alert" do
@@ -1100,6 +1102,7 @@ describe Book do
1100
1102
  end
1101
1103
 
1102
1104
  end
1105
+ =end
1103
1106
 
1104
1107
  context "with :read_only" do
1105
1108
 
@@ -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 "save" do
@@ -379,9 +379,7 @@ describe Book do
379
379
  @key_sender.puts "{enter}"
380
380
  @key_sender.puts "{enter}"
381
381
  #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
382
- expect{
383
- @book.save_as(@simple_save_file1, :if_exists => :alert)
384
- }.to raise_error(WorkbookError, "not saved or canceled by user")
382
+ @book.save_as(@simple_save_file1, :if_exists => :alert)
385
383
  File.exist?(@simple_save_file1).should be_true
386
384
  File.size?(@simple_save_file1).should == @garbage_length
387
385
  @book.excel.DisplayAlerts.should == displayalert_value
@@ -394,20 +392,7 @@ describe Book do
394
392
  @key_sender.puts "{right}{enter}"
395
393
  @key_sender.puts "{right}{enter}"
396
394
  #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
397
- expect{
398
- @book.save_as(@simple_save_file1, :if_exists => :alert)
399
- }.to raise_error(WorkbookError, "not saved or canceled by user")
400
- File.exist?(@simple_save_file1).should be_true
401
- File.size?(@simple_save_file1).should == @garbage_length
402
- @book.excel.DisplayAlerts.should == displayalert_value
403
- end
404
-
405
- it "should report save errors and leave DisplayAlerts unchanged" do
406
- #@key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
407
- @book.ole_workbook.Close
408
- expect{
409
- @book.save_as(@simple_save_file1, :if_exists => :alert)
410
- }.to raise_error(ObjectNotAlive, "workbook is not alive")
395
+ @book.save_as(@simple_save_file1, :if_exists => :alert)
411
396
  File.exist?(@simple_save_file1).should be_true
412
397
  File.size?(@simple_save_file1).should == @garbage_length
413
398
  @book.excel.DisplayAlerts.should == displayalert_value
@@ -449,9 +434,7 @@ describe Book do
449
434
  @key_sender.puts "{enter}"
450
435
  @key_sender.puts "{enter}"
451
436
  #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
452
- expect{
453
- @book.save_as(@simple_save_file1, :if_exists => :excel)
454
- }.to raise_error(WorkbookError, "not saved or canceled by user")
437
+ @book.save_as(@simple_save_file1, :if_exists => :excel)
455
438
  File.exist?(@simple_save_file1).should be_true
456
439
  File.size?(@simple_save_file1).should == @garbage_length
457
440
  @book.excel.DisplayAlerts.should == displayalert_value
@@ -464,9 +447,7 @@ describe Book do
464
447
  @key_sender.puts "{right}{enter}"
465
448
  @key_sender.puts "{right}{enter}"
466
449
  #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
467
- expect{
468
- @book.save_as(@simple_save_file1, :if_exists => :excel)
469
- }.to_not raise_error
450
+ @book.save_as(@simple_save_file1, :if_exists => :excel)
470
451
  File.exist?(@simple_save_file1).should be_true
471
452
  File.size?(@simple_save_file1).should == @garbage_length
472
453
  @book.excel.DisplayAlerts.should == displayalert_value
@@ -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
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -371,79 +371,58 @@ module RobustExcelOle
371
371
 
372
372
  it "should close one Excel instance" do
373
373
  excel1 = Excel.create
374
- Excel.close_all
374
+ result = Excel.close_all
375
375
  sleep 0.2
376
376
  excel1.should_not be_alive
377
- Excel.excels_number.should == 0
377
+ result.should == [1,0]
378
378
  end
379
379
 
380
380
  it "should close two Excel instances" do
381
381
  excel1 = Excel.create
382
382
  excel2 = Excel.create
383
- Excel.close_all
383
+ result = Excel.close_all
384
384
  sleep 0.2
385
385
  excel1.should_not be_alive
386
386
  excel2.should_not be_alive
387
- Excel.excels_number.should == 0
387
+ result.should == [2,0]
388
388
  end
389
389
  end
390
390
 
391
391
  context "with unsaved workbooks" do
392
392
 
393
- context "with three Excel instances" do
394
-
395
- before do
396
- @book1 = Book.open(@simple_file1, :force_excel => :new)
397
- @book2 = Book.open(@another_simple_file, :force_excel => :new)
398
- @book3 = Book.open(@different_file, :force_excel => :new)
399
- end
393
+ context "with one Excel instance" do
400
394
 
401
- it "should close the 2nd and 3rd Excel instances that have saved workbooks" do
402
- old_cell_value1 = @book1.sheet(1)[1,1].value
403
- @book1.sheet(1)[1,1] = old_cell_value1 == "foo" ? "bar" : "foo"
404
- expect{
405
- Excel.close_all(:if_unsaved => :raise)
406
- }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
407
- sleep 0.2
408
- @book1.excel.should be_alive
409
- @book2.excel.should_not be_alive
410
- @book3.excel.should_not be_alive
411
- @book1.should be_alive
412
- @book2.should_not be_alive
413
- @book3.should_not be_alive
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
414
402
  end
415
403
 
416
- it "should close the 1st and 3rd Excel instances that have saved workbooks" do
417
- old_cell_value2 = @book2.sheet(1)[1,1].value
418
- @book2.sheet(1)[1,1] = old_cell_value2 == "foo" ? "bar" : "foo"
419
- expect{
420
- Excel.close_all(:if_unsaved => :raise)
421
- }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
404
+ it "should save the unsaved workbook" do
405
+ result = Excel.close_all(:if_unsaved => :save)
422
406
  sleep 0.2
423
- @book2.excel.should be_alive
424
- @book1.excel.should_not be_alive
425
- @book3.excel.should_not be_alive
426
- @book2.should be_alive
427
- @book1.should_not be_alive
428
- @book3.should_not be_alive
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]
429
413
  end
430
414
 
431
- it "should close the 2nd and 3rd Excel instances that have saved workbooks" do
432
- old_cell_value3 = @book3.sheet(1)[1,1].value
433
- @book3.sheet(1)[1,1] = old_cell_value3 == "foo" ? "bar" : "foo"
434
- expect{
435
- Excel.close_all(:if_unsaved => :raise)
436
- }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
415
+ it "should forget the unsaved workbook" do
416
+ result = Excel.close_all(:if_unsaved => :forget)
437
417
  sleep 0.2
438
- @book3.excel.should be_alive
439
- @book1.excel.should_not be_alive
440
- @book2.excel.should_not be_alive
441
- @book3.should be_alive
442
- @book1.should_not be_alive
443
- @book2.should_not be_alive
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]
444
424
  end
445
-
446
- end
425
+ end
447
426
 
448
427
  context "with two Excel instances" do
449
428
 
@@ -451,9 +430,6 @@ module RobustExcelOle
451
430
  book1 = Book.open(@simple_file1, :force_excel => :new)
452
431
  book2 = Book.open(@different_file, :force_excel => :new)
453
432
  @excel1 = book1.excel
454
- sheet1 = book1.sheet(1)
455
- @old_cell_value1 = sheet1[1,1].value
456
- sheet1[1,1] = sheet1[1,1].value == "foo" ? "bar" : "foo"
457
433
  @excel2 = book2.excel
458
434
  sheet2 = book2.sheet(1)
459
435
  @old_cell_value2 = sheet2[1,1].value
@@ -465,47 +441,49 @@ module RobustExcelOle
465
441
  Excel.close_all(:if_unsaved => :raise)
466
442
  }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
467
443
  sleep 0.2
468
- @excel1.should be_alive
444
+ @excel1.should_not be_alive
469
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]
470
450
  end
471
451
 
472
- it "should close the first Excel without unsaved workbooks and then raise an error per default" do
452
+ it "should close the first Excel without unsaved workbooks and then raise an error" do
473
453
  expect{
474
454
  Excel.close_all
475
455
  }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
476
456
  sleep 0.2
477
- @excel1.should be_alive
457
+ @excel1.should_not be_alive
478
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]
479
463
  end
480
464
 
481
465
  it "should close the Excel instances with saving the unsaved workbooks" do
482
- Excel.close_all(:if_unsaved => :save)
466
+ result = Excel.close_all(:if_unsaved => :save)
483
467
  sleep 0.2
484
468
  @excel1.should_not be_alive
485
469
  @excel2.should_not be_alive
486
- new_book1 = Book.open(@simple_file1)
487
- new_sheet1 = new_book1.sheet(1)
488
- new_sheet1[1,1].value.should_not == @old_cell_value1
489
- new_book1.close
490
470
  new_book2 = Book.open(@different_file1)
491
471
  new_sheet2 = new_book2.sheet(1)
492
472
  new_sheet2[1,1].value.should_not == @old_cell_value2
493
473
  new_book2.close
474
+ result.should == [2,0]
494
475
  end
495
476
 
496
477
  it "should close the Excel instances without saving the unsaved workbooks" do
497
- Excel.close_all(:if_unsaved => :forget)
478
+ result = Excel.close_all(:if_unsaved => :forget)
498
479
  sleep 0.2
499
480
  @excel1.should_not be_alive
500
481
  @excel2.should_not be_alive
501
- new_book1 = Book.open(@simple_file1)
502
- new_sheet1 = new_book1.sheet(1)
503
- new_sheet1[1,1].value.should == @old_cell_value1
504
- new_book1.close
505
482
  new_book2 = Book.open(@different_file1)
506
483
  new_sheet2 = new_book2.sheet(1)
507
484
  new_sheet2[1,1].value.should == @old_cell_value2
508
- new_book2.close
485
+ new_book2.close
486
+ result.should == [2,0]
509
487
  end
510
488
 
511
489
  it "should raise an error for invalid option" do
@@ -513,94 +491,86 @@ module RobustExcelOle
513
491
  Excel.close_all(:if_unsaved => :invalid_option)
514
492
  }.to raise_error(OptionInvalid, ":if_unsaved: invalid option: :invalid_option")
515
493
  end
516
-
517
494
  end
518
495
 
519
- context "with one Excel instance" do
496
+ context "with three Excel instances" do
520
497
 
521
- it "should save the unsaved workbook" do
522
- book1 = Book.open(@simple_file, :visible => true)
523
- book1.sheet(1)[1,1] = "bar"
524
- book1.Saved.should be_false
525
- Excel.close_all(:if_unsaved => :save)
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"
526
504
  end
527
505
 
528
- it "should forget the unsaved workbook" do
529
- book1 = Book.open(@simple_file, :visible => true)
530
- excel1 = book1.excel
531
- excel1.displayalerts = false
532
- sheet1 = book1.sheet(1)
533
- old_cell_value1 = sheet1[1,1].value
534
- sheet1[1,1] = sheet1[1,1].value == "foo" ? "bar" : "foo"
535
- book1.Saved.should be_false
536
- Excel.close_all(:if_unsaved => :forget)
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]
537
517
  end
538
- end
539
- end
540
- end
541
-
542
- =begin
543
- context "with :if_unsaved => :alert" do
544
- before do
545
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
546
- book2 = Book.open(@simple_file, :force_excel => :new)
547
- #@excel1 = book1.excel
548
- @excel2 = book2.excel
549
- sheet2 = book2.sheet(1)
550
- @old_cell_value2 = sheet2[1,1].value
551
- sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
552
- end
553
-
554
- after do
555
- @key_sender.close
556
518
  end
557
519
 
558
- it "should save if user answers 'yes'" do
559
- @key_sender.puts "{enter}"
560
- Excel.close_all(:if_unsaved => :alert)
561
- @excel2.should_not be_alive
562
- new_book2 = Book.open(@simple_file)
563
- new_sheet2 = new_book2.sheet(1)
564
- new_sheet2[1,1].value.should_not == @old_cell_value2
565
- new_book2.close
566
- end
520
+ context "with unknown Excel instances" do
567
521
 
568
- it "should not save if user answers 'no'" do
569
- @key_sender.puts "{right}{enter}"
570
- @key_sender.puts "{right}{enter}"
571
- Excel.close_all(:if_unsaved => :alert)
572
- @excel2.should_not be_alive
573
- new_book2 = Book.open(@simple_file)
574
- new_sheet2 = new_book2.sheet(1)
575
- new_sheet2[1,1].value.should == @old_cell_value2
576
- new_book2.close
577
- end
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
578
530
 
579
- #it "should not save if user answers 'cancel'" do
580
- # @key_sender.puts "{left}{enter}"
581
- # @key_sender.puts "{left}{enter}"
582
- # @key_sender.puts "{left}{enter}"
583
- # expect{
584
- # Excel.close_all(:if_unsaved => :alert)
585
- # }.to raise_error(ExcelUserCanceled, "close: canceled by user")
586
- # end
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
587
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
588
558
  end
559
+
589
560
  end
590
561
  end
591
- =end
592
-
562
+
593
563
  describe "close" do
594
564
 
595
- context "with a saved workbook" do
565
+ context "with saved workbooks" do
596
566
 
597
567
  before do
598
568
  @excel = Excel.create
599
569
  @book = Book.open(@simple_file)
570
+ @excel.should be_alive
600
571
  end
601
572
 
602
573
  it "should close the Excel" do
603
- @excel.should be_alive
604
574
  @book.should be_alive
605
575
  @excel.close
606
576
  sleep 0.2
@@ -608,32 +578,13 @@ module RobustExcelOle
608
578
  @book.should_not be_alive
609
579
  end
610
580
 
611
- it "should close the Excel hard" do
612
- @excel.should be_alive
613
- @book.should be_alive
614
- @excel.close(:hard => true)
615
- sleep 0.2
616
- @excel.should_not be_alive
617
- @book.should_not be_alive
618
- end
619
-
620
581
  it "should close the Excel without destroying the others" do
621
- @excel.should be_alive
622
582
  excel2 = Excel.create
623
583
  @excel.close
624
584
  sleep 0.2
625
585
  @excel.should_not be_alive
626
586
  excel2.should be_alive
627
587
  end
628
-
629
- it "should close the Excel hard without destroying the others" do
630
- @excel.should be_alive
631
- excel2 = Excel.create
632
- @excel.close(:hard => true)
633
- sleep 0.2
634
- @excel.should_not be_alive
635
- excel2.should be_alive
636
- end
637
588
  end
638
589
 
639
590
  context "with unsaved workbooks" do
@@ -648,74 +599,60 @@ module RobustExcelOle
648
599
  sheet2 = @book2.sheet(1)
649
600
  @old_cell_value2 = sheet2[1,1].value
650
601
  sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
651
- end
652
-
653
- it "should raise an error" do
654
602
  @excel.should be_alive
655
603
  @book.should be_alive
656
604
  @book.saved.should be_false
657
605
  @book2.should be_alive
658
606
  @book2.saved.should be_false
607
+ end
608
+
609
+ it "should raise an error" do
659
610
  expect{
660
611
  @excel.close(:if_unsaved => :raise)
661
612
  }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
662
613
  end
663
614
 
664
- it "should close the Excel without saving the workbook even with displayalerts true" do
665
- @excel.displayalerts = false
666
- @excel.should be_alive
667
- @excel.displayalerts = true
668
- @excel.close(:if_unsaved => :forget)
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)
669
623
  sleep 0.2
670
624
  @excel.should_not be_alive
625
+ result.should == 1
671
626
  new_book = Book.open(@simple_file)
672
627
  new_sheet = new_book.sheet(1)
673
628
  new_sheet[1,1].value.should == @old_cell_value
674
629
  new_book.close
675
- new_book2 = Book.open(@another_simple_file)
676
- new_sheet2 = new_book2.sheet(1)
677
- new_sheet2[1,1].value.should == @old_cell_value2
678
- new_book2.close(:if_unsaved => :forget)
679
630
  end
680
-
681
- it "should close the Excel without saving the workbook" do
631
+
632
+ it "should close the Excel without saving the workbook even with displayalerts true" do
682
633
  @excel.displayalerts = false
683
634
  @excel.should be_alive
684
- @excel.displayalerts = false
685
- @excel.close(:if_unsaved => :forget)
635
+ @excel.displayalerts = true
636
+ result = @excel.close(:if_unsaved => :forget)
686
637
  sleep 0.2
638
+ result.should == 1
687
639
  @excel.should_not be_alive
688
640
  new_book = Book.open(@simple_file)
689
641
  new_sheet = new_book.sheet(1)
690
642
  new_sheet[1,1].value.should == @old_cell_value
691
643
  new_book.close
692
- new_book2 = Book.open(@another_simple_file)
693
- new_sheet2 = new_book2.sheet(1)
694
- new_sheet2[1,1].value.should == @old_cell_value2
695
- new_book2.close(:if_unsaved => :forget)
696
644
  end
697
645
 
698
646
  it "should close the Excel with saving the workbook" do
699
647
  @excel.should be_alive
700
- @excel.close(:if_unsaved => :save)
648
+ result = @excel.close(:if_unsaved => :save)
701
649
  sleep 0.2
650
+ result.should == 1
702
651
  @excel.should_not be_alive
703
652
  new_book = Book.open(@simple_file)
704
653
  new_sheet = new_book.sheet(1)
705
654
  new_sheet[1,1].value.should_not == @old_cell_value
706
655
  new_book.close
707
- new_book2 = Book.open(@another_simple_file)
708
- new_sheet2 = new_book2.sheet(1)
709
- new_sheet2[1,1].value.should_not == @old_cell_value2
710
- new_book2.close
711
- end
712
-
713
- it "should close the Excel with saving the workbook" do
714
- @excel.should be_alive
715
- @excel.close(:if_unsaved => :keep_open)
716
- sleep 0.2
717
- @excel.should be_alive
718
- @excel.close(:if_unsaved => :forget)
719
656
  end
720
657
 
721
658
  it "should raise an error for invalid option" do
@@ -723,45 +660,14 @@ module RobustExcelOle
723
660
  @excel.close(:if_unsaved => :invalid_option)
724
661
  }.to raise_error(OptionInvalid, ":if_unsaved: invalid option: :invalid_option")
725
662
  end
726
-
727
- it "should raise an error by default" do
728
- @excel.should be_alive
729
- expect{
730
- @excel.close
731
- }.to raise_error(UnsavedWorkbooks, "Excel contains unsaved workbooks")
732
- end
733
-
734
- it "should close the Excel without saving the workbook hard" do
735
- @excel.should be_alive
736
- @book.should be_alive
737
- @book.saved.should be_false
738
- @excel.close(:if_unsaved => :forget, :hard => true)
739
- sleep 0.2
740
- @excel.should_not be_alive
741
- @book.should_not be_alive
742
- new_book = Book.open(@simple_file)
743
- new_sheet = new_book.sheet(1)
744
- new_sheet[1,1].value.should == @old_cell_value
745
- new_book.close
746
- new_book.excel.close(:hard => true)
747
- sleep 0.2
748
- procs = WIN32OLE.connect("winmgmts:\\\\.")
749
- processes = procs.InstancesOf("win32_process")
750
- result = []
751
- processes.each do |p|
752
- result << p if p.name == "EXCEL.EXE"
753
- end
754
- result.should be_empty
755
- end
756
-
757
663
  end
758
664
 
759
665
  context "with :if_unsaved => :alert" do
760
666
 
761
667
  before do
762
668
  @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
763
- @excel = Excel.create
764
- @book = Book.open(@simple_file)
669
+ @excel = Excel.create(:visible => true)
670
+ @book = Book.open(@simple_file, :visible => true)
765
671
  sheet = @book.sheet(1)
766
672
  @old_cell_value = sheet[1,1].value
767
673
  sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
@@ -775,8 +681,9 @@ module RobustExcelOle
775
681
  # "Yes" is to the left of "No", which is the default. --> language independent
776
682
  @excel.should be_alive
777
683
  @key_sender.puts "{enter}"
778
- @excel.close(:if_unsaved => :alert)
684
+ result = @excel.close(:if_unsaved => :alert)
779
685
  @excel.should_not be_alive
686
+ result.should == 1
780
687
  new_book = Book.open(@simple_file)
781
688
  new_sheet = new_book.sheet(1)
782
689
  new_sheet[1,1].value.should_not == @old_cell_value
@@ -788,8 +695,9 @@ module RobustExcelOle
788
695
  @book.should be_alive
789
696
  @book.saved.should be_false
790
697
  @key_sender.puts "{right}{enter}"
791
- @excel.close(:if_unsaved => :alert)
698
+ result = @excel.close(:if_unsaved => :alert)
792
699
  @excel.should_not be_alive
700
+ result.should == 1
793
701
  @book.should_not be_alive
794
702
  new_book = Book.open(@simple_file)
795
703
  new_sheet = new_book.sheet(1)
@@ -806,11 +714,104 @@ module RobustExcelOle
806
714
  @key_sender.puts "{left}{enter}"
807
715
  expect{
808
716
  @excel.close(:if_unsaved => :alert)
809
- }.to_not raise_error
717
+ }.to raise_error(ExcelError, "user canceled or runtime error")
810
718
  end
719
+ end
720
+ end
811
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
+
787
+
788
+ describe "unsaved_workbooks" do
789
+
790
+ context "with standard" do
791
+
792
+ before do
793
+ @book = Book.open(@simple_file)
794
+ sheet = @book.sheet(1)
795
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
796
+ @book3 = Book.open(@different_file, :read_only => true)
797
+ sheet3 = @book3.sheet(1)
798
+ sheet3[1,1] = sheet3[1,1].value == "foo" ? "bar" : "foo"
799
+ @book.Saved.should be_false
800
+ @book3.Saved.should be_false
801
+ end
802
+
803
+ it "should list unsaved workbooks" do
804
+ excel = @book.excel
805
+ # unsaved_workbooks yields different WIN32OLE objects than book.workbook
806
+ uw_names = []
807
+ excel.unsaved_workbooks.each {|uw| uw_names << uw.Name}
808
+ uw_names.should == [@book.ole_workbook.Name]
809
+ end
810
+
811
+ it "should yield true, that there are unsaved workbooks" do
812
+ Excel.contains_unsaved_workbooks?.should be_true
813
+ end
812
814
  end
813
-
814
815
  end
815
816
 
816
817
  describe "alive" do
@@ -990,7 +991,7 @@ module RobustExcelOle
990
991
  excel2.Visible.should be_false
991
992
  excel2.visible.should be_false
992
993
  excel2.DisplayAlerts.should be_false
993
- excel2.displayalerts.should == false
994
+ excel2.displayalerts.should == :if_visible
994
995
  end
995
996
 
996
997
  it "should take Visible and DisplayAlerts from the connected Excel" do
@@ -998,8 +999,8 @@ module RobustExcelOle
998
999
  excel2 = Excel.current(:visible => true)
999
1000
  excel2.Visible.should be_true
1000
1001
  excel2.visible.should be_true
1001
- excel2.DisplayAlerts.should be_false
1002
- excel2.displayalerts.should be_false
1002
+ excel2.DisplayAlerts.should be_true
1003
+ excel2.displayalerts.should == :if_visible
1003
1004
  end
1004
1005
 
1005
1006
  it "should set Excel visible and invisible with current" do
@@ -1016,8 +1017,8 @@ module RobustExcelOle
1016
1017
  excel2 = Excel.current(:visible => true)
1017
1018
  excel2.Visible.should be_true
1018
1019
  excel2.visible.should be_true
1019
- excel2.displayalerts.should be_false
1020
- excel2.DisplayAlerts.should be_false
1020
+ excel2.displayalerts.should == :if_visible
1021
+ excel2.DisplayAlerts.should be_true
1021
1022
  end
1022
1023
 
1023
1024
  it "should set Excel visible and invisible" do
@@ -1051,19 +1052,19 @@ module RobustExcelOle
1051
1052
  excel3.Visible.should be_false
1052
1053
  excel3.visible.should be_false
1053
1054
  excel3.DisplayAlerts.should be_false
1054
- excel3.displayalerts.should be_false
1055
+ excel3.displayalerts.should == :if_visible
1055
1056
  excel4 = Excel.current(:visible => true)
1056
1057
  excel4.should === excel
1057
1058
  excel4.Visible.should be_true
1058
1059
  excel4.visible.should be_true
1059
- excel4.DisplayAlerts.should be_false
1060
- excel4.displayalerts.should be_false
1060
+ excel4.DisplayAlerts.should be_true
1061
+ excel4.displayalerts.should == :if_visible
1061
1062
  excel5 = Excel.current(:visible => false)
1062
1063
  excel5.should === excel
1063
1064
  excel5.Visible.should be_false
1064
1065
  excel5.visible.should be_false
1065
1066
  excel5.DisplayAlerts.should be_false
1066
- excel5.displayalerts.should be_false
1067
+ excel5.displayalerts.should == :if_visible
1067
1068
  end
1068
1069
 
1069
1070
  it "should enable or disable Excel DispayAlerts" do
@@ -1376,48 +1377,7 @@ module RobustExcelOle
1376
1377
  excel3.should_not == excel4
1377
1378
  end
1378
1379
  end
1379
-
1380
- describe "unsaved_workbooks" do
1381
-
1382
- context "with standard" do
1383
-
1384
- before do
1385
- @book = Book.open(@simple_file)
1386
- @book3 = Book.open(@different_file, :read_only => true)
1387
- sheet = @book.sheet(1)
1388
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1389
- sheet3 = @book3.sheet(1)
1390
- sheet3[1,1] = sheet3[1,1].value == "foo" ? "bar" : "foo"
1391
- @book2 = Book.open(@another_simple_file, :force_excel => :new)
1392
- sheet2 = @book2.sheet(1)
1393
- sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
1394
- end
1395
-
1396
- it "should list unsaved workbooks" do
1397
- @book.Saved.should be_false
1398
- @book2.Saved.should be_false
1399
- @book3.Saved.should be_false
1400
- excel = @book.excel
1401
- # unsaved_workbooks yields different WIN32OLE objects than book.workbook
1402
- uw_names = []
1403
- excel.unsaved_workbooks.each {|uw| uw_names << uw.Name}
1404
- uw_names.should == [@book.ole_workbook.Name]
1405
- end
1406
-
1407
- it "should list all unsaved workbooks" do
1408
- result = []
1409
- Excel.unsaved_workbooks_all.each do |unsaved_workbooks|
1410
- uw_names = []
1411
- unsaved_workbooks.each {|uw| uw_names << uw.Name}
1412
- result << uw_names
1413
- end
1414
- result.include?([@book.ole_workbook.Name]).should be_true
1415
- result.include?([@book2.ole_workbook.Name]).should be_true
1416
- end
1417
-
1418
- end
1419
- end
1420
-
1380
+
1421
1381
  describe "generate workbook" do
1422
1382
 
1423
1383
  context "with standard" do