robust_excel_ole 1.26 → 1.31

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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +17 -0
  3. data/README.rdoc +12 -5
  4. data/benchmarking/reo_example.rb +1 -1
  5. data/benchmarking/reo_example1.rb +1 -1
  6. data/benchmarking/reo_example2.rb +1 -1
  7. data/bin/jreo +20 -1
  8. data/bin/reo +20 -1
  9. data/docs/README_open.rdoc +8 -4
  10. data/docs/README_sheet.rdoc +1 -7
  11. data/examples/introductory_examples/example_open.rb +11 -0
  12. data/lib/robust_excel_ole.rb +19 -16
  13. data/lib/robust_excel_ole/base.rb +5 -0
  14. data/lib/robust_excel_ole/bookstore.rb +1 -1
  15. data/lib/robust_excel_ole/cell.rb +1 -1
  16. data/lib/robust_excel_ole/cygwin.rb +2 -0
  17. data/lib/robust_excel_ole/excel.rb +12 -22
  18. data/lib/robust_excel_ole/general.rb +270 -206
  19. data/lib/robust_excel_ole/list_object.rb +18 -115
  20. data/lib/robust_excel_ole/list_row.rb +128 -0
  21. data/lib/robust_excel_ole/range.rb +5 -1
  22. data/lib/robust_excel_ole/range_owners.rb +4 -71
  23. data/lib/robust_excel_ole/version.rb +1 -1
  24. data/lib/robust_excel_ole/workbook.rb +71 -29
  25. data/lib/robust_excel_ole/worksheet.rb +107 -22
  26. data/lib/spec_helper.rb +1 -1
  27. data/spec/address_tool_spec.rb +2 -2
  28. data/spec/base_spec.rb +19 -17
  29. data/spec/bookstore_spec.rb +1 -1
  30. data/spec/cell_spec.rb +1 -1
  31. data/spec/cygwin_spec.rb +1 -1
  32. data/spec/data/more_data/workbook.xls +0 -0
  33. data/spec/excel_spec.rb +1 -1
  34. data/spec/general_spec.rb +64 -7
  35. data/spec/list_object_spec.rb +85 -20
  36. data/spec/range_spec.rb +1 -14
  37. data/spec/spec_helper.rb +1 -1
  38. data/spec/workbook_spec.rb +12 -12
  39. data/spec/workbook_specs/workbook_all_spec.rb +8 -28
  40. data/spec/workbook_specs/workbook_close_spec.rb +1 -1
  41. data/spec/workbook_specs/workbook_misc_spec.rb +33 -33
  42. data/spec/workbook_specs/workbook_open_spec.rb +56 -5
  43. data/spec/workbook_specs/workbook_save_spec.rb +1 -1
  44. data/spec/workbook_specs/workbook_sheet_spec.rb +1 -1
  45. data/spec/workbook_specs/workbook_subclass_spec.rb +1 -1
  46. data/spec/workbook_specs/workbook_unobtr_spec.rb +273 -115
  47. data/spec/worksheet_spec.rb +51 -19
  48. metadata +3 -5
  49. data/jreo.bat +0 -3
  50. data/lib/reo_console.rb +0 -68
  51. data/reo.bat +0 -3
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), './spec_helper')
2
+ require_relative 'spec_helper'
3
3
 
4
4
  include RobustExcelOle
5
5
  include General
@@ -73,19 +73,6 @@ describe RobustExcelOle::Range do
73
73
  end
74
74
  end
75
75
 
76
- it "should work with [] doing cashing synchonized, from #each to #[]" do
77
- @range2[0].Value.should == 'simple'
78
- @range2[1].Value.should == 'file'
79
- @range2[2].Value.should == 'sheet2'
80
- i = 0
81
- @range2.each do |cell|
82
- cell.Value = 'foo' if i == 0
83
- cell.Value = 'bar' if i == 1
84
- cell.Value = 'simple' if i == 2
85
- i += 1
86
- end
87
- end
88
-
89
76
  end
90
77
 
91
78
  describe "#values" do
@@ -2,7 +2,7 @@
2
2
  require "rspec"
3
3
  require 'tmpdir'
4
4
  require "fileutils"
5
- require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
5
+ require_relative '../lib/robust_excel_ole'
6
6
 
7
7
  # @private
8
8
  module RobustExcelOle::SpecHelpers
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require_relative 'spec_helper'
4
4
  require 'pathname'
5
5
 
6
6
  $VERBOSE = nil
@@ -1103,12 +1103,12 @@ describe Workbook do
1103
1103
  end
1104
1104
 
1105
1105
  it "should return value of a range" do
