robust_excel_ole 1.4 → 1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog +6 -0
- data/README.rdoc +7 -1
- data/docs/README_ranges.rdoc +1 -1
- data/lib/reo_console.rb +19 -17
- data/lib/robust_excel_ole.rb +1 -2
- data/lib/robust_excel_ole/bookstore.rb +45 -45
- data/lib/robust_excel_ole/cell.rb +11 -12
- data/lib/robust_excel_ole/cygwin.rb +11 -11
- data/lib/robust_excel_ole/excel.rb +218 -201
- data/lib/robust_excel_ole/general.rb +23 -21
- data/lib/robust_excel_ole/range.rb +18 -19
- data/lib/robust_excel_ole/reo_common.rb +95 -82
- data/lib/robust_excel_ole/version.rb +1 -1
- data/lib/robust_excel_ole/workbook.rb +199 -205
- data/lib/robust_excel_ole/worksheet.rb +33 -56
- data/lib/spec_helper.rb +3 -3
- data/spec/workbook_spec.rb +18 -0
- data/spec/workbook_specs/workbook_misc_spec.rb +2 -2
- data/spec/worksheet_spec.rb +18 -0
- metadata +2 -2
@@ -9,49 +9,49 @@ module General
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def canonize(filename) # :nodoc: #
|
12
|
-
raise TypeREOError, "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
14
|
normalize(filename).downcase
|
14
15
|
end
|
15
16
|
|
16
17
|
def normalize(path) # :nodoc: #
|
17
18
|
path = path.gsub('/./', '/') + '/'
|
18
|
-
path = path.gsub(/[\/\\]+/,
|
19
|
-
nil while path.gsub!(/(\/|^)(?!\.\.?)([^\/]+)\/\.\.\//, '\1')
|
20
|
-
path = path.chomp(
|
19
|
+
path = path.gsub(/[\/\\]+/, '/')
|
20
|
+
nil while path.gsub!(/(\/|^)(?!\.\.?)([^\/]+)\/\.\.\//, '\1')
|
21
|
+
path = path.chomp('/')
|
21
22
|
path
|
22
23
|
end
|
23
24
|
|
24
25
|
module_function :absolute_path, :canonize, :normalize
|
25
26
|
|
26
|
-
class VBAMethodMissingError < RuntimeError
|
27
|
+
class VBAMethodMissingError < RuntimeError # :nodoc: #
|
27
28
|
end
|
28
29
|
|
29
30
|
end
|
30
31
|
|
31
32
|
class WIN32OLE
|
32
|
-
|
33
33
|
# promoting WIN32OLE objects to RobustExcelOle objects
|
34
34
|
def to_reo
|
35
|
-
case
|
36
|
-
when
|
37
|
-
when
|
38
|
-
when
|
39
|
-
when
|
35
|
+
case ole_type.name
|
36
|
+
when 'Range' then RobustExcelOle::Range.new(self)
|
37
|
+
when '_Worksheet' then Worksheet.new(self)
|
38
|
+
when '_Workbook' then Workbook.new(self)
|
39
|
+
when '_Application' then Excel.new(self)
|
40
40
|
else
|
41
41
|
self
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
class ::String
|
46
|
+
class ::String # :nodoc: #
|
47
47
|
def / path_part
|
48
48
|
if empty?
|
49
49
|
path_part
|
50
50
|
else
|
51
|
-
if path_part.nil?
|
51
|
+
if path_part.nil? || path_part.empty?
|
52
52
|
self
|
53
53
|
else
|
54
|
-
begin
|
54
|
+
begin
|
55
55
|
File.join self, path_part
|
56
56
|
rescue TypeError
|
57
57
|
raise TypeError, "Only strings can be parts of paths (given: #{path_part.inspect} of class #{path_part.class})"
|
@@ -65,15 +65,15 @@ class ::String # :nodoc: #
|
|
65
65
|
word = gsub('::', '/')
|
66
66
|
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
67
67
|
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
68
|
-
word.tr!(
|
68
|
+
word.tr!('-', '_')
|
69
69
|
word.downcase!
|
70
70
|
word
|
71
71
|
end
|
72
72
|
|
73
73
|
# taken from http://apidock.com/rails/ActiveSupport/Inflector/constantize
|
74
74
|
# File activesupport/lib/active_support/inflector/methods.rb, line 226
|
75
|
-
def constantize #(camel_cased_word)
|
76
|
-
names =
|
75
|
+
def constantize # (camel_cased_word)
|
76
|
+
names = split('::')
|
77
77
|
|
78
78
|
# Trigger a builtin NameError exception including the ill-formed constant in the message.
|
79
79
|
Object.const_get(self) if names.empty?
|
@@ -94,6 +94,7 @@ class ::String # :nodoc: #
|
|
94
94
|
constant = constant.ancestors.inject do |const, ancestor|
|
95
95
|
break const if ancestor == Object
|
96
96
|
break ancestor if ancestor.const_defined?(name)
|
97
|
+
|
97
98
|
const
|
98
99
|
end
|
99
100
|
|
@@ -105,13 +106,14 @@ class ::String # :nodoc: #
|
|
105
106
|
end
|
106
107
|
|
107
108
|
# taken from http://api.rubyonrails.org/v2.3.8/classes/ActiveSupport/CoreExtensions/Module.html#M000806
|
108
|
-
class Module
|
109
|
+
class Module # :nodoc: #
|
109
110
|
def parent_name
|
110
111
|
unless defined? @parent_name
|
111
112
|
@parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
|
112
113
|
end
|
113
114
|
@parent_name
|
114
115
|
end
|
116
|
+
|
115
117
|
def parent
|
116
118
|
parent_name ? parent_name.constantize : Object
|
117
119
|
end
|
@@ -119,7 +121,7 @@ end
|
|
119
121
|
|
120
122
|
module MethodHelpers
|
121
123
|
|
122
|
-
def respond_to?(meth_name, include_private = false)
|
124
|
+
def respond_to?(meth_name, include_private = false) # :nodoc: #
|
123
125
|
if alive?
|
124
126
|
methods.include?(meth_name.to_s)
|
125
127
|
else
|
@@ -127,9 +129,9 @@ module MethodHelpers
|
|
127
129
|
end
|
128
130
|
end
|
129
131
|
|
130
|
-
def methods
|
132
|
+
def methods # :nodoc: #
|
131
133
|
if alive?
|
132
|
-
(super.map{|m| m.to_s} + ole_object.ole_methods.map{|m| m.to_s}).uniq.select{|m| m =~ /^(?!\_)/}.sort
|
134
|
+
(super.map { |m| m.to_s } + ole_object.ole_methods.map { |m| m.to_s }).uniq.select { |m| m =~ /^(?!\_)/ }.sort
|
133
135
|
else
|
134
136
|
super
|
135
137
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
module RobustExcelOle
|
3
|
-
|
4
3
|
# see https://docs.microsoft.com/en-us/office/vba/api/excel.worksheet#methods
|
5
4
|
|
6
5
|
class Range < REOCommon
|
@@ -23,11 +22,11 @@ module RobustExcelOle
|
|
23
22
|
# @params [Range] a range
|
24
23
|
# @returns [Array] the values
|
25
24
|
def values(range = nil)
|
26
|
-
result =
|
27
|
-
if range
|
25
|
+
result = map { |x| x.Value }.flatten
|
26
|
+
if range
|
28
27
|
relevant_result = []
|
29
|
-
result.each_with_index{ |row_or_column, i| relevant_result << row_or_column if range.include?(i) }
|
30
|
-
relevant_result
|
28
|
+
result.each_with_index { |row_or_column, i| relevant_result << row_or_column if range.include?(i) }
|
29
|
+
relevant_result
|
31
30
|
else
|
32
31
|
result
|
33
32
|
end
|
@@ -40,7 +39,7 @@ module RobustExcelOle
|
|
40
39
|
|
41
40
|
# copies a range
|
42
41
|
# @params [Address] address of a range
|
43
|
-
# @options [Worksheet] the worksheet in which to copy
|
42
|
+
# @options [Worksheet] the worksheet in which to copy
|
44
43
|
def copy(address, sheet = :__not_provided, third_argument_deprecated = :__not_provided)
|
45
44
|
if third_argument_deprecated != :__not_provided
|
46
45
|
address = [address,sheet]
|
@@ -50,11 +49,11 @@ module RobustExcelOle
|
|
50
49
|
sheet = @worksheet if sheet == :__not_provided
|
51
50
|
destination_range = sheet.range([address.rows.min..address.rows.max,
|
52
51
|
address.columns.min..address.columns.max]).ole_range
|
53
|
-
if sheet.workbook.excel == @worksheet.workbook.excel
|
52
|
+
if sheet.workbook.excel == @worksheet.workbook.excel
|
54
53
|
begin
|
55
54
|
self.Copy(:destination => destination_range)
|
56
55
|
rescue WIN32OLERuntimeError
|
57
|
-
raise RangeNotCopied,
|
56
|
+
raise RangeNotCopied, 'cannot copy range'
|
58
57
|
end
|
59
58
|
else
|
60
59
|
self.Select
|
@@ -63,35 +62,35 @@ module RobustExcelOle
|
|
63
62
|
end
|
64
63
|
end
|
65
64
|
|
66
|
-
def self.worksheet_class
|
65
|
+
def self.worksheet_class # :nodoc: #
|
67
66
|
@worksheet_class ||= begin
|
68
|
-
module_name =
|
67
|
+
module_name = parent_name
|
69
68
|
"#{module_name}::Worksheet".constantize
|
70
69
|
rescue NameError => e
|
71
70
|
Worksheet
|
72
71
|
end
|
73
72
|
end
|
74
73
|
|
75
|
-
def worksheet_class
|
74
|
+
def worksheet_class # :nodoc: #
|
76
75
|
self.class.worksheet_class
|
77
76
|
end
|
78
77
|
|
79
|
-
|
80
|
-
|
81
|
-
|
78
|
+
private
|
79
|
+
|
80
|
+
def method_missing(name, *args) # :nodoc: #
|
81
|
+
#if name.to_s[0,1] =~ /[A-Z]/
|
82
82
|
begin
|
83
83
|
@ole_range.send(name, *args)
|
84
84
|
rescue WIN32OLERuntimeError => msg
|
85
85
|
if msg.message =~ /unknown property or method/
|
86
86
|
raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
|
87
|
-
else
|
87
|
+
else
|
88
88
|
raise msg
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
91
|
+
# else
|
92
|
+
# super
|
93
|
+
# end
|
94
94
|
end
|
95
|
-
|
96
95
|
end
|
97
96
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
LOG_TO_STDOUT = true unless Object.const_defined?(:LOG_TO_STDOUT)
|
4
|
-
REO_LOG_DIR =
|
5
|
-
REO_LOG_FILE =
|
6
|
-
|
4
|
+
REO_LOG_DIR = ''.freeze unless Object.const_defined?(:REO_LOG_DIR)
|
5
|
+
REO_LOG_FILE = 'reo.log'.freeze unless Object.const_defined?(:REO_LOG_FILE)
|
6
|
+
|
7
7
|
File.delete REO_LOG_FILE rescue nil
|
8
8
|
|
9
9
|
module RobustExcelOle
|
10
10
|
|
11
11
|
class REOError < RuntimeError # :nodoc: #
|
12
|
-
end
|
12
|
+
end
|
13
13
|
|
14
14
|
class ExcelREOError < REOError # :nodoc: #
|
15
15
|
end
|
@@ -78,10 +78,10 @@ module RobustExcelOle
|
|
78
78
|
end
|
79
79
|
|
80
80
|
class TypeREOError < REOError # :nodoc: #
|
81
|
-
end
|
81
|
+
end
|
82
82
|
|
83
83
|
class TimeOut < REOError # :nodoc: #
|
84
|
-
end
|
84
|
+
end
|
85
85
|
|
86
86
|
class AddressInvalid < REOError # :nodoc: #
|
87
87
|
end
|
@@ -95,29 +95,29 @@ module RobustExcelOle
|
|
95
95
|
class REOCommon
|
96
96
|
|
97
97
|
def excel
|
98
|
-
raise TypeREOError,
|
98
|
+
raise TypeREOError, 'receiver instance is neither an Excel nor a Workbook'
|
99
99
|
end
|
100
100
|
|
101
101
|
def own_methods
|
102
102
|
(self.methods - Object.methods).sort
|
103
103
|
end
|
104
104
|
|
105
|
-
def self.tr1(
|
105
|
+
def self.tr1(_text)
|
106
106
|
puts :text
|
107
107
|
end
|
108
108
|
|
109
109
|
def self.trace(text)
|
110
|
-
if LOG_TO_STDOUT
|
110
|
+
if LOG_TO_STDOUT
|
111
111
|
puts text
|
112
112
|
else
|
113
113
|
if REO_LOG_DIR.empty?
|
114
|
-
homes = [
|
115
|
-
home = homes.find {|h| ENV[h]
|
114
|
+
homes = ['HOME', 'HOMEPATH']
|
115
|
+
home = homes.find { |h| !ENV[h].nil? }
|
116
116
|
reo_log_dir = ENV[home]
|
117
117
|
else
|
118
118
|
reo_log_dir = REO_LOG_DIR
|
119
119
|
end
|
120
|
-
File.open(reo_log_dir +
|
120
|
+
File.open(reo_log_dir + '/' + REO_LOG_FILE,'a') do |file|
|
121
121
|
file.puts text
|
122
122
|
end
|
123
123
|
end
|
@@ -145,19 +145,19 @@ module RobustExcelOle
|
|
145
145
|
|
146
146
|
def initialize(address)
|
147
147
|
address = [address] unless address.is_a?(Array)
|
148
|
-
raise AddressInvalid,
|
148
|
+
raise AddressInvalid, 'more than two components' if address.size > 2
|
149
149
|
begin
|
150
150
|
if address.size == 1
|
151
|
-
comp1, comp2 = address[0].split(':')
|
151
|
+
comp1, comp2 = address[0].split(':')
|
152
152
|
address_comp1 = comp1.gsub(/[A-Z]/,'')
|
153
153
|
address_comp2 = comp1.gsub(/[0-9]/,'')
|
154
|
-
if comp1 != address_comp2+address_comp1
|
154
|
+
if comp1 != address_comp2 + address_comp1
|
155
155
|
raise AddressInvalid, "address #{comp1.inspect} not in A1-format"
|
156
156
|
end
|
157
157
|
unless comp2.nil?
|
158
158
|
address_comp3 = comp2.gsub(/[A-Z]/,'')
|
159
|
-
address_comp4 = comp2.gsub(/[0-9]/,'')
|
160
|
-
if comp2 != address_comp4+address_comp3
|
159
|
+
address_comp4 = comp2.gsub(/[0-9]/,'')
|
160
|
+
if comp2 != address_comp4 + address_comp3
|
161
161
|
raise AddressInvalid, "address #{comp2.inspect} not in A1-format"
|
162
162
|
end
|
163
163
|
address_comp1 = address_comp1..address_comp3
|
@@ -166,26 +166,26 @@ module RobustExcelOle
|
|
166
166
|
else
|
167
167
|
address_comp1, address_comp2 = address
|
168
168
|
end
|
169
|
-
address_comp1 = address_comp1
|
170
|
-
address_comp2 = address_comp2
|
171
|
-
@rows = address_comp1.min.to_i
|
169
|
+
address_comp1 = address_comp1..address_comp1 unless address_comp1.is_a?(Object::Range)
|
170
|
+
address_comp2 = address_comp2..address_comp2 unless address_comp2.is_a?(Object::Range)
|
171
|
+
@rows = address_comp1.min.to_i..address_comp1.max.to_i
|
172
172
|
if address_comp2.min.to_i == 0
|
173
|
-
raise AddressInvalid, "address (#{address_comp1.inspect}, #{address_comp2.inspect}) not in A1-format" if address_comp1.min.to_i == 0
|
174
|
-
@columns = str2num(address_comp2.min)
|
173
|
+
raise AddressInvalid, "address (#{address_comp1.inspect}, #{address_comp2.inspect}) not in A1-format" if address_comp1.min.to_i == 0
|
174
|
+
@columns = str2num(address_comp2.min)..str2num(address_comp2.max)
|
175
175
|
else
|
176
|
-
@columns = address_comp2.min.to_i
|
176
|
+
@columns = address_comp2.min.to_i..address_comp2.max.to_i
|
177
177
|
end
|
178
|
-
rescue
|
178
|
+
rescue
|
179
179
|
raise AddressInvalid, "address (#{address.inspect}) not in A1- or R1C1-format"
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
|
-
|
183
|
+
private
|
184
184
|
|
185
185
|
def str2num(str)
|
186
186
|
str = str.upcase
|
187
187
|
sum = 0
|
188
|
-
(1..str.length).each { |i| sum += (str[i-1].ord-64) * 26
|
188
|
+
(1..str.length).each { |i| sum += (str[i - 1].ord - 64) * 26**(str.length - i) }
|
189
189
|
sum
|
190
190
|
end
|
191
191
|
|
@@ -196,14 +196,14 @@ module RobustExcelOle
|
|
196
196
|
# returns the contents of a range with given name
|
197
197
|
# if the name could not be found or the value could not be determined,
|
198
198
|
# then return default value, if provided, raise error otherwise
|
199
|
-
# Excel Bug: if a local name without a qualifier is given,
|
199
|
+
# Excel Bug: if a local name without a qualifier is given,
|
200
200
|
# then by default Excel takes the first worksheet,
|
201
201
|
# even if a different worksheet is active
|
202
202
|
# @param [String] name the name of the range
|
203
203
|
# @param [Hash] opts the options
|
204
204
|
# @option opts [Symbol] :default the default value that is provided if no contents could be returned
|
205
205
|
# @return [Variant] the contents of a range with given name
|
206
|
-
def namevalue_glob(name, opts = {:default => :__not_provided})
|
206
|
+
def namevalue_glob(name, opts = { :default => :__not_provided })
|
207
207
|
name_obj = begin
|
208
208
|
name_object(name)
|
209
209
|
rescue NameNotFound => msg
|
@@ -214,8 +214,8 @@ module RobustExcelOle
|
|
214
214
|
name_obj.RefersToRange.Value
|
215
215
|
rescue WIN32OLERuntimeError
|
216
216
|
sheet = if self.is_a?(Worksheet) then self
|
217
|
-
|
218
|
-
|
217
|
+
elsif self.is_a?(Workbook) then self.sheet(1)
|
218
|
+
elsif self.is_a?(Excel) then self.workbook.sheet(1)
|
219
219
|
end
|
220
220
|
begin
|
221
221
|
sheet.Evaluate(name_obj.Name).Value
|
@@ -224,12 +224,12 @@ module RobustExcelOle
|
|
224
224
|
raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{self}"
|
225
225
|
end
|
226
226
|
end
|
227
|
-
if value == -2146828288 + RobustExcelOle::XlErrName
|
228
|
-
return opts[:default] unless opts[:default] == __not_provided
|
227
|
+
if value == -2146828288 + RobustExcelOle::XlErrName
|
228
|
+
return opts[:default] unless opts[:default] == :__not_provided
|
229
229
|
raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(workbook.stored_filename).inspect rescue nil}"
|
230
|
-
end
|
231
|
-
return opts[:default] unless opts[:default] == :__not_provided
|
232
|
-
value
|
230
|
+
end
|
231
|
+
return opts[:default] unless (opts[:default] == :__not_provided) || value.nil?
|
232
|
+
value
|
233
233
|
end
|
234
234
|
|
235
235
|
# sets the contents of a range
|
@@ -237,15 +237,13 @@ module RobustExcelOle
|
|
237
237
|
# @param [Variant] value the contents of the range
|
238
238
|
# @param [FixNum] color the color when setting a value
|
239
239
|
# @param [Hash] opts :color [FixNum] the color when setting the contents
|
240
|
-
def set_namevalue_glob(name, value, opts = {:color => 0})
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
raise RangeNotEvaluatable, "cannot assign value to range named #{name.inspect} in #{self.inspect}"
|
248
|
-
end
|
240
|
+
def set_namevalue_glob(name, value, opts = { :color => 0 })
|
241
|
+
cell = name_object(name).RefersToRange
|
242
|
+
cell.Interior.ColorIndex = opts[:color]
|
243
|
+
workbook.modified_cells << cell if workbook # unless cell_modified?(cell)
|
244
|
+
cell.Value = value
|
245
|
+
rescue WIN32OLERuntimeError
|
246
|
+
raise RangeNotEvaluatable, "cannot assign value to range named #{name.inspect} in #{self.inspect}"
|
249
247
|
end
|
250
248
|
|
251
249
|
# returns the contents of a range with a locally defined name
|
@@ -255,8 +253,8 @@ module RobustExcelOle
|
|
255
253
|
# @param [String] name the name of a range
|
256
254
|
# @param [Hash] opts the options
|
257
255
|
# @option opts [Symbol] :default the default value that is provided if no contents could be returned
|
258
|
-
# @return [Variant] the contents of a range with given name
|
259
|
-
def namevalue(name, opts = {:default => :__not_provided})
|
256
|
+
# @return [Variant] the contents of a range with given name
|
257
|
+
def namevalue(name, opts = { :default => :__not_provided })
|
260
258
|
return namevalue_glob(name, opts) if self.is_a?(Workbook)
|
261
259
|
begin
|
262
260
|
range = self.Range(name)
|
@@ -270,19 +268,19 @@ module RobustExcelOle
|
|
270
268
|
return opts[:default] unless opts[:default] == :__not_provided
|
271
269
|
raise RangeNotEvaluatable, "cannot determine value of range named #{name.inspect} in #{self.inspect}"
|
272
270
|
end
|
273
|
-
if value == -2146828288 + RobustExcelOle::XlErrName
|
271
|
+
if value == -2146828288 + RobustExcelOle::XlErrName
|
274
272
|
return opts[:default] unless opts[:default] == __not_provided
|
275
273
|
raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(workbook.stored_filename).inspect rescue nil}"
|
276
|
-
end
|
277
|
-
return opts[:default] unless opts[:default] == :__not_provided
|
278
|
-
value
|
274
|
+
end
|
275
|
+
return opts[:default] unless (opts[:default] == :__not_provided) || value.nil?
|
276
|
+
value
|
279
277
|
end
|
280
278
|
|
281
279
|
# assigns a value to a range given a locally defined name
|
282
280
|
# @param [String] name the name of a range
|
283
281
|
# @param [Variant] value the assigned value
|
284
282
|
# @param [Hash] opts :color [FixNum] the color when setting the contents
|
285
|
-
def set_namevalue(name, value, opts = {:color => 0})
|
283
|
+
def set_namevalue(name, value, opts = { :color => 0 })
|
286
284
|
begin
|
287
285
|
return set_namevalue_glob(name, value, opts) if self.is_a?(Workbook)
|
288
286
|
range = self.Range(name)
|
@@ -291,51 +289,69 @@ module RobustExcelOle
|
|
291
289
|
end
|
292
290
|
begin
|
293
291
|
range.Interior.ColorIndex = opts[:color]
|
294
|
-
workbook.modified_cells << range if workbook #unless cell_modified?(range)
|
292
|
+
workbook.modified_cells << range if workbook # unless cell_modified?(range)
|
295
293
|
range.Value = value
|
296
294
|
rescue WIN32OLERuntimeError
|
297
295
|
raise RangeNotEvaluatable, "cannot assign value to range named #{name.inspect} in #{self.inspect}"
|
298
296
|
end
|
299
297
|
end
|
300
298
|
|
301
|
-
def nameval(name, opts = {:default => :__not_provided}) # :deprecated: #
|
299
|
+
def nameval(name, opts = { :default => :__not_provided }) # :deprecated: #
|
302
300
|
namevalue_glob(name, opts)
|
303
301
|
end
|
304
302
|
|
305
|
-
def set_nameval(name, value, opts = {:color => 0}) # :deprecated: #
|
303
|
+
def set_nameval(name, value, opts = { :color => 0 }) # :deprecated: #
|
306
304
|
set_namevalue_glob(name, value, opts)
|
307
305
|
end
|
308
306
|
|
309
|
-
def rangeval(name, opts = {:default => :__not_provided}) # :deprecated: #
|
307
|
+
def rangeval(name, opts = { :default => :__not_provided }) # :deprecated: #
|
310
308
|
namevalue(name, opts)
|
311
309
|
end
|
312
310
|
|
313
|
-
def set_rangeval(name, value, opts = {:color => 0}) # :deprecated: #
|
311
|
+
def set_rangeval(name, value, opts = { :color => 0 }) # :deprecated: #
|
314
312
|
set_namevalue(name, value, opts)
|
315
313
|
end
|
316
314
|
|
317
|
-
#
|
318
|
-
# @
|
319
|
-
|
315
|
+
# creates a range from a given defined name or address
|
316
|
+
# @params [Variant] range name or address
|
317
|
+
# @return [Range] a range
|
318
|
+
def range(name_or_address, address2 = :__not_provided)
|
320
319
|
begin
|
321
|
-
|
320
|
+
if address2 == :__not_provided
|
321
|
+
range = RobustExcelOle::Range.new(name_object(name_or_address).RefersToRange) rescue nil
|
322
|
+
end
|
323
|
+
if self.is_a?(Worksheet) && (range.nil? || (address2 != :__not_provided))
|
324
|
+
address = name_or_address
|
325
|
+
address = [name_or_address,address2] unless address2 == :__not_provided
|
326
|
+
address = Address.new(address)
|
327
|
+
range = RobustExcelOle::Range.new(@ole_worksheet.Range(
|
328
|
+
@ole_worksheet.Cells(address.rows.min, address.columns.min),
|
329
|
+
@ole_worksheet.Cells(address.rows.max, address.columns.max)
|
330
|
+
))
|
331
|
+
end
|
322
332
|
rescue WIN32OLERuntimeError
|
323
|
-
raise RangeNotCreated, "
|
333
|
+
raise RangeNotCreated, "cannot create range (#{name_or_address.inspect},#{address2.inspect})"
|
324
334
|
end
|
335
|
+
raise RangeNotCreated, "cannot create range (#{name_or_address.inspect},#{address2.inspect})" if range.nil?
|
336
|
+
range
|
337
|
+
end
|
338
|
+
|
339
|
+
def name2range(name) # :deprecated: #
|
340
|
+
range(name)
|
325
341
|
end
|
326
342
|
|
327
343
|
# adds a name referring to a range given by the row and column
|
328
344
|
# @param [String] name the range name
|
329
|
-
# @params [Address] address of the range
|
345
|
+
# @params [Address] address of the range
|
330
346
|
def add_name(name, addr, addr_deprecated = :__not_provided)
|
331
347
|
addr = [addr,addr_deprecated] unless addr_deprecated == :__not_provided
|
332
348
|
address = Address.new(addr)
|
333
|
-
address_string =
|
334
|
-
|
349
|
+
address_string = 'Z' + address.rows.min.to_s + 'S' + address.columns.min.to_s +
|
350
|
+
':Z' + address.rows.max.to_s + 'S' + address.columns.max.to_s
|
335
351
|
begin
|
336
|
-
self.Names.Add(
|
352
|
+
self.Names.Add('Name' => name, 'RefersToR1C1' => '=' + address_string)
|
337
353
|
rescue WIN32OLERuntimeError => msg
|
338
|
-
#trace "WIN32OLERuntimeError: #{msg.message}"
|
354
|
+
# trace "WIN32OLERuntimeError: #{msg.message}"
|
339
355
|
raise RangeNotEvaluatable, "cannot add name #{name.inspect} to range #{addr.inspect}"
|
340
356
|
end
|
341
357
|
name
|
@@ -352,12 +368,12 @@ module RobustExcelOle
|
|
352
368
|
begin
|
353
369
|
item = self.Names.Item(name)
|
354
370
|
rescue WIN32OLERuntimeError
|
355
|
-
raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
|
371
|
+
raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
|
356
372
|
end
|
357
373
|
begin
|
358
374
|
item.Name = new_name
|
359
375
|
rescue WIN32OLERuntimeError
|
360
|
-
raise UnexpectedREOError, "name error in #{File.basename(self.stored_filename).inspect}"
|
376
|
+
raise UnexpectedREOError, "name error in #{File.basename(self.stored_filename).inspect}"
|
361
377
|
end
|
362
378
|
end
|
363
379
|
|
@@ -368,34 +384,31 @@ module RobustExcelOle
|
|
368
384
|
begin
|
369
385
|
item = self.Names.Item(name)
|
370
386
|
rescue WIN32OLERuntimeError
|
371
|
-
raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
|
387
|
+
raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
|
372
388
|
end
|
373
389
|
begin
|
374
390
|
item.Delete
|
375
391
|
rescue WIN32OLERuntimeError
|
376
|
-
raise UnexpectedREOError, "name error in #{File.basename(self.stored_filename).inspect}"
|
392
|
+
raise UnexpectedREOError, "name error in #{File.basename(self.stored_filename).inspect}"
|
377
393
|
end
|
378
394
|
end
|
379
395
|
|
380
|
-
|
381
|
-
private
|
396
|
+
private
|
382
397
|
|
383
398
|
def name_object(name)
|
399
|
+
self.Names.Item(name)
|
400
|
+
rescue WIN32OLERuntimeError
|
384
401
|
begin
|
385
|
-
self.Names.Item(name)
|
402
|
+
self.Parent.Names.Item(name)
|
386
403
|
rescue WIN32OLERuntimeError
|
387
|
-
|
388
|
-
self.Parent.Names.Item(name)
|
389
|
-
rescue WIN32OLERuntimeError
|
390
|
-
raise RobustExcelOle::NameNotFound, "name #{name.inspect} not in #{self.inspect}"
|
391
|
-
end
|
404
|
+
raise RobustExcelOle::NameNotFound, "name #{name.inspect} not in #{self.inspect}"
|
392
405
|
end
|
393
406
|
end
|
394
|
-
|
395
|
-
#def cell_modified?(cell)
|
396
|
-
# workbook.modified_cells.each{|c| return true if c.Name.Value == cell.Name.Value}
|
407
|
+
|
408
|
+
# def cell_modified?(cell)
|
409
|
+
# workbook.modified_cells.each{|c| return true if c.Name.Value == cell.Name.Value}
|
397
410
|
# false
|
398
|
-
#end
|
411
|
+
# end
|
399
412
|
|
400
413
|
end
|
401
414
|
|