robust_excel_ole 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,6 @@
2
2
  module RobustExcelOle
3
3
  class Sheet
4
4
  attr_reader :worksheet
5
- include Enumerable
6
5
 
7
6
  def initialize(win32_worksheet)
8
7
  @worksheet = win32_worksheet
@@ -26,9 +25,9 @@ module RobustExcelOle
26
25
  @worksheet.Name = new_name
27
26
  rescue WIN32OLERuntimeError => msg
28
27
  if msg.message =~ /800A03EC/
29
- raise ExcelErrorSheet, "sheet name already exists"
28
+ raise ExcelErrorSheet, "sheet name #{new_name.inspect} already exists"
30
29
  else
31
- puts "#{msg.message}"
30
+ t "#{msg.message}"
32
31
  raise ExcelErrorSheetUnknown
33
32
  end
34
33
  end
@@ -109,17 +108,17 @@ module RobustExcelOle
109
108
  # raise an error, otherwise
110
109
  def nvalue(name, opts = {:default => nil})
111
110
  begin
112
- item = self.Names.Item(name)
111
+ value = self.Evaluate(name)
112
+ value = value.Value if value.class == WIN32OLE
113
113
  rescue WIN32OLERuntimeError
114
114
  return opts[:default] if opts[:default]
115
- raise SheetError, "name #{name} not in sheet"
115
+ raise SheetError, "cannot evaluate name #{name.inspect} in sheet"
116
116
  end
117
- begin
118
- value = item.RefersToRange.Value
119
- rescue WIN32OLERuntimeError
117
+ if value == -2146826259
120
118
  return opts[:default] if opts[:default]
121
- raise SheetError, "RefersToRange of name #{name}"
119
+ raise SheetError, "cannot evaluate name #{name.inspect} in sheet"
122
120
  end
121
+ return opts[:default] if (value.nil? && opts[:default])
123
122
  value
124
123
  end
125
124
 
@@ -127,12 +126,12 @@ module RobustExcelOle
127
126
  begin
128
127
  item = self.Names.Item(name)
129
128
  rescue WIN32OLERuntimeError
130
- raise SheetError, "name #{name} not in sheet"
129
+ raise SheetError, "name #{name.inspect} not in sheet"
131
130
  end
132
131
  begin
133
132
  item.RefersToRange.Value = value
134
133
  rescue WIN32OLERuntimeError
135
- raise SheetError, "RefersToRange of name #{name}"
134
+ raise SheetError, "RefersToRange of name #{name.inspect}"
136
135
  end
137
136
  end
138
137
 
@@ -147,16 +146,30 @@ module RobustExcelOle
147
146
  self.Names.Add("Name" => name, "RefersToR1C1" => "=" + address)
148
147
  end
149
148
  rescue WIN32OLERuntimeError => msg
150
- puts "WIN32OLERuntimeError: #{msg.message}"
151
- raise SheetError, "cannot add name #{name} to cell with row #{row} and column #{column}"
149
+ t "WIN32OLERuntimeError: #{msg.message}"
150
+ raise SheetError, "cannot add name #{name.inspect} to cell with row #{row.inspect} and column #{column.inspect}"
152
151
  end
153
152
  end
154
153
 
155
- def method_missing(id, *args) # :nodoc: #
156
- @worksheet.send(id, *args)
154
+ private
155
+
156
+ def method_missing(name, *args)
157
+ if name.to_s[0,1] =~ /[A-Z]/
158
+ begin
159
+ @worksheet.send(name, *args)
160
+ rescue WIN32OLERuntimeError => msg
161
+ if msg.message =~ /unknown property or method/
162
+ raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
163
+ else
164
+ raise msg
165
+ end
166
+ end
167
+ else
168
+ super
157
169
  end
170
+ end
171
+
158
172
 
159
- private
160
173
  def last_row
161
174
  special_last_row = @worksheet.UsedRange.SpecialCells(RobustExcelOle::XlLastCell).Row
162
175
  used_last_row = @worksheet.UsedRange.Rows.Count
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  end
data/lib/spec_helper.rb CHANGED
@@ -4,7 +4,7 @@ require 'tmpdir'
4
4
  require "fileutils"
5
5
  require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
6
6
 
7
- module RobustExcelOle::SpecHelpers
7
+ module RobustExcelOle::SpecHelpers # :nodoc: #
8
8
  def create_tmpdir # :nodoc: #
