robust_excel_ole 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README.rdoc +73 -26
  2. data/README_detail.rdoc +92 -27
  3. data/examples/edit_sheets/example_access_sheets_and_cells.rb +3 -3
  4. data/examples/edit_sheets/example_concating.rb +12 -12
  5. data/examples/edit_sheets/example_copying.rb +47 -0
  6. data/examples/edit_sheets/example_expanding.rb +17 -26
  7. data/examples/edit_sheets/example_naming.rb +13 -10
  8. data/examples/edit_sheets/example_ranges.rb +2 -2
  9. data/examples/edit_sheets/example_saving.rb +8 -14
  10. data/examples/open_save_close/example_control_to_excel.rb +1 -1
  11. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +3 -3
  12. data/examples/open_save_close/example_if_obstructed_save.rb +3 -3
  13. data/examples/open_save_close/example_if_unsaved_accept.rb +1 -1
  14. data/examples/open_save_close/example_if_unsaved_forget.rb +4 -4
  15. data/examples/open_save_close/example_if_unsaved_forget_more.rb +5 -5
  16. data/examples/open_save_close/example_read_only.rb +1 -1
  17. data/examples/open_save_close/example_rename_cells.rb +1 -13
  18. data/examples/open_save_close/example_simple.rb +1 -1
  19. data/examples/open_save_close/example_unobtrusively.rb +3 -3
  20. data/lib/robust_excel_ole.rb +81 -2
  21. data/lib/robust_excel_ole/book.rb +171 -118
  22. data/lib/robust_excel_ole/{book_store.rb → bookstore.rb} +2 -2
  23. data/lib/robust_excel_ole/excel.rb +153 -24
  24. data/lib/robust_excel_ole/range.rb +2 -2
  25. data/lib/robust_excel_ole/sheet.rb +84 -35
  26. data/lib/robust_excel_ole/version.rb +1 -1
  27. data/reo.bat +3 -0
  28. data/spec/book_close_spec.rb +179 -0
  29. data/spec/book_misc_spec.rb +365 -0
  30. data/spec/book_open_spec.rb +793 -0
  31. data/spec/book_save_spec.rb +257 -0
  32. data/spec/book_sheet_spec.rb +160 -0
  33. data/spec/book_spec.rb +145 -1533
  34. data/spec/book_subclass_spec.rb +50 -0
  35. data/spec/book_unobtr_spec.rb +950 -0
  36. data/spec/{book_store_spec.rb → bookstore_spec.rb} +5 -5
  37. data/spec/cell_spec.rb +6 -6
  38. data/spec/data/{more_workbook.xls → another_workbook.xls} +0 -0
  39. data/spec/data/different_workbook.xls +0 -0
  40. data/spec/data/workbook.xls +0 -0
  41. data/spec/data/workbook.xlsm +0 -0
  42. data/spec/data/workbook.xlsx +0 -0
  43. data/spec/data/workbook_linked.xlsm +0 -0
  44. data/spec/data/workbook_linked_sub.xlsm +0 -0
  45. data/spec/excel_spec.rb +204 -5
  46. data/spec/range_spec.rb +6 -6
  47. data/spec/sheet_spec.rb +122 -34
  48. metadata +18 -8
  49. data/spec/data/workbook_connected_sub.xlsm +0 -0
@@ -35,7 +35,7 @@ class Book
35
35
  end
36
36
 
37
37
 
38
- describe BookStore do
38
+ describe Bookstore do
39
39
 
40
40
  before(:all) do
41
41
  excel = Excel.new(:reuse => true)
@@ -45,7 +45,7 @@ describe BookStore do
45
45
  end
46
46
 
47
47
  before do
48
- @bookstore = BookStore.new
48
+ @bookstore = Bookstore.new
49
49
  @dir = create_tmpdir
50
50
  @simple_file = @dir + '/workbook.xls'
51
51
  @simple_save_file = @dir + '/workbook_save.xls'
@@ -62,9 +62,9 @@ describe BookStore do
62
62
  context "with standard" do
63
63
  it "should create book store" do