1106
- @book1.namevalue_glob("new").should == "foo"
1107
- @book1.namevalue_glob("one").should == 1
1108
- @book1.namevalue_glob("firstrow").should == [[1,2]]
1109
- @book1.namevalue_glob("four").should == [[1,2],[3,4]]
1110
- @book1.namevalue_glob("firstrow").should_not == "12"
1111
- @book1.namevalue_glob("firstcell").should == "foo"
1106
+ @book1.namevalue_global("new").should == "foo"
1107
+ @book1.namevalue_global("one").should == 1
1108
+ @book1.namevalue_global("firstrow").should == [[1,2]]
1109
+ @book1.namevalue_global("four").should == [[1,2],[3,4]]
1110
+ @book1.namevalue_global("firstrow").should_not == "12"
1111
+ @book1.namevalue_global("firstcell").should == "foo"
1112
1112
  end
1113
1113
 
1114
1114
  it "should return value of a range via []" do
@@ -1121,17 +1121,17 @@ describe Workbook do
1121
1121
  end
1122
1122
 
1123
1123
  it "should set value of a range" do
1124
- @book1.set_namevalue_glob("new", "bar")
1125
- @book1.namevalue_glob("new").should == "bar"
1124
+ @book1.set_namevalue_global("new", "bar")
1125
+ @book1.namevalue_global("new").should == "bar"
1126
1126
  end
1127
1127
 
1128
1128
  it "should set value of a range via []=" do
1129
1129
  @book1["new"] = "bar"
1130
- @book1.namevalue_glob("new").should == "bar"
1130
+ @book1.namevalue_global("new").should == "bar"
1131
1131
  end
1132
1132
 
1133
1133
  #it "should evaluate a formula" do
1134
- # @book1.namevalue_glob("named_formula").should == 4
1134
+ # @book1.namevalue_global("named_formula").should == 4
1135
1135
  #end
1136
1136
 
1137
1137
  #it "should evaluate a formula via []" do
@@ -1139,7 +1139,7 @@ describe Workbook do
1139
1139
  #end
1140
1140
 
1141
1141
  #it "should return default value if name not defined" do
1142
- # @book1.namevalue_glob("foo", :default => 2).should == 2
1142
+ # @book1.namevalue_global("foo", :default => 2).should == 2
1143
1143
  #end
1144
1144
 
1145
1145
  end
@@ -1,33 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
3
+ require_relative '../spec_helper'
4
4
 
5
5
  $VERBOSE = nil
6
6
 
7
- require File.expand_path("workbook_open_spec", File.dirname(__FILE__))
8
- require File.expand_path("workbook_close_spec", File.dirname(__FILE__))
9
- require File.expand_path("workbook_save_spec", File.dirname(__FILE__))
10
- require File.expand_path("workbook_misc_spec", File.dirname(__FILE__))
11
- require File.expand_path("workbook_sheet_spec", File.dirname(__FILE__))
12
- require File.expand_path("workbook_unobtr_spec", File.dirname(__FILE__))
13
- require File.expand_path("workbook_subclass_spec", File.dirname(__FILE__))
14
-
15
- =begin
16
- $VERBOSE = nil
17
-
18
- include General
19
-
20
- unless Object.method_defined?(:require_relative)
21
- def require_relative path
22
- require File.expand_path(path, File.dirname(__FILE__))
23
- end
24
- end
25
-
26
- require_relative "workbook_open_spec"
27
- require_relative "workbook_close_spec"
28
- require_relative "workbook_save_spec"
29
- require_relative "workbook_misc_spec"
30
- require_relative "workbook_sheet_spec"
31
- require_relative "workbook_unobtr_spec"
32
- require_relative "workbook_subclass_spec"
33
- =end
7
+ require_relative 'workbook_open_spec'
8
+ require_relative 'workbook_close_spec'
9
+ require_relative 'workbook_save_spec'
10
+ require_relative 'workbook_misc_spec'
11
+ require_relative 'workbook_sheet_spec'
12
+ require_relative 'workbook_unobtr_spec'
13
+ require_relative 'workbook_subclass_spec'
@@ -1,7 +1,7 @@
1
1
 
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
- require File.join(File.dirname(__FILE__), './../spec_helper')
4
+ require_relative '../spec_helper'
5
5
 
6
6
 
7
7
  $VERBOSE = nil
@@ -1,13 +1,14 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
4
-
3
+ require_relative '../spec_helper'
5
4
 
6
5
  $VERBOSE = nil
7
6
 
8
7
  include RobustExcelOle
9
8
  include General
10
9
 
10
+ using ToReoRefinement
11
+
11
12
  describe Workbook do
12
13
 
13
14
  before(:all) do
@@ -24,7 +25,6 @@ describe Workbook do
24
25
  @different_file = @dir + '/different_workbook.xls'
25
26
  @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
27
  @another_simple_file = @dir + '/another_workbook.xls'
27
- @linked_file = @dir + '/workbook_linked.xlsm'
28
28
  @simple_file_xlsm = @dir + '/workbook.xls'
29
29
  @simple_file_xlsx = @dir + '/workbook.xlsx'
30
30
  @simple_file1 = @simple_file
@@ -669,7 +669,7 @@ describe Workbook do
669
669
  end
670
670
  end
671
671
 
