robust_excel_ole 1.1.3 → 1.1.4

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.
@@ -9,7 +9,7 @@ module General
9
9
  end
10
10
 
11
11
  def canonize(filename) # :nodoc: #
12
- raise TypeErrorREO, "No string given to canonize, but #{filename.inspect}" unless filename.is_a?(String)
12
+ raise TypeREOError, "No string given to canonize, but #{filename.inspect}" unless filename.is_a?(String)
13
13
  normalize(filename).downcase
14
14
  end
15
15
 
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
- LOG_TO_STDOUT = true unless Object.const_defined?(:LOG_TO_STDOUT)
4
- REO_LOG_DIR = "" unless Object.const_defined?(:REO_LOG_DIR)
3
+ LOG_TO_STDOUT = true unless Object.const_defined?(:LOG_TO_STDOUT)
4
+ REO_LOG_DIR = "" unless Object.const_defined?(:REO_LOG_DIR)
5
5
  REO_LOG_FILE = "reo.log" unless Object.const_defined?(:REO_LOG_FILE)
6
6
 
7
7
  File.delete REO_LOG_FILE rescue nil
@@ -11,76 +11,76 @@ module RobustExcelOle
11
11
  class REOError < RuntimeError # :nodoc: #
12
12
  end
13
13
 
14
- class ExcelError < REOError # :nodoc: #
14
+ class ExcelREOError < REOError # :nodoc: #
15
15
  end
16
16
 
17
- class WorkbookError < REOError # :nodoc: #
17
+ class WorkbookREOError < REOError # :nodoc: #
18
18
  end
19
19
 
20
- class FileError < REOError # :nodoc: #
20
+ class FileREOError < REOError # :nodoc: #
21
21
  end
22
22
 
23
- class NamesError < REOError # :nodoc: #
23
+ class NamesREOError < REOError # :nodoc: #
24
24
  end
25
25
 
26
- class MiscError < REOError # :nodoc: #
26
+ class MiscREOError < REOError # :nodoc: #
27
27
  end
28
28
 
29
- class ExcelDamaged < ExcelError # :nodoc: #
29
+ class ExcelDamaged < ExcelREOError # :nodoc: #
30
30
  end
31
31
 
32
- class UnsavedWorkbooks < ExcelError # :nodoc: #
32
+ class UnsavedWorkbooks < ExcelREOError # :nodoc: #
33
33
  end
34
34
 
35
- class WorkbookBlocked < WorkbookError # :nodoc: #
35
+ class WorkbookBlocked < WorkbookREOError # :nodoc: #
36
36
  end
37
37
 
38
- class WorkbookNotSaved < WorkbookError # :nodoc: #
38
+ class WorkbookNotSaved < WorkbookREOError # :nodoc: #
39
39
  end
40
40
 
41
- class WorkbookReadOnly < WorkbookError # :nodoc: #
41
+ class WorkbookReadOnly < WorkbookREOError # :nodoc: #
42
42
  end
43
43
 
44
- class WorkbookBeingUsed < WorkbookError # :nodoc: #
44
+ class WorkbookBeingUsed < WorkbookREOError # :nodoc: #
45
45
  end
46
46
 
47
- class FileNotFound < FileError # :nodoc: #
47
+ class FileNotFound < FileREOError # :nodoc: #
48
48
  end
49
49
 
50
- class FileNameNotGiven < FileError # :nodoc: #
50
+ class FileNameNotGiven < FileREOError # :nodoc: #
51
51
  end
52
52
 
53
- class FileAlreadyExists < FileError # :nodoc: #
53
+ class FileAlreadyExists < FileREOError # :nodoc: #
54
54
  end
55
55
 
56
- class NameNotFound < NamesError # :nodoc: #
56
+ class NameNotFound < NamesREOError # :nodoc: #
57
57
  end
58
58
 
59
- class NameAlreadyExists < NamesError # :nodoc: #
59
+ class NameAlreadyExists < NamesREOError # :nodoc: #
60
60
  end
61
61
 
62
- class RangeNotEvaluatable < MiscError # :nodoc: #
62
+ class RangeNotEvaluatable < MiscREOError # :nodoc: #
63
63
  end
64
64
 
65
- class OptionInvalid < MiscError # :nodoc: #
65
+ class OptionInvalid < MiscREOError # :nodoc: #
66
66
  end
67
67
 
68
- class ObjectNotAlive < MiscError # :nodoc: #
68
+ class ObjectNotAlive < MiscREOError # :nodoc: #
69
69
  end