64
64
  expect {
65
- @book_store = BookStore.new
65
+ @book_store = Bookstore.new
66
66
  }.to_not raise_error
67
- @book_store.should be_a BookStore
67
+ @book_store.should be_a Bookstore
68
68
  end
69
69
  end
70
70
  end
@@ -273,7 +273,7 @@ describe BookStore do
273
273
  @bookstore.store(@book2)
274
274
  @bookstore.store(@book3)
275
275
  sheet = @book3[0]
276
- sheet[0,0] = sheet[0,0].Value == "simple" ? "complex" : "simple"
276
+ sheet[1,1] = sheet[1,1].Value == "foo" ? "bar" : "foo"
277
277
  @book.ReadOnly.should be_true
278
278
  @book2.ReadOnly.should be_false
279
279
  @book3.ReadOnly.should be_true
data/spec/cell_spec.rb CHANGED
@@ -22,7 +22,7 @@ describe RobustExcelOle::Cell do
22
22
  before do
23
23
  @book = RobustExcelOle::Book.open(@dir + '/workbook.xls', :read_only => true)
24
24
  @sheet = @book[1]
25
- @cell = @sheet[0, 0]
25
+ @cell = @sheet[1, 1]
26
26
  end
27
27
 
28
28
  after do
@@ -61,14 +61,14 @@ describe RobustExcelOle::Cell do
61
61
  end
62
62
 
63
63
  it "merged cell get same value" do
64
- @sheet[0, 0].value.should be_nil
65
- @sheet[1, 0].value.should eq 'first merged'
64
+ @sheet[1, 1].value.should be_nil
65
+ @sheet[2, 1].value.should eq 'first merged'
66
66
  end
67
67
 
68
68
  it "set merged cell" do
69
- @sheet[1, 0].value = "set merge cell"
70
- @sheet[1, 0].value.should eq "set merge cell"
71
- @sheet[1, 1].value.should eq "set merge cell"
69
+ @sheet[2, 1].value = "set merge cell"
70
+ @sheet[2, 1].value.should eq "set merge cell"
71
+ @sheet[2, 2].value.should eq "set merge cell"
72
72
  end
73
73
  end
74
74
  end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/spec/excel_spec.rb CHANGED
@@ -14,8 +14,11 @@ module RobustExcelOle
14
14
 
15
15
  before do
16
16
  @dir = create_tmpdir
17
- @simple_file = @dir + '/simple.xls'
18
- @invalid_name_file = 'b/simple.xls'
17
+ #print "tmpdir: "; p @dir
18
+ @simple_file = @dir + '/workbook.xls'
19
+ @another_simple_file = @dir + '/another_workbook.xls'
20
+ @different_file = @dir + '/different_workbook.xls'
21
+ @invalid_name_file = 'b/workbook.xls'
19
22
  end
20
23
 
21
24
  after do
@@ -32,6 +35,12 @@ module RobustExcelOle
32
35
  @excel.Name.should == "Microsoft Excel"
33
36
  end
34
37
 
38
+ it "should access excel.excel" do
39
+ excel = Excel.new(:reuse => false)
40
+ excel.excel.should == excel
41
+ excel.excel.should be_a Excel
42
+ end
43
+
35
44
  it "should work with 'new' " do
36
45
  @excel = Excel.new
37
46
  creation_ok?
@@ -93,6 +102,156 @@ module RobustExcelOle
93
102
  end
94
103
  end
95
104
 
