robust_excel_ole 1.1.4 → 1.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 +13 -0
- data/README.rdoc +107 -73
- data/{README_excel.rdoc → docs/README_excel.rdoc} +2 -0
- data/{README_open.rdoc → docs/README_open.rdoc} +12 -16
- data/{README_ranges.rdoc → docs/README_ranges.rdoc} +1 -1
- data/{README_save_close.rdoc → docs/README_save_close.rdoc} +0 -0
- data/{README_sheet.rdoc → docs/README_sheet.rdoc} +0 -0
- data/examples/open_save_close/example_control_to_excel.rb +3 -3
- data/examples/open_save_close/example_default_excel.rb +2 -2
- data/examples/open_save_close/example_force_excel.rb +2 -2
- data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +2 -2
- data/examples/open_save_close/example_if_obstructed_forget.rb +2 -2
- data/examples/open_save_close/example_if_obstructed_save.rb +2 -2
- data/examples/open_save_close/example_if_unsaved_accept.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_forget.rb +7 -3
- data/examples/open_save_close/example_if_unsaved_forget_more.rb +1 -1
- data/examples/open_save_close/example_read_only.rb +2 -2
- data/examples/open_save_close/example_rename_cells.rb +2 -2
- data/examples/open_save_close/example_reuse.rb +2 -2
- data/examples/open_save_close/example_simple.rb +1 -1
- data/examples/open_save_close/example_unobtrusively.rb +2 -2
- data/lib/robust_excel_ole/book.rb +75 -76
- data/lib/robust_excel_ole/bookstore.rb +9 -9
- data/lib/robust_excel_ole/excel.rb +5 -5
- data/lib/robust_excel_ole/general.rb +11 -4
- data/lib/robust_excel_ole/reo_common.rb +3 -0
- data/lib/robust_excel_ole/version.rb +1 -1
- data/robust_excel_ole.gemspec +2 -2
- data/spec/book_spec.rb +67 -63
- data/spec/book_specs/book_close_spec.rb +12 -10
- data/spec/book_specs/book_misc_spec.rb +279 -279
- data/spec/book_specs/book_open_spec.rb +258 -105
- data/spec/book_specs/book_save_spec.rb +28 -28
- data/spec/book_specs/book_unobtr_spec.rb +862 -164
- data/spec/bookstore_spec.rb +22 -22
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/different_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +256 -254
- data/spec/general_spec.rb +7 -7
- data/spec/ruby1.8.6_rspec2.14/book_spec.rb +1421 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +22 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +252 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +1070 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +1855 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +514 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +395 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +51 -0
- data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +1737 -0
- data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +495 -0
- data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +76 -0
- data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +42 -0
- data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +1820 -0
- data/spec/ruby1.8.6_rspec2.14/general_spec.rb +212 -0
- data/spec/ruby1.8.6_rspec2.14/range_spec.rb +131 -0
- data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +130 -0
- data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +663 -0
- data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +35 -0
- data/spec/sheet_spec.rb +2 -2
- metadata +28 -12
- data/README_detail.rdoc +0 -852
- data/README_development.rdoc +0 -5
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "rspec"
|
3
|
+
require 'tmpdir'
|
4
|
+
require "fileutils"
|
5
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
6
|
+
|
7
|
+
module RobustExcelOle::SpecHelpers
|
8
|
+
def create_tmpdir # :nodoc: #
|
9
|
+
tmpdir = Dir.mktmpdir
|
10
|
+
FileUtils.cp_r(File.join(File.dirname(__FILE__), 'data'), tmpdir)
|
11
|
+
tmpdir + '/data'
|
12
|
+
end
|
13
|
+
|
14
|
+
def rm_tmp(tmpdir) # :nodoc: #
|
15
|
+
FileUtils.remove_entry_secure(File.dirname(tmpdir))
|
16
|
+
end
|
17
|
+
|
18
|
+
# This method is almost copy of wycats's implementation.
|
19
|
+
# http://pochi.hatenablog.jp/entries/2010/03/24
|
20
|
+
def capture(stream) # :nodoc: #
|
21
|
+
begin
|
22
|
+
stream = stream.to_s
|
23
|
+
eval "$#{stream} = StringIO.new"
|
24
|
+
yield
|
25
|
+
result = eval("$#{stream}").string
|
26
|
+
ensure
|
27
|
+
eval("$#{stream} = #{stream.upcase}")
|
28
|
+
end
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
config.include RobustExcelOle::SpecHelpers
|
35
|
+
end
|
data/spec/sheet_spec.rb
CHANGED
@@ -48,7 +48,7 @@ describe Sheet do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should be a protected sheet" do
|
51
|
-
@protected_sheet.ProtectContents.should
|
51
|
+
@protected_sheet.ProtectContents.should be true
|
52
52
|
end
|
53
53
|
|
54
54
|
it "protected sheet can't be write" do
|
@@ -644,7 +644,7 @@ describe Sheet do
|
|
644
644
|
it "should raise an error for unknown methods or properties" do
|
645
645
|
expect{
|
646
646
|
@sheet.Foo
|
647
|
-
}.to raise_error
|
647
|
+
}.to raise_error
|
648
648
|
end
|
649
649
|
|
650
650
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: robust_excel_ole
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- traths
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 2.6.0
|
27
27
|
description: |-
|
28
|
-
RobustExcelOle automates
|
28
|
+
RobustExcelOle automates reading and writing Excel workbooks in Windows by using the win32ole library.
|
29
29
|
It is disigned to cope with several kinds of concurrency of both simultaneously running
|
30
30
|
Excel instances and simultanously happening user interactions.
|
31
31
|
RobustExcelOle deals with various cases of Excel (and user) behaviour,
|
@@ -45,15 +45,13 @@ files:
|
|
45
45
|
- Guardfile
|
46
46
|
- LICENSE
|
47
47
|
- README.rdoc
|
48
|
-
- README_detail.rdoc
|
49
|
-
- README_development.rdoc
|
50
|
-
- README_excel.rdoc
|
51
|
-
- README_open.rdoc
|
52
|
-
- README_ranges.rdoc
|
53
|
-
- README_save_close.rdoc
|
54
|
-
- README_sheet.rdoc
|
55
48
|
- Rakefile
|
56
49
|
- TodoList.md
|
50
|
+
- docs/README_excel.rdoc
|
51
|
+
- docs/README_open.rdoc
|
52
|
+
- docs/README_ranges.rdoc
|
53
|
+
- docs/README_save_close.rdoc
|
54
|
+
- docs/README_sheet.rdoc
|
57
55
|
- examples/edit_sheets/example_access_sheets_and_cells.rb
|
58
56
|
- examples/edit_sheets/example_adding_sheets.rb
|
59
57
|
- examples/edit_sheets/example_concating.rb
|
@@ -125,6 +123,24 @@ files:
|
|
125
123
|
- spec/helpers/key_sender.rb
|
126
124
|
- spec/range_spec.rb
|
127
125
|
- spec/reo_common_spec.rb
|
126
|
+
- spec/ruby1.8.6_rspec2.14/book_spec.rb
|
127
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb
|
128
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb
|
129
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb
|
130
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb
|
131
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb
|
132
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb
|
133
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb
|
134
|
+
- spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb
|
135
|
+
- spec/ruby1.8.6_rspec2.14/bookstore_spec.rb
|
136
|
+
- spec/ruby1.8.6_rspec2.14/cell_spec.rb
|
137
|
+
- spec/ruby1.8.6_rspec2.14/cygwin_spec.rb
|
138
|
+
- spec/ruby1.8.6_rspec2.14/excel_spec.rb
|
139
|
+
- spec/ruby1.8.6_rspec2.14/general_spec.rb
|
140
|
+
- spec/ruby1.8.6_rspec2.14/range_spec.rb
|
141
|
+
- spec/ruby1.8.6_rspec2.14/reo_common_spec.rb
|
142
|
+
- spec/ruby1.8.6_rspec2.14/sheet_spec.rb
|
143
|
+
- spec/ruby1.8.6_rspec2.14/spec_helper.rb
|
128
144
|
- spec/sheet_spec.rb
|
129
145
|
- spec/spec_helper.rb
|
130
146
|
- version.rb
|
@@ -155,6 +171,6 @@ rubyforge_project: robust_excel_ole
|
|
155
171
|
rubygems_version: 2.4.5.2
|
156
172
|
signing_key:
|
157
173
|
specification_version: 4
|
158
|
-
summary: RobustExcelOle automates processing Excel
|
159
|
-
library.
|
174
|
+
summary: RobustExcelOle automates processing Excel workbooks in Windows by using the
|
175
|
+
win32ole library.
|
160
176
|
test_files: []
|
data/README_detail.rdoc
DELETED
@@ -1,852 +0,0 @@
|
|
1
|
-
= RobustExcelOle
|
2
|
-
|
3
|
-
This ruby gem automates reading, modifying, and writing Excel files. It is designed to support several kinds of concurrency of both simultaneously running Excel instances and simultanously happening user interactions.
|
4
|
-
RobustExcelOle deals with various cases of Excel and user behaviour,
|
5
|
-
and implements workarounds for some Excel bugs.
|
6
|
-
The gem provides convenient methods for common tasks, and facilitates referenced libraries.
|
7
|
-
It supports Excel 2010.
|
8
|
-
|
9
|
-
RobustExcelOle works by sending VBA methods via Win32OLE.
|
10
|
-
It implements a management system and keeps track of Excel files and Excel instances.
|
11
|
-
|
12
|
-
== Requirements
|
13
|
-
|
14
|
-
* Ruby 1.8.6 or higher
|
15
|
-
|
16
|
-
== Install
|
17
|
-
|
18
|
-
gem install robust_excel_ole
|
19
|
-
|
20
|
-
== Usage
|
21
|
-
|
22
|
-
require 'robust_excel_ole'
|
23
|
-
include RobustExcelOle
|
24
|
-
|
25
|
-
or call the console
|
26
|
-
|
27
|
-
reo.bat
|
28
|
-
|
29
|
-
=== Opening a workbook.
|
30
|
-
|
31
|
-
Example:
|
32
|
-
|
33
|
-
book = Workbook.open('workbook.xls')
|
34
|
-
|
35
|
-
You can also open a workbook with a block.
|
36
|
-
The semantics is similar to, e.g., +File.open+.
|
37
|
-
|
38
|
-
Workbook.open('workbook.xls') do |book|
|
39
|
-
# do something
|
40
|
-
end
|
41
|
-
|
42
|
-
Options are the following:
|
43
|
-
|
44
|
-
+:default+:: if the workbook was already open, then use the properties of this workbook.
|
45
|
-
otherwise use the properties stated in +:default+
|
46
|
-
|
47
|
-
+:force+:: no matter whether the workbook was open before, use the properties stated in +:force+
|
48
|
-
|
49
|
-
+:excel+ and +:visible+ are options stated in +:default+ or +:force+
|
50
|
-
|
51
|
-
+:excel+:: specifies the Excel instance.
|
52
|
-
+:visible+:: makes the workbook visible or invisible
|
53
|
-
|
54
|
-
+:if_unsaved+:: specify behaviour if the workbook was unsaved (default: +new_excel+)
|
55
|
-
+:if_obstructed+:: specify behaviour if the workbook is blocked by another book (default: +new_excel+)
|
56
|
-
+:read_only+:: open in read-only mode (default: +false+)
|
57
|
-
+:check_compatibility:: check compatibility when saving
|
58
|
-
+:calculation+:: forces the calculation mode to be manual (:manual) or automatic (:automatic)
|
59
|
-
+:if_absent+:: specify behaviour if the workbook with the given file name does not exist if the workbook does not exist (default: +create+)
|
60
|
-
|
61
|
-
Here are some details:
|
62
|
-
|
63
|
-
The option +:excel+ :
|
64
|
-
|
65
|
-
Valid values are : +:current+ (or +:active+ or +:reuse+), +:new+, or a given Excel instance (default: +:current).
|
66
|
-
|
67
|
-
The option +:if_unsaved+ :
|
68
|
-
|
69
|
-
If a workbook contains unsaved changes and a new workbook with the same file name shall be opened, then
|
70
|
-
|
71
|
-
+:raise+:: Raise an exeption. Don't open the workbook.
|
72
|
-
+:accept+:: Let the unsaved workbook open.
|
73
|
-
+:forget+:: Discard any changes and reopen the workbook.
|
74
|
-
+:new_excel+:: Open the new workbook in a new Excel instance
|
75
|
-
+:alert+:: Give control to Excel.
|
76
|
-
|
77
|
-
The option +:if_obstructed+ :
|
78
|
-
|
79
|
-
If a workbook is open and a new workbook with same name and a different path is open, then
|
80
|
-
|
81
|
-
+:raise+:: Raise an exception. Don't open the workbook.
|
82
|
-
+:forget+:: Close the old workbook, open the new workbook.
|
83
|
-
+:save+:: Save the old workbook, close it, open the new workbook
|
84
|
-
+:close_if_saved+:: Close the old workbook and open the new workbook, if the old workbook is saved, otherwise raise an exception.
|
85
|
-
+:new_excel+:: Open the new workbook in a new Excel instance.
|
86
|
-
|
87
|
-
The option +:if_absent :
|
88
|
-
|
89
|
-
If the Excel file does not exists, then
|
90
|
-
|
91
|
-
+:create+:: Create a new Excel file
|
92
|
-
+:raise+:: Raise an exception.
|
93
|
-
|
94
|
-
Here are a few examples:
|
95
|
-
|
96
|
-
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
|
97
|
-
|
98
|
-
Workbook.open('workbook.xls', :default => {:excel => :current})
|
99
|
-
|
100
|
-
or
|
101
|
-
|
102
|
-
Workbook.open('workbook.xls')
|
103
|
-
|
104
|
-
In case you want to open such a workbook in a new Excel instance, then use
|
105
|
-
|
106
|
-
Workbook.open('workbook.xls', :default => {:excel => :new})
|
107
|
-
|
108
|
-
If you want to open a workbook in a new Excel instance, no matter if it was opened before, you can write
|
109
|
-
|
110
|
-
Workbook.open('workbook.xls', :force => {:excel => :new})
|
111
|
-
|
112
|
-
You can also specify an Excel instance
|
113
|
-
|
114
|
-
excel1 = Excel.create
|
115
|
-
# something
|
116
|
-
Workbook.open('workbook.xls', :force => {:excel => excel1})
|
117
|
-
|
118
|
-
If you want to open the workbook and make its window visible, then use
|
119
|
-
|
120
|
-
book = Workbook.open('workbook.xls', :force => {:visible => true})
|
121
|
-
|
122
|
-
or
|
123
|
-
|
124
|
-
book = Workbook.open('workbook.xls', :visible => true)
|
125
|
-
|
126
|
-
You can combine options, e.g.
|
127
|
-
|
128
|
-
Workbook.open('workbook.xls', :force => {:excel => :new, :visible => true})
|
129
|
-
|
130
|
-
You can use some abbreviations, e.g. in this case
|
131
|
-
|
132
|
-
Workbook.open('workbook.xls', :f => {:e => :new, :v => true})
|
133
|
-
|
134
|
-
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
|
135
|
-
|
136
|
-
book = Workbook.open('workbook.xls', :if_unsaved => :accept)
|
137
|
-
|
138
|
-
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_obstructed+ handles this situation, e.g.
|
139
|
-
|
140
|
-
book = Workbook.open('path/workbook.xls', :if_obstructed => :forget)
|
141
|
-
|
142
|
-
Remarks:
|
143
|
-
|
144
|
-
Opening linked workbooks for EXCEL 2007 is supported
|
145
|
-
|
146
|
-
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.
|
147
|
-
|
148
|
-
=== Closing a workbook.
|
149
|
-
|
150
|
-
Simple close.
|
151
|
-
|
152
|
-
book.close
|
153
|
-
|
154
|
-
There is one option: +:if_unsaved+ . It can have one of the following values:
|
155
|
-
|
156
|
-
+:raise+ (default), +:save+, +:forget+, +:alert+
|
157
|
-
|
158
|
-
The option specifies: If the workbook is unsaved, then
|
159
|
-
|
160
|
-
+:save+:: Save the workbook before closing it.
|
161
|
-
+:raise+:: Raise an exception. Don't close the workbook.
|
162
|
-
+:forget+:: Close the workbook.
|
163
|
-
+:alert+:: Give control to Excel.
|
164
|
-
|
165
|
-
=== Reopening a workbook.
|
166
|
-
|
167
|
-
A special feature of robust_excel_ole is that it allows to reopen workbooks after closing them.
|
168
|
-
|
169
|
-
book = Workbook.open('workbook.xls')
|
170
|
-
book.close
|
171
|
-
book.reopen
|
172
|
-
|
173
|
-
The closed workbook is now alive again, i.e. is open and responds to Excel methods.
|
174
|
-
|
175
|
-
This feature is a result of providing identity transparence and storing the file name.
|
176
|
-
|
177
|
-
=== The Workbook objects and transperence identity
|
178
|
-
|
179
|
-
An Excel file (or workbook) is represented by a Workbook object. A Workbook object is defined by the full name of the workbook and the Excel instance in which it is opened. RobustExcelOle aims to ensure identity transperency.
|
180
|
-
Identity transparence means that the same Workbook objects refer to the same Excel files, and vice versa.
|
181
|
-
In other words, a Workbook objects is a proxy of an Excel file.
|
182
|
-
|
183
|
-
=== Promoting a workbook to a Workbook object
|
184
|
-
|
185
|
-
A Workbook object can be created when giving an Excel workbook.
|
186
|
-
|
187
|
-
book = Workbook.new(win32ole_workbook)
|
188
|
-
|
189
|
-
|
190
|
-
=== Saving a workbook.
|
191
|
-
|
192
|
-
Simple save.
|
193
|
-
|
194
|
-
book.save
|
195
|
-
|
196
|
-
If you want to save a workbook with a file name, then use
|
197
|
-
|
198
|
-
book.save_as('another_workbook.xls')
|
199
|
-
|
200
|
-
The options are the following:
|
201
|
-
|
202
|
-
+:if_exists+:: +:raise+ (default), +:overwrite+, +:alert+
|
203
|
-
+:if_obstruced+:: +:raise (default), +:forget+, +:save+, +close_if_saved
|
204
|
-
|
205
|
-
The option +:if_exists+ :
|
206
|
-
|
207
|
-
If a workbook with the file name already exists, then
|
208
|
-
|
209
|
-
+:raise+:: Raise an exeption. Don't write the file.
|
210
|
-
+:overwrite+:: Delete the existing file and write the file. If the workbook is open in an Excel instance, then raise an exception.
|
211
|
-
+:alert+:: Give the control to Excel.
|
212
|
-
|
213
|
-
Examples:
|
214
|
-
|
215
|
-
If you want to save a workbook and overwrite the file if it exists before, then use
|
216
|
-
|
217
|
-
book.save_as('another_workbook.xls', :if_exists => :overwrite)
|
218
|
-
|
219
|
-
If a workbook blocks the workbook that should be saved, then the former one can be saved and closed before.
|
220
|
-
|
221
|
-
book = Workbook.open('workbook.xls')
|
222
|
-
book2 = Workbook.open('another_workbook.xls')
|
223
|
-
book2.save_as('dir/workbook.xls', :if_exists => :overwrite, :if_obstructed => :save)
|
224
|
-
|
225
|
-
=== Opening, closing and saving a workbook under a certain name
|
226
|
-
|
227
|
-
You can open a workbook with given file name.
|
228
|
-
|
229
|
-
book = Workbook.open('workbook.xls')
|
230
|
-
|
231
|
-
You can save a workbook with given file name, if it is open.
|
232
|
-
|
233
|
-
Workbook.save('workbook.xls')
|
234
|
-
|
235
|
-
The workbook can be saved under a new file name, if it is open.
|
236
|
-
|
237
|
-
Workbook.save_as('workbook.xls', 'new_workbook.xls')
|
238
|
-
|
239
|
-
Finally the workbook can be closed with a given filename.
|
240
|
-
|
241
|
-
Workbook.close('workbook.xls')
|
242
|
-
|
243
|
-
=== Unobtrusively modifying a workbook
|
244
|
-
|
245
|
-
The method +unobtrusively+ enables the user to read or modify a workbook, no matter if it is open in some Excel instance, if it is saved or unsaved, and if it is writable or not. When opening a workbook unobtrusively, its status remains unchanged. This status includes, whether the workbook is opened or closed, saved or unsaved, readonly or writable, visible or invisible, calculation mode is automatic or manual, and checking compatibility is turned on or off.
|
246
|
-
|
247
|
-
One option chooses the Excel instance in which a closed workbook is opened. The option +:current+ (or +:active+, or +:reuse) (default) indicates that the closed workbook is opened in the Excel instance of the workbook, if it exists, or that another Excel instance is reused. Moreover, an Excel instance can be given directly where to open the closed workbook.
|
248
|
-
|
249
|
-
Options are the following:
|
250
|
-
|
251
|
-
+:current+ (or +:active, or +:reuse+:): (default) : open a closed workbook in the Excel instance where it was opened most recently, if such an Excel instance exists, otherwise open it in the current (first opened) Excel instance
|
252
|
-
|
253
|
-
+:read_only+:: Open the workbook unobtrusively for reading only (default: false)
|
254
|
-
+:readonly_excel+:: if the workbook is opened only as ReadOnly and shall be modified, then
|
255
|
-
true: close it and open it as writable in the excel instance where it was open so far
|
256
|
-
false (default) open it as writable in another running excel instance, if it exists,
|
257
|
-
otherwise open in a new excel instance
|
258
|
-
+:keep_open+:: let the workbook open after unobtrusively opening (default: false)
|
259
|
-
|
260
|
-
Workbook.unobtrusively('workbook.xls') do |book|
|
261
|
-
# some modification
|
262
|
-
sheet = book[0]
|
263
|
-
sheet[1,1] = "c"
|
264
|
-
end
|
265
|
-
|
266
|
-
The methods +for_reading+ and +for_modifying+ indicate unobtrusively reading or modifying.
|
267
|
-
|
268
|
-
Workbook.for_modifying('workbook.xls') do |book|
|
269
|
-
# some modification
|
270
|
-
sheet = book[0]
|
271
|
-
sheet[1,1] = "c"
|
272
|
-
end
|
273
|
-
|
274
|
-
=== Retaining the saved-status
|
275
|
-
|
276
|
-
This method ensures keeping the save status of the workbook
|
277
|
-
|
278
|
-
book = Workbook.open('workbook.xls')
|
279
|
-
book.retain_saved do
|
280
|
-
# some reading or modifying
|
281
|
-
end
|
282
|
-
|
283
|
-
=== Setting options
|
284
|
-
|
285
|
-
You can set options, e.g. with
|
286
|
-
|
287
|
-
book.for_this_workbook(:visible => true, :read_only => true)
|
288
|
-
|
289
|
-
=== Checking whether the workbook is alive.
|
290
|
-
|
291
|
-
This method finds out whether the Excel workbook that is referenced by the Workbook object responds to methods.
|
292
|
-
|
293
|
-
if book.alive? then sheet = book[0] end
|
294
|
-
|
295
|
-
=== Getting and setting the contents of a range in a workbook.
|
296
|
-
|
297
|
-
You can get the contents of a range with
|
298
|
-
|
299
|
-
book["name"]
|
300
|
-
=> "value"
|
301
|
-
|
302
|
-
or
|
303
|
-
|
304
|
-
book.nameval("name")
|
305
|
-
=> "value"
|
306
|
-
|
307
|
-
You can set the contents of a range with
|
308
|
-
|
309
|
-
book["name"] = "value"
|
310
|
-
|
311
|
-
or
|
312
|
-
|
313
|
-
book.set_nameval("name", value)
|
314
|
-
|
315
|
-
You can also specify the color of a changed range:
|
316
|
-
|
317
|
-
book.set_nameval("name", value, :color => 4)
|
318
|
-
|
319
|
-
=== Bringing a workbook to the focus.
|
320
|
-
|
321
|
-
If you want to make the workbook visible and available for keyboard inputs, use
|
322
|
-
|
323
|
-
book.focus
|
324
|
-
|
325
|
-
=== Making the window of the workbook visible
|
326
|
-
|
327
|
-
You can make the window of the workbook invisible or visible
|
328
|
-
|
329
|
-
book.visible = false
|
330
|
-
book.visible = true
|
331
|
-
|
332
|
-
=== Making an Excel visible or invisible, and enable and disable DisplayAlerts.
|
333
|
-
|
334
|
-
You can make an Excel visible
|
335
|
-
|
336
|
-
book.excel.visible = true
|
337
|
-
|
338
|
-
and enable DisplayAlerts
|
339
|
-
|
340
|
-
book.excel.displayalerts = true
|
341
|
-
|
342
|
-
|
343
|
-
=== Accessing a worksheet.
|
344
|
-
|
345
|
-
You can access a worksheet by giving the number
|
346
|
-
|
347
|
-
sheet = book.sheet(1)
|
348
|
-
|
349
|
-
or its name
|
350
|
-
|
351
|
-
sheet = book.sheet('Sheet1')
|
352
|
-
|
353
|
-
You can get the first and last worksheet with
|
354
|
-
|
355
|
-
sheet = book.first_sheet
|
356
|
-
|
357
|
-
and
|
358
|
-
|
359
|
-
sheet = book.last_sheet
|
360
|
-
|
361
|
-
You can access all sheet objects by using the methods Workbook#each.
|
362
|
-
|
363
|
-
book.each do |sheet|
|
364
|
-
# do something with sheet
|
365
|
-
end
|
366
|
-
|
367
|
-
=== Accessing cells, rows and columns.
|
368
|
-
|
369
|
-
A sheet object is enumerable. Use the methods Sheet#each_column, Sheet#each_row or Sheet#each.
|
370
|
-
|
371
|
-
sheet.each do |cell|
|
372
|
-
# do something with cell
|
373
|
-
# read every row every column
|
374
|
-
end
|
375
|
-
|
376
|
-
sheet.each_row do |row|
|
377
|
-
# do something with row
|
378
|
-
end
|
379
|
-
|
380
|
-
sheet.each_column do |column|
|
381
|
-
# do something with column
|
382
|
-
end
|
383
|
-
|
384
|
-
=== Accessing a cell.
|
385
|
-
|
386
|
-
You can read a cell from a sheet object
|
387
|
-
|
388
|
-
sheet[1, 1] => first cell (first row, first column).
|
389
|
-
|
390
|
-
or a range object
|
391
|
-
|
392
|
-
row_range[1] => first cell in row_range
|
393
|
-
column_range[2] => second cell in column_range
|
394
|
-
|
395
|
-
Methods to a cell are just delegated. For example you can read and write the value of a cell
|
396
|
-
|
397
|
-
cell = sheet[1,1]
|
398
|
-
cell.Value => value of the cell.
|
399
|
-
sheet[1,1] = "new_value"
|
400
|
-
|
401
|
-
=== Accessing a range of a row or column.
|
402
|
-
|
403
|
-
You access a range of a row by giving the number of the row, and optionally, the range of the cell numbers.
|
404
|
-
|
405
|
-
sheet.row_range(1) => first row
|
406
|
-
sheet.row_range(1, 1..3 ) => first three cells of the first row
|
407
|
-
|
408
|
-
Simarly you can access a range of a column.
|
409
|
-
|
410
|
-
sheet.col_range(3) => third column
|
411
|
-
sheet.col_range(3, 1..2) => first two cells of the third column
|
412
|
-
|
413
|
-
=== Naming a cell
|
414
|
-
|
415
|
-
You can (re-) name a cell range.
|
416
|
-
|
417
|
-
book.set_name(1,1,"name")
|
418
|
-
|
419
|
-
=== Getting and setting the contents of a named range in a worksheet
|
420
|
-
|
421
|
-
You get the value of a range by
|
422
|
-
|
423
|
-
sheet[name]
|
424
|
-
|
425
|
-
or
|
426
|
-
|
427
|
-
sheet.nameval(name)
|
428
|
-
|
429
|
-
You set the value if a range using
|
430
|
-
|
431
|
-
book[name] = value
|
432
|
-
|
433
|
-
or
|
434
|
-
|
435
|
-
book.set_nameval(name,value)
|
436
|
-
|
437
|
-
=== Getting and setting the contents of a named range in a Worksheet directly
|
438
|
-
|
439
|
-
You get the value of a range of a locally defined name with
|
440
|
-
|
441
|
-
sheet.rangeval(name)
|
442
|
-
|
443
|
-
and set the value of this range by
|
444
|
-
|
445
|
-
sheet.set_rangeval(name,value)
|
446
|
-
|
447
|
-
You can also specify the color of a modified range:
|
448
|
-
|
449
|
-
sheet.set_rangeval(name, value, :color => 4)
|
450
|
-
|
451
|
-
=== Adding and copying a worksheet.
|
452
|
-
|
453
|
-
You can add (append) an empty worksheet using
|
454
|
-
|
455
|
-
book.add_empty_sheet
|
456
|
-
|
457
|
-
Additionally you can name it.
|
458
|
-
|
459
|
-
book.add_empty_sheet(:as => 'sheet_name')
|
460
|
-
|
461
|
-
You can specify the position of the added empty worksheet.
|
462
|
-
|
463
|
-
book.add_empty_sheet(:as => 'new_name', :before => another_sheet)
|
464
|
-
|
465
|
-
You can copy a worksheet and add it.
|
466
|
-
|
467
|
-
book.copy_sheet sheet
|
468
|
-
|
469
|
-
Additionally you can specify a name and a position.
|
470
|
-
|
471
|
-
book.copy_sheet(sheet, :as => 'new_name', :after => another_sheet)
|
472
|
-
|
473
|
-
If you want to copy a worksheet, if a sheet is given, and add an empty worksheet, if no worksheet is given, then use
|
474
|
-
|
475
|
-
book.add_or_copy_sheet
|
476
|
-
|
477
|
-
book.add_or_copy_sheet(sheet, :as => 'new_name', :after => another_sheet)
|
478
|
-
|
479
|
-
=== Creating and reusing an Excel instance.
|
480
|
-
|
481
|
-
If you want to start a new Excel, use
|
482
|
-
|
483
|
-
excel1 = Excel.create
|
484
|
-
|
485
|
-
or
|
486
|
-
|
487
|
-
excel1 = Excel.new(:reuse => false)
|
488
|
-
|
489
|
-
In case you want to reuse an already running Excel instance, write
|
490
|
-
|
491
|
-
excel2 = Excel.current
|
492
|
-
|
493
|
-
or
|
494
|
-
|
495
|
-
excel2 = Excel.new(:reuse => true)
|
496
|
-
|
497
|
-
Options are +:reuse+ (+true+, +false+), +:visible+ (+true+, +false+), +:displayalerts+ (+true+, +false+, +:if_visible+), +:calculation+ (+:manual+, +:automatic+, +nil+) and +:screenupdating+ (+true+, +false+).
|
498
|
-
|
499
|
-
The option +:calculation+ specifies, whether the calculation mode is being forced to be manual (:manual), automatic (+:automatic+) or is not being forced (+nil+).
|
500
|
-
|
501
|
-
You can also promote an Excel instance represented as WIN32OLE object to an Excel object.
|
502
|
-
|
503
|
-
excel = Excel.new(win32ole_object, :visible => true)
|
504
|
-
|
505
|
-
=== Making a Excel visible or invisible
|
506
|
-
|
507
|
-
You can create a new Excel instance and make it visible.
|
508
|
-
|
509
|
-
excel1 = Excel.create(:visible => true)
|
510
|
-
|
511
|
-
or
|
512
|
-
|
513
|
-
excel1 = Excel.new(:reuse => false, :visible => true)
|
514
|
-
|
515
|
-
or
|
516
|
-
|
517
|
-
excel1 = Excel.create
|
518
|
-
excel1.visible = true
|
519
|
-
|
520
|
-
|
521
|
-
=== Enabling or disabling DisplayAlerts
|
522
|
-
|
523
|
-
You can enable DisplayAlerts with, e.g.
|
524
|
-
|
525
|
-
excel1 = Excel.new(:reuse => true, :displayalerts => true)
|
526
|
-
|
527
|
-
or
|
528
|
-
|
529
|
-
excel1 = Excel.current
|
530
|
-
excel1.displayalerts = true
|
531
|
-
|
532
|
-
and turn DisplayAlerts off with
|
533
|
-
|
534
|
-
excel1.displayalerts = false
|
535
|
-
|
536
|
-
You can turn off and off DisplayAlerts in a block.
|
537
|
-
|
538
|
-
excel = Excel.create
|
539
|
-
excel.with_displayalerts false do
|
540
|
-
# do something
|
541
|
-
end
|
542
|
-
|
543
|
-
=== Making all workbooks visible or invisible
|
544
|
-
|
545
|
-
excel.workbooks_visible true
|
546
|
-
|
547
|
-
excel.workbooks_visible false
|
548
|
-
|
549
|
-
=== Setting options
|
550
|
-
|
551
|
-
You can set all options in a given Excel instance
|
552
|
-
|
553
|
-
excel = Excel.current
|
554
|
-
|
555
|
-
excel.for_this_instance(:visible => true, :displayalerts => true)
|
556
|
-
|
557
|
-
=== Bringing an Excel instance to the foreground
|
558
|
-
|
559
|
-
excel1.focus
|
560
|
-
|
561
|
-
=== Closing an Excel
|
562
|
-
|
563
|
-
excel = Excel.current
|
564
|
-
excel.close
|
565
|
-
|
566
|
-
The method +close has the option +:if_unsaved+ with the values +:raise+, +:save+, +:forget+ and +:alert+.
|
567
|
-
|
568
|
-
For example, if you want to close an Excel instance and save unsaved workbooks, use
|
569
|
-
|
570
|
-
excel.close(:if_unsaved => :save)
|
571
|
-
|
572
|
-
=== Closing all Excel instances
|
573
|
-
|
574
|
-
Excel.close_all
|
575
|
-
|
576
|
-
This method has the option +:if_unsaved+ as described above. For example, if you want to close all Excel instances containing saved workbooks and raise an error for Excel instances with unsaved workbooks, use
|
577
|
-
|
578
|
-
Excel.close_all(:if_unsaved => :raise)
|
579
|
-
|
580
|
-
=== Terminating all Excel processes
|
581
|
-
|
582
|
-
Excel.kill_all
|
583
|
-
|
584
|
-
This method kills all Excel instances no matter whether they contain unsaved workbooks.
|
585
|
-
|
586
|
-
=== Recreating an Excel instance
|
587
|
-
|
588
|
-
Closed Excel instances can also be reopened. This includes reopening all workbooks that were open in that Excel instance.
|
589
|
-
|
590
|
-
excel.close
|
591
|
-
excel.recreate
|
592
|
-
|
593
|
-
The options are :reopen_workbooks, :visible and :displayalerts.
|
594
|
-
|
595
|
-
excel.recreate(:reopen_workbooks => true, :visible => true, :displayalerts => true)
|
596
|
-
|
597
|
-
=== Providing Excel instances
|
598
|
-
|
599
|
-
Providing all Excel instances (opened via RobustExcelOle) as objects of the class Excel
|
600
|
-
|
601
|
-
Excel.excel_processes
|
602
|
-
|
603
|
-
=== Setting Calculation mode.
|
604
|
-
|
605
|
-
You can set the calculation mode of an Excel instance to manual or automatic.
|
606
|
-
|
607
|
-
excel.calculation = :manual
|
608
|
-
|
609
|
-
You can do it in a block:
|
610
|
-
|
611
|
-
excel = Excel.create
|
612
|
-
book = Workbook.open('workbook.xls')
|
613
|
-
excel.with_calculation(:manual) do
|
614
|
-
# do something
|
615
|
-
end
|
616
|
-
|
617
|
-
=== Getting and setting the contents of a named range in an Excel application
|
618
|
-
|
619
|
-
excel = Excel.create
|
620
|
-
book = Workbook.open('another_workbook.xls')
|
621
|
-
|
622
|
-
You can get the value of a range by using
|
623
|
-
|
624
|
-
excel[name]
|
625
|
-
|
626
|
-
or
|
627
|
-
|
628
|
-
excel.nameval(name)
|
629
|
-
|
630
|
-
and set the value of a range by using
|
631
|
-
|
632
|
-
excel[name] = value
|
633
|
-
|
634
|
-
or
|
635
|
-
|
636
|
-
excel.set_nameval(name,value)
|
637
|
-
|
638
|
-
You can also specify the color of a changed range:
|
639
|
-
|
640
|
-
excel.set_nameval(name, value, :color => 4)
|
641
|
-
|
642
|
-
=== Retaining the saved-status of all workbooks
|
643
|
-
|
644
|
-
You can operate in some Excel instance and retain the saved-status of all workbooks.
|
645
|
-
|
646
|
-
excel.retain_saved_workbooks do
|
647
|
-
...
|
648
|
-
end
|
649
|
-
|
650
|
-
=== Getting and setting the contents of a named range in an Excel application directly
|
651
|
-
|
652
|
-
You can get the value of a range of a locally defined name by
|
653
|
-
|
654
|
-
excel.rangeval(name)
|
655
|
-
|
656
|
-
and set the value of this range with
|
657
|
-
|
658
|
-
excel.set_rangeval(name,value)
|
659
|
-
|
660
|
-
You can also specify the color of a changed range:
|
661
|
-
|
662
|
-
excel.set_rangeval(name, value, :color => 4)
|
663
|
-
|
664
|
-
|
665
|
-
=== Examples
|
666
|
-
|
667
|
-
=== Example 1
|
668
|
-
|
669
|
-
We open a workbook.
|
670
|
-
|
671
|
-
book = Workbook.open('workbook.xls')
|
672
|
-
|
673
|
-
Then we access a sheet via its name.
|
674
|
-
|
675
|
-
sheet = book.sheet('Sheet1')
|
676
|
-
|
677
|
-
Now we can modify the first cell.
|
678
|
-
|
679
|
-
sheet[1,1] = "new"
|
680
|
-
|
681
|
-
Then we save the workbook.
|
682
|
-
|
683
|
-
book.save
|
684
|
-
|
685
|
-
We can also save the workbook with a different name, and overwrite if a file with this name already exists.
|
686
|
-
|
687
|
-
book.save_as('different_workbook.xls', :if_exists => :overwrite)
|
688
|
-
|
689
|
-
Finally we close the workbook.
|
690
|
-
|
691
|
-
book.close
|
692
|
-
|
693
|
-
=== Example 2
|
694
|
-
|
695
|
-
We open a workbook.
|
696
|
-
|
697
|
-
book = Workbook.open('workbook.xls')
|
698
|
-
|
699
|
-
open it also in a new Excel instance and make it visible.
|
700
|
-
|
701
|
-
new_book = Workbook.open('workbook.xls', :force => {:excel => :new}, :visible => true)
|
702
|
-
|
703
|
-
and open another workbook in a the first Excel instance.
|
704
|
-
|
705
|
-
another_book = Workbook.open('another_workbook.xls', :force => {:excel => book.excel})
|
706
|
-
|
707
|
-
Then we close the workbooks.
|
708
|
-
|
709
|
-
book.close
|
710
|
-
new_book.close
|
711
|
-
another_book.close
|
712
|
-
|
713
|
-
We reopen the first workbook.
|
714
|
-
|
715
|
-
reopened_book = book.reopen
|
716
|
-
|
717
|
-
The writable workbook is being prefered.
|
718
|
-
|
719
|
-
reopened_book === book
|
720
|
-
=> true
|
721
|
-
|
722
|
-
We want to open yet another workbook. Since the workbook was not open before, reopening the workbook fails and the :default-excel option applies. According to :default => {:excel => :new} a new Excel is created, and the workbook is opened there.
|
723
|
-
|
724
|
-
different_book = Workbook.open('different.xls', :default => {:excel => :new})
|
725
|
-
|
726
|
-
|
727
|
-
=== Example 3
|
728
|
-
|
729
|
-
We open a workbook.
|
730
|
-
|
731
|
-
book = Workbook.open('workbook.xls')
|
732
|
-
|
733
|
-
We want to add a copy of the first sheet and insert it after the second sheet.
|
734
|
-
|
735
|
-
book.add_sheet(book.sheet(1), :as => 'Sheet1_copy', :after => book.sheet(2))
|
736
|
-
|
737
|
-
Then we open a new workbook with the same name in a new Excel, and leave the workbook that contains unsaved changes in the old Excel.
|
738
|
-
|
739
|
-
new_book = Workbook.open('workbook.xls', :if_unsaved => :new_excel)
|
740
|
-
|
741
|
-
We acccess the first sheet and change the first cell.
|
742
|
-
|
743
|
-
sheet = new_book.sheet(1)
|
744
|
-
sheet[1,1] = "another"
|
745
|
-
|
746
|
-
Then we open a workbook with the same name in the running Excel while discarding the workbook that contained unsaved changes.
|
747
|
-
|
748
|
-
third_book = Workbook.open('workbook.xls', :if_unsaved => :forget)
|
749
|
-
|
750
|
-
Now we add a sheet.
|
751
|
-
|
752
|
-
third_book.add_sheet
|
753
|
-
|
754
|
-
and close the workbook without saving it.
|
755
|
-
|
756
|
-
third_book.close(:if_unsaved => :forget)
|
757
|
-
|
758
|
-
We close the first workbook and save it before.
|
759
|
-
|
760
|
-
book.close(:if_unsaved => :save)
|
761
|
-
|
762
|
-
=== Example 4
|
763
|
-
|
764
|
-
We open a workbook.
|
765
|
-
|
766
|
-
book1 = Workbook.open('workbook.xls')
|
767
|
-
|
768
|
-
Then we open a workbook with the same name in a different path, while closing the workbook opened before.
|
769
|
-
|
770
|
-
book2 = Workbook.open('more/workbook.xls', :if_obstructed => :forget)
|
771
|
-
|
772
|
-
We change its first cell.
|
773
|
-
|
774
|
-
sheet = book2.sheet(1)
|
775
|
-
sheet[1,1] = "new"
|
776
|
-
|
777
|
-
Now we open a workbook with the same name again in a different path. while saving and closing the other workbook.
|
778
|
-
|
779
|
-
book3 = Workbook.open('workbook.xls', :if_obstructed => :save)
|
780
|
-
|
781
|
-
Then we open a workbook with the same name in a different path.
|
782
|
-
The other workbook will be closed, because it does not contain unsaved changes.
|
783
|
-
|
784
|
-
book4 = Workbook.open('more/workbook.xls', :if_obstructed => :close_if_unsaved)
|
785
|
-
|
786
|
-
|
787
|
-
Finally we close this workbook.
|
788
|
-
|
789
|
-
book4.close
|
790
|
-
|
791
|
-
|
792
|
-
=== Example 5
|
793
|
-
|
794
|
-
We open a workbook
|
795
|
-
|
796
|
-
book = Workbook.open('workbook.xls')
|
797
|
-
|
798
|
-
and print its first cell.
|
799
|
-
|
800
|
-
sheet = book.sheet(1)
|
801
|
-
p "1st cell: #{sheet[1,1].Value}"
|
802
|
-
|
803
|
-
Then we unobtrusively modify the workbook.
|
804
|
-
|
805
|
-
Workbook.unobtrusively('workbook.xls') do |book|
|
806
|
-
sheet = book.sheet(1)
|
807
|
-
sheet[1,1] = 'simple'
|
808
|
-
end
|
809
|
-
|
810
|
-
The workbook is modified, but its status is unchanged.
|
811
|
-
|
812
|
-
=== More details
|
813
|
-
|
814
|
-
link: Readme_detail.rdoc
|
815
|
-
|
816
|
-
=== Development
|
817
|
-
|
818
|
-
RobustExcelOle started as a simple fork from tomiacannondale's wrap_excel adapted to Ruby 1.8.6.
|
819
|
-
The functionality of wrap_excel is optimised and extended by a lot of new features.
|
820
|
-
Most notable extensions include:
|
821
|
-
* workbooks can be opened in already running Excel instances (instead of opening a new Excel whenever a book is opened)
|
822
|
-
* a workbook management system stores all workbooks that have been open. This workbook store is being used, e.g., for reopening a workbook that has been closed before. It provides transparency identity, i.e., equal Excel workbooks correspond to equal Workbook objects of RobustExcelOle.
|
823
|
-
|
824
|
-
Some features in RobustExcelOle that are not compatible with wrap_excel:
|
825
|
-
* +open+ uses by default a running Excel instance instead of creating a new one,
|
826
|
-
and opens a workbook by default in writable mode instead of read_only
|
827
|
-
* +close+ closes the workbook instead of closing all workbooks and the Excel instance.
|
828
|
-
* +save_as+ instead of +save+.
|
829
|
-
* position of cells and number of sheets are based on 1 instead of 0
|
830
|
-
|
831
|
-
Excel 2007 was supported until version 0.3.6.
|
832
|
-
|
833
|
-
=== Want to do more things
|
834
|
-
|
835
|
-
If you want to do something that not provide a function, you can use win32ole methods.
|
836
|
-
|
837
|
-
== Support
|
838
|
-
|
839
|
-
This is work in progress. Please contact us and to report issues and feature requests to github Issues.
|
840
|
-
https://github.com/Thomas008/robust_excel_ole/issues
|
841
|
-
|
842
|
-
== Collaborate
|
843
|
-
|
844
|
-
Please pull request on github.
|
845
|
-
|
846
|
-
== Author
|
847
|
-
|
848
|
-
thomas mailto:Thomas.Raths@gmx.net
|
849
|
-
|
850
|
-
== License
|
851
|
-
|
852
|
-
MIT License. For more imformation, please see LICENSE.
|