70
70
 
71
- class TypeErrorREO < REOError # :nodoc: #
71
+ class TypeREOError < REOError # :nodoc: #
72
72
  end
73
73
 
74
- class TimeOut < REOError # :nodoc: #
74
+ class TimeOut < REOError # :nodoc: #
75
75
  end
76
76
 
77
- class UnexpectedError < REOError # :nodoc: #
77
+ class UnexpectedREOError < REOError # :nodoc: #
78
78
  end
79
79
 
80
80
  class REOCommon
81
81
 
82
82
  def excel
83
- raise TypeErrorREO, "receiver instance is neither an Excel nor a Book"
83
+ raise TypeREOError, "receiver instance is neither an Excel nor a Book"
84
84
  end
85
85
 
86
86
  def own_methods
@@ -126,31 +126,41 @@ module RobustExcelOle
126
126
  class RangeOwners < REOCommon
127
127
 
128
128
  # returns the contents of a range with given name
129
- # evaluates formula contents of the range is a formula
130
- # if no contents could be returned, then return default value, if provided, raise error otherwise
131
- # Excel Bug: if a local name without a qualifier is given, then by default Excel takes the first worksheet,
132
- # even if a different worksheet is active
129
+ # if the name could not be found or the value could not be determined,
130
+ # then return default value, if provided, raise error otherwise
131
+ # Excel Bug: if a local name without a qualifier is given,
132
+ # then by default Excel takes the first worksheet,
133
+ # even if a different worksheet is active
133
134
  # @param [String] name the name of the range
134
135
  # @param [Hash] opts the options
135
136
  # @option opts [Symbol] :default the default value that is provided if no contents could be returned
136
137
  # @return [Variant] the contents of a range with given name
137
- def nameval(name, opts = {:default => nil})
138
- name_obj = name_object(name)
138
+ def nameval(name, opts = {:default => :__not_provided})
139
+ name_obj = begin
140
+ name_object(name)
141
+ rescue NameNotFound => msg
142
+ return opts[:default] unless opts[:default] == :__not_provided
143
+ raise
144
+ end
139
145
  value = begin
140
146
  name_obj.RefersToRange.Value
141
- rescue WIN32OLERuntimeError
142
- #begin
143
- # self.sheet(1).Evaluate(name_obj.Name)
144
- #rescue WIN32OLERuntimeError
145
- return opts[:default] if opts[:default]
146
- raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{self}"
147
- #end
147
+ rescue WIN32OLERuntimeError
148
+ sheet = if self.is_a?(Sheet) then self
149
+ elsif self.is_a?(Book) then self.sheet(1)
150
+ elsif self.is_a?(Excel) then self.workbook.sheet(1)
151
+ end
152
+ begin
153
+ sheet.Evaluate(name_obj.Name).Value
154
+ rescue # WIN32OLERuntimeError
155
+ return opts[:default] unless opts[:default] == :__not_provided
156
+ raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{self}"
157
+ end
148
158
  end
149
- if value.is_a?(Bignum) #RobustExcelOle::XlErrName
150
- return opts[:default] if opts[:default]
159
+ if value == -2146828288 + RobustExcelOle::XlErrName
160
+ return opts[:default] unless opts[:default] == __not_provided
151
161
  raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(workbook.stored_filename).inspect rescue nil}"
152
162
  end
153
- return opts[:default] if opts[:default] && value.nil?
163
+ return opts[:default] unless opts[:default] == :__not_provided or value.nil?
154
164
  value
155
165
  end
156
166
 
@@ -172,27 +182,33 @@ module RobustExcelOle
172
182
 
173
183
  # returns the contents of a range with a locally defined name
174
184
  # evaluates the formula if the contents is a formula
175
- # if no contents could be returned, then return default value, if provided, raise error otherwise
185
+ # if the name could not be found or the range or value could not be determined,
186
+ # then return default value, if provided, raise error otherwise
176
187
  # @param [String] name the name of a range
177
188
  # @param [Hash] opts the options
178
189
  # @option opts [Symbol] :default the default value that is provided if no contents could be returned
179
190
  # @return [Variant] the contents of a range with given name
180
- def rangeval(name, opts = {:default => nil})
191
+ def rangeval(name, opts = {:default => :__not_provided})
181
192
  begin
182
193
  range = self.Range(name)
183
194
  rescue WIN32OLERuntimeError