105
+ =begin
106
+ # testing private methods
107
+ context "close_excel" do
108
+
109
+ before do
110
+ @book = Book.open(@simple_file, :visible => true)
111
+ @excel = @book.excel
112
+ @book2 = Book.open(@simple_file, :force_excel => :new, :visible => true)
113
+ @excel2 = @book2.excel
114
+ end
115
+
116
+ it "should close one Excel" do
117
+ @excel.should be_alive
118
+ @excel2.should be_alive
119
+ @book.should be_alive
120
+ @book2.should be_alive
121
+ @excel.close_excel(:hard => false)
122
+ @excel.should_not be_alive
123
+ @book.should_not be_alive
124
+ @excel2.should be_alive
125
+ @book2.should be_alive
126
+ end
127
+ end
128
+
129
+ =end
130
+
131
+ describe "close_all" do
132
+
133
+ context "with saved workbooks" do
134
+
135
+ before do
136
+ book = Book.open(@simple_file, :visible => true)
137
+ book2 = Book.open(@simple_file, :force_excel => :new, :visible => true)
138
+ @excel = book.excel
139
+ @excel2 = book2.excel
140
+ end
141
+
142
+ it "should close the Excel instances" do
143
+ @excel.should be_alive
144
+ @excel2.should be_alive
145
+ Excel.close_all
146
+ @excel.should_not be_alive
147
+ @excel2.should_not be_alive
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "close" do
153
+
154
+ context "with a saved workbook" do
155
+
156
+ before do
157
+ @excel = Excel.create
158
+ @book = Book.open(@simple_file)
159
+ end
160
+
161
+ it "should close the Excel" do
162
+ @excel.should be_alive
163
+ @book.should be_alive
164
+ @excel.close
165
+ @excel.should_not be_alive
166
+ @book.should_not be_alive
167
+ end
168
+ end
169
+
170
+ context "with an unsaved workbook" do
171
+
172
+ before do
173
+ @excel = Excel.create
174
+ @book = Book.open(@simple_file)
175
+ sheet = @book[0]
176
+ @old_cell_value = sheet[1,1].value
177
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
178
+ end
179
+
180
+ it "should raise an error" do
181
+ @excel.should be_alive
182
+ @book.should be_alive
183
+ @book.saved.should be_false
184
+ expect{
185
+ @excel.close(:if_unsaved => :raise)
186
+ }.to raise_error(ExcelErrorClose, "Excel contains unsaved workbooks")
187
+ @excel.should be_alive
188
+ @book.should be_alive
189
+ end
190
+
191
+ it "should close the Excel without saving the workbook" do
192
+ @excel.should be_alive
193
+ @book.should be_alive
194
+ @book.saved.should be_false
195
+ @excel.close(:if_unsaved => :forget)
196
+ @excel.should_not be_alive
197
+ @book.should_not be_alive
198
+ new_book = Book.open(@simple_file)
199
+ new_sheet = new_book[0]
200
+ new_sheet[1,1].value.should == @old_cell_value
201
+ new_book.close
202
+ end
203
+
204
+ it "should close the Excel with saving the workbook" do
205
+ @excel.should be_alive
206
+ @book.should be_alive
207
+ @book.saved.should be_false
208
+ @excel.close(:if_unsaved => :save)
209
+ @excel.should_not be_alive
210
+ @book.should_not be_alive
211
+ new_book = Book.open(@simple_file)
212
+ new_sheet = new_book[0]
213
+ new_sheet[1,1].value.should_not == @old_cell_value
214
+ new_book.close
215
+ end
216
+
217
+ it "should raise an error for invalid option" do
218
+ expect {
219
+ @excel.close(:if_unsaved => :invalid_option)
220
+ }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: invalid_option")
221
+ end
222
+
223
+ it "should raise an error by default" do
224
+ @excel.should be_alive
225
+ @book.should be_alive
226
+ @book.saved.should be_false
227
+ expect{
228
+ @excel.close
229
+ }.to raise_error(ExcelErrorClose, "Excel contains unsaved workbooks")
230
+ @excel.should be_alive
231
+ @book.should be_alive
232
+ end
233
+ end
234
+ end
235
+
236
+ # context "with :if_unsaved => :alert" do
237
+ # before do
238
+ # @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
239
+ # end
240
+ #
241
+ # after do
242
+ # @key_sender.close
243
+ # end
244
+ #
245
+ # possible_answers = [:yes, :no, :cancel]
246
+ # possible_answers.each_with_index do |answer, position|
247
+ # it "should" + (answer == :yes ? "" : " not") + " the unsaved book and" + (answer == :cancel ? " not" : "") + " close it" + "if user answers '#{answer}'" do
248
+ # # "Yes" is the default. "No" is right of "Yes", "Cancel" is right of "No" --> language independent
249
+ # @key_sender.puts "{right}" * position + "{enter}"
250
+ # end
251
+ # end
252
+ # end
253
+
254
+
96
255
  describe "==" do
