robust_excel_ole 1.13 → 1.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog +14 -4
- data/README.rdoc +34 -43
- data/___dummy_workbook.xls +0 -0
- data/docs/README_excel.rdoc +2 -2
- data/docs/README_open.rdoc +51 -34
- data/docs/README_ranges.rdoc +80 -80
- data/docs/README_save_close.rdoc +9 -9
- data/docs/README_sheet.rdoc +17 -17
- data/lib/robust_excel_ole.rb +3 -1
- data/lib/robust_excel_ole/excel.rb +38 -33
- data/lib/robust_excel_ole/reo_common.rb +1 -2
- data/lib/robust_excel_ole/version.rb +1 -1
- data/lib/robust_excel_ole/workbook.rb +110 -83
- data/spec/data/more_data/workbook.xls +0 -0
- data/spec/workbook_spec.rb +2 -2
- data/spec/workbook_specs/workbook_open_spec.rb +36 -8
- data/spec/workbook_specs/workbook_unobtr_spec.rb +379 -16
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b63b8ec97e714c16c5b4e3f1bb60f7e160aba0469ad14d47b766fa1778919c1f
|
4
|
+
data.tar.gz: 1e0af619c00875ae92782cfa25a091335de3e1235723be9c2ecef2924b4f3cf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54d8101340e7a2c82af0a761998a466a931df0dc5eaf0bd5117a513f89ebf3e38fa6e27dd04af909f0d8cf8125117b126c8df7d642843c993862f6af2966afbd
|
7
|
+
data.tar.gz: 9f92d1018a35b7d018b7b3c3f7f3bb71383b7cb3914429a7986fe097a3ea9e8ac7ae77c011adff4075d4b56946c08ae8d9fdb9c69a51abb65b6109ea4f66cd00
|
data/Changelog
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
+
## [1.14]
|
5
|
+
|
6
|
+
### Changed
|
7
|
+
- Workbook.unobtrusively: removed parameter :rw_change_excel
|
8
|
+
|
9
|
+
|
10
|
+
## [1.13]
|
11
|
+
|
12
|
+
- no interface change
|
13
|
+
|
4
14
|
## [1.12]
|
5
15
|
|
6
16
|
### Added
|
@@ -8,9 +18,10 @@ All notable changes to this project will be documented in this file.
|
|
8
18
|
- Workbook#color_if_modified
|
9
19
|
|
10
20
|
### Changed:
|
11
|
-
- Workbook#save, save_as:
|
21
|
+
- Workbook#save, save_as: removed parameter :discoloring
|
12
22
|
- RangeOwners#set_namevalue, set_namevalue_glob, Worksheet#set_cellval:
|
13
|
-
|
23
|
+
removed optional parameter for color
|
24
|
+
- Workbook#open: removed option :excel => :reserved_new
|
14
25
|
|
15
26
|
|
16
27
|
## [1.11]
|
@@ -23,7 +34,6 @@ All notable changes to this project will be documented in this file.
|
|
23
34
|
## [1.10]
|
24
35
|
|
25
36
|
### Added
|
26
|
-
- workarounds for running in Jruby
|
27
37
|
- Range,Cell#v
|
28
38
|
|
29
39
|
## [1.9]
|
@@ -74,7 +84,7 @@ All notable changes to this project will be documented in this file.
|
|
74
84
|
- ReoCommon#namevalue, namevalue_glob, set_namevalue, set_namevalue_glob replace
|
75
85
|
set_namevalue replaces set_rangeval, set_namevalue_glob, respectively
|
76
86
|
|
77
|
-
## [1.2.1] 2018-9-
|
87
|
+
## [1.2.1] 2018-9-12
|
78
88
|
|
79
89
|
## [1.2] - 2018-11-8
|
80
90
|
|
data/README.rdoc
CHANGED
@@ -44,11 +44,7 @@ and go into the path of the gem 'robust_excel_ole'. There you start the console
|
|
44
44
|
|
45
45
|
reo
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
jreo
|
50
|
-
|
51
|
-
depending on whether you are using ruby or jruby.
|
47
|
+
If you don't want to use a version manager to switch between ruby and jruby, you can apply the command +reo+ when using ruby, and +jreo+ when using jruby.
|
52
48
|
|
53
49
|
The call of the console will include RobustExcelOle for you.
|
54
50
|
|
@@ -62,10 +58,10 @@ Let's open a workbook.
|
|
62
58
|
|
63
59
|
workbook = Workbook.open 'spec/data/workbook.xls'
|
64
60
|
|
65
|
-
Now we have a Workbook object that wraps a
|
61
|
+
Now we have a Workbook object that wraps a WIN32OLE object. That is, you can send any WIN32OLE (VBA) method to it. See
|
66
62
|
https://docs.microsoft.com/en-us/office/vba/api/excel.workbook#methods.
|
67
63
|
|
68
|
-
For example, you can determine the name and the
|
64
|
+
For example, you can determine the name and whether the workbook is visible.
|
69
65
|
|
70
66
|
workbook.Name
|
71
67
|
# => "workbook.xls"
|
@@ -79,7 +75,7 @@ First we want to make the workbook visible.
|
|
79
75
|
|
80
76
|
workbook.visible = true
|
81
77
|
|
82
|
-
This method makes both the Excel instance and the window of the Workbook
|
78
|
+
This method makes both the Excel instance and the window of the Workbook WIN32OLE object visible.
|
83
79
|
|
84
80
|
Let's read the value of a named cell.
|
85
81
|
|
@@ -94,22 +90,14 @@ Then we'll save the workbook.
|
|
94
90
|
|
95
91
|
workbook.save
|
96
92
|
|
97
|
-
|
98
|
-
|
99
|
-
workbook.close
|
100
|
-
|
101
|
-
One special feature of RobustExcelOle is that it enables reopening workbooks.
|
102
|
-
|
103
|
-
workbook.reopen
|
104
|
-
|
105
|
-
The workbook is now open again and ready for applying further operations, e.g.
|
106
|
-
|
107
|
-
workbook['firstcell'] = "another_value"
|
108
|
-
workbook.save
|
109
|
-
workbook.close
|
93
|
+
=== More features when opening, modifying, creating, saving and closing workbooks
|
110
94
|
|
111
95
|
RobustExcelOle allows unobtrusively reading and modifying workbooks, i.e. accessing workbooks without changing their "status". The status comprises whether the workbook is open or closed, saved or unsaved, read-only or writable, visible or invisible, whether the calculation mode is manual or automatic, and checking compatibility is done or not done (the Workbook object remembers its properties).
|
112
96
|
|
97
|
+
Workbook.unobtrusively('spec/data/workbook.xls') do |workbook|
|
98
|
+
# do something
|
99
|
+
end
|
100
|
+
|
113
101
|
Workbook.for_modifying('spec/data/workbook.xls') do |workbook|
|
114
102
|
# do something
|
115
103
|
end
|
@@ -122,9 +110,7 @@ You can also create a new, empty workbook.
|
|
122
110
|
|
123
111
|
Workbook.create('spec/data/new_workbook.xls', :visible => true)
|
124
112
|
|
125
|
-
|
126
|
-
|
127
|
-
We can open the workbook using a block, similar to, e.g., +File.open+.
|
113
|
+
Moreover, you can open the workbook using a block, similar to, e.g., +File.open+.
|
128
114
|
|
129
115
|
Workbook.open('spec/data/workbook.xls') do |workbook|
|
130
116
|
# do something
|
@@ -142,7 +128,7 @@ or, using abbreviations,
|
|
142
128
|
|
143
129
|
workbook = Workbook.open('spec/data/workbook.xls', :v => true)
|
144
130
|
|
145
|
-
Note that Workbook.open can also connect to workbooks which were not opened via RobustExcelOle (but had been opened before by some user).
|
131
|
+
Note that +Workbook.open+ can also connect to workbooks which were not opened via RobustExcelOle (but had been opened before by some user).
|
146
132
|
|
147
133
|
For more details about opening workbooks see {README_open}[https://github.com/Thomas008/robust_excel_ole/blob/master/docs/README_open.rdoc]
|
148
134
|
|
@@ -154,7 +140,11 @@ or save the workbook under a different name.
|
|
154
140
|
|
155
141
|
workbook.save_as('spec/data/new_workbook.xls')
|
156
142
|
|
157
|
-
Finally we can
|
143
|
+
Finally we can close the workbook
|
144
|
+
|
145
|
+
workbook.save
|
146
|
+
|
147
|
+
or save and close the workbook in one step.
|
158
148
|
|
159
149
|
workbook.close(:if_unsaved => :save)
|
160
150
|
|
@@ -196,7 +186,8 @@ For hard terminating all Excel processes we can use
|
|
196
186
|
|
197
187
|
Excel.kill_all
|
198
188
|
|
199
|
-
For more details about
|
189
|
+
For more details about opening Excel instances see {README_excel}[https://github.com/Thomas008/robust_excel_ole/blob/master/docs/README_excel.rdoc]
|
190
|
+
|
200
191
|
|
201
192
|
=== Opening workbooks in several Excel instances
|
202
193
|
|
@@ -213,12 +204,12 @@ Now we want to open another workbook in a different Excel instance.
|
|
213
204
|
We can also create a third Excel instance and open another workbook in this instance.
|
214
205
|
|
215
206
|
excel1 = Excel.create
|
216
|
-
|
207
|
+
workbook3 = Workbook.open('spec/data/another_workbook.xls', :excel => excel1)
|
217
208
|
|
218
209
|
A workbook is opened by default in the Excel instance where it was open before most recently.
|
219
210
|
|
220
|
-
|
221
|
-
|
211
|
+
workbook1.close
|
212
|
+
workbook1 = Workbook.open('spec/data/workbook.xls')
|
222
213
|
|
223
214
|
If this Excel instance is damaged or closed, then options control whether the workbook shall be opened in the current (active), a new or a given Excel instance.
|
224
215
|
|
@@ -252,22 +243,22 @@ Assume we have opened a workbook
|
|
252
243
|
|
253
244
|
We access the first worksheet by
|
254
245
|
|
255
|
-
|
246
|
+
worksheet = workbook.sheet(1)
|
256
247
|
|
257
248
|
or
|
258
249
|
|
259
|
-
|
250
|
+
worksheet = workbook.sheet('Sheet1')
|
260
251
|
|
261
252
|
or
|
262
253
|
|
263
|
-
|
254
|
+
worksheet = workbook.first_sheet
|
264
255
|
|
265
256
|
We can read and change the worksheet name.
|
266
257
|
|
267
|
-
|
258
|
+
worksheet.name
|
268
259
|
# => "Sheet1"
|
269
260
|
|
270
|
-
|
261
|
+
worksheet.name = "new_sheet"
|
271
262
|
|
272
263
|
We can copy the first worksheet, name it and add it before the third worksheet.
|
273
264
|
|
@@ -279,35 +270,35 @@ For more details about processing worksheets see {README_sheet}[https://github.c
|
|
279
270
|
|
280
271
|
We can define a rectangular range by providing the top left and the bottum down cell.
|
281
272
|
|
282
|
-
|
273
|
+
worksheet.range([1..3,1..4])
|
283
274
|
|
284
275
|
We can read the first three cells of the first row
|
285
276
|
|
286
|
-
|
277
|
+
worksheet.row_range(1, 1..3).values # => ["foo","workbook","sheet1"]
|
287
278
|
|
288
279
|
and the third column
|
289
280
|
|
290
|
-
|
281
|
+
worksheet.col_range(3).values # => ["sheet1", 2.0, 4.0]
|
291
282
|
|
292
283
|
We can read the first cell, using
|
293
284
|
|
294
|
-
|
285
|
+
worksheet[1,1].value # => "foo"
|
295
286
|
|
296
287
|
or
|
297
288
|
|
298
|
-
|
289
|
+
worksheet.row_range(1)[0].value # => "foo"
|
299
290
|
|
300
291
|
Then we modify it.
|
301
292
|
|
302
|
-
|
293
|
+
worksheet[1,1] = "hello"
|
303
294
|
|
304
295
|
We get the value of a named range
|
305
296
|
|
306
|
-
|
297
|
+
worksheet["firstcell"] # => "hello"
|
307
298
|
|
308
299
|
and set another value to that range.
|
309
300
|
|
310
|
-
|
301
|
+
worksheet["firstcell"] = "new_value"
|
311
302
|
|
312
303
|
For more details about reading and writing contents of cells and ranges see {README_ranges}[https://github.com/Thomas008/robust_excel_ole/blob/master/docs/README_ranges.rdoc]
|
313
304
|
|
Binary file
|
data/docs/README_excel.rdoc
CHANGED
@@ -110,7 +110,7 @@ You can yield an array of all Workbook objects of an Excel instance.
|
|
110
110
|
|
111
111
|
You can access all Workbook objects by using the methods Excel#each_workbook and Excel#each_workbook_with_index. Here are some examples:
|
112
112
|
|
113
|
-
excel.
|
113
|
+
excel.each_workbook {|w| puts w.filename}
|
114
114
|
|
115
115
|
and
|
116
116
|
|
@@ -146,7 +146,7 @@ Closed Excel instances can also be reopened. This includes reopening all workboo
|
|
146
146
|
excel.close
|
147
147
|
excel.recreate
|
148
148
|
|
149
|
-
The options are
|
149
|
+
The options are +:reopen_workbooks+, +:visible+ and +:displayalerts+.
|
150
150
|
|
151
151
|
excel.recreate(:reopen_workbooks => true, :visible => true, :displayalerts => true)
|
152
152
|
|
data/docs/README_open.rdoc
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
=== Opening a workbook.
|
4
4
|
|
5
|
-
If you want to open a workbook, use
|
5
|
+
If you want to open a workbook, you can use the methods +open+ or +new+.
|
6
6
|
|
7
|
-
|
7
|
+
workbook = Workbook.open('spec/data/workbook.xls')
|
8
8
|
|
9
9
|
Once you have got an Workbook object, you can apply all VBA methods that you would apply to a VBA Workbook object
|
10
10
|
(see https://docs.microsoft.com/en-us/office/vba/api/excel.workbook#methods).
|
@@ -46,7 +46,7 @@ You can use the following abbreviations or synonyms: +:f+ for +:force+, +:d+ fo
|
|
46
46
|
|
47
47
|
The option +:excel+ :
|
48
48
|
|
49
|
-
Valid values are : +:current+ (or +:active+ or +:reuse+), +:new+,
|
49
|
+
Valid values are : +:current+ (or +:active+ or +:reuse+), +:new+, or a given Excel instance (default: +:current).
|
50
50
|
|
51
51
|
The option +:if_unsaved+ :
|
52
52
|
|
@@ -80,32 +80,28 @@ Here are a few examples:
|
|
80
80
|
|
81
81
|
If you want to open a workbook that was not opened before, or reopen a workbook that was open in an Excel instance that is now closed, in the current (active) Excel instance, then use
|
82
82
|
|
83
|
-
|
83
|
+
workbook = Workbook.open('spec/data/workbook.xls', :default => {:excel => :current})
|
84
84
|
|
85
85
|
or
|
86
86
|
|
87
|
-
|
87
|
+
workbook = Workbook.open('spec/data/workbook.xls')
|
88
88
|
|
89
89
|
In case you want to open such a workbook in a new Excel instance, then use
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
You can also open the workbook in a separate, reserved Excel instance.
|
94
|
-
|
95
|
-
book = Workbook.open('spec/data/workbook.xls', :default => {:excel => :reserved_new})
|
91
|
+
workbook = Workbook.open('spec/data/workbook.xls', :default => {:excel => :new})
|
96
92
|
|
97
93
|
If you want to open a workbook in a new Excel instance, no matter if it was opened before, you can write
|
98
94
|
|
99
|
-
|
95
|
+
workbook = Workbook.open('spec/data/workbook.xls', :force => {:excel => :new})
|
100
96
|
|
101
97
|
For simplicity, you can also leave out the +:force+ option (but not the +:default+ option).
|
102
98
|
|
103
|
-
|
99
|
+
workbook = Workbook.open('spec/data/workbook.xls', :excel => :new)
|
104
100
|
|
105
101
|
You can also specify an Excel instance
|
106
102
|
|
107
103
|
excel1 = Excel.create
|
108
|
-
|
104
|
+
workbook = Workbook.open('spec/data/workbook.xls', :excel => excel1)
|
109
105
|
|
110
106
|
If you want to open the workbook and make its window visible, then use
|
111
107
|
|
@@ -114,22 +110,24 @@ If you want to open the workbook and make its window visible, then use
|
|
114
110
|
Notice, that when the workbook is visible, the DisplayAlerts of the respective Excel instance is true, if not explicitely DisplayAlerts is set to false in this Excel instance.
|
115
111
|
You can combine options, e.g.
|
116
112
|
|
117
|
-
|
113
|
+
workbook = Workbook.open('spec/data/workbook.xls', :visible => true, :default => {:excel => excel1})
|
118
114
|
|
119
115
|
You can use the abbreviations, e.g. in this case
|
120
116
|
|
121
|
-
|
117
|
+
workbook = Workbook.open('spec/data/workbook.xls', :v => true, :d => {:e => excel1})
|
122
118
|
|
123
119
|
If a workbook contains unsaved changes and a workbook with the same filename shall be opened, then the option +:if_unsaved+ manages this conflict. For example, if the workbook with the unsaved changes shall remain open, you can use
|
124
120
|
|
125
|
-
|
121
|
+
workbook = Workbook.open('spec/data/workbook.xls', :if_unsaved => :accept)
|
126
122
|
|
127
123
|
If a workbook is open and a workbook with the same name but in different path shall be opened, i.e. the first workbook blocks opening the other workbook, then the option +:if_blocked+ handles this situation, e.g.
|
128
124
|
|
129
|
-
|
125
|
+
workbook = Workbook.open('path/workbook.xls', :if_blocked => :forget)
|
130
126
|
|
131
127
|
Remarks:
|
132
128
|
|
129
|
+
The methods +open+ and +new+ connect to workbooks opened outside of RobustExcelOle as well.
|
130
|
+
|
133
131
|
Opening linked workbooks for EXCEL 2007 is supported
|
134
132
|
|
135
133
|
Doing updating links seems to be dependent on calculation mode: updates happen, if the calcultion mode is automatic, and does not happen, if calculation mode is manual.
|
@@ -138,23 +136,42 @@ Doing updating links seems to be dependent on calculation mode: updates happen,
|
|
138
136
|
|
139
137
|
A special feature of RobustExcelOle is that it allows to reopen workbooks after closing them.
|
140
138
|
|
141
|
-
|
142
|
-
|
143
|
-
|
139
|
+
workbook = Workbook.open('spec/data/workbook.xls')
|
140
|
+
workbook.close
|
141
|
+
workbook.reopen
|
144
142
|
|
145
143
|
The closed workbook is now alive again, i.e. is open and responds to Excel methods.
|
146
144
|
|
147
|
-
|
145
|
+
=== Promoting WIN32OLE objects to RobustExcelOle objects
|
146
|
+
|
147
|
+
Promoting means here: enriching a given object of a certain class by properties and methods of another class.
|
148
|
+
The method +General.to_reo+ enables promoting WIN32OLE objects to RobustExcelOle objects such that the attributes and methods of RobustExcelOle can be applied to these objects. For example, assume we have a WIN32OLE workbook +win32ole_workbook+:
|
149
|
+
|
150
|
+
win32ole_workbook.to_class
|
151
|
+
=> WIN32OLE
|
152
|
+
|
153
|
+
This object can be promoted to a RobustExcelOle workbook.
|
154
|
+
|
155
|
+
workbook = win32ole_workbook.to_reo
|
156
|
+
workbook.to_class
|
157
|
+
=> RobustExcelOle::Workbook
|
158
|
+
|
159
|
+
Similarly, Excel, Worksheet, and Range WIN32OLE objects can be promoted to corresponding RobustExcelOle objects.
|
160
|
+
|
161
|
+
The method +to_reo+ uses the method +new+. You can apply the method +new+ directly.
|
162
|
+
|
163
|
+
workbook = Workbook.new(win32ole_workbook)
|
164
|
+
|
165
|
+
You can supply options, e.g. +:visible+.
|
148
166
|
|
149
|
-
|
167
|
+
workbook = Workbook.new(win32ole_workbook, :visible => true)
|
150
168
|
|
151
|
-
A Workbook object can be created when giving an Excel workbook.
|
152
169
|
|
153
|
-
|
170
|
+
=== Identity transperence ===
|
154
171
|
|
155
|
-
|
172
|
+
A RobustExcelOle Workbook object is a proxy of an Excel WIN32OLE workbook. A Workbook object is defined by the full workbook name and the Excel instance in which it is opened. RobustExcelOle ensures identity transparency which means that identical Workbook objects refer to identical Excel workbooks, and vice versa. Identity transperence ensures that, no matter how a Workbook object was created - by filename or by promoting an Excel workbook - two Workbook objects are identical, if and only if the Excel workbooks, they are referring to, are identical.
|
156
173
|
|
157
|
-
|
174
|
+
Similarly, each Excel, Worksheet and a Range object in RobustExcelOle is a proxy of a corresponding Excel, Worksheet and a Range object in WIN32OLE. For these objects identity transperence holds as well.
|
158
175
|
|
159
176
|
=== Unobtrusively modifying a workbook
|
160
177
|
|
@@ -176,26 +193,26 @@ Options are the following:
|
|
176
193
|
|
177
194
|
Workbook.unobtrusively('spec/data/workbook.xls') do |book|
|
178
195
|
# some modification
|
179
|
-
|
180
|
-
|
196
|
+
worksheet = book[0]
|
197
|
+
worksheet[1,1] = "c"
|
181
198
|
end
|
182
199
|
|
183
200
|
The methods +for_reading+ and +for_modifying+ indicate unobtrusively reading or modifying.
|
184
201
|
|
185
202
|
Workbook.for_modifying('spec/data/workbook.xls') do |book|
|
186
203
|
# some modification
|
187
|
-
|
188
|
-
|
204
|
+
worksheet = book[0]
|
205
|
+
worksheet[1,1] = "c"
|
189
206
|
end
|
190
207
|
|
191
|
-
Note, that the methods +unobtrusively+, +for_reading+ and +for_modifying+ work not only for workbooks opened via RobustExcelOle, but
|
208
|
+
Note, that the methods +unobtrusively+, +for_reading+ and +for_modifying+ work not only for workbooks opened via RobustExcelOle, but connect to workbooks opened outside RobustExcelOle as well.
|
192
209
|
|
193
210
|
=== Retaining the saved-status
|
194
211
|
|
195
212
|
This method ensures keeping the save status of the workbook
|
196
213
|
|
197
|
-
|
198
|
-
|
214
|
+
workbook = Workbook.open('spec/data/workbook.xls')
|
215
|
+
workbook.retain_saved do
|
199
216
|
# some reading or modifying
|
200
217
|
end
|
201
218
|
|
@@ -203,7 +220,7 @@ This method ensures keeping the save status of the workbook
|
|
203
220
|
|
204
221
|
This method finds out whether the Excel workbook that is referenced by the Workbook object responds to methods.
|
205
222
|
|
206
|
-
|
223
|
+
workbook.alive?
|
207
224
|
# => true
|
208
225
|
|
209
226
|
== Code
|
data/docs/README_ranges.rdoc
CHANGED
@@ -8,39 +8,39 @@ RobustExcelOle enables to read and write the contents of ranges and cells in wor
|
|
8
8
|
|
9
9
|
Suppose you have opened a workbook.
|
10
10
|
|
11
|
-
|
11
|
+
workbook = Workbook.open('spec/data/workbook.xls', :visible => true)
|
12
12
|
|
13
13
|
We access the first worksheet:
|
14
14
|
|
15
|
-
|
15
|
+
worksheet = workbook.sheet(1)
|
16
16
|
|
17
17
|
Now we can access a range consisting of one cell by providing the row and and the column of a cell. With help of VBA methods you can put
|
18
18
|
|
19
|
-
|
19
|
+
worksheet.Range(sheet.Cells(1,2))
|
20
20
|
|
21
21
|
or using RobustExcelOle
|
22
22
|
|
23
|
-
range =
|
23
|
+
range = worksheet.range([1,2])
|
24
24
|
|
25
25
|
or, using the A1-format,
|
26
26
|
|
27
|
-
range =
|
27
|
+
range = worksheet.range("B1")
|
28
28
|
|
29
29
|
Similarly you can access a rectangular range. Using VBA methods we provide the row and column of the top left cell and the row and column of the bottum right cell.
|
30
30
|
|
31
|
-
range =
|
31
|
+
range = worksheet.Range(sheet.Cells(1,1), sheet.Cells(3,4))
|
32
32
|
|
33
33
|
In RobustExcelOle we would supply the rows and columns as integer ranges.
|
34
34
|
|
35
|
-
range =
|
35
|
+
range = worksheet.range([1..3,1..4])
|
36
36
|
|
37
37
|
or, using the A1-format
|
38
38
|
|
39
|
-
range =
|
39
|
+
range = worksheet.range([1..3,"A".."D"])
|
40
40
|
|
41
41
|
or
|
42
42
|
|
43
|
-
range =
|
43
|
+
range = worksheet.range(["A1:D3"])
|
44
44
|
|
45
45
|
You can read the values by
|
46
46
|
|
@@ -75,72 +75,72 @@ or with help of RobustExcelOle
|
|
75
75
|
|
76
76
|
You can also copy a range into another worksheet in another workbook.
|
77
77
|
|
78
|
-
|
78
|
+
workbook2 = Workbook.open('spec/data/another_workbook.xls', :excel => :new, :visible => true)
|
79
79
|
range.copy([4,5],book2.sheet(3))
|
80
80
|
|
81
81
|
Now we define a name that refers to a range consisting of only the first cell, i.e. the 1st row and 1st column. Using VBA methods, you can use
|
82
82
|
|
83
|
-
|
83
|
+
workbook.Names.Add("Name" => "name", "RefersToR1C1" => "=Z1S1")
|
84
84
|
|
85
85
|
RobustExcelOle provides the method +add_name+.
|
86
86
|
|
87
|
-
|
87
|
+
workbook.add_name("name",[1,1])
|
88
88
|
|
89
89
|
We define a name of a rectangular range. With help of VBA methods this is done by supplying the row and column of the top left cell and the row and columns of the bottum right cell of the range.
|
90
90
|
|
91
|
-
|
91
|
+
workbook.Names.Add("Name" => "name", "RefersToR1C1" => "=Z1S3:Z2S4")
|
92
92
|
|
93
|
-
Using RobustExcelOle
|
93
|
+
Using RobustExcelOle defining a name referring to a rectangular range is done by providing the rows and columns as integer range.
|
94
94
|
|
95
|
-
|
95
|
+
workbook.add_name("name",[1..2,3..4])
|
96
96
|
|
97
97
|
Now we can assign a value to that named range. With help of VBA methods this can be done by
|
98
98
|
|
99
|
-
|
99
|
+
workbook.Names.Item("name").RefersToRange.Value = [["foo", "bar"],[1.0, nil]]
|
100
100
|
|
101
101
|
Now we can read value of that range.
|
102
102
|
|
103
|
-
|
103
|
+
workbook.Names.Item("rec_name").RefersToRange.Value
|
104
104
|
=> [["foo", "bar"],[1.0, nil]]
|
105
105
|
|
106
106
|
or
|
107
107
|
|
108
|
-
|
108
|
+
workbook["name"]
|
109
109
|
=> [["foo", "bar"],[1.0, nil]]
|
110
110
|
|
111
111
|
Finally we can rename a range, and delete the name of a range. With help of VBA methods this can be achieved by
|
112
112
|
|
113
|
-
|
114
|
-
|
113
|
+
workbook.Names.Item("name").Name = "new_name"
|
114
|
+
workbook.Names.Item("new_name").Delete
|
115
115
|
|
116
116
|
Using RobustExcelOle, we write
|
117
117
|
|
118
|
-
|
119
|
-
|
118
|
+
workbook.rename_range("name", "new_name")
|
119
|
+
workbook.delete_name("name")
|
120
120
|
|
121
121
|
Now we can read the value of cell simply by providing the row and the column
|
122
122
|
|
123
|
-
|
123
|
+
worksheet.Cells.Item(1,1).Value
|
124
124
|
|
125
125
|
or with RobustExcelOle
|
126
126
|
|
127
|
-
|
127
|
+
worksheet[1,1].Value
|
128
128
|
=> "foo
|
129
129
|
|
130
130
|
or
|
131
131
|
|
132
|
-
|
132
|
+
worksheet[1,1].v
|
133
133
|
=> "foo"
|
134
134
|
|
135
135
|
Similarly, you can write a cell.
|
136
136
|
|
137
|
-
|
137
|
+
worksheet.Cells.Item(1,1).Value = "new_value"
|
138
138
|
|
139
139
|
or using RobustExcelOle
|
140
140
|
|
141
|
-
|
141
|
+
worksheet[1,1] = "new_value"
|
142
142
|
|
143
|
-
|
143
|
+
In the following some details are being summarized.
|
144
144
|
|
145
145
|
=== Accessing a range
|
146
146
|
|
@@ -148,51 +148,51 @@ You can access a range via its address or defined name. RobustExcelOle allows th
|
|
148
148
|
|
149
149
|
For example, you can access a range consisting of one cell by providing the row and and the column of a cell.
|
150
150
|
|
151
|
-
range =
|
151
|
+
range = worksheet.range([1,1])
|
152
152
|
|
153
153
|
Using the A1-format and R1C1-format you write
|
154
154
|
|
155
|
-
range =
|
155
|
+
range = worksheet.range("A1")
|
156
156
|
|
157
157
|
and
|
158
158
|
|
159
|
-
range =
|
159
|
+
range = worksheet.range("Z1S1")
|
160
160
|
|
161
161
|
respectively.
|
162
162
|
|
163
163
|
You can access a rectangular range by providing the row and column of the top left cell and the row and column of the bottum right cell.
|
164
164
|
|
165
|
-
range =
|
165
|
+
range = worksheet.range([1..3,1..4])
|
166
166
|
|
167
167
|
or using the A1-format.
|
168
168
|
|
169
|
-
range =
|
169
|
+
range = worksheet.range([1..3,"A".."D"])
|
170
170
|
|
171
171
|
or
|
172
172
|
|
173
|
-
range =
|
173
|
+
range = worksheet.range("A1:D3")
|
174
174
|
|
175
175
|
or using the R1C1-format
|
176
176
|
|
177
|
-
range =
|
177
|
+
range = worksheet.range("Z1S1:Z3S4")
|
178
178
|
|
179
179
|
Infinite ranges are defined, e.g., by setting the rows or columns to +nil+
|
180
180
|
|
181
|
-
range =
|
182
|
-
range =
|
181
|
+
range = worksheet.range([1..3,nil])
|
182
|
+
range = worksheet.range([nil,"A".."B"])
|
183
183
|
|
184
184
|
Using the A1-format you write
|
185
185
|
|
186
|
-
range =
|
187
|
-
range =
|
186
|
+
range = worksheet.range("1:3")
|
187
|
+
range = worksheet.range("A:B")
|
188
188
|
|
189
189
|
You can also apply relative references by using brackets, e.g.
|
190
190
|
|
191
|
-
range =
|
191
|
+
range = worksheet.range("Z[1]S1:Z3S[4]")
|
192
192
|
|
193
193
|
or
|
194
194
|
|
195
|
-
range =
|
195
|
+
range = worksheet.range([[1]..3,2..[4]])
|
196
196
|
|
197
197
|
You get the values of the range as flat array with help of
|
198
198
|
|
@@ -200,17 +200,17 @@ You get the values of the range as flat array with help of
|
|
200
200
|
|
201
201
|
You can access a range via its defined name with
|
202
202
|
|
203
|
-
range =
|
203
|
+
range = worksheet.range("name")
|
204
204
|
|
205
205
|
=== Copying a range
|
206
206
|
|
207
207
|
Let's assume, you have a source range
|
208
208
|
|
209
|
-
range =
|
209
|
+
range = worksheet.range(1..2,3..5)
|
210
210
|
|
211
211
|
or, in A1-format,
|
212
212
|
|
213
|
-
range =
|
213
|
+
range = worksheet.range("C1:E2")
|
214
214
|
|
215
215
|
To copy it to the destination range (3..4,6..8), you can use
|
216
216
|
|
@@ -234,47 +234,47 @@ Note that when you don't copy the values only but all formating as well, and you
|
|
234
234
|
|
235
235
|
You can (re-) define a name referring to a range by stating its name, and the address. The address is given by integer-range-format, r1c1-format or a1-format. For example, you can define a name for a rectangular range by
|
236
236
|
|
237
|
-
|
237
|
+
workbook.add_name("name",[1..2,3..5])
|
238
238
|
|
239
239
|
or
|
240
240
|
|
241
|
-
|
241
|
+
workbook.add_name("name","Z1S3:Z2S5")
|
242
242
|
|
243
243
|
or
|
244
244
|
|
245
|
-
|
245
|
+
workbook.add_name("name", "C1:E2")
|
246
246
|
|
247
247
|
Similarly you can define a name referring to a cell
|
248
248
|
|
249
|
-
|
249
|
+
workbook.add_name("name",[1,1])
|
250
250
|
|
251
251
|
or
|
252
252
|
|
253
|
-
|
253
|
+
workbook.add_name("name","Z1S1")
|
254
254
|
|
255
255
|
or
|
256
256
|
|
257
|
-
|
257
|
+
workbook.add_name("name","A1")
|
258
258
|
|
259
259
|
and infinite ranges
|
260
260
|
|
261
|
-
|
261
|
+
workbook.add_name("name",[1..2,nil])
|
262
262
|
|
263
263
|
or
|
264
264
|
|
265
|
-
|
265
|
+
workbook.add_name("name", "Z1:Z2")
|
266
266
|
|
267
267
|
or
|
268
268
|
|
269
|
-
|
269
|
+
workbook.add_name("name","1:2")
|
270
270
|
|
271
271
|
Furthermore, you can define a name using relative references with the r1c1-format.
|
272
272
|
|
273
|
-
|
273
|
+
workbook.add_name("name", "Z1S[1]:Z[2]S4")
|
274
274
|
|
275
275
|
or
|
276
276
|
|
277
|
-
|
277
|
+
workbook.add_name("name", [1..[2],[1]..4])
|
278
278
|
|
279
279
|
You can do the same for an worksheet or Excel object.
|
280
280
|
|
@@ -282,43 +282,43 @@ You can do the same for an worksheet or Excel object.
|
|
282
282
|
|
283
283
|
Assume you have opened a workbook:
|
284
284
|
|
285
|
-
|
285
|
+
workbook = Workbook.open('spec/data/workbook.xls', :visible => true)
|
286
286
|
|
287
287
|
You can get the contents of a range with a defined name with help of the method [] or +namevalue_glob+.
|
288
288
|
|
289
|
-
|
289
|
+
workbook["name"]
|
290
290
|
=> "value"
|
291
291
|
|
292
292
|
or
|
293
293
|
|
294
|
-
|
294
|
+
workbook.namevalue_glob("name")
|
295
295
|
=> "value"
|
296
296
|
|
297
297
|
Using +namevalue_glob+, via the option +:default+ you can provide a value that is returned when the name cannot be found or some other error would occur.
|
298
298
|
|
299
|
-
|
299
|
+
workbook.namvalue_glob("name", :default => "default_value")
|
300
300
|
|
301
301
|
You can set the contents of a range with
|
302
302
|
|
303
|
-
|
303
|
+
workbook["name"] = "new_value"
|
304
304
|
|
305
305
|
or
|
306
306
|
|
307
|
-
|
307
|
+
workbook.set_namevalue_glob("name", "new_value")
|
308
308
|
|
309
309
|
The method []= colors the written cell, if you have specified the color of the changed range via the method Workbook#color before, e.g.
|
310
310
|
|
311
|
-
|
312
|
-
|
311
|
+
workbook.color_if_modified = 4
|
312
|
+
workbook["name"] = "new_value"
|
313
313
|
|
314
314
|
Similarly, the contents of a named range can be read and modified in a worksheet
|
315
315
|
|
316
|
-
|
316
|
+
worksheet = workbook.sheet(1)
|
317
317
|
|
318
|
-
|
318
|
+
worksheet["name"]
|
319
319
|
=> value
|
320
320
|
|
321
|
-
|
321
|
+
worksheet["name"] = "new_value"
|
322
322
|
|
323
323
|
or an Application object.
|
324
324
|
|
@@ -334,7 +334,7 @@ or an Application object.
|
|
334
334
|
|
335
335
|
The contents of locally defined ranges can be read by
|
336
336
|
|
337
|
-
|
337
|
+
worksheet.namevalue("name")
|
338
338
|
=> "value"
|
339
339
|
|
340
340
|
or
|
@@ -344,7 +344,7 @@ or
|
|
344
344
|
|
345
345
|
and be modified by
|
346
346
|
|
347
|
-
|
347
|
+
worksheet.set_namevalue("name", "value")
|
348
348
|
|
349
349
|
or
|
350
350
|
|
@@ -352,54 +352,54 @@ or
|
|
352
352
|
|
353
353
|
Similarly to namevalue, you can provide a default value that is returned when ocurring an error.
|
354
354
|
|
355
|
-
|
355
|
+
worksheet.namevalue("name", :default => "default_value")
|
356
356
|
|
357
357
|
=== Accessing a cell
|
358
358
|
|
359
359
|
You can read a cell from a sheet object by providing the row and the column. For example, the following lines provide the value of the first cell (first row, first column):
|
360
360
|
|
361
|
-
|
361
|
+
worksheet[1,1].Value
|
362
362
|
=> "foo
|
363
363
|
|
364
364
|
or
|
365
365
|
|
366
|
-
|
366
|
+
worksheet.cellval(1,1)
|
367
367
|
=> "foo"
|
368
368
|
|
369
369
|
Similarly, you can write a cell.
|
370
370
|
|
371
|
-
|
371
|
+
worksheet[1,1] = "new_value"
|
372
372
|
|
373
373
|
or
|
374
374
|
|
375
|
-
|
375
|
+
worksheet.set_cellval(1,1,"new_value")
|
376
376
|
|
377
377
|
=== Accessing rows and columns
|
378
378
|
|
379
379
|
The methods Worksheet#each, Worksheet#each_row and Worksheet#each_column enable to access each cell, row and column, respectively.
|
380
380
|
|
381
|
-
|
381
|
+
worksheet.each do |cell|
|
382
382
|
# do something with cell
|
383
383
|
# read every row, every column
|
384
384
|
end
|
385
385
|
|
386
|
-
|
386
|
+
worksheet.each_row do |row|
|
387
387
|
# do something with row
|
388
388
|
end
|
389
389
|
|
390
|
-
|
390
|
+
worksheet.each_column do |column|
|
391
391
|
# do something with column
|
392
392
|
end
|
393
393
|
|
394
394
|
You access a range of a row by giving the number of the row, and optionally, the range of the cell numbers.
|
395
395
|
|
396
|
-
|
397
|
-
|
396
|
+
worksheet.row_range(1) # => first row
|
397
|
+
worksheet.row_range(1, 1..3 ) # => first three cells of the first row
|
398
398
|
|
399
399
|
Simarly you can access a range of a column.
|
400
400
|
|
401
|
-
|
402
|
-
|
401
|
+
worksheet.col_range(3) # => third column
|
402
|
+
worksheet.col_range(3, 1..2) # => first two cells of the third column
|
403
403
|
|
404
404
|
Within a row or column range you can access a certain cell.
|
405
405
|
|