184
- return opts[:default] if opts[:default]
195
+ return opts[:default] unless opts[:default] == :__not_provided
185
196
  raise NameNotFound, "name #{name.inspect} not in #{self.inspect}"
186
197
  end
187
198
  begin
188
199
  value = range.Value
189
200
  rescue WIN32OLERuntimeError
190
- return opts[:default] if opts[:default]
201
+ return opts[:default] unless opts[:default] == :__not_provided
191
202
  raise RangeNotEvaluatable, "cannot determine value of range named #{name.inspect} in #{self.inspect}"
192
203
  end
193
- return opts[:default] if (value.nil? && opts[:default])
194
- raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect}" if value.is_a?(Bignum)
195
- value
204
+ if value == -2146828288 + RobustExcelOle::XlErrName
205
+ return opts[:default] unless opts[:default] == __not_provided
206
+ raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(workbook.stored_filename).inspect rescue nil}"
207
+ end
208
+ return opts[:default] unless opts[:default] == :__not_provided or value.nil?
209
+ value
210
+ #return opts[:default] unless opts[:default] == :__not_provided
211
+ #raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect}" if value == -2146828288 + RobustExcelOle::XlErrName
196
212
  end
197
213
 
198
214
  # assigns a value to a range given a locally defined name
@@ -228,7 +244,7 @@ module RobustExcelOle
228
244
  end
229
245
  end
230
246
  end
231
-
247
+
232
248
  def cell_modified?(cell)
233
249
  workbook.modified_cells.each{|c| return true if c.Name.Value == cell.Name.Value}
234
250
  false
@@ -35,7 +35,7 @@ module RobustExcelOle
35
35
  if msg.message =~ /800A03EC/
36
36
  raise NameAlreadyExists, "sheet name #{new_name.inspect} already exists"
37
37
  else
38
- raise UnexpectedError, "unexpected WIN32OLERuntimeError: #{msg.message}"
38
+ raise UnexpectedREOError, "unexpected WIN32OLERuntimeError: #{msg.message}"
39
39
  end
40
40
  end
41
41
  end
@@ -57,11 +57,7 @@ module RobustExcelOle
57
57
  begin
58
58
  nameval(name)
59
59
  rescue REOError
60
- begin
61
- book_class.new(self.Parent).nameval(name)
62
- rescue REOError
63
- rangeval(name)
64
- end
60
+ rangeval(name)
65
61
  end
66
62
  end
67
63
  end
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "1.1.3"
2
+ VERSION = "1.1.4"
3
3
  end
data/spec/book_spec.rb CHANGED
@@ -41,7 +41,7 @@ describe Book do
41
41
  context "with standard" do
42
42
  it "open an existing file" do
43
43
  expect {
44
- @book = Book.new(@simple_file)
44
+ @book = Book.open(@simple_file)
45
45
  }.to_not raise_error
46
46
  @book.should be_a Book
47
47
  @book.close
@@ -400,7 +400,7 @@ describe Book do
400
400
  @key_sender.puts "{right}{enter}"
401
401
  #expect{
402
402
  # Book.open(@simple_file, :if_unsaved => :alert)
403
- # }.to raise_error(ExcelError, /user canceled or runtime error/)
403
+ # }.to raise_error(ExcelREOError, /user canceled or runtime error/)
404
404
  @book.should be_alive
405
405
  end
406
406
 
@@ -422,7 +422,7 @@ describe Book do
422
422
  @key_sender.puts "{right}{enter}"
423
423
  #expect{
424
424
  # Book.open(@simple_file, :if_unsaved => :excel)
425
- # }.to raise_error(ExcelError, /user canceled or runtime error/)
425
+ # }.to raise_error(ExcelREOError, /user canceled or runtime error/)
426
426
  @book.should be_alive
427
427
  end
428
428
 
@@ -653,8 +653,8 @@ describe Book do
653
653
  begin
654
654
  @excel1.close
655
655
  @excel2.close
656
- rescue ExcelError => msg
657
- # puts "ExcelError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
656
+ rescue ExcelREOError => msg
657
+ # puts "ExcelREOError: #{msg.message}" if msg.message =~ /Excel instance not alive or damaged/
658
658
  end
659
659
  end
660
660
 
@@ -47,6 +47,65 @@ describe Book do
47
47
  end
48
48
  end
49
49
 