97
256
  before do
98
257
  @excel1 = Excel.create
@@ -236,6 +395,35 @@ module RobustExcelOle
236
395
  end
237
396
  end
238
397
 
398
+ describe "unsaved_workbooks" do
399
+
400
+ context "with standard" do
401
+
402
+ before do
403
+ @book = Book.open(@simple_file)
404
+ @book2 = Book.open(@another_simple_file)
405
+ @book3 = Book.open(@different_file, :read_only => true)
406
+ sheet = @book[0]
407
+ sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
408
+ sheet3 = @book3[0]
409
+ sheet3[1,1] = sheet3[1,1].value == "foo" ? "bar" : "foo"
410
+ end
411
+
412
+ it "should list unsaved workbooks" do
413
+ @book.Saved.should be_false
414
+ @book2.Save
415
+ @book2.Saved.should be_true
416
+ @book3.Saved.should be_false
417
+ excel = @book.excel
418
+ # unsaved_workbooks yields different WIN32OLE objects than book.workbook
419
+ uw_names = []
420
+ excel.unsaved_workbooks.each {|uw| uw_names << uw.Name}
421
+ uw_names.should == [@book.workbook.Name]
422
+ end
423
+
424
+ end
425
+ end
426
+
239
427
  describe "generate workbook" do
240
428
 
241
429
  context "with standard" do
@@ -289,7 +477,9 @@ module RobustExcelOle
289
477
  it "should raise error when book cannot be saved" do
290
478
  expect{
291
479
  workbook = @excel1.generate_workbook(@invalid_name_file)
292
- }.to raise_error(ExcelErrorSaveUnknown)
480
+ # not always Unknown ???? ToDo #*#
481
+ #}.to raise_error(ExcelErrorSaveUnknown)
482
+ }.to raise_error(ExcelErrorSave)
293
483
  end
294
484
 
295
485
  end
@@ -298,11 +488,20 @@ module RobustExcelOle
298
488
 
299
489
  describe "RobustExcelOle" do
300
490
  context "#absolute_path" do
491
+ before do
492
+ @previous_dir = Dir.getwd
493
+ end
494
+
495
+ after do
496
+ Dir.chdir @previous_dir
497
+ end
498
+
301
499
  it "should work" do
302
500
  RobustExcelOle::absolute_path("C:/abc").should == "C:\\abc"
303
501
  RobustExcelOle::absolute_path("C:\\abc").should == "C:\\abc"
304
- RobustExcelOle::absolute_path("C:abc").should == Dir.pwd.gsub("/","\\") + "\\abc"
305
- RobustExcelOle::absolute_path("C:abc").should == File.expand_path("abc").gsub("/","\\")
502
+ Dir.chdir "C:/windows"
503
+ RobustExcelOle::absolute_path("C:abc").downcase.should == Dir.pwd.gsub("/","\\").downcase + "\\abc"
504
+ RobustExcelOle::absolute_path("C:abc").upcase.should == File.expand_path("abc").gsub("/","\\").upcase
306
505
  end
307
506
 
308
507
  it "should return right absolute path name" do
data/spec/range_spec.rb CHANGED
@@ -14,7 +14,7 @@ describe RobustExcelOle::Range do
14
14
  @dir = create_tmpdir
15
15
  @book = RobustExcelOle::Book.open(@dir + '/workbook.xls')
16
16
  @sheet = @book[1]
17
- @range = RobustExcelOle::Range.new(@sheet.sheet.UsedRange.Rows(1))
17
+ @range = RobustExcelOle::Range.new(@sheet.worksheet.UsedRange.Rows(1))
18
18
  end
19
19
 
20
20
  after do
@@ -50,9 +50,9 @@ describe RobustExcelOle::Range do
50
50
  context "when instance is column range" do
51
51
  before do
52
52
  @sheet = @book[0]