9
9
  tmpdir = Dir.mktmpdir
10
10
  FileUtils.cp_r(File.join(File.dirname(__FILE__), 'data'), tmpdir)
data/reo.bat CHANGED
@@ -1,3 +1,3 @@
1
1
  @echo off
2
2
 
3
- irb -r lib/robust_excel_ole
3
+ irb -f -r lib/robust_excel_ole -r lib/reo_console.rb
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
+
5
+
6
+ $VERBOSE = nil
7
+
8
+ include RobustExcelOle
9
+
10
+ unless Object.method_defined?(:require_relative)
11
+ def require_relative path
12
+ require File.expand_path(path, File.dirname(__FILE__))
13
+ end
14
+ end
15
+
16
+ require_relative "book_open_spec"
17
+ require_relative "book_close_spec"
18
+ require_relative "book_save_spec"
19
+ require_relative "book_misc_spec"
20
+ require_relative "book_sheet_spec"
21
+ require_relative "book_unobtr_spec"
22
+ require_relative "book_subclass_spec"
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -80,21 +80,21 @@ describe Book do
80
80
  it "should raise error with option :raise" do
81
81
  expect{
82
82
  @book.close(:if_unsaved => :raise)
83
- }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
83
+ }.to raise_error(ExcelErrorClose, /workbook is unsaved: "workbook.xls"/)
84
84
  end
85
85
 
86
86
  it "should raise error by default" do
87
87
  expect{
88
88
  @book.close(:if_unsaved => :raise)
89
- }.to raise_error(ExcelErrorClose, "book is unsaved (#{File.basename(@simple_file)})")
89
+ }.to raise_error(ExcelErrorClose, /workbook is unsaved: "workbook.xls"/)
90
90
  end
91
91
 
92
92
  it "should close the book and leave its file untouched with option :forget" do
93
93
  ole_workbook = @book.workbook
94
94
  excel = @book.excel
95
- expect {
96
- @book.close(:if_unsaved => :forget)
97
- }.to change {excel.Workbooks.Count }.by(-1)
95
+ excel.Workbooks.Count.should == 1
96
+ @book.close(:if_unsaved => :forget)
97
+ excel.Workbooks.Count.should == 0
98
98
  @book.workbook.should == nil
99
99
  @book.should_not be_alive
100
100
  expect{
@@ -110,16 +110,16 @@ describe Book do
110
110
  it "should raise an error for invalid option" do
111
111
  expect {
112
112
  @book.close(:if_unsaved => :invalid_option)
113
- }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: invalid_option")
113
+ }.to raise_error(ExcelErrorClose, ":if_unsaved: invalid option: :invalid_option")
114
114
  end
115
115
 
116
116
 
117
117
  it "should save the book before close with option :save" do
118
118
  ole_workbook = @book.workbook
119
119
  excel = @book.excel
120
- expect {
121
- @book.close(:if_unsaved => :save)
122
- }.to change {excel.Workbooks.Count }.by(-1)
120
+ excel.Workbooks.Count.should == 1
121
+ @book.close(:if_unsaved => :save)
122
+ excel.Workbooks.Count.should == 0
123
123
  @book.workbook.should == nil
124
124
  @book.should_not be_alive
125
125
  expect{
@@ -134,7 +134,7 @@ describe Book do
134
134
 
135
135
  context "with :if_unsaved => :alert" do
136
136
  before do
137
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
137
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Excel" ' , "w"
138
138
  end
139
139
 
140
140
  after do
@@ -157,9 +157,9 @@ describe Book do
157
157
  @book.workbook.should_not == nil
158
158
  @book.should be_alive
159
159
  else
160
- expect {
161
- @book.close(:if_unsaved => :alert)
162
- }.to change {@book.excel.Workbooks.Count }.by(-1)
160
+ @book.excel.Workbooks.Count.should == 1
161
+ @book.close(:if_unsaved => :alert)
162
+ @book.excel.Workbooks.Count.should == 0
163
163
  @book.workbook.should == nil
164
164
  @book.should_not be_alive
165
165
  expect{ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -63,7 +63,19 @@ describe Book do
63
63
  it "should send Fullname to workbook" do
64
64
  @book.Fullname.tr('\\','/').should == @simple_file
65
65
  end
66
+
67
+ it "should raise an error for unknown methods or properties" do
68
+ expect{
69
+ @book.Foo
70
+ }.to raise_error(VBAMethodMissingError, /unknown VBA property or method :Foo/)
71
+ end
72
+
73
+ it "should report that workbook is not alive" do
74
+ @book.close
75
+ expect{ @book.Nonexisting_method }.to raise_error(ExcelError, "method missing: workbook not alive")
76
+ end
66
77
  end
78
+
67
79
  end
68
80
 
69
81
  describe "hidden_excel" do
@@ -117,24 +129,19 @@ describe Book do
117
129
  it "should raise an error if name not defined" do
118
130
  expect {
119
131
  @book1.nvalue("foo")
120
- }.to raise_error(ExcelErrorNValue, "name foo not in another_workbook.xls")
132
+ }.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
121
133
  expect {
122
134
  @book1["foo"]
123
- }.to raise_error(ExcelErrorNValue, "name foo not in another_workbook.xls")
135
+ }.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
124
136
  end
