robust_excel_ole 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +6 -0
  3. data/README.rdoc +2 -2
  4. data/docs/README_excel.rdoc +2 -3
  5. data/docs/README_open.rdoc +8 -12
  6. data/docs/README_ranges.rdoc +21 -9
  7. data/docs/README_sheet.rdoc +1 -1
  8. data/lib/robust_excel_ole/book.rb +3 -23
  9. data/lib/robust_excel_ole/excel.rb +2 -15
  10. data/lib/robust_excel_ole/reo_common.rb +11 -7
  11. data/lib/robust_excel_ole/sheet.rb +36 -15
  12. data/lib/robust_excel_ole/version.rb +1 -1
  13. data/robust_excel_ole.gemspec +1 -1
  14. data/spec/book_spec.rb +1 -1
  15. data/spec/book_specs/book_close_spec.rb +3 -3
  16. data/spec/book_specs/book_misc_spec.rb +25 -1
  17. data/spec/book_specs/book_open_spec.rb +299 -5
  18. data/spec/book_specs/book_sheet_spec.rb +1 -1
  19. data/spec/book_specs/book_unobtr_spec.rb +275 -35
  20. data/spec/data/another_workbook.xls +0 -0
  21. data/spec/data/different_workbook.xls +0 -0
  22. data/spec/data/workbook.xls +0 -0
  23. data/spec/excel_spec.rb +118 -5
  24. data/spec/helpers/key_sender.rb +2 -2
  25. data/spec/reo_common_spec.rb +0 -4
  26. data/spec/sheet_spec.rb +32 -0
  27. data/spec/spec_helper.rb +3 -0
  28. metadata +3 -21
  29. data/spec/ruby1.8.6_rspec2.14/book_spec.rb +0 -1421
  30. data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +0 -22
  31. data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +0 -252
  32. data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +0 -1070
  33. data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +0 -1855
  34. data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +0 -514
  35. data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +0 -395
  36. data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +0 -51
  37. data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +0 -1737
  38. data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +0 -495
  39. data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +0 -76
  40. data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +0 -42
  41. data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +0 -1820
  42. data/spec/ruby1.8.6_rspec2.14/general_spec.rb +0 -212
  43. data/spec/ruby1.8.6_rspec2.14/range_spec.rb +0 -131
  44. data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +0 -130
  45. data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +0 -663
  46. data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +0 -35
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -27,13 +27,110 @@ module RobustExcelOle
27
27
 
28
28
  after do
29
29
  Excel.kill_all
30
- #rm_tmp(@dir)
30
+ rm_tmp(@dir)
31
31
  end
32
32
 
33
- context "Illegal Refrence" do
33
+ context "with already open Excel instances and an open unsaved workbook" do
34
34
 
35
35
  before do
36
+ @ole_excel1 = WIN32OLE.new('Excel.Application')
37
+ @ole_excel2 = WIN32OLE.new('Excel.Application')
38
+ ole_workbook1 = @ole_excel1.Workbooks.Open(@another_simple_file, { 'ReadOnly' => false })
39
+ ole_workbook1.Names.Item("firstcell").RefersToRange.Value = "foo"
40
+ end
41
+
42
+ it "should create a new Excel instance" do
43
+ excel1 = Excel.new(:reuse => false)
44
+ excel1.ole_excel.Hwnd.should_not == @ole_excel1.Hwnd
45
+ excel1.ole_excel.Hwnd.should_not == @ole_excel2.Hwnd
46
+ end
47
+
48
+ it "should connect to the already opened Excel instance" do
49
+ excel1 = Excel.new(:reuse => true)
50
+ excel1.ole_excel.Hwnd.should == @ole_excel1.Hwnd
51
+ end
52
+
53
+ it "should connect to the already running Excel instance" do
54
+ excel1 = Excel.create
55
+ excel1.close
56
+ sleep 0.2
57
+ excel2 = Excel.current
58
+ excel1.should_not be_alive
59
+ excel2.should be_alive
60
+ excel2.ole_excel.Hwnd.should == @ole_excel1.Hwnd
61
+ Excel.excels_number.should == 2
62
+ end
63
+
64
+ it "should make the Excel instance not alive if the Excel that was connected with was closed" do
65
+ excel1 = Excel.create
66
+ excel2 = Excel.current
67
+ excel1.close
68
+ sleep 0.2
69
+ excel1.should_not be_alive
70
+ excel2.should be_alive
71
+ excel2.ole_excel.Hwnd.should == @ole_excel1.Hwnd
72
+ end
73
+
74
+ it "should reuse the first opened Excel instance if not the first opened Excel instance was closed" do
75
+ excel1 = Excel.create
76
+ excel2 = Excel.create
77
+ excel2.close
78
+ sleep 0.2
79
+ excel3 = Excel.current
80
+ excel3.ole_excel.Hwnd.should == @ole_excel1.Hwnd
81
+ end
82
+
83
+ it "should reuse the Excel that was not closed" do
84
+ excel1 = Excel.create
85
+ excel2 = Excel.create
86
+ excel1.close
87
+ sleep 0.2
88
+ excel3 = Excel.current
89
+ excel3.ole_excel.Hwnd.should == @ole_excel1.Hwnd
90
+ end
91
+
92
+ it "should kill hard all Excel instances" do
93
+ excel1 = Excel.create
94
+ Excel.kill_all
95
+ excel1.should_not be_alive
96
+ expect{
97
+ @ole_excel1.Name
98
+ }.to raise_error
99
+ expect{
100
+ @ole_excel2.Name
101
+ }.to raise_error
102
+ end
36
103
 
104
+ it "should close all Excel instances" do
105
+ excel1 = Excel.create
106
+ result = Excel.close_all(:if_unsaved => :forget)
107
+ sleep 1
108
+ expect{
109
+ @ole_excel1.Name
110
+ }.to raise_error
111
+ expect{
112
+ @ole_excel2.Name
113
+ }.to raise_error
114
+ result.should == [2,0]
115
+ end
116
+
117
+ it "should recreate an Excel instance" do
118
+ excel1 = Excel.create
119
+ excel1.close
120
+ excel1.should_not be_alive
121
+ excel1.recreate
122
+ excel1.should be_a Excel
123
+ excel1.should be_alive
124
+ excel1.ole_excel.Hwnd.should_not == @ole_excel1.Hwnd
125
+ excel1.ole_excel.Hwnd.should_not == @ole_excel2.Hwnd
126
+ Excel.excels_number.should == 3
127
+ end
128
+
129
+ end
130
+
131
+ context "Illegal Refrence" do
132
+
133
+ before do
37
134
  book1 = Book.open(@simple_file1)
38
135
  book2 = Book.open(@simple_file1, :force_excel => :new)
39
136
  a = book1.saved
@@ -964,7 +1061,6 @@ module RobustExcelOle
964
1061
  @excel1.should_not == nil
965
1062
  end
966
1063
 
967
- # Error
968
1064
  it "should be false with dead Excel objects" do
969
1065
  excel2 = Excel.current
970
1066
  sleep 3
@@ -985,8 +1081,6 @@ module RobustExcelOle
985
1081
 
986
1082
  end
987
1083
 
988
-
989
-
990
1084
  context "with Visible and DisplayAlerts, focus" do
991
1085
 
992
1086
  it "should bring Excel in focus" do
@@ -1760,6 +1854,18 @@ module RobustExcelOle
1760
1854
  @excel1["foo"] = 1
1761
1855
  }.to raise_error(NameNotFound, /name "foo"/)
1762
1856
  end