672
- describe "namevalue_glob, set_namevalue_glob, [], []=" do
672
+ describe "namevalue_global, set_namevalue_global, [], []=" do
673
673
 
674
674
  before do
675
675
  @book1 = Workbook.open(@another_simple_file)
@@ -680,12 +680,12 @@ describe Workbook do
680
680
  end
681
681
 
682
682
  it "should return value of a range" do
683
- @book1.namevalue_glob("new").should == "foo"
684
- @book1.namevalue_glob("one").should == 1
685
- @book1.namevalue_glob("firstrow").should == [[1,2]]
686
- @book1.namevalue_glob("four").should == [[1,2],[3,4]]
687
- @book1.namevalue_glob("firstrow").should_not == "12"
688
- @book1.namevalue_glob("firstcell").should == "foo"
683
+ @book1.namevalue_global("new").should == "foo"
684
+ @book1.namevalue_global("one").should == 1
685
+ @book1.namevalue_global("firstrow").should == [[1,2]]
686
+ @book1.namevalue_global("four").should == [[1,2],[3,4]]
687
+ @book1.namevalue_global("firstrow").should_not == "12"
688
+ @book1.namevalue_global("firstcell").should == "foo"
689
689
  end
690
690
 
691
691
  it "should return value of a range via []" do
@@ -698,17 +698,17 @@ describe Workbook do
698
698
  end
699
699
 
700
700
  it "should set value of a range" do
701
- @book1.set_namevalue_glob("new", "bar")
702
- @book1.namevalue_glob("new").should == "bar"
701
+ @book1.set_namevalue_global("new", "bar")
702
+ @book1.namevalue_global("new").should == "bar"
703
703
  end
704
704
 
705
705
  it "should set value of a range via []=" do
706
706
  @book1["new"] = "bar"
707
- @book1.namevalue_glob("new").should == "bar"
707
+ @book1.namevalue_global("new").should == "bar"
708
708
  end
709
709
 
710
710
  #it "should evaluate a formula" do
711
- # @book1.namevalue_glob("named_formula").should == 4
711
+ # @book1.namevalue_global("named_formula").should == 4
712
712
  #end
713
713
 
714
714
  #it "should evaluate a formula via []" do
@@ -717,28 +717,28 @@ describe Workbook do
717
717
 
718
718
  it "should raise an error if name not defined and default value is not provided" do
719
719
  expect {
720
- @book1.namevalue_glob("foo", :default => nil)
720
+ @book1.namevalue_global("foo", :default => nil)
721
721
  }.to_not raise_error