125
137
 
126
- it "should raise an error if name was defined but contents is calcuated" do
127
- expect {
128
- @book1.nvalue("named_formula")
129
- }.to raise_error(ExcelErrorNValue, "RefersToRange error of name named_formula in another_workbook.xls")
130
- expect {
131
- @book1["named_formula"]
132
- }.to raise_error(ExcelErrorNValue, "RefersToRange error of name named_formula in another_workbook.xls")
138
+ it "should evaluate a formula" do
139
+ @book1.nvalue("named_formula").should == 4
140
+ @book1["named_formula"].should == 4
133
141
  end
134
142
 
135
143
  it "should return default value if name not defined" do
136
144
  @book1.nvalue("foo", :default => 2).should == 2
137
- @book1.nvalue("named_formula", :default => 4).should == 4
138
145
  end
139
146
  end
140
147
 
@@ -157,19 +164,19 @@ describe Book do
157
164
  it "should raise an error if name not defined" do
158
165
  expect {
159
166
  @book1.set_nvalue("foo","bar")
160
- }.to raise_error(ExcelErrorNValue, "name foo not in another_workbook.xls")
167
+ }.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
161
168
  expect {
162
169
  @book1["foo"] = "bar"
163
- }.to raise_error(ExcelErrorNValue, "name foo not in another_workbook.xls")
170
+ }.to raise_error(ExcelError, /name "foo" not in "another_workbook.xls"/)
164
171
  end
165
172
 
166
173
  it "should raise an error if name was defined but contents is calcuated" do
167
174
  expect {
168
175
  @book1.set_nvalue("named_formula","bar")
169
- }.to raise_error(ExcelErrorNValue, "RefersToRange error of name named_formula in another_workbook.xls")
176
+ }.to raise_error(ExcelError, /RefersToRange error of name "named_formula" in "another_workbook.xls"/)
170
177
  expect {
171
178
  @book1["named_formula"] = "bar"
172
- }.to raise_error(ExcelErrorNValue, "RefersToRange error of name named_formula in another_workbook.xls")
179
+ }.to raise_error(ExcelError, /RefersToRange error of name "named_formula" in "another_workbook.xls"/)
173
180
  end
174
181
 
175
182
  it "should set value of a range" do
@@ -194,7 +201,7 @@ describe Book do
194
201
  @book1.nvalue("five").should == [[1,2],[3,4]]
195
202
  expect {
196
203
  @book1.rename_range("four","five")
197
- }.to raise_error(ExcelError, "name four not in another_workbook.xls")
204
+ }.to raise_error(ExcelError, /name "four" not in "another_workbook.xls"/)
198
205
  end
199
206
  end
200
207
  end
@@ -265,12 +272,12 @@ describe Book do
265
272
  end
266
273
 
267
274
  it "should be false with same book names but different paths" do
268
- @new_book = Book.new(@simple_file_other_path, :excel => :new)
275
+ @new_book = Book.new(@simple_file_other_path, :force_excel => :new)
269
276
  @new_book.should_not == @book
270
277
  end
271
278
 
272
279
  it "should be false with same book names but different excel instances" do
273
- @new_book = Book.new(@simple_file, :excel => :new)
280
+ @new_book = Book.new(@simple_file, :force_excel => :new)
274
281
  @new_book.should_not == @book