50
+ describe "for_this_workbook" do
51
+
52
+ before do
53
+ @book = Book.open(@simple_file)
54
+ end
55
+
56
+ it "should set options" do
57
+ @book.for_this_workbook(:visible => true)
58
+ @book.excel.Visible.should be_true
59
+ @book.Windows(@book.Name).Visible.should be_true
60
+ @book.visible.should be_true
61
+ @book.ReadOnly.should be_false
62
+ @book.CheckCompatibility.should be_false
63
+ @book.for_this_workbook(:visible => false)
64
+ @book.excel.Visible.should be_true
65
+ @book.Windows(@book.Name).Visible.should be_false
66
+ @book.visible.should be_false
67
+ @book.ReadOnly.should be_false
68
+ @book.CheckCompatibility.should be_false
69
+ @book.for_this_workbook(:read_only => true)
70
+ @book.excel.Visible.should be_true
71
+ @book.Windows(@book.Name).Visible.should be_false
72
+ @book.visible.should be_false
73
+ @book.ReadOnly.should be_true
74
+ @book.CheckCompatibility.should be_false
75
+ @book.for_this_workbook(:visible => true)
76
+ @book.excel.Visible.should be_true
77
+ @book.Windows(@book.Name).Visible.should be_true
78
+ @book.visible.should be_true
79
+ @book.ReadOnly.should be_true
80
+ @book.CheckCompatibility.should be_false
81
+ @book.for_this_workbook(:check_compatibility => true)
82
+ @book.excel.Visible.should be_true
83
+ @book.Windows(@book.Name).Visible.should be_true
84
+ @book.visible.should be_true
85
+ @book.ReadOnly.should be_true
86
+ @book.CheckCompatibility.should be_true
87
+ @book.for_this_workbook(:visible => false, :check_compatibility => false)
88
+ @book.excel.Visible.should be_true
89
+ @book.Windows(@book.Name).Visible.should be_false
90
+ @book.visible.should be_false
91
+ @book.ReadOnly.should be_true
92
+ @book.CheckCompatibility.should be_false
93
+ @book.for_this_workbook(:calculation => true)
94
+ @book.excel.Visible.should be_true
95
+ @book.Windows(@book.Name).Visible.should be_false
96
+ @book.visible.should be_false
97
+ @book.ReadOnly.should be_true
98
+ @book.CheckCompatibility.should be_false
99
+ @book.excel.calculation.should be_true
100
+ end
101
+
102
+ it "should set options" do
103
+ @book.for_this_workbook(:read_only => true, :check_compatibility => true)
104
+ @book.CheckCompatibility.should be_true
105
+ end
106
+
107
+ end
108
+
50
109
  describe "excel_of" do
51
110
 
52
111
  before do
@@ -654,16 +713,25 @@ describe Book do
654
713
  # @book1["named_formula"].should == 4
655
714
  #end
656
715
 
