robust_excel_ole 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Changelog +9 -1
- data/README.rdoc +122 -169
- data/README_detail.rdoc +10 -4
- data/README_development.rdoc +5 -0
- data/README_excel.rdoc +145 -0
- data/README_open.rdoc +202 -0
- data/README_ranges.rdoc +134 -0
- data/README_save_close.rdoc +77 -0
- data/README_sheet.rdoc +71 -0
- data/examples/open_save_close/example_control_to_excel.rb +2 -2
- data/lib/robust_excel_ole/book.rb +80 -44
- data/lib/robust_excel_ole/excel.rb +30 -7
- data/lib/robust_excel_ole/general.rb +1 -1
- data/lib/robust_excel_ole/reo_common.rb +65 -49
- data/lib/robust_excel_ole/sheet.rb +2 -6
- data/lib/robust_excel_ole/version.rb +1 -1
- data/spec/book_spec.rb +5 -5
- data/spec/book_specs/book_misc_spec.rb +69 -1
- data/spec/book_specs/book_open_spec.rb +11 -17
- data/spec/book_specs/book_unobtr_spec.rb +1 -1
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +51 -53
- data/spec/general_spec.rb +2 -2
- data/spec/reo_common_spec.rb +1 -1
- data/spec/sheet_spec.rb +14 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c842707a42ecf41966021d1c4b46de412d598a5
|
4
|
+
data.tar.gz: 2ac4026904e8fab21e087bdeaae0b123d14d41cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93acf54dc229f58aa5e1cb5b1ad59f0d28fa77e513af12adc52e1bf5daaeae2920d3206637a196cc7dd78d2b40c5feb2bbfb2599e88d70ac273396dd5545bf9e
|
7
|
+
data.tar.gz: 468faa1ad3c10feeb8316fc9de2d827ba1be7444108af0c0f9cd0d7d89ba33538e87e9af5e9ae694a11ca25bef2b2ccafbd475880a753923417a011bc85baaa0
|
data/.gitignore
CHANGED
data/Changelog
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
-
## [1.1.
|
4
|
+
## [1.1.4] - 2018-10-6
|
5
|
+
|
6
|
+
### Added
|
7
|
+
- Excel#for_this_instance
|
8
|
+
- Excel#for_all_workbooks
|
9
|
+
- Book#for_this_workbooks
|
10
|
+
- Book#reopen : wirth options
|
11
|
+
|
12
|
+
## [1.1.3] - 2018-13-5
|
5
13
|
|
6
14
|
## [1.1.2] - 2018-10-5
|
7
15
|
|
data/README.rdoc
CHANGED
@@ -4,7 +4,7 @@ This ruby gem automates reading, modifying, and writing Excel files. It is desig
|
|
4
4
|
RobustExcelOle deals with various cases of Excel and user behaviour,
|
5
5
|
and implements workarounds for some Excel bugs.
|
6
6
|
The gem provides convenient methods for common tasks, and facilitates referenced libraries.
|
7
|
-
It
|
7
|
+
It can be used for any recent Excel Office version, and is tested for Excel 2010.
|
8
8
|
|
9
9
|
RobustExcelOle works by sending VBA methods via Win32OLE.
|
10
10
|
It implements a management system and keeps track of Excel files and Excel instances.
|
@@ -19,287 +19,240 @@ It implements a management system and keeps track of Excel files and Excel insta
|
|
19
19
|
|
20
20
|
== Usage
|
21
21
|
|
22
|
+
RobustExcelOle can be used either for scripts
|
23
|
+
|
22
24
|
require 'robust_excel_ole'
|
23
25
|
include RobustExcelOle
|
24
26
|
|
25
|
-
or
|
26
|
-
|
27
|
-
reo.bat
|
28
|
-
|
29
|
-
== Description
|
30
|
-
|
31
|
-
In the following, some features of RobustExcelOle are depicted.
|
32
|
-
|
33
|
-
RobustExcelOle enables opening and processing Excel files (or workbooks) in several Excel instances. Using more than one Excel process allows, e.g., running a script that operates in one Excel instance, while a user (or another script) modifies workbooks in another Excel instance.
|
34
|
-
|
35
|
-
For example, suppose you want to open a workbook and make it visible.
|
36
|
-
|
37
|
-
book1 = Workbook.open('workbook.xls', :visible => true)
|
38
|
-
|
39
|
-
Now we want to open another workbook in a different Excel instance.
|
40
|
-
|
41
|
-
book2 = Workbook.open('workbook2.xls', :force => {:excel => :new}, :visible => true)
|
42
|
-
|
43
|
-
We can also create a third Excel instance and open another workbook in this instance.
|
44
|
-
|
45
|
-
excel1 = Excel.create
|
46
|
-
book3 = Workbook.open('workbook3.xls', :force => {:excel => excel1}, :visible => true)
|
47
|
-
|
48
|
-
Another feature that RobustExcelOle povides is reopening workbooks after closing them.
|
49
|
-
|
50
|
-
book1.close
|
51
|
-
book1.reopen
|
52
|
-
|
53
|
-
A workbook is opened by default in the Excel instance where it was open before most recently.
|
54
|
-
|
55
|
-
book1.close
|
56
|
-
book1 = Workbook.open('workbook.xls')
|
57
|
-
|
58
|
-
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.
|
27
|
+
or as a console
|
59
28
|
|
60
|
-
|
29
|
+
reo
|
61
30
|
|
62
|
-
The
|
31
|
+
The call of the console will include RobustExcelOle for you.
|
63
32
|
|
64
|
-
|
33
|
+
The following examples can be used for both scripts and console. You can try them directly.
|
65
34
|
|
66
|
-
|
67
|
-
|
68
|
-
For example, suppose you want to process a list of workbooks. RobustExcelOle allows to rapidly open, manipulate, close and save these workbooks. Now assume, the workbook "workbook.xls" is being processed, while the user has opened this workbook, has modified but not saved it yet. Excel would prompt a message and ask the user what to do. RobustExcelOle solves this conflict by using an option that states whether the changes of the user should be saved (accepted) or discarded before opening the workbook:
|
69
|
-
|
70
|
-
book = Workbook.open('workbook.xls', :if_unsaved => :accept)
|
35
|
+
== Description
|
71
36
|
|
72
|
-
|
37
|
+
RobustExcelOle enables opening and processing Excel files (or workbooks). In the following example, we want to open a workbook, modify a cell, save and close the workbook.
|
73
38
|
|
74
|
-
|
75
|
-
...
|
76
|
-
book2 = Workbook.open('workbook.xls', :if_obstructed => :forget)
|
39
|
+
First we open a workbook.
|
77
40
|
|
78
|
-
|
41
|
+
workbook = Workbook.open 'spec/data/workbook.xls'
|
79
42
|
|
80
|
-
|
43
|
+
We can also open the workbook using a block, similar to, e.g., +File.open+.
|
81
44
|
|
82
|
-
Workbook.
|
83
|
-
# do something
|
84
|
-
end
|
85
|
-
|
86
|
-
Workbook.for_reading('workbook.xls') do |book|
|
45
|
+
Workbook.open('spec/data/workbook.xls') do |workbook|
|
87
46
|
# do something
|
88
47
|
end
|
89
48
|
|
90
|
-
|
49
|
+
Now we have a Workbook object that wraps a win32ole object. That is, you can send any win32ole (VBA) method to it. For some common tasks and for considering various cases of Excel and user behaviour, more convenient methods are implemented.
|
91
50
|
|
92
|
-
|
93
|
-
Identity transparency means that the same Workbook objects refer to the same Excel files, and vice versa.
|
94
|
-
In other words, a Workbook objects is a proxy of an Excel file.
|
51
|
+
For example, we can make the workbook visible, using
|
95
52
|
|
96
|
-
|
53
|
+
workbook.visible = true
|
97
54
|
|
98
|
-
|
99
|
-
|
100
|
-
Let's have a look at an example. Suppose, we want to open a workbook.
|
101
|
-
|
102
|
-
book = Workbook.open('workbook.xls')
|
103
|
-
|
104
|
-
We could do this in a block as well. The semantics is similar to, e.g., +File.open+.
|
105
|
-
|
106
|
-
Workbook.open('workbook.xls') do |book|
|
107
|
-
# do something with book
|
108
|
-
end
|
55
|
+
or
|
109
56
|
|
110
|
-
|
57
|
+
workbook.for_this_workbook(:visible => true)
|
111
58
|
|
112
|
-
|
59
|
+
You can also open a workbook and make it visible in one step.
|
113
60
|
|
114
|
-
|
61
|
+
workbook = Workbook.open('spec/data/workbook.xls', :visible => true)
|
115
62
|
|
116
|
-
|
63
|
+
or
|
117
64
|
|
118
|
-
|
65
|
+
workbook = Workbook.open('spec/data/workbook.xls', :v => true)
|
119
66
|
|
120
|
-
|
67
|
+
Then we read the value of a cell.
|
121
68
|
|
122
|
-
|
69
|
+
value = workbook['firstcell']
|
70
|
+
# => "foo"
|
123
71
|
|
124
|
-
|
72
|
+
Now we write a new value in this cell.
|
125
73
|
|
126
|
-
|
74
|
+
workbook['firstcell'] = "new"
|
127
75
|
|
128
|
-
|
76
|
+
For more details about reading and writing contents of cells and ranges see {README_ranges}[https://github.com/Thomas008/robust_excel_ole/blob/master/README_ranges.rdoc]
|
129
77
|
|
130
|
-
|
78
|
+
Then we save the workbook.
|
131
79
|
|
132
|
-
|
80
|
+
workbook.save
|
133
81
|
|
134
|
-
|
82
|
+
We can also save the workbook under a different name.
|
135
83
|
|
136
|
-
|
84
|
+
workbook.save_as('spec/data/new_workbook.xls')
|
137
85
|
|
138
|
-
|
86
|
+
Finally we close the workbook.
|
139
87
|
|
140
|
-
|
88
|
+
workbook.close
|
141
89
|
|
142
|
-
|
90
|
+
We could also have saved and closed the workbook in one step.
|
143
91
|
|
144
|
-
|
92
|
+
workbook.close(:if_unsaved => :save)
|
145
93
|
|
146
|
-
|
94
|
+
For more details about saving and closing workbooks see
|
95
|
+
{README_save_close}[https://github.com/Thomas008/robust_excel_ole/blob/master/README_save_close.rdoc]
|
147
96
|
|
148
|
-
|
97
|
+
One special feature of RobustExcelOle is that it enables reopening workbooks.
|
149
98
|
|
150
|
-
|
99
|
+
workbook.reopen
|
151
100
|
|
152
|
-
|
101
|
+
Technically, this feature is implemented in such way, that a Workbook object is a proxy of an Excel 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 the same Workbook objects refer to the same workbooks, and vice versa.
|
153
102
|
|
154
|
-
|
103
|
+
=== Using Excel instances
|
155
104
|
|
156
|
-
|
105
|
+
You can start a new Excel instance with
|
157
106
|
|
158
|
-
|
107
|
+
excel1 = Excel.create
|
159
108
|
|
160
109
|
or
|
161
|
-
|
162
|
-
sheet1 = book2.first_sheet
|
163
110
|
|
164
|
-
|
111
|
+
excel1 = Excel.new(:reuse => false)
|
165
112
|
|
166
|
-
|
113
|
+
You can also have an Excel object by connecting to the already running Excel instance.
|
167
114
|
|
168
|
-
|
115
|
+
excel2 = Excel.current
|
169
116
|
|
170
|
-
|
117
|
+
or
|
171
118
|
|
172
|
-
|
119
|
+
excel2 = Excel.new(:reuse => true)
|
173
120
|
|
174
|
-
|
121
|
+
We close the Excel instance using
|
175
122
|
|
176
|
-
|
123
|
+
excel1.close
|
177
124
|
|
178
|
-
|
125
|
+
Closed Excel instances can be reopened.
|
179
126
|
|
180
|
-
|
127
|
+
excel1.recreate(:reopen_workbooks => true, :visible => true)
|
181
128
|
|
182
|
-
|
129
|
+
Closing all Excel instances is done by
|
183
130
|
|
184
|
-
|
131
|
+
Excel.close_all(:if_unsaved => :forget)
|
185
132
|
|
186
|
-
|
133
|
+
For hard terminating all Excel processes you can use
|
187
134
|
|
188
|
-
|
135
|
+
Excel.kill_all
|
189
136
|
|
190
|
-
|
137
|
+
For more details about creating Excel instances see README_excel[https://github.com/Thomas008/robust_excel_ole/blob/master/README_excel_rdoc]
|
191
138
|
|
192
|
-
|
139
|
+
=== Opening workbooks in several Excel instances
|
193
140
|
|
194
|
-
|
141
|
+
RobustExcelOle enables opening and processing workbooks in several Excel instances. Using more than one Excel process allows, e.g., running a script that operates in one Excel instance, while a user (or another script) modifies workbooks in another Excel instance.
|
195
142
|
|
196
|
-
|
143
|
+
For example, suppose you want to open a workbook.
|
197
144
|
|
198
|
-
|
145
|
+
workbook1 = Workbook.open('spec/data/workbook.xls')
|
199
146
|
|
200
|
-
|
147
|
+
Now we want to open another workbook in a different Excel instance.
|
201
148
|
|
202
|
-
|
149
|
+
workbook2 = Workbook.open('spec/data/different_workbook.xls', :excel => :new)
|
203
150
|
|
204
|
-
We can
|
151
|
+
We can also create a third Excel instance and open another workbook in this instance.
|
205
152
|
|
206
|
-
|
153
|
+
excel1 = Excel.create
|
154
|
+
book3 = Workbook.open('spec/data/another_workbook.xls', :excel => excel1)
|
207
155
|
|
208
|
-
|
156
|
+
A workbook is opened by default in the Excel instance where it was open before most recently.
|
209
157
|
|
210
|
-
|
158
|
+
book1.close
|
159
|
+
book1 = Workbook.open('spec/data/workbook.xls')
|
211
160
|
|
212
|
-
|
161
|
+
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.
|
213
162
|
|
214
|
-
|
163
|
+
workbook1 = Workbook.open('spec/data/workbook.xls', :default => {:excel => :new})
|
215
164
|
|
216
|
-
|
165
|
+
Without the option +:default+, the workbook is forced to be opened in the current, new or given Excel instance, no matter if and where it was opened before, e.g.
|
217
166
|
|
218
|
-
|
167
|
+
workbook2 = Workbook.open('spec/data/workbook.xls', :excel => excel1)
|
219
168
|
|
220
|
-
|
169
|
+
As a further feature, RobustExcelOle allows processing workbooks, while still supporting user's interactions: The commands enable to open, close, reopen, read, modify, write and save Excel files, without the need of the user's interaction, and even without the user noticing. Thus, while running a script containing RobustExcelOle commands, the user can open and process Excel files in any Excel instances at any time. RobustExcelOle manages the complex cases of conflicts that might occur such that the user does not need to interfere and the script can continue.
|
221
170
|
|
222
|
-
|
171
|
+
For example, suppose you want to process a list of workbooks. RobustExcelOle allows to rapidly open, manipulate, close and save these workbooks. Now assume, the workbook "workbook.xls" is being processed, while the user has opened this workbook, has modified but not saved it yet. Excel would prompt a message and ask the user what to do. RobustExcelOle solves this conflict by using an option that states whether the changes of the user should be saved (accepted) or discarded before opening the workbook, e.g.
|
223
172
|
|
224
|
-
|
173
|
+
workbook = Workbook.open('workbook.xls', :if_unsaved => :accept)
|
225
174
|
|
226
|
-
|
227
|
-
|
228
|
-
book2.save(:discoloring => true)
|
175
|
+
Similarly, if the user has opened a workbook that has the same name but a different path, the conflict is solved via an option.
|
229
176
|
|
230
|
-
|
177
|
+
workbook1 = Workbook.open('spec/data/workbook.xls')
|
178
|
+
# do something
|
179
|
+
workbook2 = Workbook.open('spec/data/more/workbook.xls', :if_obstructed => :forget)
|
231
180
|
|
232
|
-
|
181
|
+
Finally, 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.
|
233
182
|
|
234
|
-
|
183
|
+
Workbook.for_modifying('workbook.xls') do |book|
|
184
|
+
# do something
|
185
|
+
end
|
235
186
|
|
236
|
-
|
187
|
+
Workbook.for_reading('workbook.xls') do |book|
|
188
|
+
# do something
|
189
|
+
end
|
237
190
|
|
238
|
-
|
191
|
+
For more details about opening and closing workbooks in Excel instances see {README_open}[https://github.com/Thomas008/robust_excel_ole/blob/master/README_open.rdoc]
|
239
192
|
|
240
|
-
|
193
|
+
=== Processing worksheets
|
241
194
|
|
242
|
-
|
195
|
+
Assime you have opened a workbook
|
196
|
+
|
197
|
+
workbook = Workbook.open('spec/data/workbook.xls')
|
243
198
|
|
244
|
-
|
199
|
+
You access the first worksheet by
|
245
200
|
|
246
|
-
|
201
|
+
sheet = workbook.sheet(1)
|
247
202
|
|
248
203
|
or
|
249
204
|
|
250
|
-
|
205
|
+
sheet = workbook.sheet('Sheet1')
|
251
206
|
|
252
|
-
|
207
|
+
or
|
208
|
+
|
209
|
+
sheet = workbook.first_sheet
|
253
210
|
|
254
|
-
|
211
|
+
You can read and change the worksheet name.
|
255
212
|
|
256
|
-
|
213
|
+
sheet.name
|
214
|
+
# => "Sheet1"
|
257
215
|
|
258
|
-
|
216
|
+
sheet.name = "new_sheet"
|
259
217
|
|
260
|
-
|
218
|
+
You can read the first three cells of the first row
|
261
219
|
|
262
|
-
|
220
|
+
sheet.row_range(1, 1..3).values # => ["foo","workbook","sheet1"]
|
263
221
|
|
264
|
-
|
222
|
+
and the third column
|
265
223
|
|
266
|
-
|
267
|
-
|
268
|
-
or
|
224
|
+
sheet.col_range(3).values # => ["sheet1", 2.0, 4.0]
|
269
225
|
|
270
|
-
|
226
|
+
You can read the first cell, using
|
271
227
|
|
272
|
-
|
228
|
+
sheet[1,1].value # => "foo"
|
273
229
|
|
274
|
-
|
230
|
+
or
|
275
231
|
|
276
|
-
|
232
|
+
sheet.row_range(1)[0].value # => "foo"
|
277
233
|
|
278
|
-
|
234
|
+
Then we modify it
|
279
235
|
|
280
|
-
|
236
|
+
sheet[1,1] = "hello"
|
281
237
|
|
282
|
-
|
238
|
+
You can get the value of a named range
|
283
239
|
|
284
|
-
|
240
|
+
sheet["firstcell"] # => "hello"
|
285
241
|
|
286
|
-
|
242
|
+
and set another value to that range.
|
287
243
|
|
288
|
-
|
244
|
+
sheet["firstcell"] = "new_value"
|
289
245
|
|
290
|
-
|
246
|
+
You can copy the first worksheet, name it and add it before the third worksheet.
|
291
247
|
|
292
|
-
|
248
|
+
workbook.add_or_copy_sheet(sheet, :as => "copied_name, :before => workbook.last_sheet)
|
293
249
|
|
294
|
-
|
250
|
+
For more details about processing worksheets see {README_sheet}[https://github.com/Thomas008/robust_excel_ole/blob/master/README_sheet.rdoc]
|
295
251
|
|
296
252
|
=== Development
|
297
253
|
|
298
|
-
|
299
|
-
|
300
|
-
=== More details
|
301
|
-
|
302
|
-
{README_detail.rdoc}[https://github.com/Thomas008/robust_excel_ole/blob/master/README_detail.rdoc]
|
254
|
+
For some details about developing RobustExcelOle see
|
255
|
+
{README_development}[https://github.com/Thomas008/robust_excel_ole/blob/master/README_development.rdoc]
|
303
256
|
|
304
257
|
=== Want to do more things
|
305
258
|
|