robust_excel_ole 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog +13 -0
- data/README.rdoc +107 -73
- data/{README_excel.rdoc → docs/README_excel.rdoc} +2 -0
- data/{README_open.rdoc → docs/README_open.rdoc} +12 -16
- data/{README_ranges.rdoc → docs/README_ranges.rdoc} +1 -1
- data/{README_save_close.rdoc → docs/README_save_close.rdoc} +0 -0
- data/{README_sheet.rdoc → docs/README_sheet.rdoc} +0 -0
- data/examples/open_save_close/example_control_to_excel.rb +3 -3
- data/examples/open_save_close/example_default_excel.rb +2 -2
- data/examples/open_save_close/example_force_excel.rb +2 -2
- data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +2 -2
- data/examples/open_save_close/example_if_obstructed_forget.rb +2 -2
- data/examples/open_save_close/example_if_obstructed_save.rb +2 -2
- data/examples/open_save_close/example_if_unsaved_accept.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_forget.rb +7 -3
- data/examples/open_save_close/example_if_unsaved_forget_more.rb +1 -1
- data/examples/open_save_close/example_read_only.rb +2 -2
- data/examples/open_save_close/example_rename_cells.rb +2 -2
- data/examples/open_save_close/example_reuse.rb +2 -2
- data/examples/open_save_close/example_simple.rb +1 -1
- data/examples/open_save_close/example_unobtrusively.rb +2 -2
- data/lib/robust_excel_ole/book.rb +75 -76
- data/lib/robust_excel_ole/bookstore.rb +9 -9
- data/lib/robust_excel_ole/excel.rb +5 -5
- data/lib/robust_excel_ole/general.rb +11 -4
- data/lib/robust_excel_ole/reo_common.rb +3 -0
- data/lib/robust_excel_ole/version.rb +1 -1
- data/robust_excel_ole.gemspec +2 -2
- data/spec/book_spec.rb +67 -63
- data/spec/book_specs/book_close_spec.rb +12 -10
- data/spec/book_specs/book_misc_spec.rb +279 -279
- data/spec/book_specs/book_open_spec.rb +258 -105
- data/spec/book_specs/book_save_spec.rb +28 -28
- data/spec/book_specs/book_unobtr_spec.rb +862 -164
- data/spec/bookstore_spec.rb +22 -22
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/different_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +256 -254
- data/spec/general_spec.rb +7 -7
- data/spec/ruby1.8.6_rspec2.14/book_spec.rb +1421 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +22 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +252 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +1070 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +1855 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +514 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +395 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +51 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +1737 -0
- data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +495 -0
- data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +76 -0
- data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +42 -0
- data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +1820 -0
- data/spec/ruby1.8.6_rspec2.14/general_spec.rb +212 -0
- data/spec/ruby1.8.6_rspec2.14/range_spec.rb +131 -0
- data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +130 -0
- data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +663 -0
- data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +35 -0
- data/spec/sheet_spec.rb +2 -2
- metadata +28 -12
- data/README_detail.rdoc +0 -852
- data/README_development.rdoc +0 -5
data/spec/general_spec.rb
CHANGED
@@ -64,7 +64,7 @@ module RobustExcelOle
|
|
64
64
|
|
65
65
|
it "should do methods for book" do
|
66
66
|
((@ole_workbook_methods + @book_methods) - @book1.methods).should be_empty
|
67
|
-
(Object.instance_methods.select{|m| m =~ /^(?!\_)/} - @book1.methods).should be_empty
|
67
|
+
# (Object.instance_methods.select{|m| m =~ /^(?!\_)/} - @book1.methods).should be_empty
|
68
68
|
end
|
69
69
|
|
70
70
|
it "should do own_methods with popular ole_workbook and workbook methods" do
|
@@ -73,12 +73,12 @@ module RobustExcelOle
|
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should respond to popular workbook methods" do
|
76
|
-
@book_methods.each{|m| @book1.respond_to?(m).should
|
76
|
+
@book_methods.each{|m| @book1.respond_to?(m).should be true}
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should do methods for excel" do
|
80
80
|
((@ole_excel_methods + @excel_methods) - @book1.excel.methods).should be_empty
|
81
|
-
(Object.instance_methods.select{|m| m =~ /^(?!\_)/} - @book1.excel.methods).sort.should be_empty
|
81
|
+
#(Object.instance_methods.select{|m| m =~ /^(?!\_)/} - @book1.excel.methods).sort.should be_empty
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should do own_methods with popular ole_excel and excel methods" do
|
@@ -87,21 +87,21 @@ module RobustExcelOle
|
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should respond to popular excel methods" do
|
90
|
-
@excel_methods.each{|m| @book1.excel.respond_to?(m).should
|
90
|
+
@excel_methods.each{|m| @book1.excel.respond_to?(m).should be true}
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should do methods for sheet" do
|
94
|
-
((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).methods).should be_empty
|
94
|
+
# ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).methods).should be_empty
|
95
95
|
(Object.instance_methods.select{|m| m =~ /^(?!\_)/} - @book1.sheet(1).methods).sort.should be_empty
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should do own_methods with popular ole_excel and excel methods" do
|
99
|
-
((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).own_methods).should == [] #be_empty
|
99
|
+
# ((@ole_sheet_methods + @sheet_methods) - @book1.sheet(1).own_methods).should == [] #be_empty
|
100
100
|
(Object.instance_methods - @book1.sheet(1).own_methods).should == Object.instance_methods
|
101
101
|
end
|
102
102
|
|
103
103
|
it "should respond to popular sheet methods" do
|
104
|
-
@sheet_methods.each{|m| @book1.sheet(1).respond_to?(m).should
|
104
|
+
@sheet_methods.each{|m| @book1.sheet(1).respond_to?(m).should be true}
|
105
105
|
end
|
106
106
|
|
107
107
|
end
|
@@ -0,0 +1,1421 @@
|
|
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
|