657
- it "should raise an error if name not defined" do
716
+ it "should raise an error if name not defined and default value is not provided" do
717
+ expect {
718
+ @book1.nameval("foo", :default => nil)
719
+ }.to_not raise_error
720
+ expect {
721
+ @book1.nameval("foo", :default => :__not_provided)
722
+ }.to raise_error(NameNotFound, /name "foo" not in #<Book: another_workbook/)
658
723
  expect {
659
724
  @book1.nameval("foo")
660
725
  }.to raise_error(NameNotFound, /name "foo" not in #<Book: another_workbook/)
726
+ @book1.nameval("foo", :default => nil).should be_nil
727
+ @book1.nameval("foo", :default => 1).should == 1
661
728
  expect {
662
729
  @book1.set_nameval("foo","bar")
663
730
  }.to raise_error(NameNotFound, /name "foo" not in #<Book: another_workbook/)
664
731
  expect {
665
732
  @book1["foo"] = "bar"
666
733
  }.to raise_error(NameNotFound, /name "foo" not in #<Book: another_workbook/)
734
+ @book1.nameval("empty", :default => 1).should be_nil
667
735
  end
668
736
 
669
737
  it "should raise an error if name was defined but contents is calcuated" do
@@ -379,7 +379,7 @@ describe Book do
379
379
  it "should raise an error if no Excel or Book is given" do
380
380
  expect{
381
381
  Book.open(@simple_file1, :force => {:excel => :b})
382
- }.to raise_error(TypeErrorREO, "given object is neither an Excel, a Workbook, nor a Win32ole")
382
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
383
383
  end
384
384
 
385
385
  it "should do force_excel even if both force_ and default_excel is given" do
@@ -537,7 +537,7 @@ describe Book do
537
537
  it "should raise an error if no Excel or Book is given" do
538
538
  expect{
539
539
  Book.open(@simple_file1, :force_excel => :b)
540
- }.to raise_error(TypeErrorREO, "given object is neither an Excel, a Workbook, nor a Win32ole")
540
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
541
541
  end
542
542
 
543
543
  it "should do force_excel even if both force_ and default_excel is given" do
@@ -766,7 +766,7 @@ describe Book do
766
766
  it "should raise an error if no Excel or Book is given" do
767
767
  expect{
768
768
  Book.open(@different_file, :default => {:excel => :a})
769
- }.to raise_error(TypeErrorREO, "given object is neither an Excel, a Workbook, nor a Win32ole")
769
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
770
770
  end
771
771
 
772
772
  end
@@ -948,7 +948,7 @@ describe Book do
948
948
  it "should raise an error if no Excel or Book is given" do
949
949
  expect{
950
950
  Book.open(@different_file, :default_excel => :a)
951
- }.to raise_error(TypeErrorREO, "given object is neither an Excel, a Workbook, nor a Win32ole")
951
+ }.to raise_error(TypeREOError, "given object is neither an Excel, a Workbook, nor a Win32ole")
952
952
  end
953
953
 
954
954
  end
@@ -1240,7 +1240,7 @@ describe Book do
1240
1240
  @key_sender.puts "{right}{enter}"
1241
1241
  expect{
1242
1242
  Book.open(@simple_file1, :if_unsaved => :alert)
1243
- }.to raise_error(ExcelError, "user canceled or runtime error")
1243
+ }.to raise_error(ExcelREOError, "user canceled or runtime error")
1244
1244
  @book.should be_alive
1245
1245
  end
1246
1246
 
@@ -1274,7 +1274,7 @@ describe Book do
1274
1274
  @key_sender.puts "{right}{enter}"
1275
1275
  expect{
1276
1276
  Book.open(@simple_file1, :if_unsaved => :excel)
1277
- }.to raise_error(ExcelError, "user canceled or runtime error")
1277
+ }.to raise_error(ExcelREOError, "user canceled or runtime error")
1278
1278
  @book.should be_alive
1279
1279
  end
1280
1280
  =end
@@ -1407,7 +1407,7 @@ describe Book do
1407
1407
  context "with :if_unsaved => #{options_value} and in the same and different path" do
1408
1408
  before do
1409
1409
  @new_book = Book.open(@simple_file1, :reuse => true, :if_unsaved => options_value)
1410
- @different_book = Book.new(@different_file, :reuse => true, :if_unsaved => options_value)
1410
+ @different_book = Book.open(@different_file, :reuse => true, :if_unsaved => options_value)
1411
1411
  end
1412
1412
  after do
1413
1413
  @new_book.close
@@ -1537,8 +1537,8 @@ describe Book do
1537
1537
  sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1538
1538
  book.Saved.should be_false
1539
1539
  new_book = Book.open(@simple_file1, :read_only => true, :if_unsaved => :accept)
1540
- new_book.ReadOnly.should be_false
1541
- new_book.Saved.should be_false
1540
+ new_book.ReadOnly.should be_true
1541
+ new_book.Saved.should be_true
1542
1542
  new_book.should == book
1543
1543
  end
1544
1544
 
@@ -1569,8 +1569,8 @@ describe Book do
1569
1569
  sheet[1,1] = sheet[1,1].value == "foo" ? "bar" : "foo"
1570
1570
  book.Saved.should be_false
1571
1571
  new_book = Book.open(@simple_file1, :force => {:excel => book.excel}, :read_only => true, :if_unsaved => :accept)
1572
- new_book.ReadOnly.should be_false
1573
- new_book.Saved.should be_false
1572
+ new_book.ReadOnly.should be_true
1573
+ new_book.Saved.should be_true
1574
1574
  new_book.should == book
1575
1575
  end
1576
1576
 
@@ -1636,12 +1636,6 @@ describe Book do
1636
1636
  book.should be_a Book
1637
1637
  end
1638
1638
  end
1639
- it 'block parameter should be instance of Book for new' do
1640
- Book.new(@simple_file) do |book|
1641
- book.should be_a Book
1642
- end
1643
- end
1644
-
1645
1639
  end
1646
1640
 
1647
1641
  context "with WIN32OLE#GetAbsolutePathName" do