robust_excel_ole 1.35 → 1.36
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Changelog +20 -2
- data/README.rdoc +31 -95
- data/docs/README_excel.rdoc +5 -6
- data/docs/README_listobjects.rdoc +8 -15
- data/docs/README_open.rdoc +48 -25
- data/docs/README_ranges.rdoc +7 -10
- data/docs/README_save_close.rdoc +4 -5
- data/docs/README_sheet.rdoc +4 -8
- data/examples/introductory_examples/example_introductory.rb +1 -1
- data/examples/introductory_examples/example_open.rb +1 -1
- data/lib/robust_excel_ole/excel.rb +98 -16
- data/lib/robust_excel_ole/general.rb +18 -6
- data/lib/robust_excel_ole/list_object.rb +11 -19
- data/lib/robust_excel_ole/list_row.rb +74 -19
- data/lib/robust_excel_ole/range.rb +6 -1
- data/lib/robust_excel_ole/range_owners.rb +24 -17
- data/lib/robust_excel_ole/version.rb +1 -1
- data/lib/robust_excel_ole/workbook.rb +107 -42
- data/lib/robust_excel_ole/worksheet.rb +1 -1
- data/robust_excel_ole.gemspec +0 -1
- data/spec/bookstore_spec.rb +1 -2
- data/spec/data/more_data/workbook.xls +0 -0
- data/spec/data/workbook_linked3.xlsm +0 -0
- data/spec/data/workbook_listobjects.xlsx +0 -0
- data/spec/data/workbook_unsaved.xlsm +0 -0
- data/spec/excel_spec.rb +4 -4
- data/spec/general_spec.rb +1 -2
- data/spec/list_object_spec.rb +26 -9
- data/spec/list_row_spec.rb +34 -20
- data/spec/range_spec.rb +9 -0
- data/spec/workbook_spec.rb +2 -2
- data/spec/workbook_specs/workbook_misc_spec.rb +249 -2
- data/spec/workbook_specs/workbook_open_spec.rb +307 -16
- data/spec/workbook_specs/workbook_unobtr_spec.rb +196 -42
- metadata +4 -16
@@ -47,6 +47,253 @@ describe Workbook do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
describe "writable=" do
|
51
|
+
|
52
|
+
it "should change from writable to readonly back to writable" do
|
53
|
+
book = Workbook.open(@simple_file1)
|
54
|
+
book.ReadOnly.should be false
|
55
|
+
book.writable = true
|
56
|
+
book.ReadOnly.should be false
|
57
|
+
book.writable = false
|
58
|
+
book.ReadOnly.should be true
|
59
|
+
book.writable = true
|
60
|
+
book.ReadOnly.should be false
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should change from readonly to writable back to readonly" do
|
64
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
65
|
+
book.ReadOnly.should be true
|
66
|
+
book.writable = false
|
67
|
+
book.ReadOnly.should be true
|
68
|
+
book.writable = true
|
69
|
+
book.ReadOnly.should be false
|
70
|
+
book.writable = false
|
71
|
+
book.ReadOnly.should be true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should change read-only to writable mode for an unsaved workbook" do
|
75
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
76
|
+
book.ReadOnly.should be true
|
77
|
+
sheet = book.sheet(1)
|
78
|
+
old_value = sheet[1,1]
|
79
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
80
|
+
new_value = sheet[1,1]
|
81
|
+
book.writable = false
|
82
|
+
book.ReadOnly.should be true
|
83
|
+
book.writable = true
|
84
|
+
book.ReadOnly.should be false
|
85
|
+
book.Saved.should be false
|
86
|
+
sheet[1,1].should == new_value
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should save changes and change from read-only to writable with option :save" do
|
90
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
91
|
+
book.ReadOnly.should be true
|
92
|
+
sheet = book.sheet(1)
|
93
|
+
old_value = sheet[1,1]
|
94
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
95
|
+
new_value = sheet[1,1]
|
96
|
+
book.writable = true, {if_unsaved: :save}
|
97
|
+
book.ReadOnly.should be false
|
98
|
+
book.Saved.should be true
|
99
|
+
sheet[1,1].should_not == old_value
|
100
|
+
sheet[1,1].should == new_value
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should discard changes and change from read-only to writable with option :forget" do
|
104
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
105
|
+
book.ReadOnly.should be true
|
106
|
+
sheet = book.sheet(1)
|
107
|
+
old_value = sheet[1,1]
|
108
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
109
|
+
new_value = sheet[1,1]
|
110
|
+
book.writable = true, {if_unsaved: :forget}
|
111
|
+
book.ReadOnly.should be false
|
112
|
+
book.close
|
113
|
+
book2 = Workbook.open(@simple_file1)
|
114
|
+
sheet2 = book2.sheet(1)
|
115
|
+
sheet2[1,1].should == old_value
|
116
|
+
sheet2[1,1].should_not == new_value
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should raise error when tying to change writable to read-only mode for an unsaved workbook with option :raise" do
|
120
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
121
|
+
book.ReadOnly.should be false
|
122
|
+
sheet = book.sheet(1)
|
123
|
+
old_value = sheet[1,1]
|
124
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
125
|
+
new_value = sheet[1,1]
|
126
|
+
book.writable = true
|
127
|
+
book.ReadOnly.should be false
|
128
|
+
expect{
|
129
|
+
book.writable = false, {if_unsaved: :raise}
|
130
|
+
}.to raise_error(WorkbookNotSaved)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should save changes and change from writable to read-only with option :save" do
|
134
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
135
|
+
book.ReadOnly.should be false
|
136
|
+
sheet = book.sheet(1)
|
137
|
+
old_value = sheet[1,1]
|
138
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
139
|
+
new_value = sheet[1,1]
|
140
|
+
book.writable = false, {if_unsaved: :save}
|
141
|
+
book.ReadOnly.should be true
|
142
|
+
sheet[1,1].should_not == old_value
|
143
|
+
sheet[1,1].should == new_value
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should discard changes and change from writable to read-only with option :forget" do
|
147
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
148
|
+
book.ReadOnly.should be false
|
149
|
+
sheet = book.sheet(1)
|
150
|
+
old_value = sheet[1,1]
|
151
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
152
|
+
new_value = sheet[1,1]
|
153
|
+
book.writable = false, {if_unsaved: :forget}
|
154
|
+
book.ReadOnly.should be true
|
155
|
+
book.close
|
156
|
+
book2 = Workbook.open(@simple_file1)
|
157
|
+
sheet2 = book2.sheet(1)
|
158
|
+
sheet2[1,1].should == old_value
|
159
|
+
sheet2[1,1].should_not == new_value
|
160
|
+
end
|
161
|
+
|
162
|
+
context "with :if_unsaved => :excel or :alert and from read-only to read-write" do
|
163
|
+
|
164
|
+
before do
|
165
|
+
@book = Workbook.open(@simple_file1, v: true, readonly: true)
|
166
|
+
@book.ReadOnly.should be false
|
167
|
+
@sheet = @book.sheet(1)
|
168
|
+
@old_value = @sheet[1,1]
|
169
|
+
@sheet[1,1] = (@sheet[1,1] == "foo" ? "bar" : "foo")
|
170
|
+
@new_value = @sheet[1,1]
|
171
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
|
172
|
+
end
|
173
|
+
|
174
|
+
after do
|
175
|
+
@key_sender.close
|
176
|
+
end
|
177
|
+
|
178
|
+
# question to the user, whether the workbook shall be reopened and discard any changes
|
179
|
+
|
180
|
+
it "should discard changes and reopen the workbook, if user answers 'yes'" do
|
181
|
+
@key_sender.puts "{enter}"
|
182
|
+
@book.writable = true, {if_unsaved: :excel}
|
183
|
+
@book.ReadOnly.should be false
|
184
|
+
@book.Saved.should be false
|
185
|
+
@sheet[1,1].should == @new_value
|
186
|
+
@book.close(if_unsaved: :forget)
|
187
|
+
book1 = Workbook.open(@simple_file1)
|
188
|
+
sheet = book1.sheet(1)
|
189
|
+
sheet[1,1].should == @old_value
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should not discard changes and reopen the workbook, if user answers 'no'" do
|
193
|
+
# "No" is right to "Yes" (the default). --> language independent
|
194
|
+
# strangely, in the "no" case, the question will sometimes be repeated three times
|
195
|
+
#@book.excel.Visible = true
|
196
|
+
@key_sender.puts "{right}{enter}"
|
197
|
+
@key_sender.puts "{right}{enter}"
|
198
|
+
@key_sender.puts "{right}{enter}"
|
199
|
+
@book.writable = true, {if_unsaved: :excel}
|
200
|
+
@book.ReadOnly.should be false
|
201
|
+
@book.Saved.should be false
|
202
|
+
@sheet[1,1].should == @new_value
|
203
|
+
@book.close(if_unsaved: :forget)
|
204
|
+
book1 = Workbook.open(@simple_file1)
|
205
|
+
sheet = book1.sheet(1)
|
206
|
+
sheet[1,1].should == @old_value
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
context "with :if_unsaved => :excel or :alert and from writable to read-only" do
|
212
|
+
|
213
|
+
before do
|
214
|
+
@book = Workbook.open(@simple_file1, v: true, readonly: false)
|
215
|
+
@book.ReadOnly.should be false
|
216
|
+
@sheet = @book.sheet(1)
|
217
|
+
@old_value = @sheet[1,1]
|
218
|
+
@sheet[1,1] = (@sheet[1,1] == "foo" ? "bar" : "foo")
|
219
|
+
@new_value = @sheet[1,1]
|
220
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
|
221
|
+
end
|
222
|
+
|
223
|
+
after do
|
224
|
+
@key_sender.close
|
225
|
+
end
|
226
|
+
|
227
|
+
# question to the user, whether to save the changes before changing read-only mode
|
228
|
+
it "should save the workbook, if user answers 'no' and 'yes'" do
|
229
|
+
# "No" is right to "Yes" (the default). --> language independent
|
230
|
+
@key_sender.puts "{right}{enter}"
|
231
|
+
# 2nd question to the user: whether the workbook shall be reopened and discard any changes
|
232
|
+
# "No" is right to "Yes" (the default). --> language independent
|
233
|
+
@key_sender.puts "{right}{enter}"
|
234
|
+
@key_sender.puts "{right}{enter}"
|
235
|
+
@key_sender.puts "{right}{enter}"
|
236
|
+
# 3rd question whether the workbook shall be saved
|
237
|
+
# "Yes"
|
238
|
+
@key_sender.puts "{enter}"
|
239
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
240
|
+
book2.ReadOnly.should be true
|
241
|
+
book2.Saved.should be false
|
242
|
+
@sheet[1,1].should == @new_value
|
243
|
+
book2.close(if_unsaved: :forget)
|
244
|
+
book3 = Workbook.open(@simple_file1)
|
245
|
+
sheet3 = book3.sheet(1)
|
246
|
+
sheet3[1,1].should == @old_value
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should discard (not save) the workbook, if user answers 'no' and 'no'" do
|
250
|
+
# "No" is right to "Yes" (the default). --> language independent
|
251
|
+
@key_sender.puts "{right}{enter}"
|
252
|
+
# 2nd question to the user: whether the workbook shall be reopened and discard any changes
|
253
|
+
# "No" is right to "Yes" (the default). --> language independent
|
254
|
+
@key_sender.puts "{right}{enter}"
|
255
|
+
@key_sender.puts "{right}{enter}"
|
256
|
+
@key_sender.puts "{right}{enter}"
|
257
|
+
# 3rd question whether the workbook shall be saved
|
258
|
+
# "No"
|
259
|
+
@key_sender.puts "{right}{enter}"
|
260
|
+
@key_sender.puts "{right}{enter}"
|
261
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
262
|
+
book2.ReadOnly.should be true
|
263
|
+
book2.Saved.should be false
|
264
|
+
@sheet[1,1].should == @new_value
|
265
|
+
book2.close(if_unsaved: :forget)
|
266
|
+
book3 = Workbook.open(@simple_file1)
|
267
|
+
sheet3 = book3.sheet(1)
|
268
|
+
sheet3[1,1].should == @old_value
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should not save (discard) changes and not reopen the workbook, if user answers 'no' and 'cancel'" do
|
272
|
+
# "No" is right to "Yes" (the default). --> language independent
|
273
|
+
@key_sender.puts "{right}{enter}"
|
274
|
+
# 2nd question to the user: whether the workbook shall be reopened and discard any changes
|
275
|
+
# "No" is right to "Yes" (the default). --> language independent
|
276
|
+
@key_sender.puts "{right}{enter}"
|
277
|
+
@key_sender.puts "{right}{enter}"
|
278
|
+
@key_sender.puts "{right}{enter}"
|
279
|
+
# 3rd question whether the workbook shall be saved
|
280
|
+
# "Cancel"
|
281
|
+
@key_sender.puts "{right}{right}{enter}"
|
282
|
+
@key_sender.puts "{right}{right}{enter}"
|
283
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
284
|
+
book2.ReadOnly.should be true
|
285
|
+
book2.Saved.should be false
|
286
|
+
@sheet[1,1].should == @new_value
|
287
|
+
book2.close(if_unsaved: :forget)
|
288
|
+
book3 = Workbook.open(@simple_file1, if_unsaved: :forget)
|
289
|
+
sheet3 = book3.sheet(1)
|
290
|
+
sheet3[1,1].should == @old_value
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
end
|
296
|
+
|
50
297
|
describe "for_this_workbook" do
|
51
298
|
|
52
299
|
before do
|
@@ -728,10 +975,10 @@ describe Workbook do
|
|
728
975
|
}.to_not raise_error
|
729
976
|
expect {
|
730
977
|
@book1.namevalue_global("foo", :default => :__not_provided)
|
731
|
-
}.to raise_error(NameNotFound, /name "foo"
|
978
|
+
}.to raise_error(NameNotFound, /cannot find name "foo"/)
|
732
979
|
expect {
|
733
980
|
@book1.namevalue_global("foo")
|
734
|
-
}.to raise_error(NameNotFound, /name "foo"
|
981
|
+
}.to raise_error(NameNotFound, /cannot find name "foo"/)
|
735
982
|
@book1.namevalue_global("foo", :default => nil).should be_nil
|
736
983
|
@book1.namevalue_global("foo", :default => 1).should == 1
|
737
984
|
expect {
|
@@ -35,13 +35,13 @@ describe Workbook do
|
|
35
35
|
@simple_file_network_path = "N:/data/workbook.xls"
|
36
36
|
network = WIN32OLE.new('WScript.Network')
|
37
37
|
computer_name = network.ComputerName
|
38
|
-
@simple_file_hostname_share_path = "//#{computer_name}
|
38
|
+
@simple_file_hostname_share_path = "//#{computer_name}/spec/data/workbook.xls"
|
39
|
+
@simple_file_hostname_share_other_path = "//#{computer_name}/spec/more_data/workbook.xls"
|
39
40
|
@simple_file_network_path_other_path = "N:/data/more_data/workbook.xls"
|
40
|
-
@simple_file_hostname_share_path_other_path = "//#{computer_name}/#{absolute_path('spec/more_data/workbook.xls').tr('\\','/').gsub('C:','c$')}"
|
41
41
|
@simple_file_network_path1 = @simple_file_network_path
|
42
42
|
@simple_file_hostname_share_path1 = @simple_file_hostname_share_path
|
43
43
|
@simple_file_network_path_other_path1 = @simple_file_network_path_other_path
|
44
|
-
@
|
44
|
+
@simple_file_hostname_share_other_path1 = @simple_file_hostname_share_other_path
|
45
45
|
@simple_file_xlsm1 = @simple_file_xlsm
|
46
46
|
@simple_file_xlsx1 = @simple_file_xlsx
|
47
47
|
#@linked_file = @dir + '/workbook_linked.xlsm'
|
@@ -56,6 +56,289 @@ describe Workbook do
|
|
56
56
|
rm_tmp(@dir)
|
57
57
|
end
|
58
58
|
|
59
|
+
describe "changing ReadOnly mode" do
|
60
|
+
|
61
|
+
it "should change from writable to readonly back to writable" do
|
62
|
+
book = Workbook.open(@simple_file1)
|
63
|
+
book.ReadOnly.should be false
|
64
|
+
book2 = Workbook.open(@simple_file1, read_only: true)
|
65
|
+
book2.should == book
|
66
|
+
book2.ReadOnly.should be true
|
67
|
+
book3 = Workbook.open(@simple_file1, read_only: false)
|
68
|
+
book3.should == book
|
69
|
+
book3.ReadOnly.should be false
|
70
|
+
book3.close
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should change from readonly to writable back to readonly" do
|
74
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
75
|
+
book.ReadOnly.should be true
|
76
|
+
book2 = Workbook.open(@simple_file1, read_only: false)
|
77
|
+
book2.should == book
|
78
|
+
book2.ReadOnly.should be false
|
79
|
+
book3 = Workbook.open(@simple_file1, read_only: true)
|
80
|
+
book3.should == book
|
81
|
+
book3.ReadOnly.should be true
|
82
|
+
book3.close
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should raise error when read-only workbook unsaved and trying to reopen workbook writable by default" do
|
86
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
87
|
+
book.ReadOnly.should be true
|
88
|
+
sheet = book.sheet(1)
|
89
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
90
|
+
expect{
|
91
|
+
Workbook.open(@simple_file1, read_only: false)
|
92
|
+
}.to raise_error(WorkbookNotSaved)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should raise error when read-only workbook unsaved and trying to reopen workbook writable with option :raise" do
|
96
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
97
|
+
book.ReadOnly.should be true
|
98
|
+
sheet = book.sheet(1)
|
99
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
100
|
+
expect{
|
101
|
+
Workbook.open(@simple_file1, read_only: false, if_unsaved: :raise)
|
102
|
+
}.to raise_error(WorkbookNotSaved)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should save changes and change from read-only to writable with option :save" do
|
106
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
107
|
+
book.ReadOnly.should be true
|
108
|
+
sheet = book.sheet(1)
|
109
|
+
old_value = sheet[1,1]
|
110
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
111
|
+
new_value = sheet[1,1]
|
112
|
+
book2 = Workbook.open(@simple_file1, if_unsaved: :save, read_only: false)
|
113
|
+
book2.should == book
|
114
|
+
book2.ReadOnly.should be false
|
115
|
+
sheet[1,1].should_not == old_value
|
116
|
+
sheet[1,1].should == new_value
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should discard changes and change from read-only to writable with options :forget" do
|
120
|
+
book = Workbook.open(@simple_file1, read_only: true)
|
121
|
+
book.ReadOnly.should be true
|
122
|
+
sheet = book.sheet(1)
|
123
|
+
old_value = sheet[1,1]
|
124
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
125
|
+
new_value = sheet[1,1]
|
126
|
+
book2 = Workbook.open(@simple_file1, if_unsaved: :forget, read_only: false)
|
127
|
+
book2.ReadOnly.should be false
|
128
|
+
book2.close
|
129
|
+
book3 = Workbook.open(@simple_file1)
|
130
|
+
sheet3 = book3.sheet(1)
|
131
|
+
sheet3[1,1].should == old_value
|
132
|
+
sheet3[1,1].should_not == new_value
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
it "should raise error when writable workbook unsaved and trying to reopen workbook read-only by default" do
|
137
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
138
|
+
book.ReadOnly.should be false
|
139
|
+
sheet = book.sheet(1)
|
140
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
141
|
+
expect{
|
142
|
+
Workbook.open(@simple_file1, read_only: true)
|
143
|
+
}.to raise_error(WorkbookNotSaved)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should raise error when writable workbook unsaved and trying to reopen workbook read-only with option :raise" do
|
147
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
148
|
+
book.ReadOnly.should be false
|
149
|
+
sheet = book.sheet(1)
|
150
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
151
|
+
expect{
|
152
|
+
Workbook.open(@simple_file1, read_only: true, if_unsaved: :raise)
|
153
|
+
}.to raise_error(WorkbookNotSaved)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should save changes and change from writable to read-only with option :save" do
|
157
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
158
|
+
book.ReadOnly.should be false
|
159
|
+
sheet = book.sheet(1)
|
160
|
+
old_value = sheet[1,1]
|
161
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
162
|
+
new_value = sheet[1,1]
|
163
|
+
book2 = Workbook.open(@simple_file1, if_unsaved: :save, read_only: true)
|
164
|
+
book2.should == book
|
165
|
+
book2.ReadOnly.should be true
|
166
|
+
sheet[1,1].should_not == old_value
|
167
|
+
sheet[1,1].should == new_value
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should discard changes and change from writable to read-only with options :forget" do
|
171
|
+
book = Workbook.open(@simple_file1, read_only: false)
|
172
|
+
book.ReadOnly.should be false
|
173
|
+
sheet = book.sheet(1)
|
174
|
+
old_value = sheet[1,1]
|
175
|
+
sheet[1,1] = (sheet[1,1] == "foo" ? "bar" : "foo")
|
176
|
+
new_value = sheet[1,1]
|
177
|
+
book2 = Workbook.open(@simple_file1, if_unsaved: :forget, read_only: true)
|
178
|
+
book2.should == book
|
179
|
+
book2.ReadOnly.should be true
|
180
|
+
book2.close
|
181
|
+
book3 = Workbook.open(@simple_file1)
|
182
|
+
sheet3 = book3.sheet(1)
|
183
|
+
sheet3[1,1].should == old_value
|
184
|
+
sheet3[1,1].should_not == new_value
|
185
|
+
end
|
186
|
+
|
187
|
+
context "with :if_unsaved => :excel or :alert and from read-only to writable" do
|
188
|
+
|
189
|
+
before do
|
190
|
+
@book = Workbook.open(@simple_file1, v: true, readonly: true)
|
191
|
+
@book.ReadOnly.should be false
|
192
|
+
@sheet = @book.sheet(1)
|
193
|
+
@old_value = @sheet[1,1]
|
194
|
+
@sheet[1,1] = (@sheet[1,1] == "foo" ? "bar" : "foo")
|
195
|
+
@new_value = @sheet[1,1]
|
196
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
|
197
|
+
end
|
198
|
+
|
199
|
+
after do
|
200
|
+
@key_sender.close
|
201
|
+
end
|
202
|
+
|
203
|
+
# question to the user, whether the workbook shall be reopened and discard any changes
|
204
|
+
it "should discard changes and reopen the workbook, if user answers 'yes'" do
|
205
|
+
@key_sender.puts "{enter}"
|
206
|
+
book2 = Workbook.open(@simple_file1, read_only: false, if_unsaved: :excel)
|
207
|
+
book2.ReadOnly.should be false
|
208
|
+
book2.Saved.should be true
|
209
|
+
book2.sheet(1)[1,1].should == @old_value
|
210
|
+
book2.close
|
211
|
+
book3 = Workbook.open(@simple_file1)
|
212
|
+
sheet3 = book3.sheet(1)
|
213
|
+
sheet3[1,1].should == @old_value
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should not discard changes and reopen the workbook, if user answers 'no'" do
|
217
|
+
# "No" is right to "Yes" (the default). --> language independent
|
218
|
+
# strangely, in the "no" case, the question will sometimes be repeated three times
|
219
|
+
#@book.excel.Visible = true
|
220
|
+
@key_sender.puts "{right}{enter}"
|
221
|
+
@key_sender.puts "{right}{enter}"
|
222
|
+
@key_sender.puts "{right}{enter}"
|
223
|
+
book2 = Workbook.open(@simple_file1, read_only: false, if_unsaved: :excel)
|
224
|
+
book2.ReadOnly.should be false
|
225
|
+
book2.Saved.should be false
|
226
|
+
book2.sheet(1)[1,1].should == @new_value
|
227
|
+
book2.close(if_unsaved: :forget)
|
228
|
+
book3 = Workbook.open(@simple_file1)
|
229
|
+
sheet3 = book3.sheet(1)
|
230
|
+
sheet3[1,1].should == @old_value
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
context "with :if_unsaved => :excel or :alert and from writable to read-only" do
|
236
|
+
|
237
|
+
before do
|
238
|
+
Excel.kill_all
|
239
|
+
sleep 1
|
240
|
+
@book = Workbook.open(@simple_file1, v: true, readonly: false)
|
241
|
+
@book.ReadOnly.should be false
|
242
|
+
@sheet = @book.sheet(1)
|
243
|
+
@old_value = @sheet[1,1]
|
244
|
+
@sheet[1,1] = (@sheet[1,1] == "foo" ? "bar" : "foo")
|
245
|
+
@new_value = @sheet[1,1]
|
246
|
+
@key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
|
247
|
+
end
|
248
|
+
|
249
|
+
after do
|
250
|
+
@key_sender.close
|
251
|
+
end
|
252
|
+
|
253
|
+
# question to the user, whether to save the changes before changing read-only mode
|
254
|
+
it "should discard changes and reopen the workbook, if user answers 'yes'" do
|
255
|
+
@key_sender.puts "{enter}"
|
256
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
257
|
+
book2.ReadOnly.should be true
|
258
|
+
book2.Saved.should be true
|
259
|
+
book2.sheet(1)[1,1].should == @new_value
|
260
|
+
book2.close
|
261
|
+
book3 = Workbook.open(@simple_file1)
|
262
|
+
sheet3 = book3.sheet(1)
|
263
|
+
sheet3[1,1].should == @new_value
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should save the workbook, if user answers 'no' and 'yes'" do
|
267
|
+
# "No" is right to "Yes" (the default). --> language independent
|
268
|
+
@key_sender.puts "{right}{enter}"
|
269
|
+
# 2nd question to the user: whether the workbook shall be reopened and discard any changes
|
270
|
+
# "No" is right to "Yes" (the default). --> language independent
|
271
|
+
@key_sender.puts "{right}{enter}"
|
272
|
+
@key_sender.puts "{right}{enter}"
|
273
|
+
@key_sender.puts "{right}{enter}"
|
274
|
+
# 3rd question whether the workbook shall be saved
|
275
|
+
# "Yes"
|
276
|
+
@key_sender.puts "{enter}"
|
277
|
+
@key_sender.puts "{enter}"
|
278
|
+
# another question: asking the user to save the workbook as a copy
|
279
|
+
# so far no automatic key response here
|
280
|
+
|
281
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
282
|
+
book2.ReadOnly.should be true
|
283
|
+
book2.Saved.should be false
|
284
|
+
book2.sheet(1)[1,1].should == @new_value
|
285
|
+
book2.close(if_unsaved: :forget)
|
286
|
+
book3 = Workbook.open(@simple_file1)
|
287
|
+
sheet3 = book3.sheet(1)
|
288
|
+
sheet3[1,1].should == @old_value
|
289
|
+
end
|
290
|
+
|
291
|
+
|
292
|
+
it "should discard (not save) the workbook, if user answers 'no' and 'no'" do
|
293
|
+
# "No" is right to "Yes" (the default). --> language independent
|
294
|
+
@key_sender.puts "{right}{enter}"
|
295
|
+
# 2nd question to the user: whether the workbook shall be reopened and discard any changes
|
296
|
+
# "No" is right to "Yes" (the default). --> language independent
|
297
|
+
@key_sender.puts "{right}{enter}"
|
298
|
+
@key_sender.puts "{right}{enter}"
|
299
|
+
@key_sender.puts "{right}{enter}"
|
300
|
+
# 3rd question whether the workbook shall be saved
|
301
|
+
# "No"
|
302
|
+
@key_sender.puts "{right}{enter}"
|
303
|
+
@key_sender.puts "{right}{enter}"
|
304
|
+
@key_sender.puts "{right}{enter}"
|
305
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
306
|
+
book2.ReadOnly.should be true
|
307
|
+
book2.Saved.should be false
|
308
|
+
book2.sheet(1)[1,1].should == @new_value
|
309
|
+
book2.close(if_unsaved: :forget)
|
310
|
+
book3 = Workbook.open(@simple_file1)
|
311
|
+
sheet3 = book3.sheet(1)
|
312
|
+
sheet3[1,1].should == @old_value
|
313
|
+
end
|
314
|
+
|
315
|
+
it "should not save (discard) changes and not reopen the workbook, if user answers 'no' and 'cancel'" do
|
316
|
+
# "No" is right to "Yes" (the default). --> language independent
|
317
|
+
@key_sender.puts "{right}{enter}"
|
318
|
+
# 2nd question to the user: whether the workbook shall be reopened and discard any changes
|
319
|
+
# "No" is right to "Yes" (the default). --> language independent
|
320
|
+
@key_sender.puts "{right}{enter}"
|
321
|
+
@key_sender.puts "{right}{enter}"
|
322
|
+
@key_sender.puts "{right}{enter}"
|
323
|
+
# 3rd question whether the workbook shall be saved
|
324
|
+
# "Cancel"
|
325
|
+
@key_sender.puts "{right}{right}{enter}"
|
326
|
+
@key_sender.puts "{right}{right}{enter}"
|
327
|
+
@key_sender.puts "{right}{right}{enter}"
|
328
|
+
book2 = Workbook.open(@simple_file1, read_only: true, if_unsaved: :excel)
|
329
|
+
book2.ReadOnly.should be true
|
330
|
+
book2.Saved.should be false
|
331
|
+
book2.sheet(1)[1,1].should == @new_value
|
332
|
+
book2.close(if_unsaved: :forget)
|
333
|
+
book3 = Workbook.open(@simple_file1, if_unsaved: :forget)
|
334
|
+
sheet3 = book3.sheet(1)
|
335
|
+
sheet3[1,1].should == @old_value
|
336
|
+
end
|
337
|
+
|
338
|
+
end
|
339
|
+
|
340
|
+
end
|
341
|
+
|
59
342
|
describe "linked workbooks" do
|
60
343
|
|
61
344
|
context "standard" do
|
@@ -88,9 +371,8 @@ describe Workbook do
|
|
88
371
|
end
|
89
372
|
|
90
373
|
it "should raise error when trying to change the read-only mode of the linked workbook" do
|
91
|
-
|
92
|
-
|
93
|
-
}.to raise_error(WorkbookReadOnly, /could not change read-only mode/)
|
374
|
+
book2 = Workbook.open(@sub_file, :read_only => true)
|
375
|
+
book2.ReadOnly.should be true
|
94
376
|
end
|
95
377
|
end
|
96
378
|
end
|
@@ -396,12 +678,12 @@ describe Workbook do
|
|
396
678
|
it "should raise an WorkbookBlockederror" do
|
397
679
|
book1 = Workbook.open(@simple_file_hostname_share_path1)
|
398
680
|
expect{
|
399
|
-
Workbook.open(@
|
681
|
+
Workbook.open(@simple_file_hostname_share_other_path1)
|
400
682
|
}.to raise_error(WorkbookBlocked)
|
401
683
|
end
|
402
684
|
|
403
685
|
it "should raise an WorkbookBlockederror" do
|
404
|
-
book1 = Workbook.open(@
|
686
|
+
book1 = Workbook.open(@simple_file_hostname_share_other_path1)
|
405
687
|
expect{
|
406
688
|
Workbook.open(@simple_file_hostname_share_path1)
|
407
689
|
}.to raise_error(WorkbookBlocked)
|
@@ -415,7 +697,7 @@ describe Workbook do
|
|
415
697
|
end
|
416
698
|
|
417
699
|
it "should raise an WorkbookBlockederror" do
|
418
|
-
book1 = Workbook.open(@
|
700
|
+
book1 = Workbook.open(@simple_file_hostname_share_other_path1)
|
419
701
|
expect{
|
420
702
|
Workbook.open(@simple_file_network_path1)
|
421
703
|
}.to raise_error(WorkbookBlocked)
|
@@ -424,7 +706,7 @@ describe Workbook do
|
|
424
706
|
it "should raise an WorkbookBlockederror" do
|
425
707
|
book1 = Workbook.open(@simple_file_network_path1)
|
426
708
|
expect{
|
427
|
-
Workbook.open(@
|
709
|
+
Workbook.open(@simple_file_hostname_share_other_path1)
|
428
710
|
}.to raise_error(WorkbookBlocked)
|
429
711
|
end
|
430
712
|
|
@@ -572,14 +854,14 @@ describe Workbook do
|
|
572
854
|
abs_filename = General.absolute_path(@simple_file_hostname_share_path1)
|
573
855
|
@ole_wb = ws.Open(abs_filename)
|
574
856
|
expect{
|
575
|
-
Workbook.open(@
|
857
|
+
Workbook.open(@simple_file_hostname_share_other_path1)
|
576
858
|
}.to raise_error(WorkbookBlocked)
|
577
859
|
end
|
578
860
|
|
579
861
|
it "should raise WorkbookBlocked error" do
|
580
862
|
ole_e1 = WIN32OLE.new('Excel.Application')
|
581
863
|
ws = ole_e1.Workbooks
|
582
|
-
abs_filename = General.absolute_path(@
|
864
|
+
abs_filename = General.absolute_path(@simple_file_hostname_share_other_path1)
|
583
865
|
@ole_wb = ws.Open(abs_filename)
|
584
866
|
expect{
|
585
867
|
Workbook.open(@simple_file_hostname_share_path1)
|
@@ -612,14 +894,14 @@ describe Workbook do
|
|
612
894
|
abs_filename = General.absolute_path(@simple_file_network_path1)
|
613
895
|
@ole_wb = ws.Open(abs_filename)
|
614
896
|
expect{
|
615
|
-
Workbook.open(@
|
897
|
+
Workbook.open(@simple_file_hostname_share_other_path1)
|
616
898
|
}.to raise_error(WorkbookBlocked)
|
617
899
|
end
|
618
900
|
|
619
901
|
it "should raise WorkbookBlocked error" do
|
620
902
|
ole_e1 = WIN32OLE.new('Excel.Application')
|
621
903
|
ws = ole_e1.Workbooks
|
622
|
-
abs_filename = General.absolute_path(@
|
904
|
+
abs_filename = General.absolute_path(@simple_file_hostname_share_other_path1)
|
623
905
|
@ole_wb = ws.Open(abs_filename)
|
624
906
|
expect{
|
625
907
|
Workbook.open(@simple_file_network_path1)
|
@@ -902,7 +1184,7 @@ describe Workbook do
|
|
902
1184
|
book2 = book1
|
903
1185
|
book1.close(:if_unsaved => :forget)
|
904
1186
|
book1.should_not be_alive
|
905
|
-
book1.
|
1187
|
+
book1.open
|
906
1188
|
book1.should be_a Workbook
|
907
1189
|
book1.should be_alive
|
908
1190
|
book1.should === book2
|
@@ -2734,6 +3016,15 @@ describe Workbook do
|
|
2734
3016
|
old_book.sheet(1)[1,1].should == @old_value
|
2735
3017
|
end
|
2736
3018
|
|
3019
|
+
it "should let the old book open, if :if_obstructed is :accept" do
|
3020
|
+
new_book = Workbook.open(@simple_file1, :if_obstructed => :accept)
|
3021
|
+
@book.should be_alive
|
3022
|
+
new_book.should be_alive
|
3023
|
+
new_book.filename.downcase.should == @simple_file_other_path1.downcase
|
3024
|
+
old_book = Workbook.open(@simple_file_other_path1, :if_unsaved => :forget)
|
3025
|
+
old_book.sheet(1)[1,1].should == @old_value
|
3026
|
+
end
|
3027
|
+
|
2737
3028
|
it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
|
2738
3029
|
new_book = Workbook.open(@simple_file1, :if_obstructed => :save)
|
2739
3030
|
@book.should_not be_alive
|
@@ -3082,7 +3373,7 @@ describe Workbook do
|
|
3082
3373
|
book1 = @book
|
3083
3374
|
@book.close
|
3084
3375
|
@book.should_not be_alive
|
3085
|
-
@book.
|
3376
|
+
@book.open
|
3086
3377
|
@book.should be_a Workbook
|
3087
3378
|
@book.should be_alive
|
3088
3379
|
@book.should === book1
|