275
282
  end
276
283
 
@@ -302,7 +309,7 @@ describe Book do
302
309
  end
303
310
  end
304
311
 
305
- context "with visible and displayalerts" do
312
+ context "with visible" do
306
313
 
307
314
  before do
308
315
  @book = Book.open(@simple_file)
@@ -312,25 +319,27 @@ describe Book do
312
319
  @book.close
313
320
  end
314
321
 
315
- it "should make Excel visible" do
316
- @book.excel.visible = false
317
- @book.excel.visible.should be_false
322
+ it "should make the workbook visible" do
318
323
  @book.excel.visible = true
319
324
  @book.excel.visible.should be_true
325
+ @book.visible.should be_true
326
+ @book.excel.Windows(@book.workbook.Name).Visible.should be_true
327
+ @book.visible = false
328
+ @book.excel.visible.should be_true
329
+ @book.visible.should be_false
330
+ @book.excel.Windows(@book.workbook.Name).Visible.should be_false
331
+ @book.visible = true
332
+ @book.excel.visible.should be_true
333
+ @book.visible.should be_true
334
+ @book.excel.Windows(@book.workbook.Name).Visible.should be_true
320
335
  end
321
336
 
322
- it "should enable DisplayAlerts in Excel" do
323
- @book.excel.displayalerts = false
324
- @book.excel.displayalerts.should be_false
325
- @book.excel.displayalerts = true
326
- @book.excel.displayalerts.should be_true
327
- end
328
337
  end
329
338
 
330
339
  context "with activate" do
331
340
 
332
341
  before do
333
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
342
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
334
343
  @book = Book.open(@simple_file, :visible => true)
335
344
  @book2 = Book.open(@another_simple_file, :force_excel => :new, :visible => true)
336
345
  end
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- require File.join(File.dirname(__FILE__), './spec_helper')
3
+ require File.join(File.dirname(__FILE__), './../spec_helper')
4
4
 
5
5
 
6
6
  $VERBOSE = nil
@@ -13,7 +13,8 @@ describe Book do
13
13
  excel = Excel.new(:reuse => true)
14
14
  open_books = excel == nil ? 0 : excel.Workbooks.Count
15
15
  puts "*** open books *** : #{open_books}" if open_books > 0
16
- Excel.close_all
16
+ #Excel.close_all
17
+ Excel.kill_all
17
18
  end
18
19
 
19
20
  before do
@@ -29,7 +30,8 @@ describe Book do
29
30
  end
30
31
 
31
32
  after do
32
- Excel.close_all
33
+ Excel.kill_all
34
+ #Excel.close_all
33
35
  rm_tmp(@dir)
34
36
  end
35
37
 
@@ -229,7 +231,7 @@ describe Book do
229
231
  @book.close
230
232
  expect{
231
233
  Book.open(@simple_file, :force_excel => :book)
232
- }.to raise_error(ExcelErrorOpen, "provided instance is neither an Excel nor a Book")
234
+ }.to raise_error(ExcelError, "receiver instance is neither an Excel nor a Book")
233
235
  end
234
236
 
235
237
  it "should do force_excel even if both force_ and default_excel is given" do
@@ -396,7 +398,7 @@ describe Book do
396
398
  it "should raise an error, if :if_unsaved is :raise" do
397
399
  expect {
398
400
  @new_book = Book.open(@simple_file, :if_unsaved => :raise)
399
- }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
401
+ }.to raise_error(ExcelErrorOpen, /workbook is already open but not saved: "workbook.xls"/)
400
402
  end
401
403
 
402
404
  it "should let the book open, if :if_unsaved is :accept" do
@@ -417,7 +419,7 @@ describe Book do
417
419
 
418
420
  context "with :if_unsaved => :alert" do
419
421
  before do
420
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
422
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '../helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
421
423
  end
422
424
 
423
425
  after do
@@ -459,13 +461,13 @@ describe Book do
459
461
  it "should raise an error, if :if_unsaved is default" do
460
462
  expect {
461
463
  @new_book = Book.open(@simple_file, :if_unsaved => :raise)
462
- }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
464
+ }.to raise_error(ExcelErrorOpen, /workbook is already open but not saved: "workbook.xls"/)
463
465
  end
464
466
 
465
467
  it "should raise an error, if :if_unsaved is invalid option" do