722
722
  expect {
723
- @book1.namevalue_glob("foo", :default => :__not_provided)
723
+ @book1.namevalue_global("foo", :default => :__not_provided)
724
724
  }.to raise_error(NameNotFound, /name "foo" not in #<Workbook: another_workbook/)
725
725
  expect {
726
- @book1.namevalue_glob("foo")
726
+ @book1.namevalue_global("foo")
727
727
  }.to raise_error(NameNotFound, /name "foo" not in #<Workbook: another_workbook/)
728
- @book1.namevalue_glob("foo", :default => nil).should be_nil
729
- @book1.namevalue_glob("foo", :default => 1).should == 1
728
+ @book1.namevalue_global("foo", :default => nil).should be_nil
729
+ @book1.namevalue_global("foo", :default => 1).should == 1
730
730
  expect {
731
- @book1.set_namevalue_glob("foo","bar")
731
+ @book1.set_namevalue_global("foo","bar")
732
732
  }.to raise_error(RangeNotEvaluatable, /cannot assign value to range named "foo"/)
733
733
  expect {
734
734
  @book1["foo"] = "bar"
735
735
  }.to raise_error(RangeNotEvaluatable, /cannot assign value to range named "foo"/)
736
- @book1.namevalue_glob("empty", :default => 1).should be_nil
736
+ @book1.namevalue_global("empty", :default => 1).should be_nil
737
737
  end
738
738
 
739
739
  it "should raise an error if name was defined but contents is calcuated" do
740
740
  expect {
741
- @book1.set_namevalue_glob("named_formula","bar")
741
+ @book1.set_namevalue_global("named_formula","bar")
742
742
  }.to raise_error(RangeNotEvaluatable, /cannot assign value to range named "named_formula" in #<Workbook: another_workbook/)
743
743
  expect {
744
744
  @book1["named_formula"] = "bar"
@@ -747,19 +747,19 @@ describe Workbook do
747
747
 
748
748
  # Excel Bug: for local names without uqifier: takes the first sheet as default even if another sheet is activated
749
749
  it "should take the first sheet as default even if the second sheet is activated" do
750
- @book1.namevalue_glob("Sheet1!localname").should == "bar"
751
- @book1.namevalue_glob("Sheet2!localname").should == "simple"
752
- @book1.namevalue_glob("localname").should == "bar"
750
+ @book1.namevalue_global("Sheet1!localname").should == "bar"
751
+ @book1.namevalue_global("Sheet2!localname").should == "simple"
752
+ @book1.namevalue_global("localname").should == "bar"
753
753
  @book1.Worksheets.Item(2).Activate
754
- @book1.namevalue_glob("localname").should == "bar"
754
+ @book1.namevalue_global("localname").should == "bar"
755
755
  @book1.Worksheets.Item(1).Delete
756
- @book1.namevalue_glob("localname").should == "simple"
756
+ @book1.namevalue_global("localname").should == "simple"
757
757
  end
758
758
 
759
759
  it "should color the cell (deprecated)" do
760
- @book1.set_namevalue_glob("new", "bar")
760
+ @book1.set_namevalue_global("new", "bar")
761
761
  @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == -4142
762
- @book1.set_namevalue_glob("new", "bar", :color => 4)
762
+ @book1.set_namevalue_global("new", "bar", :color => 4)
763
763
  @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
764
764
  @book1["new"].should == "bar"
765
765
  @book1["new"] = "bar"
@@ -771,9 +771,9 @@ describe Workbook do
771
771
  end
772
772
 
773
773
  it "should color the cell" do
774
- @book1.set_namevalue_glob("new", "bar")
774
+ @book1.set_namevalue_global("new", "bar")
775
775
  @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == -4142
776
- @book1.set_namevalue_glob("new", "bar", :color => 4)
776
+ @book1.set_namevalue_global("new", "bar", :color => 4)
777
777
  @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
778
778
  @book1["new"].should == "bar"
779
779
  @book1["new"] = "bar"
@@ -796,7 +796,7 @@ describe Workbook do
796
796
 
797
797
  it "should rename a range" do
798
798
  @book1.rename_range("four","five")
799
- @book1.namevalue_glob("five").should == [[1,2],[3,4]]
799
+ @book1.namevalue_global("five").should == [[1,2],[3,4]]
800
800
  expect {
801
801
  @book1.rename_range("four","five")
802
802
  }.to raise_error(NameNotFound, /name "four" not in "another_workbook.xls"/)
@@ -1165,7 +1165,7 @@ describe Workbook do
1165
1165
  @book1.add_name("foo",[1,1])
1166
1166
  @book1.delete_name("foo")
1167
1167
  expect{
1168
- @book1.namevalue_glob("foo")
1168
+ @book1.namevalue_global("foo")
1169
1169
  }.to raise_error(NameNotFound, /name "foo"/)
1170
1170
  end
1171
1171
 
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
3
+ require_relative '../spec_helper'
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -24,7 +24,6 @@ describe Workbook do
24
24
  @different_file = @dir + '/different_workbook.xls'
25
25
  @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
26
  @another_simple_file = @dir + '/another_workbook.xls'
27
- @linked_file = @dir + '/workbook_linked.xlsm'
28
27
  @simple_file_xlsm = @dir + '/workbook.xlsm'
29
28
  @simple_file_xlsx = @dir + '/workbook.xlsx'
30
29
  @simple_file1 = @simple_file
@@ -43,6 +42,10 @@ describe Workbook do
43
42
  @simple_file_hostname_share_path_other_path1 = @simple_file_hostname_share_path_other_path
44
43
  @simple_file_xlsm1 = @simple_file_xlsm
45
44
  @simple_file_xlsx1 = @simple_file_xlsx
45
+ #@linked_file = @dir + '/workbook_linked.xlsm'
46
+ #@sub_file = @dir + '/workbook_sub.xlsm'
47
+ @main_file = @dir + '/workbook_linked3.xlsm'
48
+ @sub_file = @dir + '/workbook_linked_sub.xlsm'
46
49
  @error_message_excel = "provided Excel option value is neither an Excel object nor a valid option"
47
50
  end
48
51
 
@@ -51,6 +54,45 @@ describe Workbook do
51
54
  rm_tmp(@dir)
52
55
  end
53
56
 
57
+ describe "linked workbooks" do
58
+
59
+ context "standard" do
60
+
61
+ before do
62
+ @book1 = Workbook.open(@main_file)
63
+ end
64
+
65
+ it "should open the main workbook and the linked workbook" do
66
+ @book1.should be_alive
67
+ @book1.should be_a Workbook
68
+ @book1.filename.should == @main_file
69
+ Excel.current.workbooks.map{|b| b.filename}.should == [@main_file, @sub_file]
70
+ book2 = Workbook.open(@sub_file)
71
+ book2.should be_alive
72
+ book2.should be_a Workbook
73
+ book2.filename.should == @sub_file
74
+ end
75
+
76
+ it "should close the main workbook" do
77
+ @book1.close
78
+ Excel.current.workbooks.map{|b| b.filename}.should == [@sub_file]
79
+ end
80
+
81
+ it "should raise error when trying to close the linked workbook" do
82
+ book2 = Workbook.open(@sub_file)
83
+ expect{
84
+ book2.close
85
+ }.to raise_error(WorkbookLinked)
86
+ end
87
+
88
+ it "should raise error when trying to change the read-only mode of the linked workbook" do
89
+ expect{
90
+ book2 = Workbook.open(@sub_file, :read_only => true)
91
+ }.to raise_error(WorkbookLinked)
92
+ end
93
+ end
94
+ end
95
+
54
96
  describe "basic tests with xlsx-workbooks" do
55
97
 
56
98
  context "with simple file" do
@@ -273,6 +315,17 @@ describe Workbook do
273
315
  bookstore = Bookstore.new
274
316
  end
275
317
 
318
+ it "should fetch a network path file given a not via Reo opened hostname share file" do
319
+ ole_e1 = WIN32OLE.new('Excel.Application')
320
+ ws = ole_e1.Workbooks
321
+ abs_filename = General.absolute_path(@simple_file_hostname_share_path)
322
+ @ole_wb = ws.Open(abs_filename)
323
+ book2 = Workbook.open(@simple_file_network_path)
324
+ #book2.should === @ole_wb.to_reo
325
+ book2.Fullname.should == @ole_wb.Fullname
326
+ book2.excel.Workbooks.Count.should == 1
327
+ end
328
+
276
329
  it "should fetch a network path file given a hostname share file" do
277
330
  book1 = Workbook.open(@simple_file_hostname_share_path)
278
331
  book2 = Workbook.open(@simple_file_network_path)
@@ -2620,8 +2673,6 @@ describe Workbook do
2620
2673
  @book.should be_alive
2621
2674
  end
2622
2675
 
2623
-
2624
-
2625
2676
  it "should not open the new book and not close the unsaved book, if user answers 'no'" do
2626
2677
  # "No" is right to "Yes" (the default). --> language independent
2627
2678
  # strangely, in the "no" case, the question will sometimes be repeated three time
@@ -2973,7 +3024,7 @@ describe Workbook do
2973
3024
  context "with various file formats" do
2974
3025
 
2975
3026
  it "should open linked workbook" do
2976
- book = Workbook.open(@linked_file, :visible => true)
3027
+ book = Workbook.open(@main_file, :visible => true)
2977
3028
  book.close
2978
3029
  end
2979
3030
 
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
3
+ require_relative '../spec_helper'
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
3
+ require_relative '../spec_helper'
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
3
+ require_relative '../spec_helper'
4
4
 
5
5
  # @private
6
6
  module My
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './../spec_helper')
3
+ require_relative '../spec_helper'
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -24,7 +24,10 @@ describe Workbook do
24
24
  @different_file = @dir + '/different_workbook.xls'
25
25
  @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
26
  @another_simple_file = @dir + '/another_workbook.xls'
27
- @linked_file = @dir + '/workbook_linked.xlsm'
27
+ #@main_file = @dir + '/workbook_linked.xlsm'
28
+ #@linked_sub_file = @dir + '/workbook_sub.xlsm'
29
+ @main_file = @dir + '/workbook_linked3.xlsm'
30
+ @sub_file = @dir + '/workbook_linked_sub.xlsm'
28
31
  @simple_file_xlsm = @dir + '/workbook.xlsm'
29
32
  @simple_file_xlsx = @dir + '/workbook.xlsx'
30
33
  @simple_file1 = @simple_file
@@ -35,9 +38,203 @@ describe Workbook do
35
38
  rm_tmp(@dir)
36
39
  end
37
40
 
38
- describe "Workbook#for_reading, #for_modifying" do
39
-
40
- context "with standard" do
41
+ describe "writable" do
42
+
43
+ context "with no book" do
44
+
45
+ it "should open in read-only mode" do
46
+ Workbook.unobtrusively(@simple_file1, :writable => false) do |book|
47
+ book.ReadOnly.should be true
48
+ end
49
+ end
50
+
51
+ it "should open in writable mode" do
52
+ Workbook.unobtrusively(@simple_file1, :writable => true) do |book|
53
+ book.ReadOnly.should be false
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ context "with open writable book" do
60
+
61
+ before do
62
+ @book = Workbook.open(@simple_file1)
63
+ end
64
+
65
+ it "should keep the read-only mode" do
66
+ Workbook.unobtrusively(@simple_file1, :writable => false) do |book|
67
+ book.ReadOnly.should be false
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ context "with open read-only book" do
74
+
75
+ before do
76
+ @book = Workbook.open(@simple_file1, :read_only => true)
77
+ end
78
+
79
+ it "should keep the read-only mode" do
80
+ Workbook.unobtrusively(@simple_file1, :writable => true) do |book|
81
+ book.ReadOnly.should be true
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ describe "for_reading, for_modifying" do
90
+
91
+ context "with no workbook" do
92
+
93
+ it "should open with read-only" do
94
+ Workbook.for_reading(@simple_file1) do |book|
95
+ book.should be_a Workbook
96
+ book.should be_alive
97
+ book.ReadOnly.should be true
98
+ book.Saved.should be true
99
+ sheet = book.sheet(1)
100
+ cell = sheet[1,1]
101
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
102
+ @new_cell_value = sheet[1,1].Value
103
+ book.Saved.should be false
104
+ end
105
+ Excel.kill_all
106
+ new_book = Workbook.open(@simple_file1)
107
+ sheet = new_book.sheet(1)
108
+ sheet[1,1].Value.should_not == @new_cell_value
109
+ end
110
+
111
+ it "should change the value" do
112
+ Workbook.for_modifying(@simple_file1) do |book|
113
+ book.should be_a Workbook
114
+ book.should be_alive
115
+ book.ReadOnly.should be false
116
+ book.Saved.should be true
117
+ sheet = book.sheet(1)
118
+ cell = sheet[1,1]
119
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
120
+ @new_cell_value = sheet[1,1].Value
121
+ book.Saved.should be false
122
+ end
123
+ Excel.kill_all
124
+ new_book = Workbook.open(@simple_file1)
125
+ sheet = new_book.sheet(1)
126
+ sheet[1,1].Value.should == @new_cell_value
127
+ end
128
+
129
+ end
130
+
131
+ context "with closed writable workbook" do
132
+
133
+ before do
134
+ @book = Workbook.open(@simple_file1)
135
+ sheet = @book.sheet(1)
136
+ @old_cell_value = sheet[1,1].Value
137
+ @book.close
138
+ end
139
+
140
+ it "should not change the value" do
141
+ Workbook.for_reading(@simple_file1) do |book|
142
+ book.should be_a Workbook
143
+ book.should be_alive
144
+ book.ReadOnly.should be true
145
+ book.Saved.should be true
146
+ sheet = book.sheet(1)
147
+ cell = sheet[1,1]
148
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
149
+ book.Saved.should be false
150
+ book.excel.should == @book.excel
151
+ end
152
+ Excel.kill_all
153
+ new_book = Workbook.open(@simple_file1)
154
+ sheet = new_book.sheet(1)
155
+ sheet[1,1].Value.should == @old_cell_value
156
+ end
157
+
158
+ it "should not change the value and use a given Excel" do
159
+ new_excel = Excel.new(:reuse => false)
160
+ another_excel = Excel.new(:reuse => false)
161
+ Workbook.for_reading(@simple_file1, :if_closed => another_excel) do |book|
162
+ sheet = book.sheet(1)
163
+ cell = sheet[1,1]
164
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
165
+ book.excel.should == another_excel
166
+ end
167
+ Excel.kill_all
168
+ new_book = Workbook.open(@simple_file1)
169
+ sheet = new_book.sheet(1)
170
+ sheet[1,1].Value.should == @old_cell_value
171
+ end
172
+
173
+ it "should not change the value and use the new Excel instance" do
174
+ new_excel = Excel.new(:reuse => false)
175
+ Workbook.for_reading(@simple_file1, :if_closed => :new) do |book|
176
+ sheet = book.sheet(1)
177
+ cell = sheet[1,1]
178
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
179
+ book.excel.should_not == @book.excel
180
+ book.excel.should_not == new_excel
181
+ book.excel.properties[:visible].should be false
182
+ book.excel.properties[:displayalerts].should == :if_visible
183
+ end
184
+ Excel.kill_all
185
+ new_book = Workbook.open(@simple_file1)
186
+ sheet = new_book.sheet(1)
187
+ sheet[1,1].Value.should == @old_cell_value
188
+ end
189
+
190
+ it "should change the value" do
191
+ Workbook.for_modifying(@simple_file1) do |book|
192
+ sheet = book.sheet(1)
193
+ cell = sheet[1,1]
194
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
195
+ book.excel.should == @book.excel
196
+ end
197
+ new_book = Workbook.open(@simple_file, :visible => true)
198
+ sheet = new_book.sheet(1)
199
+ sheet[1,1].Value.should_not == @old_cell_value
200
+ end
201
+
202
+ it "should change the value and use a given Excel" do
203
+ @book.close
204
+ new_excel = Excel.new(:reuse => false)
205
+ another_excel = Excel.new(:reuse => false)
206
+ Workbook.for_modifying(@simple_file1, :if_closed => another_excel) do |book|
207
+ sheet = book.sheet(1)
208
+ cell = sheet[1,1]
209
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
210
+ book.excel.should == another_excel
211
+ end
212
+ new_book = Workbook.open(@simple_file1, :visible => true)
213
+ sheet = new_book.sheet(1)
214
+ sheet[1,1].Value.should_not == @old_cell_value
215
+ end
216
+
217
+ it "should change the value and use the new Excel instance" do
218
+ new_excel = Excel.new(:reuse => false)
219
+ Workbook.for_modifying(@simple_file1, :if_closed => :new) do |book|
220
+ sheet = book.sheet(1)
221
+ cell = sheet[1,1]
222
+ sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
223
+ book.excel.should_not == @book.excel
224
+ book.excel.should_not == new_excel
225
+ book.excel.properties[:visible].should be false
226
+ book.excel.properties[:displayalerts].should == :if_visible
227
+ end
228
+ new_book = Workbook.open(@simple_file1, :visible => true)
229
+ sheet = new_book.sheet(1)
230
+ sheet[1,1].Value.should_not == @old_cell_value
231
+ end
232
+ end
233
+ end
234
+
235
+ describe "Workbook.for_reading, for_modifying" do
236
+
237
+ context "with open writable book" do
41
238
 
42
239
  before do
43
240
  @book = Workbook.open(@simple_file1)
@@ -52,6 +249,7 @@ describe Workbook do
52
249
  @book.for_reading do |book|
53
250
  book.should be_a Workbook
54
251
  book.should be_alive
252
+ book.ReadOnly.should be false
55
253
  book.Saved.should be true
56
254
  book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
57
255
  book.Saved.should be false
@@ -106,6 +304,70 @@ describe Workbook do
106
304
 
107
305
  end
108
306
 
307
+ describe "with referenced workbooks" do
308
+
309
+ context "with no books" do
310
+
311
+ it "should open the linked workbook in read-only" do
312
+ Workbook.unobtrusively(@sub_file, :writable => false) do |book|
313
+ book.ReadOnly.should be true
314
+ book.filename.should == @sub_file
315
+ end
316
+ Excel.current.workbooks.should == []
317
+ end
318
+
319
+ it "should open the workbook and its linked workbook in read-only" do
320
+ Workbook.unobtrusively(@main_file, :writable => false) do |book|
321
+ book.ReadOnly.should be true
322
+ book.filename.should == @main_file
323
+ book.excel.workbooks.map{|b| b.filename}.should == [@sub_file, @main_file]
324
+ end
325
+ Excel.current.workbooks.map{|b| b.filename}.should == [@sub_file]
326
+ end
327
+
328
+ it "should not write the linked workbook and close it" do
329
+ Workbook.unobtrusively(@sub_file, :writable => false) do |book|
330
+ book.ReadOnly.should be true
331
+ book.filename.should == @sub_file
332
+ @old_value = book.sheet(1)[1,1].Value
333
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
334
+ end
335
+ Excel.current.workbooks.should == []
336
+ end
337
+
338
+ it "should not write the main workbook and close the linked file as well" do
339
+ Workbook.unobtrusively(@main_file, :writable => false) do |book|
340
+ book.ReadOnly.should be true
341
+ book.filename.should == @main_file
342
+ @old_value = book.sheet(1)[1,1].Value
343
+ book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
344
+ end
345
+ Excel.current.workbooks.map{|b| b.filename}.should == [@sub_file]
346
+ end
347
+
348
+ end
349
+
350
+ context "with open books" do
351
+
352
+ before do
353
+ @book_main = Workbook.open(@main_file)
354
+ end
355
+
356
+ it "should leave the read-only mode" do
357
+ Workbook.unobtrusively(@sub_file, :read_only_default => true) do |book|
358
+ book.ReadOnly.should be false
359
+ end
360
+ end
361
+
362
+ it "should force to read-only" do
363
+ expect{
364
+ Workbook.unobtrusively(@sub_file, :read_only => true) do
365
+ end
366
+ }.to raise_error(WorkbookLinked)
367
+ end
368
+ end
369
+ end
370
+
109
371
  describe "Workbook#unobtrusively" do
110
372
 
111
373
  context "with a writable saved workbook" do
@@ -778,7 +1040,7 @@ describe Workbook do
778
1040
  b1.sheet(1)[1,1].Value.should_not == @old_value
779
1041
  end
780
1042
 
781
- it "should open as read-write" do
1043
+ it "should open as read-write but not save changes" do
782
1044
  Workbook.unobtrusively(@simple_file, :read_only => false, :writable => false) do |book|
783
1045
  book.ReadOnly.should be false
784
1046
  @old_value = book.sheet(1)[1,1].Value
@@ -838,9 +1100,9 @@ describe Workbook do
838
1100
  b1.sheet(1)[1,1].Value.should == @old_value
839
1101
  end
840
1102
 
841
- it "should open not writable" do
1103
+ it "should open not writable and read-only" do
842
1104
  Workbook.unobtrusively(@simple_file, :writable => false) do |book|
843
- #
1105
+ book.ReadOnly.should be true
844
1106
  @old_value = book.sheet(1)[1,1].Value
845
1107
  book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
846
1108
  book.Saved.should be false
@@ -935,7 +1197,7 @@ describe Workbook do
935
1197
  book.ReadOnly.should be true
936
1198
  book.should == @book
937
1199
  book.filename.should == @book.filename
938
- book.excel.should_not == @book.excel
1200
+ book.excel.should == @book.excel
939
1201
  book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
940
1202
  end
941
1203
  }.to raise_error(WorkbookReadOnly)
@@ -1013,7 +1275,7 @@ describe Workbook do
1013
1275
  book.Readonly.should be false
1014
1276
  book.should == @book
1015
1277
  book.filename.should == @book.filename
1016
- book.excel.should_not == @book.excel
1278
+ book.excel.should == @book.excel
1017
1279
  book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
1018
1280
  end
1019
1281
  @book.close
@@ -1026,7 +1288,7 @@ describe Workbook do
1026
1288
  book.Readonly.should be false
1027
1289
  book.should == @book
1028
1290
  book.filename.should == @book.filename
1029
- book.excel.should_not == @book.excel
1291
+ book.excel.should == @book.excel
1030
1292
  book.sheet(1)[1,1] = book.sheet(1)[1,1].Value == "foo" ? "bar" : "foo"
1031
1293
  end
1032
1294
  @book.close
@@ -2336,108 +2598,4 @@ describe Workbook do
2336
2598
 
2337
2599
  end
2338
2600
 
2339
- describe "for_reading, for_modifying" do
2340
-
2341
- context "open unobtrusively for reading and modifying" do
2342
-
2343
- before do
2344
- @book = Workbook.open(@simple_file1)
2345
- sheet = @book.sheet(1)
2346
- @old_cell_value = sheet[1,1].Value
2347
- @book.close
2348
- end
2349
-
2350
- it "should not change the value" do
2351
- Workbook.for_reading(@simple_file1) do |book|
2352
- book.should be_a Workbook
2353
- book.should be_alive
2354
- book.Saved.should be true
2355
- sheet = book.sheet(1)
2356
- cell = sheet[1,1]
2357
- sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
2358
- book.Saved.should be false
2359
- book.excel.should == @book.excel
2360
- end
2361
- Excel.kill_all
2362
- new_book = Workbook.open(@simple_file1)
2363
- sheet = new_book.sheet(1)
2364
- sheet[1,1].Value.should == @old_cell_value
2365
- end
2366
-
2367
- it "should not change the value and use a given Excel" do
2368
- new_excel = Excel.new(:reuse => false)
2369
- another_excel = Excel.new(:reuse => false)
2370
- Workbook.for_reading(@simple_file1, :if_closed => another_excel) do |book|
2371
- sheet = book.sheet(1)
2372
- cell = sheet[1,1]
2373
- sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
2374
- book.excel.should == another_excel
2375
- end
2376
- Excel.kill_all
2377
- new_book = Workbook.open(@simple_file1)
2378
- sheet = new_book.sheet(1)
2379
- sheet[1,1].Value.should == @old_cell_value
2380
- end
2381
-
2382
- it "should not change the value and use the new Excel instance" do
2383
- new_excel = Excel.new(:reuse => false)
2384
- Workbook.for_reading(@simple_file1, :if_closed => :new) do |book|
2385
- sheet = book.sheet(1)
2386
- cell = sheet[1,1]
2387
- sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
2388
- book.excel.should_not == @book.excel
2389
- book.excel.should_not == new_excel
2390
- book.excel.properties[:visible].should be false
2391
- book.excel.properties[:displayalerts].should == :if_visible
2392
- end
2393
- Excel.kill_all
2394
- new_book = Workbook.open(@simple_file1)
2395
- sheet = new_book.sheet(1)
2396
- sheet[1,1].Value.should == @old_cell_value
2397
- end
2398
-
2399
- it "should change the value" do
2400
- Workbook.for_modifying(@simple_file1) do |book|
2401
- sheet = book.sheet(1)
2402
- cell = sheet[1,1]
2403
- sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
2404
- book.excel.should == @book.excel
2405
- end
2406
- new_book = Workbook.open(@simple_file, :visible => true)
2407
- sheet = new_book.sheet(1)
2408
- sheet[1,1].Value.should_not == @old_cell_value
2409
- end
2410
-
2411
- it "should change the value and use a given Excel" do
2412
- new_excel = Excel.new(:reuse => false)
2413
- another_excel = Excel.new(:reuse => false)
2414
- Workbook.for_modifying(@simple_file1, :if_closed => another_excel) do |book|
2415
- sheet = book.sheet(1)
2416
- cell = sheet[1,1]
2417
- sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
2418
- book.excel.should == another_excel
2419
- end
2420
- new_book = Workbook.open(@simple_file1, :visible => true)
2421
- sheet = new_book.sheet(1)
2422
- sheet[1,1].Value.should_not == @old_cell_value
2423
- end
2424
-
2425
- it "should change the value and use the new Excel instance" do
2426
- new_excel = Excel.new(:reuse => false)
2427
- Workbook.for_modifying(@simple_file1, :if_closed => :new) do |book|
2428
- sheet = book.sheet(1)
2429
- cell = sheet[1,1]
2430
- sheet[1,1] = cell.Value == "foo" ? "bar" : "foo"
2431
- book.excel.should_not == @book.excel
2432
- book.excel.should_not == new_excel
2433
- book.excel.properties[:visible].should be false
2434
- book.excel.properties[:displayalerts].should == :if_visible
2435
- end
2436
- new_book = Workbook.open(@simple_file1, :visible => true)
2437
- sheet = new_book.sheet(1)
2438
- sheet[1,1].Value.should_not == @old_cell_value
2439
- end
2440
- end
2441
- end
2442
-
2443
2601
  end