robust_excel_ole 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README.rdoc +73 -26
  2. data/README_detail.rdoc +92 -27
  3. data/examples/edit_sheets/example_access_sheets_and_cells.rb +3 -3
  4. data/examples/edit_sheets/example_concating.rb +12 -12
  5. data/examples/edit_sheets/example_copying.rb +47 -0
  6. data/examples/edit_sheets/example_expanding.rb +17 -26
  7. data/examples/edit_sheets/example_naming.rb +13 -10
  8. data/examples/edit_sheets/example_ranges.rb +2 -2
  9. data/examples/edit_sheets/example_saving.rb +8 -14
  10. data/examples/open_save_close/example_control_to_excel.rb +1 -1
  11. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +3 -3
  12. data/examples/open_save_close/example_if_obstructed_save.rb +3 -3
  13. data/examples/open_save_close/example_if_unsaved_accept.rb +1 -1
  14. data/examples/open_save_close/example_if_unsaved_forget.rb +4 -4
  15. data/examples/open_save_close/example_if_unsaved_forget_more.rb +5 -5
  16. data/examples/open_save_close/example_read_only.rb +1 -1
  17. data/examples/open_save_close/example_rename_cells.rb +1 -13
  18. data/examples/open_save_close/example_simple.rb +1 -1
  19. data/examples/open_save_close/example_unobtrusively.rb +3 -3
  20. data/lib/robust_excel_ole.rb +81 -2
  21. data/lib/robust_excel_ole/book.rb +171 -118
  22. data/lib/robust_excel_ole/{book_store.rb → bookstore.rb} +2 -2
  23. data/lib/robust_excel_ole/excel.rb +153 -24
  24. data/lib/robust_excel_ole/range.rb +2 -2
  25. data/lib/robust_excel_ole/sheet.rb +84 -35
  26. data/lib/robust_excel_ole/version.rb +1 -1
  27. data/reo.bat +3 -0
  28. data/spec/book_close_spec.rb +179 -0
  29. data/spec/book_misc_spec.rb +365 -0
  30. data/spec/book_open_spec.rb +793 -0
  31. data/spec/book_save_spec.rb +257 -0
  32. data/spec/book_sheet_spec.rb +160 -0
  33. data/spec/book_spec.rb +145 -1533
  34. data/spec/book_subclass_spec.rb +50 -0
  35. data/spec/book_unobtr_spec.rb +950 -0
  36. data/spec/{book_store_spec.rb → bookstore_spec.rb} +5 -5
  37. data/spec/cell_spec.rb +6 -6
  38. data/spec/data/{more_workbook.xls → another_workbook.xls} +0 -0
  39. data/spec/data/different_workbook.xls +0 -0
  40. data/spec/data/workbook.xls +0 -0
  41. data/spec/data/workbook.xlsm +0 -0
  42. data/spec/data/workbook.xlsx +0 -0
  43. data/spec/data/workbook_linked.xlsm +0 -0
  44. data/spec/data/workbook_linked_sub.xlsm +0 -0
  45. data/spec/excel_spec.rb +204 -5
  46. data/spec/range_spec.rb +6 -6
  47. data/spec/sheet_spec.rb +122 -34
  48. metadata +18 -8
  49. data/spec/data/workbook_connected_sub.xlsm +0 -0