1857
+
1858
+ it "should color the cell" do
1859
+ @excel1.set_nameval("firstcell", "foo")
1860
+ @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == -4142
1861
+ @excel1.set_nameval("firstcell", "foo", :color => 4)
1862
+ @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == 4
1863
+ @excel1["firstcell"].should == "foo"
1864
+ @excel1["firstcell"] = "foo"
1865
+ @excel1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == 42
1866
+ @book1.save
1867
+ end
1868
+
1763
1869
  end
1764
1870
 
1765
1871
  describe "rangeval, set_rangeval" do
@@ -1813,6 +1919,13 @@ module RobustExcelOle
1813
1919
  }.to raise_error(NameNotFound, /name "foo" not in/)
1814
1920
  end
1815
1921
 
1922
+ it "should color the cell" do
1923
+ @excel1.set_rangeval("firstcell", "foo")
1924
+ @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == -4142
1925
+ @excel1.set_rangeval("firstcell", "foo", :color => 4)
1926
+ @book1.Names.Item("firstcell").RefersToRange.Interior.ColorIndex.should == 4
1927
+ end
1928
+
1816
1929
  end
1817
1930
 
1818
1931
  end
@@ -25,7 +25,7 @@ class KeySender # :nodoc: #
25
25
  print "-" unless options[:silent]
26
26
  end
27
27
  else
28
- true # Keine Window_name, immer senden'
28
+ true # no window_name, send always'
29
29
  end
30
30
 
31
31
  if ready_to_send
@@ -53,7 +53,7 @@ class KeySender # :nodoc: #
53
53
  end
54
54
  break false if Time.now > break_time
55
55
 
56
- print " (noch #{'%.1f'%(break_time - Time.now)}s) " unless options[:silent]
56
+ print " (more #{'%.1f'%(break_time - Time.now)}s) " unless options[:silent]
57
57
  sleep 0.813
58
58
  end
59
59
  end
@@ -12,10 +12,6 @@ module RobustExcelOle
12
12
 
13
13
  describe REOCommon do
14
14
 
15
- it "should simple test" do
16
- puts "a"
17
- end
18
-
19
15
  before(:all) do
20
16
  excel = Excel.new(:reuse => true)
21
17
  open_books = excel == nil ? 0 : excel.Workbooks.Count
data/spec/sheet_spec.rb CHANGED
@@ -137,6 +137,13 @@ describe Sheet do
137
137
 
138
138
  end
139
139
 
140
+ describe "cellval" do
141
+
142
+ it "should return value" do
143
+ @sheet.cellval(1,1).should == "foo"
144
+ end
145
+ end
146
+
140
147
  it "change a cell to 'bar'" do
141
148
  @sheet[1, 1] = 'bar'
142
149
  @sheet[1, 1].value.should eq 'bar'
@@ -156,6 +163,15 @@ describe Sheet do
156
163
  }.to raise_error(RangeNotEvaluatable, /cannot assign value/)
157
164
  end
158
165
 
166
+ describe "set_cellval" do
167
+
168
+ it "should set color" do
169
+ @sheet.set_cellval(1,1,"foo",:color => 42)
170
+ @sheet.cellval(1,1).should == "foo"
171
+ @sheet[1,1].Interior.ColorIndex.should == 42
172
+ end
173
+ end
174
+
159
175
  describe '#each' do
160
176
  it "should sort line in order of column" do
161
177
  @sheet.each_with_index do |cell, i|
@@ -520,6 +536,14 @@ describe Sheet do
520
536
  @sheet1.set_nameval("foo", 1)