53
- @range = RobustExcelOle::Range.new(@sheet.sheet.UsedRange.Columns(1))
53
+ @range = RobustExcelOle::Range.new(@sheet.worksheet.UsedRange.Columns(1))
54
54
  end
55
- it { @range.values.should eq ['simple', 'foo', 'matz'] }
55
+ it { @range.values.should eq ['foo', 'foo', 'matz'] }
56
56
  end
57
57
 
58
58
  context "read 'merge_cells.xls'" do
@@ -67,7 +67,7 @@ describe RobustExcelOle::Range do
67
67
 
68
68
  context "only merged_cell" do
69
69
  before do
70
- @only_merged_range = @merge_cells_sheet.row_range(3)
70
+ @only_merged_range = @merge_cells_sheet.row_range(4)
71
71
  end
72
72
 
73
73
  context "without argument" do
@@ -75,14 +75,14 @@ describe RobustExcelOle::Range do
75
75
  end
76
76
 
77
77
  context "with (1..2)" do
78
- it { @only_merged_range.values(1..2).should eq ['merged', 'merged'] }
78
+ it { @only_merged_range.values(2..3).should eq ['merged', 'merged'] }
79
79
  end
80
80
 
81
81
  end
82
82
 
83
83
  context "mix merged cell and no merge cell" do
84
84
  before do
85
- @mix_merged_no_merged_range = @merge_cells_sheet.row_range(1)
85
+ @mix_merged_no_merged_range = @merge_cells_sheet.row_range(2)
86
86
  end
87
87
 
88
88
  context "without argument" do
data/spec/sheet_spec.rb CHANGED
@@ -35,7 +35,7 @@ describe RobustExcelOle::Sheet do
35
35
  end
36
36
 
37
37
  it "protected sheet can't be write" do
38
- expect { @protected_sheet[0,0] = 'write' }.to raise_error
38
+ expect { @protected_sheet[1,1] = 'write' }.to raise_error
39
39
  end
40
40
  end
41
41
 
@@ -60,32 +60,58 @@ describe RobustExcelOle::Sheet do
60
60
  end
61
61
 
62
62
  describe "#name=" do
63
+
63
64
  it 'change sheet1 name to foo' do
64
65
  @sheet.name = 'foo'
65
66
  @sheet.name.should eq 'foo'
66
67
  end
68
+
69
+ it "should raise error when adding the same name" do
70
+ @sheet.name = 'foo'
71
+ @sheet.name.should eq 'foo'
72
+ new_sheet = @book.add_sheet @sheet
73
+ expect{
74
+ new_sheet.name = 'foo'
75
+ }.to raise_error(ExcelErrorSheet, "sheet name already exists")
76
+ end
67
77
  end
68
78
  end
69
79
 
70
80
  describe 'access cell' do
71
- describe "#[]" do
72
- context "access [0,0]" do
73
- it { @sheet[0, 0].should be_kind_of RobustExcelOle::Cell }
74
- it { @sheet[0, 0].value.should eq 'simple' }
81
+
82
+ describe "#[]" do
83
+
84
+ context "access [1,1]" do
85
+
86
+ it { @sheet[1, 1].should be_kind_of RobustExcelOle::Cell }
87
+ it { @sheet[1, 1].value.should eq 'foo' }
75
88
  end
76
89
 
77
- context "access [0, 0], [0, 1], [2, 0]" do
90
+ context "access [1, 1], [1, 2], [3, 1]" do
78
91
  it "should get every values" do
79
- @sheet[0, 0].value.should eq 'simple'
80
- @sheet[0, 1].value.should eq 'workbook'
81
- @sheet[2, 0].value.should eq 'matz'
92
+ @sheet[1, 1].value.should eq 'foo'
93
+ @sheet[1, 2].value.should eq 'workbook'
94
+ @sheet[3, 1].value.should eq 'matz'
82
95
  end
83
96
  end
97
+
98
+ context "supplying nil as parameter" do
99
+ it "should access [1,1]" do
100
+ @sheet[1, nil].value.should eq 'foo'
101
+ @sheet[nil, 1].value.should eq 'foo'
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ it "change a cell to 'bar'" do
108
+ @sheet[1, 1] = 'bar'
109
+ @sheet[1, 1].value.should eq 'bar'
84
110
  end