@@ -0,0 +1,257 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+
10
+ describe Book do
11
+
12
+ before(:all) do
13
+ excel = Excel.new(:reuse => true)
14
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
15
+ puts "*** open books *** : #{open_books}" if open_books > 0
16
+ Excel.close_all
17
+ end
18
+
19
+ before do
20
+ @dir = create_tmpdir
21
+ @simple_file = @dir + '/workbook.xls'
22
+ @simple_save_file = @dir + '/workbook_save.xls'
23
+ @different_file = @dir + '/different_workbook.xls'
24
+ @simple_file_other_path = @dir + '/more_data/workbook.xls'
25
+ @another_simple_file = @dir + '/another_workbook.xls'
26
+ @linked_file = @dir + '/workbook_linked.xlsm'
27
+ @simple_file_xlsm = @dir + '/workbook.xls'
28
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
29
+ end
30
+
31
+ after do
32
+ Excel.close_all
33
+ rm_tmp(@dir)
34
+ end
35
+
36
+ describe "save" do
37
+
38
+ context "with simple save" do
39
+
40
+ it "should save for a file opened without :read_only" do
41
+ @book = Book.open(@simple_file)
42
+ @book.add_sheet(@sheet, :as => 'a_name')
43
+ @new_sheet_count = @book.workbook.Worksheets.Count
44
+ expect {
45
+ @book.save
46
+ }.to_not raise_error
47
+ @book.workbook.Worksheets.Count.should == @new_sheet_count
48
+ @book.close
49
+ end
50
+
51
+ it "should raise error with read_only" do
52
+ @book = Book.open(@simple_file, :read_only => true)
53
+ expect {
54
+ @book.save
55
+ }.to raise_error(ExcelErrorSave, "Not opened for writing (opened with :read_only option)")
56
+ @book.close
57
+ end
58
+
59
+ end
60
+
61
+ context "with open with read only" do
62
+ before do
63
+ @book = Book.open(@simple_file, :read_only => true)
64
+ end
65
+
66
+ after do
67
+ @book.close
68
+ end
69
+
70
+ it {
71
+ expect {
72
+ @book.save_as(@simple_file)
73
+ }.to raise_error(IOError,
74
+ "Not opened for writing(open with :read_only option)")
75
+ }
76
+ end
77
+
78
+ context "with argument" do
79
+ before do
80
+ Book.open(@simple_file) do |book|
81
+ book.save_as(@simple_save_file, :if_exists => :overwrite)
82
+ end
83
+ end
84
+
85
+ it "should save to 'simple_save_file.xlsx'" do
86
+ File.exist?(@simple_save_file).should be_true
87
+ end
88
+ end
89
+
90
+ context "with different extensions" do
91
+ before do
92
+ @book = Book.open(@simple_file)
93
+ end
94
+
95
+ after do
96
+ @book.close
97
+ end
98
+
99
+ possible_extensions = ['xls', 'xlsm', 'xlsx']
100
+ possible_extensions.each do |extensions_value|
101
+ it "should save to 'simple_save_file.#{extensions_value}'" do
102
+ simple_save_file = @dir + '/simple_save_file.' + extensions_value
103
+ File.delete simple_save_file rescue nil
104
+ @book.save_as(simple_save_file, :if_exists => :overwrite)
105
+ File.exist?(simple_save_file).should be_true
106
+ new_book = Book.open(simple_save_file)
107
+ new_book.should be_a Book
108
+ new_book.close
109
+ end
110
+ end
111
+ end
112
+
113
+ # options :overwrite, :raise, :excel, no option, invalid option
114
+ possible_displayalerts = [true, false]
115
+ possible_displayalerts.each do |displayalert_value|
116
+ context "with displayalerts=#{displayalert_value}" do
117
+ before do
118
+ @book = Book.open(@simple_file, :displayalerts => displayalert_value)
119
+ end
120
+
121
+ after do
122
+ @book.close
123
+ end
124
+
125
+ it "should raise an error if the book is open" do
126
+ File.delete @simple_save_file rescue nil
127
+ FileUtils.copy @simple_file, @simple_save_file
128
+ book_save = Book.open(@simple_save_file, :excel => :new)
129
+ expect{
130
+ @book.save_as(@simple_save_file, :if_exists => :overwrite)
131
+ }.to raise_error(ExcelErrorSave, "book is open and used in Excel")
132
+ book_save.close
133
+ end
134
+
135
+ it "should save to simple_save_file.xls with :if_exists => :overwrite" do
136
+ File.delete @simple_save_file rescue nil
137
+ File.open(@simple_save_file,"w") do | file |
138
+ file.puts "garbage"
139
+ end
140
+ @book.save_as(@simple_save_file, :if_exists => :overwrite)
141
+ File.exist?(@simple_save_file).should be_true
142
+ new_book = Book.open(@simple_save_file)
143
+ new_book.should be_a Book
144
+ new_book.close
145
+ end
146
+ it "should save to 'simple_save_file.xls' with :if_exists => :raise" do
147
+ dirname, basename = File.split(@simple_save_file)
148
+ File.delete @simple_save_file rescue nil
149
+ File.open(@simple_save_file,"w") do | file |
150
+ file.puts "garbage"
151
+ end
152
+ File.exist?(@simple_save_file).should be_true
153
+ booklength = File.size?(@simple_save_file)
154
+ expect {
155
+ @book.save_as(@simple_save_file, :if_exists => :raise)
156
+ }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
157
+ File.exist?(@simple_save_file).should be_true
158
+ File.size?(@simple_save_file).should == booklength
159
+ end
160
+
161
+ context "with :if_exists => :alert" do
162
+ before do
163
+ File.delete @simple_save_file rescue nil
164
+ File.open(@simple_save_file,"w") do | file |
165
+ file.puts "garbage"
166
+ end
167
+ @garbage_length = File.size?(@simple_save_file)
168
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
169
+ end
170
+
171
+ after do
172
+ @key_sender.close
173
+ end
174
+
175
+ it "should save if user answers 'yes'" do
176
+ # "Yes" is to the left of "No", which is the default. --> language independent
177
+ @key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
178
+ @book.save_as(@simple_save_file, :if_exists => :alert)
179
+ File.exist?(@simple_save_file).should be_true
180
+ File.size?(@simple_save_file).should > @garbage_length
181
+ @book.excel.DisplayAlerts.should == displayalert_value
182
+ new_book = Book.open(@simple_save_file, :excel => :new)
183
+ new_book.should be_a Book
184
+ new_book.close
185
+ @book.excel.DisplayAlerts.should == displayalert_value
186
+ end
187
+
188
+ it "should not save if user answers 'no'" do
189
+ # Just give the "Enter" key, because "No" is the default. --> language independent
190
+ # strangely, in the "no" case, the question will sometimes be repeated three times
191
+ @key_sender.puts "{enter}"
192
+ @key_sender.puts "{enter}"
193
+ @key_sender.puts "{enter}"
194
+ #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
195
+ expect{
196
+ @book.save_as(@simple_save_file, :if_exists => :alert)
197
+ }.to raise_error(ExcelErrorSave, "not saved or canceled by user")
198
+ File.exist?(@simple_save_file).should be_true
199
+ File.size?(@simple_save_file).should == @garbage_length
200
+ @book.excel.DisplayAlerts.should == displayalert_value
201
+ end
202
+
203
+ it "should not save if user answers 'cancel'" do
204
+ # 'Cancel' is right from 'yes'
205
+ # strangely, in the "no" case, the question will sometimes be repeated three times
206
+ @key_sender.puts "{right}{enter}"
207
+ @key_sender.puts "{right}{enter}"
208
+ @key_sender.puts "{right}{enter}"
209
+ #@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
210
+ expect{
211
+ @book.save_as(@simple_save_file, :if_exists => :alert)
212
+ }.to raise_error(ExcelErrorSave, "not saved or canceled by user")
213
+ File.exist?(@simple_save_file).should be_true
214
+ File.size?(@simple_save_file).should == @garbage_length
215
+ @book.excel.DisplayAlerts.should == displayalert_value
216
+ end
217
+
218
+ it "should report save errors and leave DisplayAlerts unchanged" do
219
+ #@key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
220
+ @book.workbook.Close
221
+ expect{
222
+ @book.save_as(@simple_save_file, :if_exists => :alert)
223
+ }.to raise_error(ExcelErrorSaveUnknown)
224
+ File.exist?(@simple_save_file).should be_true
225
+ File.size?(@simple_save_file).should == @garbage_length
226
+ @book.excel.DisplayAlerts.should == displayalert_value
227
+ end
228
+
229
+ end
230
+
231
+ it "should save to 'simple_save_file.xls' with :if_exists => nil" do
232
+ dirname, basename = File.split(@simple_save_file)
233
+ File.delete @simple_save_file rescue nil
234
+ File.open(@simple_save_file,"w") do | file |
235
+ file.puts "garbage"
236
+ end
237
+ File.exist?(@simple_save_file).should be_true
238
+ booklength = File.size?(@simple_save_file)
239
+ expect {
240
+ @book.save_as(@simple_save_file)
241
+ }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
242
+ File.exist?(@simple_save_file).should be_true
243
+ File.size?(@simple_save_file).should == booklength
244
+ end
245
+
246
+ it "should save to 'simple_save_file.xls' with :if_exists => :invalid_option" do
247
+ File.delete @simple_save_file rescue nil
248
+ @book.save_as(@simple_save_file)
249
+ expect {
250
+ @book.save_as(@simple_save_file, :if_exists => :invalid_option)
251
+ }.to raise_error(ExcelErrorSave, ':if_exists: invalid option: invalid_option')
252
+ end
253
+ end
254
+ end
255
+ end
256
+ end
257
+
@@ -0,0 +1,160 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+
10
+ describe Book do
11
+
12
+ before(:all) do
13
+ excel = Excel.new(:reuse => true)
14
+ open_books = excel == nil ? 0 : excel.Workbooks.Count
15
+ puts "*** open books *** : #{open_books}" if open_books > 0
16
+ Excel.close_all
17
+ end
18
+
19
+ before do
20
+ @dir = create_tmpdir
21
+ @simple_file = @dir + '/workbook.xls'
22
+ @simple_save_file = @dir + '/workbook_save.xls'
23
+ @different_file = @dir + '/different_workbook.xls'
24
+ @simple_file_other_path = @dir + '/more_data/workbook.xls'
25
+ @another_simple_file = @dir + '/another_workbook.xls'
26
+ @linked_file = @dir + '/workbook_linked.xlsm'
27
+ @simple_file_xlsm = @dir + '/workbook.xls'
28
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
29
+ end
30
+
31
+ after do
32
+ Excel.close_all
33
+ rm_tmp(@dir)
34
+ end
35
+
36
+ describe "#add_sheet" do
37
+ before do
38
+ @book = Book.open(@simple_file)
39
+ @sheet = @book[0]
40
+ end
41
+
42
+ after do
43
+ @book.close(:if_unsaved => :forget)
44
+ end
45
+
46
+ context "only first argument" do
47
+ it "should add worksheet" do
48
+ expect { @book.add_sheet @sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
49
+ end
50
+
51
+ it "should return copyed sheet" do
52
+ sheet = @book.add_sheet @sheet
53
+ copyed_sheet = @book.workbook.Worksheets.Item(@book.workbook.Worksheets.Count)
54
+ sheet.name.should eq copyed_sheet.name
55
+ end
56
+ end
57
+
58
+ context "with first argument" do
59
+ context "with second argument is {:as => 'copyed_name'}" do
60
+ it "copyed sheet name should be 'copyed_name'" do
61
+ @book.add_sheet(@sheet, :as => 'copyed_name').name.should eq 'copyed_name'
62
+ end
63
+ end
64
+
65
+ context "with second argument is {:before => @sheet}" do
66
+ it "should add the first sheet" do
67
+ @book.add_sheet(@sheet, :before => @sheet).name.should eq @book[0].name
68
+ end
69
+ end
70
+
71
+ context "with second argument is {:after => @sheet}" do
72
+ it "should add the first sheet" do
73
+ @book.add_sheet(@sheet, :after => @sheet).name.should eq @book[1].name
74
+ end
75
+ end
76
+
77
+ context "with second argument is {:before => @book[2], :after => @sheet}" do
78
+ it "should arguments in the first is given priority" do
79
+ @book.add_sheet(@sheet, :before => @book[2], :after => @sheet).name.should eq @book[2].name
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ context "without first argument" do
86
+ context "second argument is {:as => 'new sheet'}" do
87
+ it "should return new sheet" do
88
+ @book.add_sheet(:as => 'new sheet').name.should eq 'new sheet'
89
+ end
90
+ end
91
+
92
+ context "second argument is {:before => @sheet}" do
93
+ it "should add the first sheet" do
94
+ @book.add_sheet(:before => @sheet).name.should eq @book[0].name
95
+ end
96
+ end
97
+
98
+ context "second argument is {:after => @sheet}" do
99
+ it "should add the second sheet" do
100
+ @book.add_sheet(:after => @sheet).name.should eq @book[1].name
101
+ end
102
+ end
103
+ end
104
+
105
+ context "without argument" do
106
+ it "should add empty sheet" do
107
+ expect { @book.add_sheet }.to change{ @book.workbook.Worksheets.Count }.from(3).to(4)
108
+ end
109
+
110
+ it "should return copyed sheet" do
111
+ sheet = @book.add_sheet
112
+ copyed_sheet = @book.workbook.Worksheets.Item(@book.workbook.Worksheets.Count)
113
+ sheet.name.should eq copyed_sheet.name
114
+ end
115
+ end
116
+
117
+ context "should raise error if the sheet name already exists" do
118
+ it "should raise error with giving a name that already exists" do
119
+ @book.add_sheet(@sheet, :as => 'new_sheet')
120
+ expect{
121
+ @book.add_sheet(@sheet, :as => 'new_sheet')
122
+ }.to raise_error(ExcelErrorSheet, "sheet name already exists")
123
+ end
124
+ end
125
+ end
126
+
127
+ describe 'access sheet' do
128
+ before do
129
+ @book = Book.open(@simple_file)
130
+ end
131
+
132
+ after do
133
+ @book.close
134
+ end
135
+
136
+ it 'with sheet name' do
137
+ @book['Sheet1'].should be_kind_of Sheet
138
+ end
139
+
140
+ it 'with integer' do
141
+ @book[0].should be_kind_of Sheet
142
+ end
143
+
144
+ it 'with block' do
145
+ @book.each do |sheet|
146
+ sheet.should be_kind_of Sheet
147
+ end
148
+ end
149
+
150
+ context 'open with block' do
151
+ it {
152
+ Book.open(@simple_file) do |book|
153
+ book['Sheet1'].should be_a Sheet
154
+ end
155
+ }
156
+ end
157
+ end
158
+
159
+ end
160
+
data/spec/book_spec.rb CHANGED
@@ -22,8 +22,10 @@ describe Book do
22
22
  @simple_save_file = @dir + '/workbook_save.xls'
23
23
  @different_file = @dir + '/different_workbook.xls'
24
24
  @simple_file_other_path = @dir + '/more_data/workbook.xls'
25
- @more_simple_file = @dir + '/more_workbook.xls'
26
- @connected_file = @dir + '/workbook.xlsx'
25
+ @another_simple_file = @dir + '/another_workbook.xls'
26
+ @linked_file = @dir + '/workbook_linked.xlsm'
27
+ @simple_file_xlsm = @dir + '/workbook.xls'
28
+ @simple_file_xlsx = @dir + '/workbook.xlsx'
27
29
  end
28
30
 
29
31
  after do
@@ -42,71 +44,22 @@ describe Book do
42
44
  end
43
45
  end
44
46
  end
45
-
46
- describe "open" do
47
-
48
- context "with connected workbook" do
49
- it "should open connected workbook" do
50
- book = Book.open(@connected_file, :visible => true)
51
- book.close
52
- end
53
- end
54
-
55
- context "standard use cases" do
56
-
57
- it "should read in a seperate excel instance" do
58
- first_excel = Excel.new
59
- book = Book.open(@simple_file, :read_only => true, :force_excel => :new)
60
- book.should be_a Book
61
- book.should be_alive
62
- book.ReadOnly.should be_true
63
- book.Saved.should be_true
64
- book.excel.should_not == first_excel
65
- sheet = book[0]
66
- sheet[0,0].value.should == "simple"
67
- book.close
68
- end
69
47
 
70
- it "should read not bothering about excel instances" do
71
- first_excel = Excel.new
72
- book = Book.open(@simple_file, :read_only => true)
73
- book.should be_a Book
74
- book.should be_alive
75
- book.ReadOnly.should be_true
76
- book.Saved.should be_true
77
- book.excel.should == first_excel
78
- sheet = book[0]
79
- sheet[0,0].value.should == "simple"
80
- book.close
81
- end
48
+ describe "open" do
82
49
 
83
- it "should open writable" do
84
- book = Book.open(@simple_file, :if_locked => :take_writable,
85
- :if_unsaved => :forget, :if_obstructed => :save)
86
- book.close
87
- end
50
+ context "with various file formats" do
88
51
 
89
- it "should open unobtrusively" do
90
- book = Book.open(@simple_file, :if_locked => :take_writable,
91
- :if_unsaved => :accept, :if_obstructed => :new_excel)
52
+ it "should open linked workbook" do
53
+ book = Book.open(@linked_file, :visible => true)
92
54
  book.close
93
55
  end
94
56
 
95
- it "should open in a given instance" do
96
- book1 = Book.open(@simple_file)
97
- book2 = Book.open(@simple_file, :force_excel => book1.excel, :if_locked => :force_writable)
98
- book2.close
99
- book1.close
100
- end
101
-
102
- it "should open writable" do
103
- book = Book.open(@simple_file, :if_locked => :take_writable,
104
- :if_unsaved => :save, :if_obstructed => :save)
57
+ it "should open xlsm file" do
58
+ book = Book.open(@simple_file_xlsm, :visible => true)
105
59
  book.close
106
60
  end
107
61
  end
108
62
 
109
-
110
63
  context "with standard options" do
111
64
  before do
112
65
  @book = Book.open(@simple_file)
@@ -131,43 +84,16 @@ describe Book do
131
84
  @book.close
132
85
  end
133
86
 
134
- it "should yield identical Book objects for identical Excel books" do
135
- book2 = Book.open(@simple_file)
136
- book2.should === @book
137
- book2.close
138
- end
139
-
140
- it "should yield different Book objects for different Excel books" do
141
- book2 = Book.open(@different_file)
142
- book2.should_not === @book
143
- book2.close
144
- end
145
-
146
- it "should yield different Book objects when opened the same file in different Excel instances" do
147
- book2 = Book.open(@simple_file, :force_excel => :new)
148
- book2.should_not === @book
149
- book2.close
150
- end
151
-
152
87
  it "should yield identical Book objects for identical Excel books when reopening" do
153
88
  @book.should be_alive
154
89
  @book.close
90
+ @book.should_not be_alive
155
91
  book2 = Book.open(@simple_file)
156
92
  book2.should === @book
157
93
  book2.should be_alive
158
94
  book2.close
159
95
  end
160
96
 
161
- it "should yield identical Book objects when reopening and the Excel is closed" do
162
- @book.should be_alive
163
- @book.close
164
- Excel.close_all
165
- book2 = Book.open(@simple_file)
166
- book2.should be_alive
167
- book2.should === @book
168
- book2.close
169
- end
170
-
171
97
  it "should yield different Book objects when reopening in a new Excel" do
172
98
  @book.should be_alive
173
99
  old_excel = @book.excel
@@ -179,33 +105,6 @@ describe Book do
179
105
  book2.excel.should_not == old_excel
180
106
  book2.close
181
107
  end
182
-
183
- it "should yield different Book objects when reopening in a new given Excel instance" do
184
- old_excel = @book.excel
185
- new_excel = Excel.new(:reuse => false)
186
- @book.close
187
- @book.should_not be_alive
188
- book2 = Book.open(@simple_file, :force_excel => new_excel)
189
- book2.should_not === @book
190
- book2.should be_alive
191
- book2.excel.should == new_excel
192
- book2.excel.should_not == old_excel
193
- book2.close
194
- end
195
-
196
- it "should yield identical Book objects when reopening in the old excel" do
197
- old_excel = @book.excel
198
- new_excel = Excel.new(:reuse => false)
199
- @book.close
200
- @book.should_not be_alive
201
- book2 = Book.open(@simple_file, :force_excel => old_excel)
202
- book2.should === @book
203
- book2.should be_alive
204
- book2.excel.should == old_excel
205
- @book.should be_alive
206
- book2.close
207
- end
208
-
209
108
  end
210
109
 
211
110
  context "with :force_excel" do
@@ -229,35 +128,9 @@ describe Book do
229
128
  book2.close
230
129
  end
231
130
 
232
-
233
- it "should open in a given Excel, not provide identity transparency, because old book readonly, new book writable" do
234
- book2 = Book.open(@simple_file, :force_excel => :new)
235
- book2.excel.should_not == @book.excel
236
- book3 = Book.open(@simple_file, :force_excel => :new)
237
- book3.excel.should_not == book2.excel
238
- book3.excel.should_not == @book.excel
239
- book2.close
240
- book4 = Book.open(@simple_file, :force_excel => book2.excel)
241
- book4.should be_alive
242
- book4.should be_a Book
243
- book4.excel.should == book2.excel
244
- book4.Readonly.should == true
245
- book4.should_not == book2
246
- book4.close
247
- book5 = Book.open(@simple_file, :force_excel => book2)
248
- book5.should be_alive
249
- book5.should be_a Book
250
- book5.excel.should == book2.excel
251
- book5.Readonly.should == true
252
- book5.should_not == book2
253
- book5.close
254
- book3.close
255
- end
256
-
257
131
  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
258
132
  book2 = Book.open(@simple_file, :force_excel => :new)
259
133
  book2.excel.should_not == @book.excel
260
- p "book2.excel: #{book2.excel}"
261
134
  book3 = Book.open(@simple_file, :force_excel => :new)
262
135
  book3.excel.should_not == book2.excel
263
136
  book3.excel.should_not == @book.excel
@@ -280,36 +153,6 @@ describe Book do
280
153
  book5.close
281
154
  book3.close
282
155
  end
283
-
284
- 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
285
- book2 = Book.open(@simple_file, :force_excel => :new)
286
- book2.excel.should_not == @book.excel
287
- book2.close
288
- @book.close
289
- book4 = Book.open(@simple_file, :force_excel => book2, :read_only => true)
290
- book4.should be_alive
291
- book4.should be_a Book
292
- book4.excel.should == book2.excel
293
- book4.ReadOnly.should be_true
294
- book4.should == book2
295
- book4.close
296
- end
297
-
298
- it "should do force_excel even if both force_ and default_excel is given" do
299
- book2 = Book.open(@simple_file, :default_excel => @book.excel, :force_excel => :new)
300
- book2.should be_alive
301
- book2.should be_a Book
302
- book2.excel.should_not == @book.excel
303
- book2.should_not == @book
304
- end
305
- end
306
-
307
- context "with another :force_excel" do
308
- it "should do force_excel even if both force_ and default_excel is given" do
309
- book2 = Book.open(@simple_file, :force_excel => nil)
310
- book2.should be_alive
311
- book2.should be_a Book
312
- end
313
156
  end
314
157
 
315
158
  context "with :default_excel" do
@@ -323,15 +166,6 @@ describe Book do
323
166
  @book.close rescue nil
324
167
  end
325
168
 
326
- it "should use the open book" do
327
- book2 = Book.open(@simple_file, :default_excel => :reuse)
328
- book2.excel.should == @book.excel
329
- book2.should be_alive
330
- book2.should be_a Book
331
- book2.should == @book
332
- book2.close
333
- end
334
-
335
169
  it "should reopen the book in the excel instance where it was opened before" do
336
170
  excel = Excel.new(:reuse => false)
337
171
  @book.close
@@ -346,63 +180,6 @@ describe Book do
346
180
  book2.close
347
181
  end
348
182
 
349
- it "should reopen a book in a new Excel if all Excel instances are closed" do
350
- excel = Excel.new(:reuse => false)
351
- excel2 = @book.excel
352
- fn = @book.filename
353
- @book.close
354
- Excel.close_all
355
- book2 = Book.open(@simple_file, :default_excel => :reuse)
356
- book2.should be_alive
357
- book2.should be_a Book
358
- book2.filename.should == fn
359
- @book.should be_alive
360
- book2.should == @book
361
- book2.close
362
- end
363
-
364
- it "should reopen a book in the first opened Excel if the old Excel is closed" do
365
- excel = @book.excel
366
- Excel.close_all
367
- new_excel = Excel.new(:reuse => false)
368
- new_excel2 = Excel.new(:reuse => false)
369
- book2 = Book.open(@simple_file, :default_excel => :reuse)
370
- book2.should be_alive
371
- book2.should be_a Book
372
- book2.excel.should_not == excel
373
- book2.excel.should_not == new_excel2
374
- book2.excel.should == new_excel
375
- @book.should be_alive
376
- book2.should == @book
377
- book2.close
378
- end
379
-
380
- it "should reopen a book in the first opened excel, if the book cannot be reopened" do
381
- @book.close
382
- Excel.close_all
383
- excel1 = Excel.new(:reuse => false)
384
- excel2 = Excel.new(:reuse => false)
385
- book2 = Book.open(@different_file, :default_excel => :reuse)
386
- book2.should be_alive
387
- book2.should be_a Book
388
- book2.excel.should == excel1
389
- book2.excel.should_not == excel2
390
- book2.close
391
- end
392
-
393
- it "should reopen a book in the excel instance where it was opened most recently" do
394
- book2 = Book.open(@simple_file, :force_excel => :new)
395
- @book.close
396
- book2.close
397
- book3 = Book.open(@simple_file)
398
- book2.should be_alive
399
- book2.should be_a Book
400
- book3.excel.should == book2.excel
401
- book3.excel.should_not == @book.excel
402
- book3.should == book2
403
- book3.should_not == @book
404
- end
405
-
406
183
  it "should open a new excel, if the book cannot be reopened" do
407
184
  @book.close
408
185
  new_excel = Excel.new(:reuse => false)
@@ -413,34 +190,6 @@ describe Book do
413
190
  book2.excel.should_not == @book.excel
414
191
  book2.close
415
192
  end
416
-
417
- it "should open a given excel, if the book cannot be reopened" do
418
- @book.close
419
- new_excel = Excel.new(:reuse => false)
420
- book2 = Book.open(@different_file, :default_excel => @book.excel)
421
- book2.should be_alive
422
- book2.should be_a Book
423
- book2.excel.should_not == new_excel
424
- book2.excel.should == @book.excel
425
- book2.close
426
- end
427
-
428
- it "should open a given excel, if the book cannot be reopened" do
429
- @book.close
430
- new_excel = Excel.new(:reuse => false)
431
- book2 = Book.open(@different_file, :default_excel => @book)
432
- book2.should be_alive
433
- book2.should be_a Book
434
- book2.excel.should_not == new_excel
435
- book2.excel.should == @book.excel
436
- book2.close
437
- end
438
-
439
- it "should reuse an open book by default" do
440
- book2 = Book.open(@simple_file)
441
- book2.excel.should == @book.excel
442
- book2.should == @book
443
- end
444
193
  end
445
194
 
446
195
  context "with :if_unsaved" do
@@ -471,13 +220,6 @@ describe Book do
471
220
  @new_book.should == @book
472
221
  end
473
222
 
474
- it "should open book and close old book, if :if_unsaved is :forget" do
475
- @new_book = Book.open(@simple_file, :if_unsaved => :forget)
476
- @book.should_not be_alive
477
- @new_book.should be_alive
478
- @new_book.filename.downcase.should == @simple_file.downcase
479
- end
480
-
481
223
  context "with :if_unsaved => :alert" do
482
224
  before do
483
225
  @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
@@ -509,27 +251,6 @@ describe Book do
509
251
  @book.should be_alive
510
252
  end
511
253
  end
512
-
513
- it "should open the book in a new excel instance, if :if_unsaved is :new_excel" do
514
- @new_book = Book.open(@simple_file, :if_unsaved => :new_excel)
515
- @book.should be_alive
516
- @new_book.should be_alive
517
- @new_book.filename.should == @book.filename
518
- @new_book.excel.should_not == @book.excel
519
- @new_book.close
520
- end
521
-
522
- it "should raise an error, if :if_unsaved is default" do
523
- expect {
524
- @new_book = Book.open(@simple_file, :if_unsaved => :raise)
525
- }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
526
- end
527
-
528
- it "should raise an error, if :if_unsaved is invalid option" do
529
- expect {
530
- @new_book = Book.open(@simple_file, :if_unsaved => :invalid_option)
531
- }.to raise_error(ExcelErrorOpen, ":if_unsaved: invalid option: invalid_option")
532
- end
533
254
  end
534
255
 
535
256
  context "with :if_obstructed" do
@@ -554,19 +275,6 @@ describe Book do
554
275
  @new_book.close rescue nil
555
276
  end
556
277
 
557
- it "should raise an error, if :if_obstructed is :raise" do
558
- expect {
559
- @new_book = Book.open(@simple_file, :if_obstructed => :raise)
560
- }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path: workbook.xls")
561
- end
562
-
563
- it "should close the other book and open the new book, if :if_obstructed is :forget" do
564
- @new_book = Book.open(@simple_file, :if_obstructed => :forget)
565
- @book.should_not be_alive
566
- @new_book.should be_alive
567
- @new_book.filename.downcase.should == @simple_file.downcase
568
- end
569
-
570
278
  it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
571
279
  @new_book = Book.open(@simple_file, :if_obstructed => :save)
572
280
  @book.should_not be_alive
@@ -591,1040 +299,135 @@ describe Book do
591
299
  old_book.workbook.Worksheets.Count.should == @sheet_count + 1
592
300
  old_book.close
593
301
  end
594
-
595
- it "should open the book in a new excel instance, if :if_obstructed is :new_excel" do
596
- @new_book = Book.open(@simple_file, :if_obstructed => :new_excel)
597
- @book.should be_alive
598
- @new_book.should be_alive
599
- @new_book.filename.should_not == @book.filename
600
- @new_book.excel.should_not == @book.excel
601
- end
602
-
603
- it "should raise an error, if :if_obstructed is default" do
604
- expect {
605
- @new_book = Book.open(@simple_file)
606
- }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path: workbook.xls")
607
- end
608
-
609
- it "should raise an error, if :if_obstructed is invalid option" do
610
- expect {
611
- @new_book = Book.open(@simple_file, :if_obstructed => :invalid_option)
612
- }.to raise_error(ExcelErrorOpen, ":if_obstructed: invalid option: invalid_option")
613
- end
614
- end
615
- end
616
- end
617
-
618
- context "with an already saved book" do
619
- before do
620
- @book = Book.open(@simple_file)
621
- end
622
-
623
- after do
624
- @book.close
625
- end
626
-
627
- possible_options = [:read_only, :raise, :accept, :forget, nil]
628
- possible_options.each do |options_value|
629
- context "with :if_unsaved => #{options_value} and in the same and different path" do
630
- before do
631
- @new_book = Book.open(@simple_file, :reuse=> true, :if_unsaved => options_value)
632
- @different_book = Book.new(@different_file, :reuse=> true, :if_unsaved => options_value)
633
- end
634
- after do
635
- @new_book.close
636
- @different_book.close
637
- end
638
- it "should open without problems " do
639
- @new_book.should be_a Book
640
- @different_book.should be_a Book
641
- end
642
- it "should belong to the same Excel instance" do
643
- @new_book.excel.should == @book.excel
644
- @different_book.excel.should == @book.excel
645
- end
646
- end
647
- end
648
- end
649
-
650
- context "with non-existing file" do
651
-
652
- it "should raise an exception" do
653
- File.delete @simple_save_file rescue nil
654
- expect {
655
- Book.open(@simple_save_file, :if_absent => :raise)
656
- }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
657
- end
658
-
659
- it "should create a workbook" do
660
- File.delete @simple_save_file rescue nil
661
- book = Book.open(@simple_save_file, :if_absent => :create)
662
- book.should be_a Book
663
- book.close
664
- File.exist?(@simple_save_file).should be_true
665
- end
666
-
667
- it "should create a workbook by default" do
668
- File.delete @simple_save_file rescue nil
669
- book = Book.open(@simple_save_file)
670
- book.should be_a Book
671
- book.close
672
- File.exist?(@simple_save_file).should be_true
673
- end
674
-
675
- end
676
-
677
- context "with attr_reader excel" do
678
-
679
- before do
680
- @new_book = Book.open(@simple_file)
681
- end
682
- after do
683
- @new_book.close
684
- end
685
- it "should provide the excel instance of the book" do
686
- excel = @new_book.excel
687
- excel.class.should == Excel
688
- excel.should be_a Excel
689
- end
690
- end
691
-
692
- context "with :read_only" do
693
-
694
- it "should reopen the book with writable (unsaved changes from readonly will not be saved)" do
695
- book = Book.open(@simple_file, :read_only => true)
696
- book.ReadOnly.should be_true
697
- book.should be_alive
698
- sheet = book[0]
699
- old_cell_value = sheet[0,0].value
700
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
701
- book.Saved.should be_false
702
- new_book = Book.open(@simple_file, :read_only => false, :if_unsaved => :accept)
703
- new_book.ReadOnly.should be_false
704
- new_book.should be_alive
705
- book.should be_alive
706
- new_book.should == book
707
- new_sheet = new_book[0]
708
- new_cell_value = new_sheet[0,0].value
709
- new_cell_value.should == old_cell_value
710
- end
711
-
712
- it "should not raise an error when trying to reopen the book as read_only while the writable book had unsaved changes" do
713
- book = Book.open(@simple_file, :read_only => false)
714
- book.ReadOnly.should be_false
715
- book.should be_alive
716
- sheet = book[0]
717
- old_cell_value = sheet[0,0].value
718
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
719
- book.Saved.should be_false
720
- new_book = Book.open(@simple_file, :read_only => true, :if_unsaved => :accept)
721
- new_book.ReadOnly.should be_false
722
- new_book.Saved.should be_false
723
- new_book.should == book
724
- end
725
-
726
- it "should reopen the book with writable in the same Excel instance (unsaved changes from readonly will not be saved)" do
727
- book = Book.open(@simple_file, :read_only => true)
728
- book.ReadOnly.should be_true
729
- book.should be_alive
730
- sheet = book[0]
731
- old_cell_value = sheet[0,0].value
732
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
733
- book.Saved.should be_false
734
- new_book = Book.open(@simple_file, :if_unsaved => :accept, :force_excel => book.excel, :read_only => false)
735
- new_book.ReadOnly.should be_false
736
- new_book.should be_alive
737
- book.should be_alive
738
- new_book.should == book
739
- new_sheet = new_book[0]
740
- new_cell_value = new_sheet[0,0].value
741
- new_cell_value.should == old_cell_value
742
- end
743
-
744
- it "should reopen the book with readonly (unsaved changes of the writable should be saved)" do
745
- book = Book.open(@simple_file, :force_excel => :new, :read_only => false)
746
- book.ReadOnly.should be_false
747
- book.should be_alive
748
- sheet = book[0]
749
- old_cell_value = sheet[0,0].value
750
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
751
- book.Saved.should be_false
752
- new_book = Book.open(@simple_file, :force_excel => book.excel, :read_only => true, :if_unsaved => :accept)
753
- new_book.ReadOnly.should be_false
754
- new_book.Saved.should be_false
755
- new_book.should == book
756
- end
757
-
758
- it "should open the second book in another Excel as writable" do
759
- book = Book.open(@simple_file, :read_only => true)
760
- book.ReadOnly.should be_true
761
- new_book = Book.open(@simple_file, :force_excel => :new, :read_only => false)
762
- new_book.ReadOnly.should be_false
763
- new_book.close
764
- book.close
765
- end
766
-
767
- it "should be able to save, if :read_only => false" do
768
- book = Book.open(@simple_file, :read_only => false)
769
- book.should be_a Book
770
- expect {
771
- book.save_as(@simple_save_file, :if_exists => :overwrite)
772
- }.to_not raise_error
773
- book.close
774
- end
775
-
776
- it "should be able to save, if :read_only is default" do
777
- book = Book.open(@simple_file)
778
- book.should be_a Book
779
- expect {
780
- book.save_as(@simple_save_file, :if_exists => :overwrite)
781
- }.to_not raise_error
782
- book.close
783
- end
784
-
785
- it "should raise an error, if :read_only => true" do
786
- book = Book.open(@simple_file, :read_only => true)
787
- book.should be_a Book
788
- expect {
789
- book.save_as(@simple_save_file, :if_exists => :overwrite)
790
- }.to raise_error
791
- book.close
792
- end
793
- end
794
-
795
- context "with block" do
796
- it 'block parameter should be instance of Book' do
797
- Book.open(@simple_file) do |book|
798
- book.should be_a Book
799
- end
800
- end
801
- end
802
-
803
- context "with WIN32OLE#GetAbsolutePathName" do
804
- it "'~' should be HOME directory" do
805
- path = '~/Abrakadabra.xlsx'
806
- expected_path = Regexp.new(File.expand_path(path).gsub(/\//, "."))
807
- expect {
808
- Book.open(path, :if_absent => :raise)
809
- }.to raise_error(ExcelErrorOpen, "file #{path} not found")
810
- end
811
- end
812
- end
813
-
814
- describe "send methods to workbook" do
815
-
816
- context "with standard" do
817
- before do
818
- @book = Book.open(@simple_file)
819
- end
820
-
821
- after do
822
- @book.close
823
- end
824
-
825
- it "should send Saved to workbook" do
826
- @book.Saved.should be_true
827
- end
828
-
829
- it "should send Fullname to workbook" do
830
- @book.Fullname.tr('\\','/').should == @simple_file
831
- end
832
- end
833
- end
834
-
835
- describe "hidden_excel" do
836
-
837
- context "with some open book" do
838
-
839
- before do
840
- @book = Book.open(@simple_file)
841
- end
842
-
843
- after do
844
- @book.close
845
- end
846
-
847
- it "should create and use a hidden Excel instance" do
848
- book2 = Book.open(@simple_file, :force_excel => @book.book_store.hidden_excel)
849
- book2.excel.should_not == @book.excel
850
- book2.excel.Visible.should be_false
851
- book2.excel.DisplayAlerts.should be_false
852
- book2.close
853
- end
854
- end
855
- end
856
-
857
- describe "unobtrusively" do
858
-
859
- def unobtrusively_ok? # :nodoc: #
860
- Book.unobtrusively(@simple_file) do |book|
861
- book.should be_a Book
862
- sheet = book[0]
863
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
864
- book.should be_alive
865
- book.Saved.should be_false
866
- end
867
- end
868
-
869
- context "with no open book" do
870
-
871
- it "should open unobtrusively if no Excel is open" do
872
- Excel.close_all
873
- Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
874
- book.should be_a Book
875
- end
876
- end
877
-
878
- it "should open unobtrusively in a new Excel" do
879
- expect{ unobtrusively_ok? }.to_not raise_error
880
- end
881
-
882
- it "should open unobtrusively in the first opened Excel" do
883
- excel = Excel.new(:reuse => false)
884
- new_excel = Excel.new(:reuse => false)
885
- Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
886
- book.should be_a Book
887
- book.should be_alive
888
- book.excel.should == excel
889
- book.excel.should_not == new_excel
890
- end
891
- end
892
-
893
- it "should open unobtrusively in a new Excel" do
894
- excel = Excel.new(:reuse => false)
895
- new_excel = Excel.new(:reuse => false)
896
- Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
897
- book.should be_a Book
898
- book.should be_alive
899
- book.excel.should_not == excel
900
- book.excel.should_not == new_excel
901
- end
902
- end
903
-
904
- it "should open unobtrusively in a given Excel" do
905
- excel = Excel.new(:reuse => false)
906
- new_excel = Excel.new(:reuse => false)
907
- Book.unobtrusively(@simple_file, :if_closed => new_excel) do |book|
908
- book.should be_a Book
909
- book.should be_alive
910
- book.excel.should_not == excel
911
- book.excel.should == new_excel
912
- end
913
- end
914
-
915
- it "should open unobtrusively in a given Excel via a book" do
916
- book1 = Book.open(@different_file)
917
- book2 = Book.open(@more_simple_file, :force_excel => :new)
918
- Book.unobtrusively(@simple_file, :if_closed => book2) do |book|
919
- book.should be_a Book
920
- book.should be_alive
921
- book.excel.should_not == book1.excel
922
- book.excel.should == book2.excel
923
- end
924
- end
925
-
926
- it "should raise an error if the excel instance is not alive" do
927
- excel = Excel.new(:reuse => false)
928
- new_excel = Excel.new(:reuse => false)
929
- Excel.close_all
930
- expect{
931
- Book.unobtrusively(@simple_file, :if_closed => new_excel) do |book|
932
- end
933
- }.to raise_error(ExcelErrorOpen, "provided Excel instance is not alive")
934
- end
935
-
936
- it "should raise an error if the option is invalid" do
937
- expect{
938
- Book.unobtrusively(@simple_file, :if_closed => :invalid_option) do |book|
939
- end
940
- }.to raise_error(ExcelErrorOpen, "provided instance is neither an Excel nor a Book: invalid_option")
941
- end
942
-
943
- end
944
-
945
- context "with an open book" do
946
-
947
- before do
948
- @book = Book.open(@simple_file)
949
- end
950
-
951
- after do
952
- @book.close(:if_unsaved => :forget)
953
- @book2.close(:if_unsaved => :forget) rescue nil
954
- end
955
-
956
- it "should let a saved book saved" do
957
- @book.Saved.should be_true
958
- @book.should be_alive
959
- sheet = @book[0]
960
- old_cell_value = sheet[0,0].value
961
- unobtrusively_ok?
962
- @book.Saved.should be_true
963
- @book.should be_alive
964
- sheet = @book[0]
965
- sheet[0,0].value.should_not == old_cell_value
966
- end
967
-
968
- it "should let the unsaved book unsaved" do
969
- sheet = @book[0]
970
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
971
- old_cell_value = sheet[0,0].value
972
- @book.Saved.should be_false
973
- unobtrusively_ok?
974
- @book.should be_alive
975
- @book.Saved.should be_false
976
- @book.close(:if_unsaved => :forget)
977
- @book2 = Book.open(@simple_file)
978
- sheet2 = @book2[0]
979
- sheet2[0,0].value.should_not == old_cell_value
980
- end
981
-
982
- it "should modify unobtrusively the second, writable book" do
983
- @book2 = Book.open(@simple_file, :force_excel => :new)
984
- @book.ReadOnly.should be_false
985
- @book2.ReadOnly.should be_true
986
- sheet = @book2[0]
987
- old_cell_value = sheet[0,0].value
988
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
989
- unobtrusively_ok?
990
- @book2.should be_alive
991
- @book2.Saved.should be_false
992
- @book2.close(:if_unsaved => :forget)
993
- @book.close
994
- @book = Book.open(@simple_file)
995
- sheet2 = @book[0]
996
- sheet2[0,0].value.should_not == old_cell_value
997
- end
998
- end
999
-
1000
- context "with a closed book" do
1001
-
1002
- before do
1003
- @book = Book.open(@simple_file)
1004
- end
1005
-
1006
- after do
1007
- @book.close(:if_unsaved => :forget)
1008
- end
1009
-
1010
- it "should let the closed book closed by default" do
1011
- sheet = @book[0]
1012
- old_cell_value = sheet[0,0].value
1013
- @book.close
1014
- @book.should_not be_alive
1015
- unobtrusively_ok?
1016
- @book.should_not be_alive
1017
- @book = Book.open(@simple_file)
1018
- sheet = @book[0]
1019
- sheet[0,0].value.should_not == old_cell_value
1020
- end
1021
-
1022
- # The bold reanimation of the @book
1023
- it "should use the excel of the book and keep open the book" do
1024
- excel = Excel.new(:reuse => false)
1025
- sheet = @book[0]
1026
- old_cell_value = sheet[0,0].value
1027
- @book.close
1028
- @book.should_not be_alive
1029
- Book.unobtrusively(@simple_file, :if_closed => :reuse, :keep_open => true) do |book|
1030
- book.should be_a Book
1031
- book.excel.should == @book.excel
1032
- book.excel.should_not == excel
1033
- sheet = book[0]
1034
- cell = sheet[0,0]
1035
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1036
- book.Saved.should be_false
1037
- end
1038
- @book.should be_alive
1039
- @book.close
1040
- new_book = Book.open(@simple_file)
1041
- sheet = new_book[0]
1042
- sheet[0,0].value.should_not == old_cell_value
1043
- end
1044
-
1045
- # book shall be reanimated even with if_closed => hidden
1046
- it "should use the excel of the book and keep open the book" do
1047
- excel = Excel.new(:reuse => false)
1048
- sheet = @book[0]
1049
- old_cell_value = sheet[0,0].value
1050
- @book.close
1051
- @book.should_not be_alive
1052
- Book.unobtrusively(@simple_file) do |book|
1053
- book.should be_a Book
1054
- book.should be_alive
1055
- book.excel.should_not == @book.excel
1056
- book.excel.should_not == excel
1057
- sheet = book[0]
1058
- cell = sheet[0,0]
1059
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1060
- book.Saved.should be_false
1061
- end
1062
- @book.should_not be_alive
1063
- new_book = Book.open(@simple_file)
1064
- sheet = new_book[0]
1065
- sheet[0,0].value.should_not == old_cell_value
1066
- end
1067
-
1068
- it "should use another excel if the Excels are closed" do
1069
- excel = Excel.new(:reuse => false)
1070
- sheet = @book[0]
1071
- old_cell_value = sheet[0,0].value
1072
- @book.close
1073
- @book.should_not be_alive
1074
- Excel.close_all
1075
- Book.unobtrusively(@simple_file, :keep_open => true, :if_closed => :reuse) do |book|
1076
- book.should be_a Book
1077
- book.excel.should == @book.excel
1078
- book.excel.should_not == excel
1079
- sheet = book[0]
1080
- cell = sheet[0,0]
1081
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1082
- book.Saved.should be_false
1083
- end
1084
- @book.should be_alive
1085
- @book.close
1086
- new_book = Book.open(@simple_file)
1087
- sheet = new_book[0]
1088
- sheet[0,0].value.should_not == old_cell_value
1089
- end
1090
-
1091
- it "should use another excel if the Excels are closed" do
1092
- excel = Excel.new(:reuse => false)
1093
- sheet = @book[0]
1094
- old_cell_value = sheet[0,0].value
1095
- @book.close
1096
- @book.should_not be_alive
1097
- Excel.close_all
1098
- Book.unobtrusively(@simple_file, :keep_open => true) do |book|
1099
- book.should be_a Book
1100
- book.excel.should_not == @book.excel
1101
- book.excel.should_not == excel
1102
- sheet = book[0]
1103
- cell = sheet[0,0]
1104
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1105
- book.Saved.should be_false
1106
- end
1107
- @book.should_not be_alive
1108
- new_book = Book.open(@simple_file)
1109
- sheet = new_book[0]
1110
- sheet[0,0].value.should_not == old_cell_value
1111
- end
1112
-
1113
- it "should use a given Excel" do
1114
- @book.close
1115
- excel = Excel.new(:reuse => false)
1116
- new_excel = Excel.new(:reuse => false)
1117
- Book.unobtrusively(@simple_file, :if_closed => new_excel) do |book|
1118
- book.should be_a Book
1119
- book.excel.should_not == @book.excel
1120
- book.excel.should == new_excel
1121
- end
1122
- end
1123
- end
1124
-
1125
- context "with a read_only book" do
1126
-
1127
- before do
1128
- @book = Book.open(@simple_file, :read_only => true)
1129
- end
1130
-
1131
- after do
1132
- @book.close
1133
- end
1134
-
1135
- it "should let the saved book saved" do
1136
- @book.ReadOnly.should be_true
1137
- @book.Saved.should be_true
1138
- sheet = @book[0]
1139
- old_cell_value = sheet[0,0].value
1140
- unobtrusively_ok?
1141
- @book.should be_alive
1142
- @book.Saved.should be_true
1143
- @book.ReadOnly.should be_true
1144
- @book.close
1145
- book2 = Book.open(@simple_file)
1146
- sheet2 = book2[0]
1147
- sheet2[0,0].value.should_not == old_cell_value
1148
- end
1149
-
1150
- it "should let the unsaved book unsaved" do
1151
- @book.ReadOnly.should be_true
1152
- sheet = @book[0]
1153
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1154
- @book.Saved.should be_false
1155
- @book.should be_alive
1156
- cell_value = sheet[0,0].value
1157
- unobtrusively_ok?
1158
- @book.should be_alive
1159
- @book.Saved.should be_false
1160
- @book.ReadOnly.should be_true
1161
- @book.close
1162
- book2 = Book.open(@simple_file)
1163
- sheet2 = book2[0]
1164
- # modifies unobtrusively the saved version, not the unsaved version
1165
- sheet2[0,0].value.should == cell_value
1166
- end
1167
-
1168
- it "should open unobtrusively by default the writable book" do
1169
- book2 = Book.open(@simple_file, :force_excel => :new, :read_only => false)
1170
- @book.ReadOnly.should be_true
1171
- book2.Readonly.should be_false
1172
- sheet = @book[0]
1173
- cell_value = sheet[0,0].value
1174
- Book.unobtrusively(@simple_file) do |book|
1175
- book.should be_a Book
1176
- book.excel.should == book2.excel
1177
- book.excel.should_not == @book.excel
1178
- sheet = book[0]
1179
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1180
- book.should be_alive
1181
- book.Saved.should be_false
1182
- end
1183
- @book.Saved.should be_true
1184
- @book.ReadOnly.should be_true
1185
- @book.close
1186
- book2.close
1187
- book3 = Book.open(@simple_file)
1188
- new_sheet = book3[0]
1189
- new_sheet[0,0].value.should_not == cell_value
1190
- book3.close
1191
- end
1192
-
1193
- it "should open unobtrusively by default the book in a new Excel such that the book is writable" do
1194
- book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1195
- @book.ReadOnly.should be_true
1196
- book2.Readonly.should be_true
1197
- sheet = @book[0]
1198
- cell_value = sheet[0,0].value
1199
- Book.unobtrusively(@simple_file) do |book|
1200
- book.should be_a Book
1201
- book.excel.should_not == book2.excel
1202
- book.excel.should_not == @book.excel
1203
- sheet = book[0]
1204
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1205
- book.should be_alive
1206
- book.Saved.should be_false
1207
- end
1208
- @book.Saved.should be_true
1209
- @book.ReadOnly.should be_true
1210
- @book.close
1211
- book2.close
1212
- book3 = Book.open(@simple_file)
1213
- new_sheet = book3[0]
1214
- new_sheet[0,0].value.should_not == cell_value
1215
- book3.close
1216
- end
1217
-
1218
- it "should open unobtrusively the book in a new Excel such that the book is writable" do
1219
- book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1220
- @book.ReadOnly.should be_true
1221
- book2.Readonly.should be_true
1222
- sheet = @book[0]
1223
- cell_value = sheet[0,0].value
1224
- Book.unobtrusively(@simple_file, :use_this => false) do |book|
1225
- book.should be_a Book
1226
- book.excel.should_not == book2.excel
1227
- book.excel.should_not == @book.excel
1228
- sheet = book[0]
1229
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1230
- book.should be_alive
1231
- book.Saved.should be_false
1232
- end
1233
- @book.Saved.should be_true
1234
- @book.ReadOnly.should be_true
1235
- @book.close
1236
- book2.close
1237
- book3 = Book.open(@simple_file)
1238
- new_sheet = book3[0]
1239
- new_sheet[0,0].value.should_not == cell_value
1240
- book3.close
1241
- end
1242
-
1243
- it "should open unobtrusively the book in a new Excel to open the book writable" do
1244
- excel1 = Excel.new(:reuse => false)
1245
- excel2 = Excel.new(:reuse => false)
1246
- book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1247
- @book.ReadOnly.should be_true
1248
- book2.Readonly.should be_true
1249
- sheet = @book[0]
1250
- cell_value = sheet[0,0].value
1251
- Book.unobtrusively(@simple_file, :use_readonly_excel => false) do |book|
1252
- book.should be_a Book
1253
- book.ReadOnly.should be_false
1254
- book.excel.should_not == book2.excel
1255
- book.excel.should_not == @book.excel
1256
- book.excel.should_not == excel1
1257
- book.excel.should_not == excel2
1258
- sheet = book[0]
1259
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1260
- book.should be_alive
1261
- book.Saved.should be_false
1262
- end
1263
- @book.Saved.should be_true
1264
- @book.ReadOnly.should be_true
1265
- @book.close
1266
- book2.close
1267
- book3 = Book.open(@simple_file)
1268
- new_sheet = book3[0]
1269
- new_sheet[0,0].value.should_not == cell_value
1270
- book3.close
1271
- end
1272
-
1273
- it "should open unobtrusively the book in the same Excel to open the book writable" do
1274
- excel1 = Excel.new(:reuse => false)
1275
- excel2 = Excel.new(:reuse => false)
1276
- book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1277
- @book.ReadOnly.should be_true
1278
- book2.Readonly.should be_true
1279
- sheet = @book[0]
1280
- cell_value = sheet[0,0].value
1281
- Book.unobtrusively(@simple_file, :use_readonly_excel => true) do |book|
1282
- book.should be_a Book
1283
- book.excel.should == book2.excel
1284
- book.ReadOnly.should be_false
1285
- sheet = book[0]
1286
- sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
1287
- book.should be_alive
1288
- book.Saved.should be_false
1289
- end
1290
- book2.Saved.should be_true
1291
- book2.ReadOnly.should be_false
1292
- @book.close
1293
- book2.close
1294
- book3 = Book.open(@simple_file)
1295
- new_sheet = book3[0]
1296
- new_sheet[0,0].value.should_not == cell_value
1297
- book3.close
1298
- end
1299
-
1300
- it "should open unobtrusively the book in the Excel where it was opened most recently" do
1301
- book2 = Book.open(@simple_file, :force_excel => :new, :read_only => true)
1302
- @book.ReadOnly.should be_true
1303
- book2.Readonly.should be_true
1304
- sheet = @book[0]
1305
- cell_value = sheet[0,0].value
1306
- Book.unobtrusively(@simple_file, :read_only => true) do |book|
1307
- book.should be_a Book
1308
- book.excel.should == book2.excel
1309
- book.excel.should_not == @book.excel
1310
- book.should be_alive
1311
- book.Saved.should be_true
1312
- end
1313
- @book.Saved.should be_true
1314
- @book.ReadOnly.should be_true
1315
- @book.close
1316
- book2.close
302
+ end
1317
303
  end
1318
-
1319
304
  end
1320
305
 
1321
- context "with a virgin Book class" do
1322
- before do
1323
- class Book
1324
- @@bookstore = nil
1325
- end
306
+ context "with non-existing file" do
307
+
308
+ it "should create a workbook" do
309
+ File.delete @simple_save_file rescue nil
310
+ book = Book.open(@simple_save_file, :if_absent => :create)
311
+ book.should be_a Book
312
+ book.close
313
+ File.exist?(@simple_save_file).should be_true
1326
314
  end
1327
- it "should work" do
1328
- expect{ unobtrusively_ok? }.to_not raise_error
315
+
316
+ it "should raise an exception by default" do
317
+ File.delete @simple_save_file rescue nil
318
+ expect {
319
+ Book.open(@simple_save_file)
320
+ }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
1329
321
  end
1330
322
  end
1331
323
 
1332
- context "with a book never opened before" do
1333
- before do
1334
- class Book
1335
- @@bookstore = nil
1336
- end
1337
- other_book = Book.open(@different_file)
324
+ context "with :read_only" do
325
+
326
+ it "should reopen the book with writable (unsaved changes from readonly will not be saved)" do
327
+ book = Book.open(@simple_file, :read_only => true)
328
+ book.ReadOnly.should be_true
329
+ book.should be_alive
330
+ sheet = book[0]
331
+ old_cell_value = sheet[1,1].value
332
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
333
+ book.Saved.should be_false
334
+ new_book = Book.open(@simple_file, :read_only => false, :if_unsaved => :accept)
335
+ new_book.ReadOnly.should be_false
336
+ new_book.should be_alive
337
+ book.should be_alive
338
+ new_book.should == book
339
+ new_sheet = new_book[0]
340
+ new_cell_value = new_sheet[1,1].value
341
+ new_cell_value.should == old_cell_value
1338
342
  end
1339
- it "should open the book" do
1340
- expect{ unobtrusively_ok? }.to_not raise_error
343
+
344
+ context "with block" do
345
+ it 'block parameter should be instance of Book' do
346
+ Book.open(@simple_file) do |book|
347
+ book.should be_a Book
348
+ end
1341
349
  end
1342
350
  end
351
+ end
1343
352
 
1344
- context "with a saved book" do
353
+ describe "reopen" do
1345
354
 
355
+ context "with standard" do
356
+
1346
357
  before do
1347
- @book1 = Book.open(@simple_file)
358
+ @book = Book.open(@simple_file)
1348
359
  end
1349
360
 
1350
361
  after do
1351
- @book1.close(:if_unsaved => :forget)
362
+ @book.close
1352
363
  end
1353
364
 
1354
- it "should save if the book was modified during unobtrusively" do
1355
- m_time = File.mtime(@book1.stored_filename)
1356
- Book.unobtrusively(@simple_file) do |book|
1357
- @book1.Saved.should be_true
1358
- book.Saved.should be_true
1359
- sheet = book[0]
1360
- cell = sheet[0,0]
1361
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1362
- @book1.Saved.should be_false
1363
- book.Saved.should be_false
1364
- sleep 1
1365
- end
1366
- @book1.Saved.should be_true
1367
- m_time2 = File.mtime(@book1.stored_filename)
1368
- m_time2.should_not == m_time
1369
- end
1370
-
1371
- it "should not save the book if it was not modified during unobtrusively" do
1372
- m_time = File.mtime(@book1.stored_filename)
1373
- Book.unobtrusively(@simple_file) do |book|
1374
- @book1.Saved.should be_true
1375
- book.Saved.should be_true
1376
- sleep 1
1377
- end
1378
- m_time2 = File.mtime(@book1.stored_filename)
1379
- m_time2.should == m_time
1380
- end
365
+ it "should reopen the closed book" do
366
+ @book.should be_alive
367
+ book1 = @book
368
+ @book.close
369
+ @book.should_not be_alive
370
+ @book.reopen
371
+ @book.should be_a Book
372
+ @book.should be_alive
373
+ @book.should === book1
374
+ end
1381
375
  end
376
+ end
377
+
378
+ describe "send methods to workbook" do
1382
379
 
1383
- context "with block result" do
380
+ context "with standard" do
1384
381
  before do
1385
- @book1 = Book.open(@simple_file)
382
+ @book = Book.open(@simple_file)
1386
383
  end
1387
384
 
1388
385
  after do
1389
- @book1.close(:if_unsaved => :forget)
1390
- end
1391
-
1392
- it "should yield the block result true" do
1393
- result =
1394
- Book.unobtrusively(@simple_file) do |book|
1395
- @book1.Saved.should be_true
1396
- end
1397
- result.should == true
1398
- end
1399
-
1400
- it "should yield the block result nil" do
1401
- result =
1402
- Book.unobtrusively(@simple_file) do |book|
1403
- end
1404
- result.should == nil
1405
- end
1406
-
1407
- it "should yield the block result with an unmodified book" do
1408
- sheet1 = @book1[0]
1409
- cell1 = sheet1[0,0].value
1410
- result =
1411
- Book.unobtrusively(@simple_file) do |book|
1412
- sheet = book[0]
1413
- cell = sheet[0,0].value
1414
- end
1415
- result.should == cell1
1416
- end
1417
-
1418
- it "should yield the block result even if the book gets saved" do
1419
- sheet1 = @book1[0]
1420
- @book1.save
1421
- result =
1422
- Book.unobtrusively(@simple_file) do |book|
1423
- sheet = book[0]
1424
- sheet[0,0].value = 22
1425
- @book1.Saved.should be_false
1426
- 42
1427
- end
1428
- result.should == 42
1429
- @book1.Saved.should be_true
386
+ @book.close
1430
387
  end
1431
- end
1432
388
 
1433
- context "with visible" do
1434
-
1435
- after do
1436
- @book1.close
1437
- end
1438
-
1439
- it "should let the book invisible" do
1440
- @book1 = Book.open(@simple_file)
1441
- @book1.excel.Visible.should be_false
1442
- Book.unobtrusively(@simple_file) do |book|
1443
- book.excel.Visible.should be_false
1444
- end
1445
- @book1.excel.Visible.should be_false
1446
- Book.unobtrusively(@simple_file, :visible => false) do |book|
1447
- book.excel.Visible.should be_false
1448
- end
1449
- @book1.excel.Visible.should be_false
1450
- end
1451
-
1452
- it "should let the book visible" do
1453
- @book1 = Book.open(@simple_file, :visible => true)
1454
- @book1.excel.Visible.should be_true
1455
- Book.unobtrusively(@simple_file) do |book|
1456
- book.excel.Visible.should be_true
1457
- end
1458
- @book1.excel.Visible.should be_true
1459
- Book.unobtrusively(@simple_file, :visible => true) do |book|
1460
- book.excel.Visible.should be_true
1461
- end
1462
- @book1.excel.Visible.should be_true
389
+ it "should send Saved to workbook" do
390
+ @book.Saved.should be_true
1463
391
  end
1464
392
 
1465
- end
1466
-
1467
- context "with several Excel instances" do
1468
-
1469
- before do
1470
- @book1 = Book.open(@simple_file)
1471
- @book2 = Book.open(@simple_file, :force_excel => :new)
1472
- @book1.Readonly.should == false
1473
- @book2.Readonly.should == true
1474
- old_sheet = @book1[0]
1475
- @old_cell_value = old_sheet[0,0].value
1476
- @book1.close
1477
- @book2.close
1478
- @book1.should_not be_alive
1479
- @book2.should_not be_alive
1480
- end
1481
-
1482
- it "should open unobtrusively the closed book in the most recent Excel where it was open before" do
1483
- Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
1484
- book.excel.should == @book2.excel
1485
- book.excel.should_not == @book1.excel
1486
- book.ReadOnly.should == false
1487
- sheet = book[0]
1488
- cell = sheet[0,0]
1489
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1490
- book.Saved.should be_false
1491
- end
1492
- new_book = Book.open(@simple_file)
1493
- sheet = new_book[0]
1494
- sheet[0,0].value.should_not == @old_cell_value
1495
- end
1496
-
1497
- it "should open unobtrusively the closed book in the new hidden Excel" do
1498
- Book.unobtrusively(@simple_file) do |book|
1499
- book.excel.should_not == @book2.excel
1500
- book.excel.should_not == @book1.excel
1501
- book.ReadOnly.should == false
1502
- sheet = book[0]
1503
- cell = sheet[0,0]
1504
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1505
- book.Saved.should be_false
1506
- end
1507
- new_book = Book.open(@simple_file)
1508
- sheet = new_book[0]
1509
- sheet[0,0].value.should_not == @old_cell_value
1510
- end
1511
-
1512
- it "should open unobtrusively the closed book in a new Excel if the Excel is not alive anymore" do
1513
- Excel.close_all
1514
- Book.unobtrusively(@simple_file) do |book|
1515
- book.ReadOnly.should == false
1516
- book.excel.should_not == @book1.excel
1517
- book.excel.should_not == @book2.excel
1518
- sheet = book[0]
1519
- cell = sheet[0,0]
1520
- sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1521
- book.Saved.should be_false
1522
- end
1523
- new_book = Book.open(@simple_file)
1524
- sheet = new_book[0]
1525
- sheet[0,0].value.should_not == @old_cell_value
393
+ it "should send Fullname to workbook" do
394
+ @book.Fullname.tr('\\','/').should == @simple_file
1526
395
  end
1527
396
  end
397
+ end
1528
398
 
1529
- context "with :hidden" do
1530
-
1531
- before do
1532
- @book1 = Book.open(@simple_file)
1533
- @book1.close
1534
- end
399
+ describe "hidden_excel" do
1535
400
 
1536
- it "should create a new hidden Excel instance" do
1537
- Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1538
- book.should be_a Book
1539
- book.should be_alive
1540
- book.excel.Visible.should be_false
1541
- book.excel.DisplayAlerts.should be_false
1542
- end
1543
- end
1544
-
1545
- it "should create a new hidden Excel instance and use this afterwards" do
1546
- hidden_excel = nil
1547
- Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1548
- book.should be_a Book
1549
- book.should be_alive
1550
- book.excel.Visible.should be_false
1551
- book.excel.DisplayAlerts.should be_false
1552
- hidden_excel = book.excel
1553
- end
1554
- Book.unobtrusively(@different_file, :if_closed => :hidden) do |book|
1555
- book.should be_a Book
1556
- book.should be_alive
1557
- book.excel.Visible.should be_false
1558
- book.excel.DisplayAlerts.should be_false
1559
- book.excel.should == hidden_excel
1560
- end
1561
- end
1562
-
1563
- it "should create a new hidden Excel instance if the Excel is closed" do
1564
- Excel.close_all
1565
- Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1566
- book.should be_a Book
1567
- book.should be_alive
1568
- book.excel.Visible.should be_false
1569
- book.excel.DisplayAlerts.should be_false
1570
- book.excel.should_not == @book1.excel
1571
- end
1572
- end
401
+ context "with some open book" do
1573
402
 
1574
- it "should exclude hidden Excel when reuse in unobtrusively" do
1575
- hidden_excel = nil
1576
- Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1577
- book.should be_a Book
1578
- book.should be_alive
1579
- book.excel.Visible.should be_false
1580
- book.excel.DisplayAlerts.should be_false
1581
- book.excel.should_not == @book1.excel
1582
- hidden_excel = book.excel
1583
- end
1584
- Book.unobtrusively(@simple_file, :if_closed => :reuse) do |book|
1585
- book.should be_a Book
1586
- book.should be_alive
1587
- book.excel.Visible.should be_false
1588
- book.excel.DisplayAlerts.should be_false
1589
- book.excel.should_not == hidden_excel
1590
- end
403
+ before do
404
+ @book = Book.open(@simple_file)
1591
405
  end
1592
406
 
1593
- it "should exclude hidden Excel when reuse in open" do
1594
- hidden_excel = nil
1595
- Book.unobtrusively(@simple_file, :if_closed => :hidden) do |book|
1596
- book.should be_a Book
1597
- book.should be_alive
1598
- book.excel.Visible.should be_false
1599
- book.excel.DisplayAlerts.should be_false
1600
- book.excel.should_not == @book1.excel
1601
- hidden_excel = book.excel
1602
- end
1603
- book2 = Book.open(@simple_file, :default_excel => :reuse)
1604
- book2.excel.should_not == hidden_excel
407
+ after do
408
+ @book.close
1605
409
  end
1606
410
 
1607
- it "should exclude hidden Excel when reuse in open" do
1608
- book1 = Book.open(@simple_file)
1609
- book1.close
1610
- book2 = Book.open(@simple_file, :default_excel => :reuse)
1611
- book2.excel.should == book1.excel
1612
- book1.should be_alive
1613
- book2.close
411
+ it "should create and use a hidden Excel instance" do
412
+ book2 = Book.open(@simple_file, :force_excel => @book.bookstore.hidden_excel)
413
+ book2.excel.should_not == @book.excel
414
+ book2.excel.visible.should be_false
415
+ book2.excel.displayalerts.should be_false
416
+ book2.close
1614
417
  end
1615
418
  end
1616
419
  end
1617
420
 
1618
421
  describe "nvalue, set_nvalue, rename_range" do
1619
422
 
1620
- context "nvalue" do
423
+ context "nvalue, book[<name>]" do
1621
424
 
1622
425
  before do
1623
- @book1 = Book.open(@more_simple_file)
426
+ @book1 = Book.open(@another_simple_file)
1624
427
  end
1625
428
 
1626
429
  after do
1627
- @book1.close
430
+ @book1.close(:if_unsaved => :forget)
1628
431
  end
1629
432
 
1630
433
  it "should return value of a range" do
@@ -1633,25 +436,19 @@ describe Book do
1633
436
  @book1.nvalue("firstrow").should == [[1,2]]
1634
437
  @book1.nvalue("four").should == [[1,2],[3,4]]
1635
438
  @book1.nvalue("firstrow").should_not == "12"
1636
- end
1637
-
1638
- it "should raise an error if name not defined" do
1639
- expect {
1640
- value = @book1.nvalue("foo")
1641
- }.to raise_error(ExcelErrorNValue, "name foo not in more_workbook.xls")
1642
- end
1643
-
1644
- it "should raise an error if name was defined but contents is calcuated" do
1645
- expect {
1646
- value = @book1.nvalue("named_formula")
1647
- }.to raise_error(ExcelErrorNValue, "range error in more_workbook.xls")
439
+ @book1.nvalue("firstcell").should == "foo"
440
+ @book1["new"].should == "foo"
441
+ @book1["one"].should == 1
442
+ @book1["firstrow"].should == [[1,2]]
443
+ @book1["four"].should == [[1,2],[3,4]]
444
+ @book1["firstcell"].should == "foo"
1648
445
  end
1649
446
  end
1650
447
 
1651
- context "set_nvalue" do
448
+ context "set_nvalue, book[<name>]=" do
1652
449
 
1653
450
  before do
1654
- @book1 = Book.open(@more_simple_file)
451
+ @book1 = Book.open(@another_simple_file)
1655
452
  end
1656
453
 
1657
454
  after do
@@ -1663,43 +460,17 @@ describe Book do
1663
460
  @book1.set_nvalue("new","bar")
1664
461
  @book1.nvalue("new").should == "bar"
1665
462
  end
1666
- end
1667
-
1668
- context "rename_range" do
1669
-
1670
- before do
1671
- @book1 = Book.open(@more_simple_file)
1672
- end
1673
-
1674
- after do
1675
- @book1.close(:if_unsaved => :forget)
1676
- end
1677
463
 
1678
- it "should rename a range" do
1679
- @book1.rename_range("four","five")
1680
- @book1.nvalue("five").should == [[1,2],[3,4]]
1681
- expect {
1682
- @book1.rename_range("four","five")
1683
- }.to raise_error(ExcelErrorRename, "name four not in more_workbook.xls")
464
+ it "should set value of a range" do
465
+ @book1.nvalue("new").should == "foo"
466
+ @book1["new"] = "bar"
467
+ @book1.nvalue("new").should == "bar"
1684
468
  end
1685
469
  end
1686
470
  end
1687
471
 
1688
472
  describe "close" do
1689
473
 
1690
- context "with saved book" do
1691
- before do
1692
- @book = Book.open(@simple_file)
1693
- end
1694
-
1695
- it "should close book" do
1696
- expect{
1697
- @book.close
1698
- }.to_not raise_error
1699
- @book.should_not be_alive
1700
- end
1701
- end
1702
-
1703
474
  context "with unsaved read_only book" do
1704
475
  before do
1705
476
  @book = Book.open(@simple_file, :read_only => true)
@@ -1715,7 +486,6 @@ describe Book do
1715
486
  new_book.workbook.Worksheets.Count.should == @sheet_count
1716
487
  new_book.close
1717
488
  end
1718
-
1719
489
  end
1720
490
 
1721
491
  context "with unsaved book" do
@@ -1730,43 +500,12 @@ describe Book do
1730
500
  @book.close(:if_unsaved => :forget) rescue nil
1731
501
  end
1732
502
 
1733
- it "should raise error with option :raise" do
1734
- expect{
1735
- @book.close(:if_unsaved => :raise)
1736
- }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
1737
- end
1738
-
1739
503
  it "should raise error by default" do
1740
504
  expect{
1741
505
  @book.close(:if_unsaved => :raise)
1742
506
  }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
1743
507
  end
1744
508
 
1745
- it "should close the book and leave its file untouched with option :forget" do
1746
- ole_workbook = @book.workbook
1747
- excel = @book.excel
1748
- expect {
1749
- @book.close(:if_unsaved => :forget)
1750
- }.to change {excel.Workbooks.Count }.by(-1)
1751
- @book.workbook.should == nil
1752
- @book.should_not be_alive
1753
- expect{
1754
- ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
1755
- new_book = Book.open(@simple_file)
1756
- begin
1757
- new_book.workbook.Worksheets.Count.should == @sheet_count
1758
- ensure
1759
- new_book.close
1760
- end
1761
- end
1762
-
1763
- it "should raise an error for invalid option" do
1764
- expect {
1765
- @book.close(:if_unsaved => :invalid_option)
1766
- }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: invalid_option")
1767
- end
1768
-
1769
-
1770
509
  it "should save the book before close with option :save" do
1771
510
  ole_workbook = @book.workbook
1772
511
  excel = @book.excel
@@ -1784,49 +523,6 @@ describe Book do
1784
523
  new_book.close
1785
524
  end
1786
525
  end
1787
-
1788
- context "with :if_unsaved => :alert" do
1789
- before do
1790
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
1791
- end
1792
-
1793
- after do
1794
- @key_sender.close
1795
- end
1796
-
1797
- possible_answers = [:yes, :no, :cancel]
1798
- possible_answers.each_with_index do |answer, position|
1799
- it "should" + (answer == :yes ? "" : " not") + " the unsaved book and" + (answer == :cancel ? " not" : "") + " close it" + "if user answers '#{answer}'" do
1800
- # "Yes" is the default. "No" is right of "Yes", "Cancel" is right of "No" --> language independent
1801
- @key_sender.puts "{right}" * position + "{enter}"
1802
- ole_workbook = @book.workbook
1803
- excel = @book.excel
1804
- displayalert_value = @book.excel.DisplayAlerts
1805
- if answer == :cancel then
1806
- expect {
1807
- @book.close(:if_unsaved => :alert)
1808
- }.to raise_error(ExcelUserCanceled, "close: canceled by user")
1809
- @book.workbook.Saved.should be_false
1810
- @book.workbook.should_not == nil
1811
- @book.should be_alive
1812
- else
1813
- expect {
1814
- @book.close(:if_unsaved => :alert)
1815
- }.to change {@book.excel.Workbooks.Count }.by(-1)
1816
- @book.workbook.should == nil
1817
- @book.should_not be_alive
1818
- expect{ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
1819
- end
1820
- new_book = Book.open(@simple_file, :if_unsaved => :forget)
1821
- begin
1822
- new_book.workbook.Worksheets.Count.should == @sheet_count + (answer==:yes ? 1 : 0)
1823
- new_book.excel.DisplayAlerts.should == displayalert_value
1824
- ensure
1825
- new_book.close
1826
- end
1827
- end
1828
- end
1829
- end
1830
526
  end
1831
527
  end
1832
528
 
@@ -1844,15 +540,6 @@ describe Book do
1844
540
  @book.workbook.Worksheets.Count.should == @new_sheet_count
1845
541
  @book.close
1846
542
  end
1847
-
1848
- it "should raise error with read_only" do
1849
- @book = Book.open(@simple_file, :read_only => true)
1850
- expect {
1851
- @book.save
1852
- }.to raise_error(ExcelErrorSave, "Not opened for writing (opened with :read_only option)")
1853
- @book.close
1854
- end
1855
-
1856
543
  end
1857
544
 
1858
545
  context "with open with read only" do
@@ -1929,32 +616,6 @@ describe Book do
1929
616
  book_save.close
1930
617
  end
1931
618
 
1932
- it "should save to simple_save_file.xls with :if_exists => :overwrite" do
1933
- File.delete @simple_save_file rescue nil
1934
- File.open(@simple_save_file,"w") do | file |
1935
- file.puts "garbage"
1936
- end
1937
- @book.save_as(@simple_save_file, :if_exists => :overwrite)
1938
- File.exist?(@simple_save_file).should be_true
1939
- new_book = Book.open(@simple_save_file)
1940
- new_book.should be_a Book
1941
- new_book.close
1942
- end
1943
- it "should save to 'simple_save_file.xls' with :if_exists => :raise" do
1944
- dirname, basename = File.split(@simple_save_file)
1945
- File.delete @simple_save_file rescue nil
1946
- File.open(@simple_save_file,"w") do | file |
1947
- file.puts "garbage"
1948
- end
1949
- File.exist?(@simple_save_file).should be_true
1950
- booklength = File.size?(@simple_save_file)
1951
- expect {
1952
- @book.save_as(@simple_save_file, :if_exists => :raise)
1953
- }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
1954
- File.exist?(@simple_save_file).should be_true
1955
- File.size?(@simple_save_file).should == booklength
1956
- end
1957
-
1958
619
  context "with :if_exists => :alert" do
1959
620
  before do
1960
621
  File.delete @simple_save_file rescue nil
@@ -2011,47 +672,12 @@ describe Book do
2011
672
  File.size?(@simple_save_file).should == @garbage_length
2012
673
  @book.excel.DisplayAlerts.should == displayalert_value
2013
674
  end
2014
-
2015
- it "should report save errors and leave DisplayAlerts unchanged" do
2016
- #@key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
2017
- @book.workbook.Close
2018
- expect{
2019
- @book.save_as(@simple_save_file, :if_exists => :alert)
2020
- }.to raise_error(ExcelErrorSaveUnknown)
2021
- File.exist?(@simple_save_file).should be_true
2022
- File.size?(@simple_save_file).should == @garbage_length
2023
- @book.excel.DisplayAlerts.should == displayalert_value
2024
- end
2025
-
2026
- end
2027
-
2028
- it "should save to 'simple_save_file.xls' with :if_exists => nil" do
2029
- dirname, basename = File.split(@simple_save_file)
2030
- File.delete @simple_save_file rescue nil
2031
- File.open(@simple_save_file,"w") do | file |
2032
- file.puts "garbage"
2033
- end
2034
- File.exist?(@simple_save_file).should be_true
2035
- booklength = File.size?(@simple_save_file)
2036
- expect {
2037
- @book.save_as(@simple_save_file)
2038
- }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
2039
- File.exist?(@simple_save_file).should be_true
2040
- File.size?(@simple_save_file).should == booklength
2041
- end
2042
-
2043
- it "should save to 'simple_save_file.xls' with :if_exists => :invalid_option" do
2044
- File.delete @simple_save_file rescue nil
2045
- @book.save_as(@simple_save_file)
2046
- expect {
2047
- @book.save_as(@simple_save_file, :if_exists => :invalid_option)
2048
- }.to raise_error(ExcelErrorSave, ':if_exists: invalid option: invalid_option')
2049
675
  end
2050
676
  end
2051
677
  end
2052
678
  end
2053
679
 
2054
- describe "== , alive?, filename, visible, empty_workbook" do
680
+ describe "alive?, filename, ==, visible, displayalerts, activate, saved" do
2055
681
 
2056
682
  context "with alive?" do
2057
683
 
@@ -2088,11 +714,6 @@ describe Book do
2088
714
  @book.filename.should == @simple_file
2089
715
  end
2090
716
 
2091
- it "should return nil for dead book" do
2092
- @book.close
2093
- @book.filename.should == nil
2094
- end
2095
-
2096
717
  end
2097
718
 
2098
719
  context "with ==" do
@@ -2120,45 +741,42 @@ describe Book do
2120
741
  @new_book = Book.new(@simple_file_other_path, :excel => :new)
2121
742
  @new_book.should_not == @book
2122
743
  end
2123
-
2124
- it "should be false with same book names but different excel instances" do
2125
- @new_book = Book.new(@simple_file, :excel => :new)
2126
- @new_book.should_not == @book
2127
- end
2128
-
2129
- it "should be false with non-Books" do
2130
- @book.should_not == "hallo"
2131
- @book.should_not == 7
2132
- @book.should_not == nil
2133
- end
2134
744
  end
2135
745
 
2136
- context "with visible and displayalerts" do
746
+ context "with activate" do
2137
747
 
2138
748
  before do
2139
- @book = Book.open(@simple_file)
749
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
750
+ @book = Book.open(@simple_file, :visible => true)
751
+ @book2 = Book.open(@another_simple_file, :force_excel => :new, :visible => true)
2140
752
  end
2141
753
 
2142
754
  after do
2143
- @book.close
2144
- end
2145
-
2146
- it "should make Excel visible" do
2147
- @book.excel.visible = false
2148
- @book.excel.visible.should be_false
2149
- @book.excel.visible = true
2150
- @book.excel.visible.should be_true
2151
- end
2152
-
2153
- it "should enable DisplayAlerts in Excel" do
2154
- @book.excel.displayalerts = false
2155
- @book.excel.displayalerts.should be_false
2156
- @book.excel.displayalerts = true
2157
- @book.excel.displayalerts.should be_true
755
+ @book.close(:if_unsaved => :forget)
756
+ @book2.close(:if_unsaved => :forget)
757
+ @key_sender.close
758
+ end
759
+
760
+ it "should activate a book" do
761
+ sheet = @book[1]
762
+ sheet.Activate
763
+ sheet[2,3].Activate
764
+ sheet2 = @book2[2]
765
+ sheet2.Activate
766
+ sheet2[3,2].Activate
767
+ Excel.current.should == @book.excel
768
+ @book2.activate
769
+ @key_sender.puts "{a}{enter}"
770
+ sleep 1
771
+ sheet2[3,2].Value.should == "a"
772
+ #Excel.current.should == @book2.excel
773
+ @book.activate
774
+ @key_sender.puts "{a}{enter}"
775
+ sleep 1
776
+ sheet[2,3].Value.should == "a"
777
+ Excel.current.should == @book.excel
2158
778
  end
2159
-
2160
779
  end
2161
-
2162
780
  end
2163
781
 
2164
782
  describe "#add_sheet" do
@@ -2207,7 +825,6 @@ describe Book do
2207
825
  @book.add_sheet(@sheet, :before => @book[2], :after => @sheet).name.should eq @book[2].name
2208
826
  end
2209
827
  end
2210
-
2211
828
  end
2212
829
 
2213
830
  context "without first argument" do
@@ -2241,15 +858,6 @@ describe Book do
2241
858
  sheet.name.should eq copyed_sheet.name
2242
859
  end
2243
860
  end
2244
-
2245
- context "should raise error if the sheet name already exists" do
2246
- it "should raise error with giving a name that already exists" do
2247
- @book.add_sheet(@sheet, :as => 'new_sheet')
2248
- expect{
2249
- @book.add_sheet(@sheet, :as => 'new_sheet')
2250
- }.to raise_error(ExcelErrorSheet, "sheet name already exists")
2251
- end
2252
- end
2253
861
  end
2254
862
 
2255
863
  describe 'access sheet' do
@@ -2261,17 +869,20 @@ describe Book do
2261
869
  @book.close
2262
870
  end
2263
871
 
2264
- it 'with sheet name' do
2265
- @book['Sheet1'].should be_kind_of Sheet
2266
- end
872
+ context "standard" do
2267
873
 
2268
- it 'with integer' do
2269
- @book[0].should be_kind_of Sheet
2270
- end
874
+ it 'with sheet name' do
875
+ @book['Sheet1'].should be_kind_of Sheet
876
+ end
2271
877
 
2272
- it 'with block' do
2273
- @book.each do |sheet|
2274
- sheet.should be_kind_of Sheet
878
+ it 'with integer' do
879
+ @book[0].should be_kind_of Sheet
880
+ end
881
+
882
+ it 'with block' do
883
+ @book.each do |sheet|
884
+ sheet.should be_kind_of Sheet
885
+ end
2275
886
  end
2276
887
  end
2277
888
 
@@ -2284,3 +895,4 @@ describe Book do
2284
895
  end
2285
896
  end
2286
897
  end
898
+ end