521
537
  }.to raise_error(NameNotFound, /name "foo" not in #<Sheet: Sheet1/)
522
538
  end
539
+
540
+ it "should color the cell" do
541
+ @sheet1.set_nameval("new", "bar")
542
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == -4142
543
+ @sheet1.set_nameval("new", "bar", :color => 4)
544
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
545
+ end
546
+
523
547
  end
524
548
 
525
549
  describe "rangeval, set_rangeval" do
@@ -597,6 +621,14 @@ describe Sheet do
597
621
  @sheet1.rangeval("foo", :default => 1).should == 1
598
622
  @sheet1.nameval("empty", :default => 1).should be_nil
599
623
  end
624
+
625
+ it "should color the cell" do
626
+ @sheet1.set_rangeval("new", "bar")
627
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == -4142
628
+ @sheet1.set_rangeval("new", "bar", :color => 4)
629
+ @book1.Names.Item("new").RefersToRange.Interior.ColorIndex.should == 4
630
+ end
631
+
600
632
  end
601
633
 
602
634
  describe "set_name" do
data/spec/spec_helper.rb CHANGED
@@ -32,4 +32,7 @@ end
32
32
 
33
33
  RSpec.configure do |config|
34
34
  config.include RobustExcelOle::SpecHelpers
35
+ config.expect_with :rspec do |expectations|
36
+ expectations.syntax = [:should, :expect]
37
+ end
35
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robust_excel_ole
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - traths
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-04 00:00:00.000000000 Z
11
+ date: 2018-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -26,7 +26,7 @@ dependencies:
26
26
  version: 2.6.0
27
27
  description: |-
28
28
  RobustExcelOle automates reading and writing Excel workbooks in Windows by using the win32ole library.
29
- It is disigned to cope with several kinds of concurrency of both simultaneously running
29
+ It is designed to cope with several kinds of concurrency of both simultaneously running
30
30
  Excel instances and simultanously happening user interactions.
31
31
  RobustExcelOle deals with various cases of Excel (and user) behaviour,
32
32
  supplies workarounds for some Excel bugs, and supports referenced libraries.
@@ -123,24 +123,6 @@ files:
123
123
  - spec/helpers/key_sender.rb
124
124
  - spec/range_spec.rb
125
125
  - spec/reo_common_spec.rb
126
- - spec/ruby1.8.6_rspec2.14/book_spec.rb
127
- - spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb
128
- - spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb
129
- - spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb
130
- - spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb
131
- - spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb
132
- - spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb
133
- - spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb
134
- - spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb
135
- - spec/ruby1.8.6_rspec2.14/bookstore_spec.rb
136
- - spec/ruby1.8.6_rspec2.14/cell_spec.rb
137
- - spec/ruby1.8.6_rspec2.14/cygwin_spec.rb
138
- - spec/ruby1.8.6_rspec2.14/excel_spec.rb
139
- - spec/ruby1.8.6_rspec2.14/general_spec.rb
140
- - spec/ruby1.8.6_rspec2.14/range_spec.rb
141
- - spec/ruby1.8.6_rspec2.14/reo_common_spec.rb
142
- - spec/ruby1.8.6_rspec2.14/sheet_spec.rb
143
- - spec/ruby1.8.6_rspec2.14/spec_helper.rb
144
126
  - spec/sheet_spec.rb
145
127
  - spec/spec_helper.rb
146
128
  - version.rb
@@ -1,1421 +0,0 @@
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 Book do
12
-
13
- before(:all) do
14
- excel = Excel.new(:reuse => true)
15
- open_books = excel == nil ? 0 : excel.Workbooks.Count
16
- puts "*** open books *** : #{open_books}" if open_books > 0
17
- Excel.kill_all
18
- end
19
-
20
- before do
21
- @dir = create_tmpdir
22
- @simple_file = @dir + '/workbook.xls'
23
- @simple_save_file = @dir + '/workbook_save.xls'
24
- @different_file = @dir + '/different_workbook.xls'
25
- @simple_file_other_path = @dir + '/more_data/workbook.xls'
26
- @another_simple_file = @dir + '/another_workbook.xls'
27
- @linked_file = @dir + '/workbook_linked.xlsm'
28
- @simple_file_xlsm = @dir + '/workbook.xls'
29
- @simple_file_xlsx = @dir + '/workbook.xlsx'
30
- @simple_file1 = @simple_file
31
- @simple_file_other_path1 = @simple_file_other_path
32
- @simple_save_file1 = @simple_save_file
33
- end
34
-
35
- after do
36
- Excel.kill_all
37
- #rm_tmp(@dir)
38
- end
39
-
40
- describe "create file" do
41
- context "with standard" do
42
- it "open an existing file" do
43
- expect {
44
- @book = Book.open(@simple_file)
45
- }.to_not raise_error
46
- @book.should be_a Book
47
- @book.close
48
- end
49
- end
50
- end
51
-
52
- describe "Book::save" do
53
-
54
- it "should save a file, if it is open" do
55
- @book = Book.open(@simple_file)
56
- @book.add_sheet(@sheet, :as => 'a_name')
57
- @new_sheet_count = @book.ole_workbook.Worksheets.Count
58
- expect {
59
- Book.save(@simple_file)
60
- }.to_not raise_error
61
- @book.ole_workbook.Worksheets.Count.should == @new_sheet_count
62
- @book.close
63
- end
64
-
65
- it "should not save a file, if it is not open" do
66
- @book = Book.open(@simple_file)
67
- @book.add_sheet(@sheet, :as => 'a_name')
68
- @new_sheet_count = @book.ole_workbook.Worksheets.Count
69
- @book.close(:if_unsaved => :forget)
70
- expect {
71
- Book.save(@simple_file)
72
- }.to_not raise_error
73
- end
74
-
75
- end
76
-
77
- describe "Book::save_as" do
78
-
79
- it "should save to 'simple_save_file.xls'" do
80
- book = Book.open(@simple_file1)
81
- Book.save_as(@simple_file1, @simple_save_file1, :if_exists => :overwrite)
82
- File.exist?(@simple_save_file1).should be_true
83
- end
84
- end
85
-
86
- describe "Book::close" do
87
-
88
- it "should close the book if it is open" do
89
- book = Book.open(@simple_file1)
90
- Book.close(@simple_file1)
91
- book.should_not be_alive
92
- end
93
-
94
- it "should not close the book if it is not open" do
95
- book = Book.open(@simple_file1, :visible => true)
96
- book.close
97
- Book.close(@simple_file1)
98
- book.should_not be_alive
99
- end
100
-
101
- it "should raise error if the book is unsaved and open" do
102
- book = Book.open(@simple_file1)
103
- sheet = book.sheet(1)
104
- book.add_sheet(sheet, :as => 'a_name')
105
- expect{
106
- Book.close(@simple_file1)
107
- }.to raise_error(WorkbookNotSaved, /workbook is unsaved: "workbook.xls"/)
108
- expect{
109
- Book.close(@simple_file, :if_unsaved => :raise)
110
- }.to raise_error(WorkbookNotSaved, /workbook is unsaved: "workbook.xls"/)
111
- end
112
-
113
- it "should save and close the book" do
114
- book = Book.open(@simple_file1)
115
- sheet_count = book.ole_workbook.Worksheets.Count
116
- sheet = book.sheet(1)
117
- book.add_sheet(sheet, :as => 'a_name')
118
- ole_workbook = book.ole_workbook
119
- excel = book.excel
120
- excel.Workbooks.Count.should == 1
121
- Book.close(@simple_file1, {:if_unsaved => :save})
122
- excel.Workbooks.Count.should == 0
123
- book.ole_workbook.should == nil
124
- book.should_not be_alive
125
- expect{ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
126
- new_book = Book.open(@simple_file1)
127
- begin
128
- new_book.ole_workbook.Worksheets.Count.should == sheet_count + 1
129
- ensure
130
- new_book.close
131
- end
132
- end
133
- end
134
-
135
- describe "open" do
136
-
137
- context "with various file formats" do
138
-
139
- it "should open linked workbook" do
140
- book = Book.open(@linked_file, :visible => true)
141
- book.close
142
- end
143
- end
144
-
145
-
146
- context "with class identifier 'Workbook'" do
147
-
148
- before do
149
- @book = Workbook.open(@simple_file)
150
- end
151
-
152
- after do
153
- @book.close rescue nil
154
- end
155
-
156
- it "should open in a new Excel" do
157
- book2 = Workbook.open(@simple_file, :force => {:excel => :new})
158
- book2.should be_alive
159
- book2.should be_a Book
160
- book2.excel.should_not == @book.excel
161
- book2.should_not == @book
162
- @book.Readonly.should be_false
163
- book2.Readonly.should be_true
164
- book2.close
165
- end
166
- end
167
-
168
- context "lift a workbook to a Book object" do
169
-
170
- before do
171
- @book = Book.open(@simple_file)
172
- end
173
-
174
- after do
175
- @book.close
176
- end
177
-
178
- it "should yield an identical Book and set visible and displayalerts values" do
179
- workbook = @book.ole_workbook
180
- new_book = Book.new(workbook, :visible => true)
181
- new_book.excel.displayalerts = true
182
- new_book.should be_a Book
183
- new_book.should be_alive
184
- new_book.should == @book
185
- new_book.filename.should == @book.filename
186
- new_book.excel.should == @book.excel
187
- new_book.should === @book
188
- new_book.excel.Visible.should be_true
189
- new_book.excel.DisplayAlerts.should be_true
190
- new_book.close
191
- end
192
-
193
- end
194
-
195
-
196
- context "with standard options" do
197
- before do
198
- @book = Book.open(@simple_file)
199
- end
200
-
201
- after do
202
- @book.close
203
- end
204
-
205
- it "should say that it lives" do
206
- @book.should be_alive
207
- end
208
- end
209
-
210
- context "with identity transperence" do
211
-
212
- before do
213
- @book = Book.open(@simple_file)
214
- end
215
-
216
- after do
217
- @book.close
218
- end
219
-
220
- it "should yield identical Book objects for identical Excel books when reopening" do
221
- @book.should be_alive
222
- @book.close
223
- @book.should_not be_alive
224
- book2 = Book.open(@simple_file)
225
- book2.should === @book
226
- book2.should be_alive
227
- book2.close
228
- end
229
-
230
- it "should yield different Book objects when reopening in a new Excel" do
231
- @book.should be_alive
232
- old_excel = @book.excel
233
- @book.close
234
- @book.should_not be_alive
235
- book2 = Book.open(@simple_file, :force => {:excel => :new})
236
- book2.should_not === @book
237
- book2.should be_alive
238
- book2.excel.should_not == old_excel
239
- book2.close
240
- end
241
- end
242
-
243
- context "with :force_excel" do
244
-
245
- before do
246
- @book = Book.open(@simple_file)
247
- end
248
-
249
- after do
250
- @book.close rescue nil
251
- end
252
-
253
- it "should open in a new Excel" do
254
- book2 = Book.open(@simple_file, :force => {:excel => :new})
255
- book2.should be_alive
256
- book2.should be_a Book
257
- book2.excel.should_not == @book.excel
258
- book2.should_not == @book
259
- @book.Readonly.should be_false
260
- book2.Readonly.should be_true
261
- book2.close
262
- end
263
-
264
- it "should open in a given Excel, provide identity transparency, because book can be readonly, such that the old and the new book are readonly" do
265
- book2 = Book.open(@simple_file1, :force => {:excel => :new})
266
- book2.excel.should_not == @book.excel
267
- book3 = Book.open(@simple_file1, :force => {:excel => :new})
268
- book3.excel.should_not == book2.excel
269
- book3.excel.should_not == @book.excel
270
- book2.close
271
- book3.close
272
- @book.close
273
- book4 = Book.open(@simple_file1, :force => {:excel => book2.excel}, :read_only => true)
274
- book4.should be_alive
275
- book4.should be_a Book
276
- book4.excel.should == book2.excel
277
- book4.ReadOnly.should be_true
278
- book4.should == book2
279
- book4.close
280
- book5 = Book.open(@simple_file1, :force => {:excel => book2}, :read_only => true)
281
- book5.should be_alive
282
- book5.should be_a Book
283
- book5.excel.should == book2.excel
284
- book5.ReadOnly.should be_true
285
- book5.should == book2
286
- book5.close
287
- book3.close
288
- end
289
- end
290
-
291
- context "with :default_excel" do
292
-
293
- before do
294
- excel = Excel.new(:reuse => false)
295
- @book = Book.open(@simple_file)
296
- end
297
-
298
- after do
299
- @book.close rescue nil
300
- end
301
-
302
- it "should reopen the book in the excel instance where it was opened before" do
303
- excel = Excel.new(:reuse => false)
304
- @book.close
305
- book2 = Book.open(@simple_file)
306
- book2.should be_alive
307
- book2.should be_a Book
308
- book2.excel.should == @book.excel
309
- book2.excel.should_not == excel
310
- book2.filename.should == @book.filename
311
- @book.should be_alive
312
- book2.should == @book
313
- book2.close
314
- end
315
-
316
- it "should open a new excel, if the book cannot be reopened" do
317
- @book.close
318
- new_excel = Excel.new(:reuse => false)
319
- book2 = Book.open(@different_file, :default => {:excel => :new})
320
- book2.should be_alive
321
- book2.should be_a Book
322
- book2.excel.should_not == new_excel
323
- book2.excel.should_not == @book.excel
324
- book2.close
325
- end
326
-
327
- it "should open in a given Excel provided as Excel, Book, or WIN32OLE representing an Excel or Workbook" do
328
- book2 = Book.open(@another_simple_file)
329
- different_file1 = @different_file
330
- book3 = Book.open(different_file1, :default => {:excel => book2.excel})
331
- book3.excel.should === book2.excel
332
- book3.close
333
- book4 = Book.open(different_file1, :default => {:excel => book2})
334
- book4.excel.should === book2.excel
335
- book4.close
336
- book5 = Book.open(different_file1, :default_excel => book2.ole_workbook)
337
- book5.excel.should === book2.excel
338
- book5.close
339
- win32ole_excel1 = WIN32OLE.connect(book2.ole_workbook.Fullname).Application
340
- book6 = Book.open(different_file1, :default => {:excel => win32ole_excel1})
341
- book6.excel.should === book2.excel
342
- book6.close
343
- end
344
-
345
-
346
- end
347
-
348
- context "with :if_unsaved" do
349
-
350
- before do
351
- @book = Book.open(@simple_file)
352
- @sheet = @book.sheet(1)
353
- @book.add_sheet(@sheet, :as => 'a_name')
354
- end
355
-
356
- after do
357
- @book.close(:if_unsaved => :forget)
358
- @new_book.close rescue nil
359
- end
360
-
361
- it "should raise an error, if :if_unsaved is :raise" do
362
- expect {
363
- @new_book = Book.open(@simple_file, :if_unsaved => :raise)
364
- }.to raise_error(WorkbookNotSaved, /workbook is already open but not saved: "workbook.xls"/)
365
- end
366
-
367
- it "should let the book open, if :if_unsaved is :accept" do
368
- expect {
369
- @new_book = Book.open(@simple_file, :if_unsaved => :accept)
370
- }.to_not raise_error
371
- @book.should be_alive
372
- @new_book.should be_alive
373
- @new_book.should == @book
374
- end
375
-
376
- context "with :if_unsaved => :alert or :if_unsaved => :excel" do
377
- before do
378
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
379
- end
380
-
381
- after do
382
- @key_sender.close
383
- end
384
-
385
- it "should open the new book and close the unsaved book, if user answers 'yes'" do
386
- # "Yes" is the default. --> language independent
387
- @key_sender.puts "{enter}"
388
- @new_book = Book.open(@simple_file1, :if_unsaved => :alert)
389
- @new_book.should be_alive
390
- @new_book.filename.downcase.should == @simple_file1.downcase
391
- @book.should_not be_alive
392
- end
393
-
394
- it "should not open the new book and not close the unsaved book, if user answers 'no'" do
395
- # "No" is right to "Yes" (the default). --> language independent
396
- # strangely, in the "no" case, the question will sometimes be repeated three times
397
- #@book.excel.Visible = true
398
- @key_sender.puts "{right}{enter}"
399
- @key_sender.puts "{right}{enter}"
400
- @key_sender.puts "{right}{enter}"
401
- #expect{
402
- # Book.open(@simple_file, :if_unsaved => :alert)
403
- # }.to raise_error(ExcelREOError, /user canceled or runtime error/)
404
- @book.should be_alive
405
- end
406
-
407
- it "should open the new book and close the unsaved book, if user answers 'yes'" do
408
- # "Yes" is the default. --> language independent
409
- @key_sender.puts "{enter}"
410
- @new_book = Book.open(@simple_file1, :if_unsaved => :excel)
411
- @new_book.should be_alive
412
- @new_book.filename.downcase.should == @simple_file1.downcase
413
- #@book.should_not be_alive
414
- end
415
-
416
- it "should not open the new book and not close the unsaved book, if user answers 'no'" do
417
- # "No" is right to "Yes" (the default). --> language independent
418
- # strangely, in the "no" case, the question will sometimes be repeated three times
419
- #@book.excel.Visible = true
420
- @key_sender.puts "{right}{enter}"
421
- @key_sender.puts "{right}{enter}"
422
- @key_sender.puts "{right}{enter}"
423
- #expect{
424
- # Book.open(@simple_file, :if_unsaved => :excel)
425
- # }.to raise_error(ExcelREOError, /user canceled or runtime error/)
426
- @book.should be_alive
427
- end
428
-
429
- end
430
- end
431
-
432
- context "with :if_obstructed" do
433
-
434
- for i in 1..2 do
435
-
436
- context "with and without reopen" do
437
-
438
- before do
439
- if i == 1 then
440
- book_before = Book.open(@simple_file)
441
- book_before.close
442
- end
443
- @book = Book.open(@simple_file_other_path1)
444
- @sheet_count = @book.ole_workbook.Worksheets.Count
445
- @sheet = @book.sheet(1)
446
- @book.add_sheet(@sheet, :as => 'a_name')
447
- end
448
-
449
- after do
450
- @book.close(:if_unsaved => :forget)
451
- @new_book.close rescue nil
452
- end
453
-
454
- it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
455
- @new_book = Book.open(@simple_file1, :if_obstructed => :save)
456
- @book.should_not be_alive
457
- @new_book.should be_alive
458
- @new_book.filename.downcase.should == @simple_file1.downcase
459
- old_book = Book.open(@simple_file_other_path1, :if_obstructed => :forget)
460
- old_book.ole_workbook.Worksheets.Count.should == @sheet_count + 1
461
- old_book.close
462
- end
463
-
464
- it "should raise an error, if the old book is unsaved, and close the old book and open the new book,
465
- if :if_obstructed is :close_if_saved" do
466
- expect{
467
- @new_book = Book.open(@simple_file1, :if_obstructed => :close_if_saved)
468
- }.to raise_error(WorkbookBlocked, /workbook with the same name in a different path is unsaved/)
469
- @book.save
470
- @new_book = Book.open(@simple_file1, :if_obstructed => :close_if_saved)
471
- @book.should_not be_alive
472
- @new_book.should be_alive
473
- @new_book.filename.downcase.should == @simple_file1.downcase
474
- old_book = Book.open(@simple_file_other_path1, :if_obstructed => :forget)
475
- old_book.ole_workbook.Worksheets.Count.should == @sheet_count + 1
476
- old_book.close
477
- end
478
- end
479
- end
480
- end
481
-
482
- context "with non-existing file" do
483
-
484
- it "should create a workbook" do
485
- File.delete @simple_save_file rescue nil
486
- book = Book.open(@simple_save_file, :if_absent => :create)
487
- book.should be_a Book
488
- book.close
489
- File.exist?(@simple_save_file).should be_true
490
- end
491
-
492
- it "should raise an exception by default" do
493
- File.delete @simple_save_file rescue nil
494
- expect {
495
- Book.open(@simple_save_file)
496
- }.to raise_error(FileNotFound, "file #{General::absolute_path(@simple_save_file).gsub("/","\\").inspect} not found")
497
- end
498
- end
499
-
500
- context "with :read_only" do
501
-
502
- it "should reopen the book with writable (unsaved changes from readonly will not be saved)" do
503
- book = Book.open(@simple_file1, :read_only => true)
504
- book.ReadOnly.should be_true
505
- book.should be_alive
506
- sheet = book.sheet(1)
507
- old_cell_value = sheet[1,1].value
508
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
509
- book.Saved.should be_false
510
- new_book = Book.open(@simple_file1, :read_only => false, :if_unsaved => :accept)
511
- new_book.ReadOnly.should be_false
512
- new_book.should be_alive
513
- book.should be_alive
514
- new_book.should == book
515
- new_sheet = new_book.sheet(1)
516
- new_cell_value = new_sheet[1,1].value
517
- new_cell_value.should == old_cell_value
518
- end
519
-
520
- context "with block" do
521
- it 'block parameter should be instance of Book' do
522
- Book.open(@simple_file) do |book|
523
- book.should be_a Book
524
- end
525
- end
526
- end
527
- end
528
-
529
- describe "reopen" do
530
-
531
- context "with standard" do
532
-
533
- before do
534
- @book = Book.open(@simple_file)
535
- end
536
-
537
- after do
538
- @book.close
539
- end
540
-
541
- it "should reopen the closed book" do
542
- @book.should be_alive
543
- book1 = @book
544
- @book.close
545
- @book.should_not be_alive
546
- @book.reopen
547
- @book.should be_a Book
548
- @book.should be_alive
549
- @book.should === book1
550
- end
551
- end
552
- end
553
-
554
- describe "uplifting" do
555
-
556
- context "with standard" do
557
-
558
- before do
559
- @book = Book.open(@simple_file)
560
- end
561
-
562
- after do
563
- @book.close
564
- end
565
-
566
- it "should uplift a workbook to a book with an open book" do
567
- workbook = @book.ole_workbook
568
- book1 = Book.new(workbook)
569
- book1.should be_a Book
570
- book1.should be_alive
571
- book1.should == @book
572
- end
573
- end
574
- end
575
-
576
- describe "visible" do
577
-
578
- it "should preserve :visible if they are not set" do
579
- excel1 = Excel.create(:visible => true)
580
- book1 = Book.open(@simple_file)
581
- book1.excel.Visible.should be_true
582
- book1.close
583
- end
584
-
585
- it "should preserve :visible if they are not set" do
586
- excel1 = Excel.create
587
- book1 = Book.open(@simple_file, :visible => true)
588
- book1.excel.Visible.should be_true
589
- end
590
-
591
- it "should preserve :visible if they are not set" do
592
- excel1 = Excel.create(:visible => true)
593
- book1 = Book.open(@different_file, :default => {:excel => :new})
594
- book1.excel.Visible.should be_false
595
- end
596
-
597
- it "should preserve :visible if they are not set" do
598
- excel1 = Excel.create(:visible => true)
599
- excel2 = Excel.create(:visible => true)
600
- book1 = Book.open(@different_file, :force => {:excel => excel2})
601
- book1.excel.Visible.should be_true
602
- book1.close
603
- end
604
-
605
- it "should let an open Book open" do
606
- @book = Book.open(@simple_file, :visible => true)
607
- Book.unobtrusively(@simple_file) do |book|
608
- book.should be_a Book
609
- book.should be_alive
610
- book.excel.should == @book.excel
611
- book.excel.Visible.should be_true
612
- end
613
- @book.should be_alive
614
- @book.should be_a Book
615
- @book.excel.Visible.should be_true
616
- @book.close(:if_unsaved => :forget)
617
- @book2.close(:if_unsaved => :forget) rescue nil
618
- end
619
- end
620
-
621
-
622
- describe "unobtrusively" do
623
-
624
- def unobtrusively_ok? # :nodoc: #
625
- Book.unobtrusively(@simple_file) do |book|
626
- book.should be_a Book
627
- sheet = book.sheet(1)
628
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
629
- book.should be_alive
630
- book.Saved.should be_false
631
- end
632
- end
633
-
634
- context "with no open book" do
635
-
636
- it "should open unobtrusively in a new Excel" do
637
- expect{ unobtrusively_ok? }.to_not raise_error
638
- end
639
- end
640
-
641
- context "with two running excel instances" do
642
- before :all do
643
- Excel.close_all
644
- end
645
-
646
- before do
647
- @excel1 = Excel.new(:reuse => false)
648
- @excel2 = Excel.new(:reuse => false)
649
- end
650
-
651
- after do
652
- #Excel.close_all
653
- begin
654
- @excel1.close
655
- @excel2.close
656
- rescue ExcelREOError => msg
657
- # puts "ExcelREOError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
658
- end
659
- end
660
-
661
- it "should open unobtrusively in a new Excel" do
662
- Book.unobtrusively(@simple_file, :if_closed => :current) do |book|
663
- book.should be_a Book
664
- book.should be_alive
665
- book.excel.should == @excel1
666
- book.excel.should_not == @excel2
667
- end
668
- end
669
-
670
- it "should open unobtrusively in a given Excel" do
671
- Book.unobtrusively(@simple_file, :if_closed => @excel2) do |book|
672
- book.should be_a Book
673
- book.should be_alive
674
- book.excel.should_not == @excel1
675
- book.excel.should == @excel2
676
- end
677
- end
678
- end
679
-
680
- context "with an open book" do
681
-
682
- before do
683
- @book = Book.open(@simple_file1)
684
- end
685
-
686
- after do
687
- @book.close(:if_unsaved => :forget)
688
- @book2.close(:if_unsaved => :forget) rescue nil
689
- end
690
-
691
- it "should let an open Book open if two books have been opened and one has been closed and opened again" do
692
- book2 = Book.open(@different_file, :force => {:excel => :new})
693
- @book.close
694
- book2.close
695
- @book.reopen
696
- Book.unobtrusively(@simple_file1) do |book|
697
- book.should be_a Book
698
- book.should be_alive
699
- book.excel.should == @book.excel
700
- end
701
- @book.should be_alive
702
- @book.should be_a Book
703
- end
704
-
705
- it "should let a saved book saved" do
706
- @book.Saved.should be_true
707
- @book.should be_alive
708
- sheet = @book.sheet(1)
709
- old_cell_value = sheet[1,1].value
710
- unobtrusively_ok?
711
- @book.Saved.should be_true
712
- @book.should be_alive
713
- sheet = @book.sheet(1)
714
- sheet[1,1].value.should_not == old_cell_value
715
- end
716
-
717
- it "should let the unsaved book unsaved" do
718
- sheet = @book.sheet(1)
719
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
720
- old_cell_value = sheet[1,1].value
721
- @book.Saved.should be_false
722
- unobtrusively_ok?
723
- @book.should be_alive
724
- @book.Saved.should be_false
725
- @book.close(:if_unsaved => :forget)
726
- @book2 = Book.open(@simple_file1)
727
- sheet2 = @book2.sheet(1)
728
- sheet2[1,1].value.should_not == old_cell_value
729
- end
730
- end
731
-
732
- context "with a closed book" do
733
-
734
- before do
735
- @book = Book.open(@simple_file1)
736
- end
737
-
738
- after do
739
- @book.close(:if_unsaved => :forget)
740
- end
741
-
742
- it "should let the closed book closed by default" do
743
- sheet = @book.sheet(1)
744
- old_cell_value = sheet[1,1].value
745
- @book.close
746
- @book.should_not be_alive
747
- unobtrusively_ok?
748
- @book.should_not be_alive
749
- book2 = Book.open(@simple_file1)
750
- sheet = book2.sheet(1)
751
- sheet[1,1].Value.should_not == old_cell_value
752
- end
753
-
754
- # The bold reanimation of the @book
755
- it "should use the excel of the book and keep open the book" do
756
- excel = Excel.new(:reuse => false)
757
- sheet = @book.sheet(1)
758
- old_cell_value = sheet[1,1].value
759
- @book.close
760
- @book.should_not be_alive
761
- Book.unobtrusively(@simple_file1, :keep_open => true) do |book|
762
- book.should be_a Book
763
- book.excel.should == @book.excel
764
- book.excel.should_not == excel
765
- sheet = book.sheet(1)
766
- cell = sheet[1,1]
767
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
768
- book.Saved.should be_false
769
- end
770
- @book.should be_alive
771
- @book.close
772
- new_book = Book.open(@simple_file1)
773
- sheet = new_book.sheet(1)
774
- sheet[1,1].value.should_not == old_cell_value
775
- end
776
-
777
- it "should use the excel of the book and keep open the book" do
778
- excel = Excel.new(:reuse => false)
779
- sheet = @book.sheet(1)
780
- old_cell_value = sheet[1,1].value
781
- @book.close
782
- @book.should_not be_alive
783
- Book.unobtrusively(@simple_file1, :if_closed => :current) do |book|
784
- book.should be_a Book
785
- book.should be_alive
786
- book.excel.should == @book.excel
787
- book.excel.should_not == excel
788
- sheet = book.sheet(1)
789
- cell = sheet[1,1]
790
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
791
- book.Saved.should be_false
792
- end
793
- @book.should_not be_alive
794
- new_book = Book.open(@simple_file1)
795
- sheet = new_book.sheet(1)
796
- sheet[1,1].value.should_not == old_cell_value
797
- end
798
- end
799
-
800
-
801
- context "with a read_only book" do
802
-
803
- before do
804
- @book = Book.open(@simple_file1, :read_only => true)
805
- end
806
-
807
- after do
808
- @book.close
809
- end
810
-
811
- it "should open unobtrusively the book in a new Excel such that the book is writable" do
812
- book2 = Book.open(@simple_file1, :force => {:excel => :new}, :read_only => true)
813
- @book.ReadOnly.should be_true
814
- book2.Readonly.should be_true
815
- sheet = @book.sheet(1)
816
- cell_value = sheet[1,1].value
817
- Book.unobtrusively(@simple_file1, :rw_change_excel => :new, :if_closed => :current, :writable => true) do |book|
818
- book.should be_a Book
819
- book.excel.should_not == book2.excel
820
- book.excel.should_not == @book.excel
821
- sheet = book.sheet(1)
822
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
823
- book.should be_alive
824
- book.Saved.should be_false
825
- end
826
- @book.Saved.should be_true
827
- @book.ReadOnly.should be_true
828
- @book.close
829
- book2.close
830
- book3 = Book.open(@simple_file1)
831
- new_sheet = book3.sheet(1)
832
- new_sheet[1,1].value.should_not == cell_value
833
- book3.close
834
- end
835
- end
836
-
837
- context "with a virgin Book class" do
838
- before do
839
- class Book # :nodoc: #
840
- @@bookstore = nil
841
- end
842
- end
843
- it "should work" do
844
- expect{ unobtrusively_ok? }.to_not raise_error
845
- end
846
- end
847
-
848
- context "with a book never opened before" do
849
- before do
850
- class Book # :nodoc: #
851
- @@bookstore = nil
852
- end
853
- other_book = Book.open(@different_file)
854
- end
855
- it "should open the book" do
856
- expect{ unobtrusively_ok? }.to_not raise_error
857
- end
858
- end
859
-
860
- context "with block result" do
861
- before do
862
- @book1 = Book.open(@simple_file)
863
- end
864
-
865
- after do
866
- @book1.close(:if_unsaved => :forget)
867
- end
868
-
869
- it "should yield the block result true" do
870
- result =
871
- Book.unobtrusively(@simple_file) do |book|
872
- @book1.Saved.should be_true
873
- end
874
- result.should == true
875
- end
876
- end
877
-
878
- context "with several Excel instances" do
879
-
880
- before do
881
- @book1 = Book.open(@simple_file1)
882
- @book2 = Book.open(@simple_file1, :force => {:excel => :new})
883
- @book1.Readonly.should == false
884
- @book2.Readonly.should == true
885
- old_sheet = @book1.sheet(1)
886
- @old_cell_value = old_sheet[1,1].value
887
- @book1.close
888
- @book2.close
889
- @book1.should_not be_alive
890
- @book2.should_not be_alive
891
- end
892
-
893
- it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
894
- Book.unobtrusively(@simple_file) do |book|
895
- book.excel.should_not == @book2.excel
896
- book.excel.should == @book1.excel
897
- book.ReadOnly.should == false
898
- sheet = book.sheet(1)
899
- cell = sheet[1,1]
900
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
901
- book.Saved.should be_false
902
- end
903
- new_book = Book.open(@simple_file1)
904
- sheet = new_book.sheet(1)
905
- sheet[1,1].value.should_not == @old_cell_value
906
- end
907
-
908
- it "should open unobtrusively the closed book in the new hidden Excel" do
909
- Book.unobtrusively(@simple_file, :if_closed => :current) do |book|
910
- book.excel.should_not == @book2.excel
911
- book.excel.should == @book1.excel
912
- book.ReadOnly.should == false
913
- sheet = book.sheet(1)
914
- cell = sheet[1,1]
915
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
916
- book.Saved.should be_false
917
- end
918
- new_book = Book.open(@simple_file1)
919
- sheet = new_book.sheet(1)
920
- sheet[1,1].Value.should_not == @old_cell_value
921
- end
922
- end
923
- end
924
-
925
- =begin
926
- context "with :hidden" do
927
-
928
- before do
929
- @book1 = Book.open(@simple_file1)
930
- @book1.close
931
- end
932
-
933
- it "should create a new hidden Excel instance and use this afterwards" do
934
- hidden_excel = nil
935
- Book.unobtrusively(@simple_file1, :hidden) do |book|
936
- book.should be_a Book
937
- book.should be_alive
938
- book.excel.Visible.should be_false
939
- book.excel.DisplayAlerts.should be_false
940
- hidden_excel = book.excel
941
- end
942
- Book.unobtrusively(@different_file, :hidden) do |book|
943
- book.should be_a Book
944
- book.should be_alive
945
- book.excel.Visible.should be_false
946
- book.excel.DisplayAlerts.should be_false
947
- book.excel.should == hidden_excel
948
- end
949
- end
950
- end
951
- end
952
- =end
953
-
954
- describe "for_reading, for_modifying" do
955
-
956
- context "open unobtrusively for reading and modifying" do
957
-
958
- before do
959
- @book = Book.open(@simple_file1)
960
- sheet = @book.sheet(1)
961
- @old_cell_value = sheet[1,1].value
962
- @book.close
963
- end
964
-
965
- it "should not change the value" do
966
- Book.for_reading(@simple_file) do |book|
967
- book.should be_a Book
968
- book.should be_alive
969
- book.Saved.should be_true
970
- sheet = book.sheet(1)
971
- cell = sheet[1,1]
972
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
973
- book.Saved.should be_false
974
- book.excel.should == @book.excel
975
- end
976
- new_book = Book.open(@simple_file1, :visible => true)
977
- sheet = new_book.sheet(1)
978
- sheet[1,1].Value.should == @old_cell_value
979
- end
980
-
981
-
982
- it "should not change the value and use the hidden Excel instance" do
983
- new_excel = Excel.new(:reuse => false)
984
- Book.for_reading(@simple_file1, :if_closed => :new) do |book|
985
- sheet = book.sheet(1)
986
- cell = sheet[1,1]
987
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
988
- book.excel.should_not == @book.excel
989
- book.excel.should_not == new_excel
990
- book.excel.visible.should be_false
991
- book.excel.displayalerts.should == :if_visible
992
- end
993
- new_book = Book.open(@simple_file1, :visible => true)
994
- sheet = new_book.sheet(1)
995
- sheet[1,1].Value.should == @old_cell_value
996
- end
997
-
998
- it "should change the value" do
999
- Book.for_modifying(@simple_file1) do |book|
1000
- sheet = book.sheet(1)
1001
- cell = sheet[1,1]
1002
- sheet[1,1] = cell.value == "foo" ? "bar" : "foo"
1003
- book.excel.should == @book.excel
1004
- end
1005
- new_book = Book.open(@simple_file1, :visible => true)
1006
- sheet = new_book.sheet(1)
1007
- sheet[1,1].Value.should_not == @old_cell_value
1008
- end
1009
- end
1010
- end
1011
-
1012
- describe "send methods to workbook" do
1013
-
1014
- context "with standard" do
1015
- before do
1016
- @book = Book.open(@simple_file)
1017
- end
1018
-
1019
- after do
1020
- @book.close
1021
- end
1022
-
1023
- it "should send Saved to workbook" do
1024
- @book.Saved.should be_true
1025
- end
1026
- end
1027
- end
1028
-
1029
- describe "nameval, set_nameval, [], []=" do
1030
-
1031
- before do
1032
- @book1 = Book.open(@another_simple_file)
1033
- end
1034
-
1035
- after do
1036
- @book1.close(:if_unsaved => :forget)
1037
- end
1038
-
1039
- it "should return value of a range" do
1040
- @book1.nameval("new").should == "foo"
1041
- @book1.nameval("one").should == 1
1042
- @book1.nameval("firstrow").should == [[1,2]]
1043
- @book1.nameval("four").should == [[1,2],[3,4]]
1044
- @book1.nameval("firstrow").should_not == "12"
1045
- @book1.nameval("firstcell").should == "foo"
1046
- end
1047
-
1048
- it "should return value of a range via []" do
1049
- @book1["new"].should == "foo"
1050
- @book1["one"].should == 1
1051
- @book1["firstrow"] == [[1,2]]
1052
- @book1["four"].should == [[1,2],[3,4]]
1053
- @book1["firstrow"].should_not == "12"
1054
- @book1["firstcell"].should == "foo"
1055
- end
1056
-
1057
- it "should set value of a range" do
1058
- @book1.set_nameval("new", "bar")
1059
- @book1.nameval("new").should == "bar"
1060
- end
1061
-
1062
- it "should set value of a range via []=" do
1063
- @book1["new"] = "bar"
1064
- @book1.nameval("new").should == "bar"
1065
- end
1066
-
1067
- #it "should evaluate a formula" do
1068
- # @book1.nameval("named_formula").should == 4
1069
- #end
1070
-
1071
- #it "should evaluate a formula via []" do
1072
- # @book1["named_formula"].should == 4
1073
- #end
1074
-
1075
- #it "should return default value if name not defined" do
1076
- # @book1.nameval("foo", :default => 2).should == 2
1077
- #end
1078
-
1079
- end
1080
-
1081
- describe "close" do
1082
-
1083
- context "with unsaved read_only book" do
1084
- before do
1085
- @book = Book.open(@simple_file, :read_only => true)
1086
- @sheet_count = @book.ole_workbook.Worksheets.Count
1087
- @book.add_sheet(@sheet, :as => 'a_name')
1088
- end
1089
-
1090
- it "should close the unsaved book without error and without saving" do
1091
- expect{
1092
- @book.close
1093
- }.to_not raise_error
1094
- new_book = Book.open(@simple_file)
1095
- new_book.ole_workbook.Worksheets.Count.should == @sheet_count
1096
- new_book.close
1097
- end
1098
- end
1099
-
1100
- context "with unsaved book" do
1101
- before do
1102
- @book = Book.open(@simple_file)
1103
- @sheet_count = @book.ole_workbook.Worksheets.Count
1104
- @book.add_sheet(@sheet, :as => 'a_name')
1105
- @sheet = @book.sheet(1)
1106
- end
1107
-
1108
- after do
1109
- @book.close(:if_unsaved => :forget) rescue nil
1110
- end
1111
-
1112
- it "should raise error by default" do
1113
- expect{
1114
- @book.close(:if_unsaved => :raise)
1115
- }.to raise_error(WorkbookNotSaved, /workbook is unsaved: "workbook.xls"/)
1116
- end
1117
-
1118
- it "should save the book before close with option :save" do
1119
- ole_workbook = @book.ole_workbook
1120
- excel = @book.excel
1121
- excel.Workbooks.Count.should == 1
1122
- @book.close(:if_unsaved => :save)
1123
- excel.Workbooks.Count.should == 0
1124
- @book.ole_workbook.should == nil
1125
- @book.should_not be_alive
1126
- expect{
1127
- ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
1128
- new_book = Book.open(@simple_file)
1129
- begin
1130
- new_book.ole_workbook.Worksheets.Count.should == @sheet_count + 1
1131
- ensure
1132
- new_book.close
1133
- end
1134
- end
1135
- end
1136
- end
1137
-
1138
- describe "save" do
1139
-
1140
- context "with simple save" do
1141
-
1142
- it "should save for a file opened without :read_only" do
1143
- @book = Book.open(@simple_file)
1144
- @book.add_sheet(@sheet, :as => 'a_name')
1145
- @new_sheet_count = @book.ole_workbook.Worksheets.Count
1146
- expect {
1147
- @book.save
1148
- }.to_not raise_error
1149
- @book.ole_workbook.Worksheets.Count.should == @new_sheet_count
1150
- @book.close
1151
- end
1152
- end
1153
-
1154
- context "with argument" do
1155
- before do
1156
- Book.open(@simple_file) do |book|
1157
- book.save_as(@simple_save_file, :if_exists => :overwrite)
1158
- end
1159
- end
1160
-
1161
- it "should save to 'simple_save_file.xlsx'" do
1162
- File.exist?(@simple_save_file).should be_true
1163
- end
1164
- end
1165
- end
1166
-
1167
- describe "alive?, filename, ==, focus, saved" do
1168
-
1169
- context "with alive?" do
1170
-
1171
- before do
1172
- @book = Book.open(@simple_file)
1173
- end
1174
-
1175
- after do
1176
- @book.close
1177
- end
1178
-
1179
- it "should return true, if book is alive" do
1180
- @book.should be_alive
1181
- end
1182
-
1183
- it "should return false, if book is dead" do
1184
- @book.close
1185
- @book.should_not be_alive
1186
- end
1187
-
1188
- end
1189
-
1190
- context "with filename" do
1191
-
1192
- before do
1193
- @book = Book.open(@simple_file)
1194
- end
1195
-
1196
- after do
1197
- @book.close
1198
- end
1199
-
1200
- it "should return full file name" do
1201
- @book.filename.should == @simple_file
1202
- end
1203
-
1204
- end
1205
-
1206
- context "with ==" do
1207
-
1208
- before do
1209
- @book = Book.open(@simple_file)
1210
- end
1211
-
1212
- after do
1213
- @book.close
1214
- @new_book.close rescue nil
1215
- end
1216
-
1217
- it "should be true with two identical books" do
1218
- @new_book = Book.open(@simple_file)
1219
- @new_book.should == @book
1220
- end
1221
- end
1222
-
1223
- context "with focus" do
1224
-
1225
- before do
1226
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
1227
- @book = Book.open(@simple_file, :visible => true)
1228
- @book2 = Book.open(@another_simple_file, :visible => true)
1229
- end
1230
-
1231
- after do
1232
- @book.close(:if_unsaved => :forget)
1233
- @book2.close(:if_unsaved => :forget)
1234
- @key_sender.close
1235
- end
1236
-
1237
- it "should bring a book to focus" do
1238
- sheet = @book.sheet(2)
1239
- sheet.Activate
1240
- sheet[2,3].Activate
1241
- sheet2 = @book2.sheet(2)
1242
- sheet2.Activate
1243
- sheet2[3,2].Activate
1244
- @book2.focus
1245
- @key_sender.puts "{a}{enter}"
1246
- sleep 0.2
1247
- sheet2[3,2].Value.should == "a"
1248
- @book.focus
1249
- @book.Windows(1).Visible.should be_true
1250
- @book.Windows(@book.Name).Visible.should be_true
1251
- @key_sender.puts "{a}{enter}"
1252
- sleep 0.2
1253
- sheet[2,3].Value.should == "a"
1254
- Excel.current.should == @book.excel
1255
- end
1256
- end
1257
- end
1258
-
1259
- describe "#add_sheet" do
1260
- before do
1261
- @book = Book.open(@simple_file)
1262
- @sheet = @book.sheet(1)
1263
- end
1264
-
1265
- after do
1266
- @book.close(:if_unsaved => :forget)
1267
- end
1268
-
1269
- context "only first argument" do
1270
- it "should add worksheet" do
1271
- @book.ole_workbook.Worksheets.Count.should == 3
1272
- @book.add_sheet @sheet
1273
- @book.ole_workbook.Worksheets.Count.should == 4
1274
- #expect { @book.add_sheet @sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
1275
- end
1276
-
1277
- it "should return copyed sheet" do
1278
- sheet = @book.add_sheet @sheet
1279
- copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
1280
- sheet.name.should eq copyed_sheet.name
1281
- end
1282
- end
1283
-
1284
- context "with first argument" do
1285
-
1286
- context "with second argument is {:before => @book.sheet(3), :after => @sheet}" do
1287
- it "should arguments in the first is given priority" do
1288
- @book.add_sheet(@sheet, :before => @book.sheet(3), :after => @sheet)
1289
- @book.Worksheets.Count.should == 4
1290
- end
1291
- end
1292
- end
1293
-
1294
- context "without first argument" do
1295
- context "second argument is {:as => 'new sheet'}" do
1296
- it "should return new sheet" do
1297
- @book.add_sheet(:as => 'new sheet').name.should eq 'new sheet'
1298
- end
1299
- end
1300
-
1301
- context "second argument is {:before => @sheet}" do
1302
- it "should add the first sheet" do
1303
- @book.add_sheet(:before => @sheet).name.should eq @book.sheet(1).name
1304
- end
1305
- end
1306
-
1307
- context "second argument is {:after => @sheet}" do
1308
- it "should add the second sheet" do
1309
- @book.add_sheet(:after => @sheet).name.should eq @book.sheet(2).name
1310
- end
1311
- end
1312
- end
1313
-
1314
- context "without argument" do
1315
-
1316
- it "should return copyed sheet" do
1317
- sheet = @book.add_sheet
1318
- copyed_sheet = @book.ole_workbook.Worksheets.Item(@book.ole_workbook.Worksheets.Count)
1319
- sheet.name.should eq copyed_sheet.name
1320
- end
1321
- end
1322
- end
1323
-
1324
- describe 'access sheet' do
1325
- before do
1326
- @book = Book.open(@simple_file)
1327
- end
1328
-
1329
- after do
1330
- @book.close
1331
- end
1332
-
1333
- context "standard" do
1334
-
1335
- it 'with sheet name' do
1336
- @book.sheet('Sheet1').should be_kind_of Sheet
1337
- end
1338
-
1339
- it 'with integer' do
1340
- @book.sheet(1).should be_kind_of Sheet
1341
- end
1342
-
1343
- it 'with block' do
1344
- @book.each do |sheet|
1345
- sheet.should be_kind_of Sheet
1346
- end
1347
- end
1348
-
1349
- it 'with each_with_index' do
1350
- @book.each_with_index do |sheet,i|
1351
- sheet.should be_kind_of Sheet
1352
- end
1353
- end
1354
- end
1355
-
1356
- describe "with retain_saved" do
1357
-
1358
- before do
1359
- @book = Book.open(@simple_file)
1360
- end
1361
-
1362
- after do
1363
- @book.close(:if_unsaved => :forget)
1364
- end
1365
-
1366
- it "should keep the save state 'unsaved'" do
1367
- sheet = @book.sheet(1)
1368
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1369
- @book.Saved.should be_false
1370
- @book.retain_saved do
1371
- sheet = @book.sheet(1)
1372
- a = sheet[1,1]
1373
- b = @book.visible
1374
- end
1375
- @book.Saved.should be_false
1376
- end
1377
-
1378
- it "should keep the save state 'unsaved' even when the workbook was saved before" do
1379
- sheet = @book.sheet(1)
1380
- sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1381
- @book.Saved.should be_false
1382
- @book.retain_saved do
1383
- @book.save
1384
- @book.Saved.should be_true
1385
- end
1386
- @book.Saved.should be_false
1387
- end
1388
- end
1389
-
1390
- =begin
1391
- context "with test what happens with save-status when setting calculation status" do
1392
-
1393
- it "should keep the save status" do
1394
- book1 = Book.open(@simple_file, :visible => true)
1395
- book1.Saved.should be_true
1396
- book2 = Book.open(@another_simple_file, :visible => true)
1397
- book1.Saved.should be_true
1398
- book2.Saved.should be_true
1399
- sheet2 = book2.sheet(1)
1400
- sheet2[1,1] = sheet2[1,1].value == "foo" ? "bar" : "foo"
1401
- book1.Saved.should be_true
1402
- book2.Saved.should be_false
1403
- book3 = Book.open(@different_file, :visible => true)
1404
- book1.Saved.should be_true
1405
- book2.Saved.should be_false
1406
- book3.Saved.should be_true
1407
- end
1408
- end
1409
- =end
1410
-
1411
- context 'open with block' do
1412
- it {
1413
- Book.open(@simple_file) do |book|
1414
- book.sheet('Sheet1').should be_a Sheet
1415
- end
1416
- }
1417
- end
1418
- end
1419
- end
1420
- end
1421
- end