85
111
 
86
- it "change a cell to 'foo'" do
87
- @sheet[0, 0] = 'foo'
88
- @sheet[0, 0].value.should eq 'foo'
112
+ it "should change a cell to nil" do
113
+ @sheet[1, 1] = nil
114
+ @sheet[1, 1].value.should eq nil
89
115
  end
90
116
 
91
117
  describe '#each' do
@@ -93,7 +119,7 @@ describe RobustExcelOle::Sheet do
93
119
  @sheet.each_with_index do |cell, i|
94
120
  case i
95
121
  when 0
96
- cell.value.should eq 'simple'
122
+ cell.value.should eq 'foo'
97
123
  when 1
98
124
  cell.value.should eq 'workbook'
99
125
  when 2
@@ -179,7 +205,7 @@ describe RobustExcelOle::Sheet do
179
205
  @sheet.each_row_with_index do |rows, idx|
180
206
  case idx
181
207
  when 0
182
- rows.values.should eq ['simple', 'workbook', 'sheet1']
208
+ rows.values.should eq ['foo', 'workbook', 'sheet1']
183
209
  when 1
184
210
  rows.values.should eq ['foo', nil, 'foobaaa']
185
211
  when 2
@@ -266,9 +292,7 @@ describe RobustExcelOle::Sheet do
266
292
  [nil, nil, 'third', 'merged']
267
293
  ]
268
294
  end
269
-
270
295
  end
271
-
272
296
  end
273
297
 
274
298
  describe "#each_column_with_index" do
@@ -276,7 +300,7 @@ describe RobustExcelOle::Sheet do
276
300
  @sheet.each_column_with_index do |columns, idx|
277
301
  case idx
278
302
  when 0
279
- columns.values.should eq ['simple', 'foo', 'matz']
303
+ columns.values.should eq ['foo', 'foo', 'matz']
280
304
  when 1
281
305
  columns.values.should eq ['workbook', nil, 'is']
282
306
  when 2
@@ -302,7 +326,7 @@ describe RobustExcelOle::Sheet do
302
326
  describe "#row_range" do
303
327
  context "with second argument" do
304
328
  before do
305
- @row_range = @sheet.row_range(0, 1..2)
329
+ @row_range = @sheet.row_range(1, 2..3)
306
330
  end
307
331
 
308
332
  it { @row_range.should be_kind_of RobustExcelOle::Range }
@@ -314,7 +338,7 @@ describe RobustExcelOle::Sheet do
314
338
 
315
339
  context "without second argument" do
316
340
  before do
317
- @row_range = @sheet.row_range(2)
341
+ @row_range = @sheet.row_range(3)
318
342
  end
319
343
 
320
344
  it "should get all cells" do
@@ -327,7 +351,7 @@ describe RobustExcelOle::Sheet do
327
351
  describe "#col_range" do
328
352
  context "with second argument" do
329
353
  before do
330
- @col_range = @sheet.col_range(0, 1..2)
354
+ @col_range = @sheet.col_range(1, 2..3)
331
355
  end
332
356
 
333
357
  it { @col_range.should be_kind_of RobustExcelOle::Range }
@@ -339,7 +363,7 @@ describe RobustExcelOle::Sheet do
339
363
 
340
364
  context "without second argument" do
341
365
  before do
342
- @col_range = @sheet.col_range(1)
366
+ @col_range = @sheet.col_range(2)
343
367
  end
344
368
 
345
369
  it "should get all cells" do
@@ -349,9 +373,11 @@ describe RobustExcelOle::Sheet do
349
373
  end
350
374
 
351
375
  describe "nvalue" do
352
- context "with standard" do
376
+
377
+ context "returning the value of a range" do
378
+
353
379
  before do
354
- @book1 = RobustExcelOle::Book.open(@dir + '/more_workbook.xls', :read_only => true)
380
+ @book1 = RobustExcelOle::Book.open(@dir + '/another_workbook.xls', :read_only => true)
355
381
  @sheet1 = @book1[0]