466
468
  expect {
467
469
  @new_book = Book.open(@simple_file, :if_unsaved => :invalid_option)
468
- }.to raise_error(ExcelErrorOpen, ":if_unsaved: invalid option: invalid_option")
470
+ }.to raise_error(ExcelErrorOpen, ":if_unsaved: invalid option: :invalid_option")
469
471
  end
470
472
  end
471
473
 
@@ -494,7 +496,7 @@ describe Book do
494
496
  it "should raise an error, if :if_obstructed is :raise" do
495
497
  expect {
496
498
  @new_book = Book.open(@simple_file, :if_obstructed => :raise)
497
- }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path: workbook.xls")
499
+ }.to raise_error(ExcelErrorOpen, /blocked by a book with the same name in a different path: "workbook.xls"/)
498
500
  end
499
501
 
500
502
  it "should close the other book and open the new book, if :if_obstructed is :forget" do
@@ -518,7 +520,7 @@ describe Book do
518
520
  if :if_obstructed is :close_if_saved" do
519
521
  expect{
520
522
  @new_book = Book.open(@simple_file, :if_obstructed => :close_if_saved)
521
- }.to raise_error(ExcelErrorOpen, "book with the same name in a different path is unsaved: workbook.xls")
523
+ }.to raise_error(ExcelErrorOpen, /workbook with the same name in a different path is unsaved: "workbook.xls"/)
522
524
  @book.save
523
525
  @new_book = Book.open(@simple_file, :if_obstructed => :close_if_saved)
524
526
  @book.should_not be_alive
@@ -539,14 +541,14 @@ describe Book do
539
541
 
540
542
  it "should raise an error, if :if_obstructed is default" do
541
543
  expect {
542
- @new_book = Book.open(@simple_file)
543
- }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path: workbook.xls")
544
+ @new_book = Book.open(@simple_file)
545
+ }.to raise_error(ExcelErrorOpen, /blocked by a book with the same name in a different path: "workbook.xls"/)
544
546
  end
545
547
 
546
548
  it "should raise an error, if :if_obstructed is invalid option" do
547
549
  expect {
548
550
  @new_book = Book.open(@simple_file, :if_obstructed => :invalid_option)
549
- }.to raise_error(ExcelErrorOpen, ":if_obstructed: invalid option: invalid_option")
551
+ }.to raise_error(ExcelErrorOpen, ":if_obstructed: invalid option: :invalid_option")
550
552
  end
551
553
  end
552
554
  end
@@ -590,7 +592,7 @@ describe Book do
590
592
  File.delete @simple_save_file rescue nil
591
593
  expect {
592
594
  Book.open(@simple_save_file, :if_absent => :raise)
593
- }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
595
+ }.to raise_error(ExcelErrorOpen, "file \"#{@simple_save_file}\" not found")
594
596
  end
595
597
 
596
598
  it "should create a workbook" do
@@ -605,7 +607,7 @@ describe Book do
605
607
  File.delete @simple_save_file rescue nil
606
608
  expect {
607
609
  Book.open(@simple_save_file)
608
- }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
610
+ }.to raise_error(ExcelErrorOpen, "file \"#{@simple_save_file}\" not found")
609
611
  end
610
612
 
611
613
  end
@@ -761,7 +763,7 @@ describe Book do
761
763
  expected_path = Regexp.new(File.expand_path(path).gsub(/\//, "."))
762
764
  expect {
763
765
  Book.open(path)
764
- }.to raise_error(ExcelErrorOpen, "file #{path} not found")
766
+ }.to raise_error(ExcelErrorOpen, "file \"#{path}\" not found")
765
767
  end
766
768
  end
767
769
  end
@@ -790,4 +792,26 @@ describe Book do
790
792
  end
791
793
  end
792
794
  end
795
+
796
+ describe "uplifting" do
797
+
798
+ context "with standard" do
799
+
800
+ before do
801
+ @book = Book.open(@simple_file)
802
+ end
803
+
804
+ after do
805
+ @book.close
806
+ end
807
+
808
+ it "should uplift a workbook to a book with an open book" do
809
+ workbook = @book.workbook
810
+ book1 = Book.new(workbook)
811
+ book1.should be_a Book
812
+ book1.should be_alive
813
+ book1.should == @book
814
+ end
815
+ end
816
+ end
793
817
  end