robust_excel_ole 0.4 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Changelog +15 -0
- data/README.rdoc +128 -63
- data/README_detail.rdoc +130 -60
- data/examples/edit_sheets/example_access_sheets_and_cells.rb +1 -1
- data/examples/edit_sheets/example_adding_sheets.rb +2 -2
- data/examples/edit_sheets/example_copying.rb +1 -1
- data/examples/edit_sheets/example_expanding.rb +1 -1
- data/examples/edit_sheets/example_ranges.rb +1 -1
- data/examples/edit_sheets/example_saving.rb +2 -2
- data/examples/open_save_close/example_control_to_excel.rb +1 -1
- data/examples/open_save_close/example_if_obstructed_closeifsaved.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 +1 -1
- data/examples/open_save_close/example_if_unsaved_forget.rb +2 -2
- data/examples/open_save_close/example_if_unsaved_forget_more.rb +3 -3
- data/examples/open_save_close/example_read_only.rb +1 -1
- data/examples/open_save_close/example_rename_cells.rb +1 -1
- data/examples/open_save_close/example_simple.rb +1 -1
- data/examples/open_save_close/example_unobtrusively.rb +3 -3
- data/lib/robust_excel_ole.rb +1 -0
- data/lib/robust_excel_ole/book.rb +249 -193
- data/lib/robust_excel_ole/bookstore.rb +1 -1
- data/lib/robust_excel_ole/cell.rb +1 -1
- data/lib/robust_excel_ole/excel.rb +125 -4
- data/lib/robust_excel_ole/general.rb +1 -92
- data/lib/robust_excel_ole/range.rb +1 -1
- data/lib/robust_excel_ole/reo_common.rb +37 -0
- data/lib/robust_excel_ole/sheet.rb +77 -24
- data/lib/robust_excel_ole/version.rb +1 -1
- data/spec/book_spec.rb +112 -82
- data/spec/book_specs/book_close_spec.rb +44 -1
- data/spec/book_specs/book_misc_spec.rb +97 -92
- data/spec/book_specs/book_open_spec.rb +40 -8
- data/spec/book_specs/book_save_spec.rb +77 -7
- data/spec/book_specs/book_sheet_spec.rb +290 -66
- data/spec/book_specs/book_unobtr_spec.rb +99 -73
- data/spec/bookstore_spec.rb +1 -1
- data/spec/cell_spec.rb +2 -2
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +174 -23
- data/spec/general_spec.rb +3 -18
- data/spec/range_spec.rb +3 -3
- data/spec/reo_common_spec.rb +104 -0
- data/spec/sheet_spec.rb +101 -60
- metadata +6 -4
@@ -71,7 +71,7 @@ describe Book do
|
|
71
71
|
@book = Book.open(@simple_file)
|
72
72
|
@sheet_count = @book.ole_workbook.Worksheets.Count
|
73
73
|
@book.add_sheet(@sheet, :as => 'a_name')
|
74
|
-
@sheet = @book
|
74
|
+
@sheet = @book.sheet(1)
|
75
75
|
end
|
76
76
|
|
77
77
|
after do
|
@@ -184,6 +184,49 @@ describe Book do
|
|
184
184
|
end
|
185
185
|
end
|
186
186
|
end
|
187
|
+
|
188
|
+
context "with :if_unsaved => :excel" do
|
189
|
+
before do
|
190
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
|
191
|
+
end
|
192
|
+
|
193
|
+
after do
|
194
|
+
@key_sender.close
|
195
|
+
end
|
196
|
+
|
197
|
+
possible_answers = [:yes, :no, :cancel]
|
198
|
+
possible_answers.each_with_index do |answer, position|
|
199
|
+
it "should" + (answer == :yes ? "" : " not") + " the unsaved book and" + (answer == :cancel ? " not" : "") + " close it" + "if user answers '#{answer}'" do
|
200
|
+
# "Yes" is the default. "No" is right of "Yes", "Cancel" is right of "No" --> language independent
|
201
|
+
@key_sender.puts "{right}" * position + "{enter}"
|
202
|
+
ole_workbook = @book.ole_workbook
|
203
|
+
excel = @book.excel
|
204
|
+
displayalert_value = @book.excel.DisplayAlerts
|
205
|
+
if answer == :cancel then
|
206
|
+
expect {
|
207
|
+
@book.close(:if_unsaved => :excel)
|
208
|
+
}.to raise_error(ExcelUserCanceled, "close: canceled by user")
|
209
|
+
@book.ole_workbook.Saved.should be_false
|
210
|
+
@book.ole_workbook.should_not == nil
|
211
|
+
@book.should be_alive
|
212
|
+
else
|
213
|
+
@book.excel.Workbooks.Count.should == 1
|
214
|
+
@book.close(:if_unsaved => :excel)
|
215
|
+
@book.excel.Workbooks.Count.should == 0
|
216
|
+
@book.ole_workbook.should == nil
|
217
|
+
@book.should_not be_alive
|
218
|
+
expect{ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
|
219
|
+
end
|
220
|
+
new_book = Book.open(@simple_file, :if_unsaved => :forget)
|
221
|
+
begin
|
222
|
+
new_book.ole_workbook.Worksheets.Count.should == @sheet_count + (answer==:yes ? 1 : 0)
|
223
|
+
new_book.excel.DisplayAlerts.should == displayalert_value
|
224
|
+
ensure
|
225
|
+
new_book.close
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
187
230
|
end
|
188
231
|
end
|
189
232
|
end
|
@@ -31,7 +31,7 @@ describe Book do
|
|
31
31
|
|
32
32
|
after do
|
33
33
|
Excel.kill_all
|
34
|
-
rm_tmp(@dir)
|
34
|
+
#rm_tmp(@dir)
|
35
35
|
end
|
36
36
|
|
37
37
|
describe "create file" do
|
@@ -101,113 +101,99 @@ describe Book do
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
|
-
describe "
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
@book1 = Book.open(@another_simple_file)
|
110
|
-
end
|
104
|
+
describe "nameval, set_nameval, [], []=" do
|
105
|
+
|
106
|
+
before do
|
107
|
+
@book1 = Book.open(@another_simple_file)
|
108
|
+
end
|
111
109
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
@book1["one"].should == 1
|
125
|
-
@book1["firstrow"].should == [[1,2]]
|
126
|
-
@book1["four"].should == [[1,2],[3,4]]
|
127
|
-
@book1["firstcell"].should == "foo"
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should raise an error if name not defined" do
|
131
|
-
expect {
|
132
|
-
@book1.nvalue("foo")
|
133
|
-
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
134
|
-
expect {
|
135
|
-
@book1["foo"]
|
136
|
-
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
137
|
-
end
|
110
|
+
after do
|
111
|
+
@book1.close(:if_unsaved => :forget)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should return value of a range" do
|
115
|
+
@book1.nameval("new").should == "foo"
|
116
|
+
@book1.nameval("one").should == 1
|
117
|
+
@book1.nameval("firstrow").should == [[1,2]]
|
118
|
+
@book1.nameval("four").should == [[1,2],[3,4]]
|
119
|
+
@book1.nameval("firstrow").should_not == "12"
|
120
|
+
@book1.nameval("firstcell").should == "foo"
|
121
|
+
end
|
138
122
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
123
|
+
it "should return value of a range via []" do
|
124
|
+
@book1["new"].should == "foo"
|
125
|
+
@book1["one"].should == 1
|
126
|
+
@book1["firstrow"] == [[1,2]]
|
127
|
+
@book1["four"].should == [[1,2],[3,4]]
|
128
|
+
@book1["firstrow"].should_not == "12"
|
129
|
+
@book1["firstcell"].should == "foo"
|
130
|
+
end
|
143
131
|
|
144
|
-
|
145
|
-
|
146
|
-
|
132
|
+
it "should set value of a range" do
|
133
|
+
@book1.set_nameval("new", "bar")
|
134
|
+
@book1.nameval("new").should == "bar"
|
147
135
|
end
|
148
136
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
end
|
137
|
+
it "should set value of a range via []=" do
|
138
|
+
@book1["new"] = "bar"
|
139
|
+
@book1.nameval("new").should == "bar"
|
140
|
+
end
|
154
141
|
|
155
|
-
|
156
|
-
|
157
|
-
|
142
|
+
it "should evaluate a formula" do
|
143
|
+
@book1.nameval("named_formula").should == 4
|
144
|
+
end
|
158
145
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
@book1.nvalue("new").should == "bar"
|
163
|
-
end
|
146
|
+
it "should evaluate a formula via []" do
|
147
|
+
@book1["named_formula"].should == 4
|
148
|
+
end
|
164
149
|
|
165
|
-
it "should raise an error if name not defined" do
|
166
|
-
expect {
|
167
|
-
@book1.set_nvalue("foo","bar")
|
168
|
-
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
169
|
-
expect {
|
170
|
-
@book1["foo"] = "bar"
|
171
|
-
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
172
|
-
end
|
173
150
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
}.to raise_error(ExcelError, /RefersToRange error of name "named_formula" in "another_workbook.xls"/)
|
178
|
-
expect {
|
179
|
-
@book1["named_formula"] = "bar"
|
180
|
-
}.to raise_error(ExcelError, /RefersToRange error of name "named_formula" in "another_workbook.xls"/)
|
181
|
-
end
|
151
|
+
it "should return default value if name not defined" do
|
152
|
+
@book1.nameval("foo", :default => 2).should == 2
|
153
|
+
end
|
182
154
|
|
183
|
-
|
184
|
-
|
185
|
-
@book1
|
186
|
-
|
187
|
-
|
155
|
+
it "should raise an error if name not defined" do
|
156
|
+
expect {
|
157
|
+
@book1.nameval("foo")
|
158
|
+
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
159
|
+
expect {
|
160
|
+
@book1.set_nameval("foo","bar")
|
161
|
+
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
162
|
+
expect {
|
163
|
+
@book1["foo"] = "bar"
|
164
|
+
}.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should raise an error if name was defined but contents is calcuated" do
|
168
|
+
expect {
|
169
|
+
@book1.set_nameval("named_formula","bar")
|
170
|
+
}.to raise_error(ExcelError, /cannot assign value to range named "named_formula" in "another_workbook.xls"/)
|
171
|
+
expect {
|
172
|
+
@book1["named_formula"] = "bar"
|
173
|
+
}.to raise_error(ExcelError, /cannot assign value to range named "named_formula" in "another_workbook.xls"/)
|
188
174
|
end
|
175
|
+
end
|
189
176
|
|
190
|
-
|
177
|
+
describe "rename_range" do
|
191
178
|
|
192
|
-
|
193
|
-
|
194
|
-
|
179
|
+
before do
|
180
|
+
@book1 = Book.open(@another_simple_file)
|
181
|
+
end
|
195
182
|
|
196
|
-
|
197
|
-
|
198
|
-
|
183
|
+
after do
|
184
|
+
@book1.close(:if_unsaved => :forget)
|
185
|
+
end
|
199
186
|
|
200
|
-
|
187
|
+
it "should rename a range" do
|
188
|
+
@book1.rename_range("four","five")
|
189
|
+
@book1.nameval("five").should == [[1,2],[3,4]]
|
190
|
+
expect {
|
201
191
|
@book1.rename_range("four","five")
|
202
|
-
|
203
|
-
expect {
|
204
|
-
@book1.rename_range("four","five")
|
205
|
-
}.to raise_error(ExcelError, /name "four" not in "another_workbook.xls"/)
|
206
|
-
end
|
192
|
+
}.to raise_error(ExcelError, /name "four" not in "another_workbook.xls"/)
|
207
193
|
end
|
208
194
|
end
|
209
195
|
|
210
|
-
describe "alive?, filename, ==, visible, displayalerts, activate, saved" do
|
196
|
+
describe "alive?, filename, ==, visible, displayalerts, activate, saved, check_compatibility" do
|
211
197
|
|
212
198
|
context "with alive?" do
|
213
199
|
|
@@ -304,7 +290,7 @@ describe Book do
|
|
304
290
|
end
|
305
291
|
|
306
292
|
it "should yield false for an unsaved book" do
|
307
|
-
sheet = @book
|
293
|
+
sheet = @book.sheet(1)
|
308
294
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
309
295
|
@book.saved.should be_false
|
310
296
|
end
|
@@ -352,10 +338,10 @@ describe Book do
|
|
352
338
|
end
|
353
339
|
|
354
340
|
it "should activate a book" do
|
355
|
-
sheet = @book
|
341
|
+
sheet = @book.sheet(2)
|
356
342
|
sheet.Activate
|
357
343
|
sheet[2,3].Activate
|
358
|
-
sheet2 = @book2
|
344
|
+
sheet2 = @book2.sheet(2)
|
359
345
|
sheet2.Activate
|
360
346
|
sheet2[3,2].Activate
|
361
347
|
Excel.current.should == @book.excel
|
@@ -371,5 +357,24 @@ describe Book do
|
|
371
357
|
Excel.current.should == @book.excel
|
372
358
|
end
|
373
359
|
end
|
360
|
+
|
361
|
+
context "with compatibility" do
|
362
|
+
|
363
|
+
it "should open and check compatibility" do
|
364
|
+
book = Book.open(@simple_file, :visible => true, :check_compatibility => false)
|
365
|
+
book.CheckCompatibility.should be_false
|
366
|
+
book.CheckCompatibility = true
|
367
|
+
book.CheckCompatibility.should be_true
|
368
|
+
Book.unobtrusively(@simple_file, :visible => true, :check_compatibility => false) do |book|
|
369
|
+
book.CheckCompatibility.should be_false
|
370
|
+
end
|
371
|
+
Book.unobtrusively(@simple_file, :visible => true, :check_compatibility => true) do |book|
|
372
|
+
book.CheckCompatibility.should be_true
|
373
|
+
end
|
374
|
+
|
375
|
+
end
|
376
|
+
|
377
|
+
end
|
378
|
+
|
374
379
|
end
|
375
380
|
end
|
@@ -474,7 +474,7 @@ describe Book do
|
|
474
474
|
|
475
475
|
before do
|
476
476
|
@book = Book.open(@simple_file)
|
477
|
-
@sheet = @book
|
477
|
+
@sheet = @book.sheet(1)
|
478
478
|
@book.add_sheet(@sheet, :as => 'a_name')
|
479
479
|
end
|
480
480
|
|
@@ -537,6 +537,38 @@ describe Book do
|
|
537
537
|
end
|
538
538
|
end
|
539
539
|
|
540
|
+
context "with :if_unsaved => :excel" do
|
541
|
+
before do
|
542
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
|
543
|
+
end
|
544
|
+
|
545
|
+
after do
|
546
|
+
@key_sender.close
|
547
|
+
end
|
548
|
+
|
549
|
+
it "should open the new book and close the unsaved book, if user answers 'yes'" do
|
550
|
+
# "Yes" is the default. --> language independent
|
551
|
+
@key_sender.puts "{enter}"
|
552
|
+
@new_book = Book.open(@simple_file, :if_unsaved => :excel)
|
553
|
+
@new_book.should be_alive
|
554
|
+
@new_book.filename.downcase.should == @simple_file.downcase
|
555
|
+
@book.should_not be_alive
|
556
|
+
end
|
557
|
+
|
558
|
+
it "should not open the new book and not close the unsaved book, if user answers 'no'" do
|
559
|
+
# "No" is right to "Yes" (the default). --> language independent
|
560
|
+
# strangely, in the "no" case, the question will sometimes be repeated three times
|
561
|
+
#@book.excel.Visible = true
|
562
|
+
@key_sender.puts "{right}{enter}"
|
563
|
+
@key_sender.puts "{right}{enter}"
|
564
|
+
@key_sender.puts "{right}{enter}"
|
565
|
+
expect{
|
566
|
+
Book.open(@simple_file, :if_unsaved => :excel)
|
567
|
+
}.to raise_error(ExcelErrorOpen, "open: user canceled or open error")
|
568
|
+
@book.should be_alive
|
569
|
+
end
|
570
|
+
end
|
571
|
+
|
540
572
|
it "should open the book in a new excel instance, if :if_unsaved is :new_excel" do
|
541
573
|
@new_book = Book.open(@simple_file, :if_unsaved => :new_excel)
|
542
574
|
@book.should be_alive
|
@@ -572,7 +604,7 @@ describe Book do
|
|
572
604
|
end
|
573
605
|
@book = Book.open(@simple_file_other_path)
|
574
606
|
@sheet_count = @book.ole_workbook.Worksheets.Count
|
575
|
-
@sheet = @book
|
607
|
+
@sheet = @book.sheet(1)
|
576
608
|
@book.add_sheet(@sheet, :as => 'a_name')
|
577
609
|
end
|
578
610
|
|
@@ -721,7 +753,7 @@ describe Book do
|
|
721
753
|
book = Book.open(@simple_file, :read_only => true)
|
722
754
|
book.ReadOnly.should be_true
|
723
755
|
book.should be_alive
|
724
|
-
sheet = book
|
756
|
+
sheet = book.sheet(1)
|
725
757
|
old_cell_value = sheet[1,1].value
|
726
758
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
727
759
|
book.Saved.should be_false
|
@@ -730,7 +762,7 @@ describe Book do
|
|
730
762
|
new_book.should be_alive
|
731
763
|
book.should be_alive
|
732
764
|
new_book.should == book
|
733
|
-
new_sheet = new_book
|
765
|
+
new_sheet = new_book.sheet(1)
|
734
766
|
new_cell_value = new_sheet[1,1].value
|
735
767
|
new_cell_value.should == old_cell_value
|
736
768
|
end
|
@@ -739,7 +771,7 @@ describe Book do
|
|
739
771
|
book = Book.open(@simple_file, :read_only => false)
|
740
772
|
book.ReadOnly.should be_false
|
741
773
|
book.should be_alive
|
742
|
-
sheet = book
|
774
|
+
sheet = book.sheet(1)
|
743
775
|
old_cell_value = sheet[1,1].value
|
744
776
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
745
777
|
book.Saved.should be_false
|
@@ -753,7 +785,7 @@ describe Book do
|
|
753
785
|
book = Book.open(@simple_file, :read_only => true)
|
754
786
|
book.ReadOnly.should be_true
|
755
787
|
book.should be_alive
|
756
|
-
sheet = book
|
788
|
+
sheet = book.sheet(1)
|
757
789
|
old_cell_value = sheet[1,1].value
|
758
790
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
759
791
|
book.Saved.should be_false
|
@@ -762,7 +794,7 @@ describe Book do
|
|
762
794
|
new_book.should be_alive
|
763
795
|
book.should be_alive
|
764
796
|
new_book.should == book
|
765
|
-
new_sheet = new_book
|
797
|
+
new_sheet = new_book.sheet(1)
|
766
798
|
new_cell_value = new_sheet[1,1].value
|
767
799
|
new_cell_value.should == old_cell_value
|
768
800
|
end
|
@@ -771,7 +803,7 @@ describe Book do
|
|
771
803
|
book = Book.open(@simple_file, :force_excel => :new, :read_only => false)
|
772
804
|
book.ReadOnly.should be_false
|
773
805
|
book.should be_alive
|
774
|
-
sheet = book
|
806
|
+
sheet = book.sheet(1)
|
775
807
|
old_cell_value = sheet[1,1].value
|
776
808
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
777
809
|
book.Saved.should be_false
|
@@ -152,7 +152,7 @@ describe Book do
|
|
152
152
|
File.open(@simple_file_other_path,"w") do | file |
|
153
153
|
file.puts "garbage"
|
154
154
|
end
|
155
|
-
sheet = @book
|
155
|
+
sheet = @book.sheet(1)
|
156
156
|
cell_value = sheet[1,1].value
|
157
157
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
158
158
|
@book.Saved.should be_false
|
@@ -164,7 +164,7 @@ describe Book do
|
|
164
164
|
new_book.should be_a Book
|
165
165
|
new_book.close
|
166
166
|
old_book = Book.open(@simple_file)
|
167
|
-
old_sheet = old_book
|
167
|
+
old_sheet = old_book.sheet(1)
|
168
168
|
old_sheet[1,1].value.should == cell_value
|
169
169
|
old_book.close
|
170
170
|
end
|
@@ -174,7 +174,7 @@ describe Book do
|
|
174
174
|
File.open(@simple_file_other_path,"w") do | file |
|
175
175
|
file.puts "garbage"
|
176
176
|
end
|
177
|
-
sheet = @book
|
177
|
+
sheet = @book.sheet(1)
|
178
178
|
cell_value = sheet[1,1].value
|
179
179
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
180
180
|
@book.Saved.should be_false
|
@@ -186,7 +186,7 @@ describe Book do
|
|
186
186
|
new_book.should be_a Book
|
187
187
|
new_book.close
|
188
188
|
old_book = Book.open(@simple_file)
|
189
|
-
old_sheet = old_book
|
189
|
+
old_sheet = old_book.sheet(1)
|
190
190
|
old_sheet[1,1].value.should_not == cell_value
|
191
191
|
old_book.close
|
192
192
|
end
|
@@ -207,7 +207,7 @@ describe Book do
|
|
207
207
|
end
|
208
208
|
|
209
209
|
it "should raise an error if the blocking workbook was unsaved with :if_obstructed => :close_if_saved" do
|
210
|
-
sheet = @book
|
210
|
+
sheet = @book.sheet(1)
|
211
211
|
cell_value = sheet[1,1].value
|
212
212
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
213
213
|
@book.Saved.should be_false
|
@@ -295,12 +295,12 @@ describe Book do
|
|
295
295
|
end
|
296
296
|
|
297
297
|
it "should simple save if file name is equal to the old one with :if_exists => :overwrite" do
|
298
|
-
sheet = @book
|
298
|
+
sheet = @book.sheet(1)
|
299
299
|
sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
|
300
300
|
new_value = sheet[1,1].value
|
301
301
|
@book.save_as(@simple_file, :if_exists => :overwrite)
|
302
302
|
new_book = Book.open(@simple_file)
|
303
|
-
new_sheet = new_book
|
303
|
+
new_sheet = new_book.sheet(1)
|
304
304
|
new_sheet[1,1].value.should == new_value
|
305
305
|
new_book.close
|
306
306
|
end
|
@@ -390,6 +390,76 @@ describe Book do
|
|
390
390
|
|
391
391
|
end
|
392
392
|
|
393
|
+
context "with :if_exists => :excel" do
|
394
|
+
before do
|
395
|
+
File.delete @simple_save_file rescue nil
|
396
|
+
File.open(@simple_save_file,"w") do | file |
|
397
|
+
file.puts "garbage"
|
398
|
+
end
|
399
|
+
@garbage_length = File.size?(@simple_save_file)
|
400
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
|
401
|
+
end
|
402
|
+
|
403
|
+
after do
|
404
|
+
@key_sender.close
|
405
|
+
end
|
406
|
+
|
407
|
+
it "should save if user answers 'yes'" do
|
408
|
+
# "Yes" is to the left of "No", which is the default. --> language independent
|
409
|
+
@key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
|
410
|
+
@book.save_as(@simple_save_file, :if_exists => :excel)
|
411
|
+
File.exist?(@simple_save_file).should be_true
|
412
|
+
File.size?(@simple_save_file).should > @garbage_length
|
413
|
+
@book.excel.DisplayAlerts.should == displayalert_value
|
414
|
+
new_book = Book.open(@simple_save_file, :excel => :new)
|
415
|
+
new_book.should be_a Book
|
416
|
+
new_book.close
|
417
|
+
@book.excel.DisplayAlerts.should == displayalert_value
|
418
|
+
end
|
419
|
+
|
420
|
+
it "should not save if user answers 'no'" do
|
421
|
+
# Just give the "Enter" key, because "No" is the default. --> language independent
|
422
|
+
# strangely, in the "no" case, the question will sometimes be repeated three times
|
423
|
+
@key_sender.puts "{enter}"
|
424
|
+
@key_sender.puts "{enter}"
|
425
|
+
@key_sender.puts "{enter}"
|
426
|
+
#@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
|
427
|
+
expect{
|
428
|
+
@book.save_as(@simple_save_file, :if_exists => :excel)
|
429
|
+
}.to raise_error(ExcelErrorSave, "not saved or canceled by user")
|
430
|
+
File.exist?(@simple_save_file).should be_true
|
431
|
+
File.size?(@simple_save_file).should == @garbage_length
|
432
|
+
@book.excel.DisplayAlerts.should == displayalert_value
|
433
|
+
end
|
434
|
+
|
435
|
+
it "should not save if user answers 'cancel'" do
|
436
|
+
# 'Cancel' is right from 'yes'
|
437
|
+
# strangely, in the "no" case, the question will sometimes be repeated three times
|
438
|
+
@key_sender.puts "{right}{enter}"
|
439
|
+
@key_sender.puts "{right}{enter}"
|
440
|
+
@key_sender.puts "{right}{enter}"
|
441
|
+
#@key_sender.puts "%{n}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
|
442
|
+
expect{
|
443
|
+
@book.save_as(@simple_save_file, :if_exists => :excel)
|
444
|
+
}.to raise_error(ExcelErrorSave, "not saved or canceled by user")
|
445
|
+
File.exist?(@simple_save_file).should be_true
|
446
|
+
File.size?(@simple_save_file).should == @garbage_length
|
447
|
+
@book.excel.DisplayAlerts.should == displayalert_value
|
448
|
+
end
|
449
|
+
|
450
|
+
it "should report save errors and leave DisplayAlerts unchanged" do
|
451
|
+
#@key_sender.puts "{left}{enter}" #, :initial_wait => 0.2, :if_target_missing=>"Excel window not found")
|
452
|
+
@book.ole_workbook.Close
|
453
|
+
expect{
|
454
|
+
@book.save_as(@simple_save_file, :if_exists => :excel)
|
455
|
+
}.to raise_error(ExcelErrorSave, "Workbook is not alive")
|
456
|
+
File.exist?(@simple_save_file).should be_true
|
457
|
+
File.size?(@simple_save_file).should == @garbage_length
|
458
|
+
@book.excel.DisplayAlerts.should == displayalert_value
|
459
|
+
end
|
460
|
+
|
461
|
+
end
|
462
|
+
|
393
463
|
it "should save to 'simple_save_file.xls' with :if_exists => nil" do
|
394
464
|
dirname, basename = File.split(@simple_save_file)
|
395
465
|
File.delete @simple_save_file rescue nil
|