356
382
  end
357
383
 
@@ -359,28 +385,90 @@ describe RobustExcelOle::Sheet do
359
385
  @book1.close
360
386
  end
361
387
 
362
- it "should return value of a range" do
363
- @sheet1.nvalue("Sheet1!firstcell").should == "simple"
388
+ it "should return value of a range with nvalue and brackets operator" do
389
+ @sheet1.nvalue("firstcell").should == "foo"
390
+ @sheet1["firstcell"].should == "foo"
364
391
  end
365
392
 
366
393
  it "should raise an error if name not defined" do
367
394
  expect {
368
- value = @sheet1.nvalue("Sheet1!foo")
369
- }.to raise_error(SheetErrorNValue, "name Sheet1!foo not in sheet")
395
+ value = @sheet1.nvalue("foo")
396
+ }.to raise_error(SheetError, "name foo not in sheet")
397
+ expect {
398
+ @sheet1["foo"]
399
+ }.to raise_error(SheetError, "name foo not in sheet")
400
+ end
401
+
402
+ it "should return default value if name not defined" do
403
+ @sheet1.nvalue("foo", :default => 2).should == 2
370
404
  end
405
+ end
406
+ end
371
407
 
372
- # fails
373
- #it "should raise an error if name was defined but contents is calcuated" do
374
- # expect {
375
- # value = @sheet1.nvalue("Sheet1!named_formula")
376
- # }.to raise_error(SheetErrorNValue, "range error in sheet")
377
- #end
408
+ describe "set_nvalue" do
409
+
410
+ context "setting the value of a range" do
411
+
412
+ before do
413
+ @book1 = RobustExcelOle::Book.open(@dir + '/another_workbook.xls', :read_only => true)
414
+ @sheet1 = @book1[0]
415
+ end
416
+
417
+ after do
418
+ @book1.close
419
+ end
420
+
421
+ it "should set a range to a value" do
422
+ @sheet1.nvalue("firstcell").should == "foo"
423
+ @sheet1[1,1].Value.should == "foo"
424
+ @sheet1.set_nvalue("firstcell","foo")
425
+ @sheet1.nvalue("firstcell").should == "foo"
426
+ @sheet1[1,1].Value.should == "foo"
427
+ @sheet1["firstcell"] = "bar"
428
+ @sheet1.nvalue("firstcell").should == "bar"
429
+ @sheet1[1,1].Value.should == "bar"
430
+ end
431
+ end
432
+ end
433
+
434
+ describe "set_name" do
435
+
436
+ context "setting the name of a range" do
437
+
438
+ before do
439
+ @book1 = RobustExcelOle::Book.open(@dir + '/another_workbook.xls', :read_only => true, :visible => true)
440
+ @sheet1 = @book1[0]
441
+ end
442
+
443
+ after do
444
+ @book1.close
445
+ end
446
+
447
+ it "should name an unnamed range with a giving address" do
448
+ expect{
449
+ @sheet1[1,2].Name.Name
450
+ }.to raise_error
451
+ @sheet1.set_name("foo",1,2)
452
+ @sheet1[1,2].Name.Name.should == "Sheet1!foo"
453
+ end
454
+
455
+ it "should rename an already named range with a giving address" do
456
+ @sheet1[1,1].Name.Name.should == "Sheet1!firstcell"
457
+ @sheet1.set_name("foo",1,1)
458
+ @sheet1[1,1].Name.Name.should == "Sheet1!foo"
459
+ end
460
+
461
+ it "should raise an error" do
462
+ expect{
463
+ @sheet1.set_name("foo",-2,1)
464
+ }.to raise_error(SheetError, "cannot add name foo to cell with row -2 and column 1")
465
+ end
378
466
  end
379
467
  end
380
468
 
381
469
  describe "#method_missing" do
382
470
  it "can access COM method" do
383
- @sheet.Cells(1,1).Value.should eq 'simple'
471
+ @sheet.Cells(1,1).Value.should eq 'foo'
384
472
  end
385
473
 
386
474
  context "unknown method" do