robust_excel_ole 0.2.0.5 → 0.2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +76 -90
- data/examples/example1.rb +35 -0
- data/examples/example2.rb +33 -0
- data/examples/example3.rb +32 -0
- data/examples/example4.rb +29 -0
- data/examples/example5.rb +35 -0
- data/lib/robust_excel_ole/book.rb +33 -30
- data/lib/robust_excel_ole/excel_app.rb +2 -2
- data/lib/robust_excel_ole/version.rb +1 -1
- data/spec/book_spec.rb +45 -18
- data/spec/data/book_with_blank.xls +0 -0
- data/spec/data/different_simple.xls +0 -0
- data/spec/data/simple.xls +0 -0
- metadata +9 -4
data/README.rdoc
CHANGED
@@ -1,13 +1,19 @@
|
|
1
1
|
= RobustExcelOle
|
2
2
|
|
3
|
-
|
3
|
+
robust_excel_ole wraps the win32ole, and uses the Excel operations with ruby.
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
robust_excel_ole started as a simple fork from tomiacannondale's wrap_excel adapted to Ruby 1.8.6.
|
4
8
|
The functionality of wrap_excel is optimised and extended by new features.
|
5
9
|
|
6
10
|
This is work in progress.
|
7
11
|
|
8
|
-
|
9
|
-
|
10
|
-
|
12
|
+
Notice some features in robust_excel_ole that are not compatible with wrap_excel:
|
13
|
+
* +save_as+ instead of +save+.
|
14
|
+
* +open+ uses by default a running Excel application instead of creating a new one,
|
15
|
+
and opens a book by default in writable mode instead of read_only
|
16
|
+
* +close+ closes the workbook instead of closing all workbooks and the Excel application.
|
11
17
|
|
12
18
|
== Requirements
|
13
19
|
|
@@ -20,156 +26,135 @@ RobustExcelOle wraps the win32ole, and uses the Excel operations with ruby.
|
|
20
26
|
|
21
27
|
== Usage
|
22
28
|
|
23
|
-
===
|
29
|
+
=== Open a book.
|
24
30
|
|
25
|
-
|
31
|
+
Example:
|
26
32
|
|
27
|
-
book = RobustExcelOle::Book.open('./sample.xls')
|
33
|
+
book = RobustExcelOle::Book.open('./sample.xls')
|
28
34
|
|
29
|
-
Open a book with a block.
|
35
|
+
Open a book with a block.
|
36
|
+
The semantics is similar to, e.g., File.open.
|
30
37
|
|
31
38
|
RobustExcelOle::Book.open('./sample.xls') do |book|
|
32
39
|
# do something
|
33
40
|
end
|
41
|
+
|
34
42
|
|
35
43
|
Options are the following:
|
36
44
|
|
37
|
-
[:reuse] boolean (default true)
|
38
|
-
[:read_only] boolean (default false) open in read-only mode
|
39
|
-
[:displayalerts] boolean (default false) allow display alerts in Excel
|
40
|
-
[:visible] boolean (default false) make visibe in Excel
|
45
|
+
[:reuse] boolean (default: true). use an already open Excel-application
|
46
|
+
[:read_only] boolean (default: false). open in read-only mode
|
47
|
+
[:displayalerts] boolean (default: false). allow display alerts in Excel
|
48
|
+
[:visible] boolean (default: false). make visibe in Excel
|
41
49
|
[:if_unsaved] :raise (default), :accept, :forget, :new_app, :excel
|
42
|
-
if an unsaved book with the same name is open
|
43
|
-
|
44
|
-
:raise
|
45
|
-
|
46
|
-
Raise an exception. Don't open the book.
|
47
|
-
|
48
|
-
:accept
|
49
|
-
|
50
|
-
Let the unsaved book open.
|
51
|
-
|
52
|
-
:forget
|
53
|
-
|
54
|
-
Close the unsaved book, open the new book.
|
55
|
-
|
56
|
-
:new_app
|
57
|
-
|
58
|
-
Open the new book in a new Excel application.
|
59
|
-
|
60
|
-
:excel
|
61
|
-
|
62
|
-
Give control to Excel.
|
63
|
-
|
64
|
-
|
65
50
|
[:if_unsaved_other_book] :raise (default), :save, :forget, :new_app
|
66
|
-
|
67
|
-
|
68
|
-
:raise
|
69
|
-
|
70
|
-
Raise an exception. Don't open the book.
|
71
|
-
|
72
|
-
:save
|
51
|
+
|
73
52
|
|
74
|
-
|
53
|
+
The option :if_unsaved :
|
75
54
|
|
76
|
-
|
55
|
+
If an unsaved book with the same name is open, then
|
56
|
+
|
57
|
+
[:raise ] Raise an exeption. Don't open the book.
|
58
|
+
[:accept] Let the unsaved book open.
|
59
|
+
[:forget] Close the unsaved book, open the new book.
|
60
|
+
[:new_app] Open the new book in a new Excel application
|
61
|
+
[:excel] Give control to Excel.
|
77
62
|
|
78
|
-
|
79
|
-
|
80
|
-
:new_app
|
63
|
+
The option :if_unsaved_other_book :
|
81
64
|
|
82
|
-
|
65
|
+
If an unsaved book with same name in a different path is open, then
|
66
|
+
|
67
|
+
[:raise] Raise an exception. Don't open the book.
|
68
|
+
[:save] Save and close the unsaved book and open the new book.
|
69
|
+
[:forget] Close the unsaved book, open the new book.
|
70
|
+
[:new_app] Open the new book in a new Excel application.
|
83
71
|
|
84
72
|
|
85
73
|
Examples:
|
86
74
|
|
87
75
|
Open a book.
|
88
76
|
|
89
|
-
book = RobustExcelOle::Book.open('./sample.xls')
|
77
|
+
book = RobustExcelOle::Book.open('./sample.xls')
|
90
78
|
|
91
79
|
Open another book in the same Excel application.
|
92
80
|
|
93
|
-
new_book = RobustExcelOle::Book.open('./different.xls')
|
81
|
+
new_book = RobustExcelOle::Book.open('./different.xls')
|
94
82
|
|
95
83
|
Open another book in a new Excel applications.
|
96
84
|
|
97
|
-
new_book = RobustExcelOle::Book.open('./different.xls', :reuse => false)
|
85
|
+
new_book = RobustExcelOle::Book.open('./different.xls', :reuse => false)
|
98
86
|
|
99
87
|
Change the book (see below).
|
100
88
|
|
101
|
-
sheet = book[0]
|
102
|
-
book.add_sheet(sheet, :as => 'copyed_name')
|
89
|
+
sheet = book[0]
|
90
|
+
book.add_sheet(sheet, :as => 'copyed_name')
|
103
91
|
|
104
|
-
Let the unsaved book open, when
|
92
|
+
Let the unsaved book open, when opening a book and a book with the same name is unsaved and open.
|
105
93
|
|
106
|
-
new_book = RobustExcelOle::Book.open('./sample.xls', :if_unsaved => accept)
|
94
|
+
new_book = RobustExcelOle::Book.open('./sample.xls', :if_unsaved => accept)
|
107
95
|
|
108
|
-
===
|
96
|
+
=== Save a book.
|
109
97
|
|
110
|
-
|
98
|
+
Simple save:
|
111
99
|
|
112
|
-
book.save
|
100
|
+
book.save
|
113
101
|
|
114
102
|
Save a book with a file name.
|
115
103
|
|
116
|
-
book.save_as('./another_sample.xls')
|
104
|
+
book.save_as('./another_sample.xls')
|
117
105
|
|
118
106
|
Options are the following:
|
119
107
|
|
120
|
-
[:if_exists] :
|
121
|
-
|
122
|
-
:overwrite:
|
123
|
-
|
124
|
-
Delete the old file and write the file.
|
108
|
+
[:if_exists] :raise (default), :overwrite, :excel
|
125
109
|
|
126
|
-
:
|
110
|
+
The option :if_exists :
|
127
111
|
|
128
|
-
|
112
|
+
If a book with the file name already exists, then
|
129
113
|
|
130
|
-
:raise
|
114
|
+
[:raise] Raise an exeption. Don't write the file.
|
115
|
+
[:overwrite] Delete the existing file and write the file. If the book is open in an Excel application, then raise an exeption.
|
116
|
+
[:excel] Give the control to Excel.
|
131
117
|
|
132
|
-
Raise an exception. Don't write the file.
|
133
118
|
|
134
119
|
Example:
|
135
120
|
|
136
121
|
Overwrite a book with the same name.
|
137
122
|
|
138
|
-
book.save('./another_sample.xls', :if_exists => :overwrite)
|
123
|
+
book.save('./another_sample.xls', :if_exists => :overwrite)
|
139
124
|
|
140
125
|
=== == (equal)
|
141
126
|
|
142
|
-
Check whether two
|
127
|
+
Check whether two referenced Excel workbooks are identical.
|
143
128
|
|
144
129
|
Example.
|
145
130
|
|
146
|
-
if (book == new_book) then
|
147
|
-
|
148
|
-
end
|
131
|
+
if (book == new_book) then
|
132
|
+
puts "the workbooks are identical"
|
133
|
+
end
|
149
134
|
|
150
135
|
=== alive?
|
151
136
|
|
152
|
-
Check whether the
|
153
|
-
|
154
|
-
book.alive?
|
137
|
+
Check whether the referenced Excel workbook responds to methods.
|
155
138
|
|
156
|
-
Example
|
139
|
+
Example:
|
157
140
|
|
158
|
-
if book.alive? then
|
159
|
-
|
160
|
-
end
|
141
|
+
if book.alive? then
|
142
|
+
puts "the book is alive"
|
143
|
+
end
|
161
144
|
|
162
145
|
=== filename
|
163
146
|
|
164
147
|
Get the file name of the book.
|
165
148
|
|
166
|
-
|
149
|
+
Example:
|
150
|
+
|
151
|
+
book.filename
|
167
152
|
|
168
|
-
===
|
153
|
+
=== Close a book.
|
169
154
|
|
170
|
-
|
155
|
+
Example:
|
171
156
|
|
172
|
-
book.close
|
157
|
+
book.close
|
173
158
|
|
174
159
|
Options are the following:
|
175
160
|
|
@@ -178,7 +163,7 @@ Options are the following:
|
|
178
163
|
|
179
164
|
:raise
|
180
165
|
|
181
|
-
Raise an exception. Don't close the book
|
166
|
+
Raise an exception. Don't close the book.
|
182
167
|
|
183
168
|
:save
|
184
169
|
|
@@ -190,11 +175,11 @@ Close the book.
|
|
190
175
|
|
191
176
|
:excel
|
192
177
|
|
193
|
-
Give control to Excel
|
178
|
+
Give control to Excel.
|
194
179
|
|
195
180
|
|
196
181
|
|
197
|
-
===
|
182
|
+
=== Access a sheet.
|
198
183
|
|
199
184
|
A sheet object can be accessed with a Book#[] method.
|
200
185
|
|
@@ -204,7 +189,7 @@ Access a sheet object with the sheet name.
|
|
204
189
|
|
205
190
|
book['Sheet1']
|
206
191
|
|
207
|
-
===
|
192
|
+
=== Access a row or a column.
|
208
193
|
|
209
194
|
A sheet object is enumerable. Use the methods Sheet#each_column, Sheet#each_row or Sheet#each.
|
210
195
|
|
@@ -221,7 +206,7 @@ A sheet object is enumerable. Use the methods Sheet#each_column, Sheet#each_row
|
|
221
206
|
# do something with column_range
|
222
207
|
end
|
223
208
|
|
224
|
-
===
|
209
|
+
=== Access a cell.
|
225
210
|
|
226
211
|
Read a cell from a sheet object.
|
227
212
|
|
@@ -241,7 +226,8 @@ If you want to do something that not provide a function, you can use win32ole me
|
|
241
226
|
|
242
227
|
== Support
|
243
228
|
|
244
|
-
This is work in progress. Please
|
229
|
+
This is work in progress. Please contact us and to report issues and feature requests to github Issues.
|
230
|
+
https://github.com/Thomas008/robust_excel_ole/issues
|
245
231
|
|
246
232
|
== Collaborate
|
247
233
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# example 1: open a book, print the cells, rows, and columns of a sheet
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
4
|
+
|
5
|
+
module RobustExcelOle
|
6
|
+
|
7
|
+
ExcelApp.close_all
|
8
|
+
begin
|
9
|
+
filename = '../spec/data/simple.xls'
|
10
|
+
book = RobustExcelOle::Book.open(filename)
|
11
|
+
sheet = book[0]
|
12
|
+
cell = sheet[0,0]
|
13
|
+
i = 0
|
14
|
+
sheet.each do |cell|
|
15
|
+
i = i + 1
|
16
|
+
puts "#{i}. cell: #{cell.value}"
|
17
|
+
end
|
18
|
+
i = 0
|
19
|
+
sheet.each_row do |row|
|
20
|
+
i = i + 1
|
21
|
+
puts "#{i}. row: #{row.value}"
|
22
|
+
end
|
23
|
+
i = 0
|
24
|
+
sheet.each_column do |column|
|
25
|
+
i = i + 1
|
26
|
+
puts "#{i}. column: #{column.value}"
|
27
|
+
end
|
28
|
+
sheet[0,0] = "complex"
|
29
|
+
book.save
|
30
|
+
book.close
|
31
|
+
ensure
|
32
|
+
ExcelApp.close_all
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# example 2: open a book in a running Excel application and in a new one. make visible
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
4
|
+
|
5
|
+
module RobustExcelOle
|
6
|
+
|
7
|
+
ExcelApp.close_all
|
8
|
+
begin
|
9
|
+
dir = '../spec/data/'
|
10
|
+
file_name1 = dir + 'simple.xls'
|
11
|
+
file_name2 = dir + 'different_simple.xls'
|
12
|
+
file_name3 = dir + 'different_simple.xls'
|
13
|
+
file_name4 = dir + 'book_with_blank.xls'
|
14
|
+
book1 = RobustExcelOle::Book.open(file_name1) # open a book in a new Excel application since no Excel is open
|
15
|
+
ExcelApp.reuse_if_possible.Visible = true # make Excel visible
|
16
|
+
sleep 2
|
17
|
+
book2 = RobustExcelOle::Book.open(file_name2) # open a new book in the same Excel application
|
18
|
+
sleep 2 # (by default: :reuse => true)
|
19
|
+
book3 = RobustExcelOle::Book.open(file_name3, :reuse => false, :visible => true)
|
20
|
+
# open another book in a new Excel application, make Excel visible
|
21
|
+
sleep 2
|
22
|
+
book4 = RobustExcelOle::Book.open(file_name4, :reuse => true, :visible => true)
|
23
|
+
# open anther book, and use a running Excel application
|
24
|
+
sleep 2 # (Excel chooses the first Excel application)
|
25
|
+
book1.close # close the books
|
26
|
+
book2.close
|
27
|
+
book3.close
|
28
|
+
book4.close
|
29
|
+
ensure
|
30
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# example 3: open a book, simple save, save_as, close
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
4
|
+
|
5
|
+
module RobustExcelOle
|
6
|
+
|
7
|
+
ExcelApp.close_all
|
8
|
+
begin
|
9
|
+
dir = '../spec/data/'
|
10
|
+
file_name = dir + 'simple.xls'
|
11
|
+
other_file_name = dir + 'different_simple.xls'
|
12
|
+
book = RobustExcelOle::Book.open(file_name) # open a book. default: :read_only => false
|
13
|
+
ExcelApp.reuse_if_possible.Visible = true # make Excel visible
|
14
|
+
sheet = book[0] # access a sheet
|
15
|
+
sleep 1
|
16
|
+
sheet[0,0] =
|
17
|
+
sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
18
|
+
sleep 1
|
19
|
+
book.save # simple save
|
20
|
+
begin
|
21
|
+
book.save_as(other_file_name) # save_as : default :if_exists => :raise
|
22
|
+
rescue ExcelErrorSave => msg
|
23
|
+
puts "save_as error: #{msg.message}"
|
24
|
+
end
|
25
|
+
book.save_as(other_file_name, :if_exists => :overwrite) # save_as with :if_exists => :overwrite
|
26
|
+
puts "save_as: saved successfully with option :if_exists => :overwrite"
|
27
|
+
book.close # close the book
|
28
|
+
ensure
|
29
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# example 4: open with read_only mode. save, close
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
4
|
+
|
5
|
+
module RobustExcelOle
|
6
|
+
|
7
|
+
ExcelApp.close_all
|
8
|
+
begin
|
9
|
+
dir = '../spec/data/'
|
10
|
+
file_name = dir + 'simple.xls'
|
11
|
+
other_file_name = dir + 'different_simple.xls'
|
12
|
+
# open a book with read_only and make Excel visible
|
13
|
+
book = RobustExcelOle::Book.open(file_name, :read_only => true, :visible => true)
|
14
|
+
sheet = book[0] # access a sheet
|
15
|
+
sleep 1
|
16
|
+
sheet[0,0] =
|
17
|
+
sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
18
|
+
sleep 1
|
19
|
+
begin
|
20
|
+
book.save # simple save.
|
21
|
+
rescue ExcelErrorSave => msg # raises exception because book is opened in of read_only mode
|
22
|
+
puts "save_as error: #{msg.message}"
|
23
|
+
end
|
24
|
+
book.close # close the book without saving it.
|
25
|
+
ensure
|
26
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# example 5: open with :if_unsaved => :accept, close with :if_unsaved => :save
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
4
|
+
|
5
|
+
module RobustExcelOle
|
6
|
+
|
7
|
+
ExcelApp.close_all
|
8
|
+
begin
|
9
|
+
file_name = '../spec/data/simple.xls'
|
10
|
+
book = RobustExcelOle::Book.open(file_name) # open a book
|
11
|
+
sheet = book[0] # access a sheet
|
12
|
+
sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
13
|
+
begin
|
14
|
+
new_book = RobustExcelOle::Book.open(file_name) # open another book with the same file name
|
15
|
+
rescue ExcelErrorOpen => msg # by default: raises an exception:
|
16
|
+
puts "open error: #{msg.message}" # a book with the same name is already open and unsaved
|
17
|
+
end
|
18
|
+
new_book = RobustExcelOle::Book.open(file_name, :if_unsaved => :accept) # open another book with the same file name
|
19
|
+
# and let the unsaved book open
|
20
|
+
if book.alive? && new_book.alive? then # check whether the referenced workbooks
|
21
|
+
puts "open with :if_unsaved => :accept : the two books are alive." # respond to methods
|
22
|
+
end
|
23
|
+
begin
|
24
|
+
book.close # close the book. by default: raises an exception:
|
25
|
+
rescue ExcelErrorClose => msg # book is unsaved
|
26
|
+
puts "close error: #{msg.message}"
|
27
|
+
end
|
28
|
+
book.close(:if_unsaved => :save) # save the book before closing it
|
29
|
+
puts "closed the book successfully with option :if_unsaved => :save"
|
30
|
+
new_book.close # close the other book. It is already saved.
|
31
|
+
ensure
|
32
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -41,7 +41,7 @@ module RobustExcelOle
|
|
41
41
|
:if_unsaved_other_book => :raise
|
42
42
|
}.merge(opts)
|
43
43
|
excel_app_options = {:reuse => true}.merge(opts).delete_if{|k,v|
|
44
|
-
k== :
|
44
|
+
k== :read_only || k== :if_unsaved || k == :if_unsaved_other_book}
|
45
45
|
if not File.exist?(file)
|
46
46
|
raise ExcelErrorOpen, "file #{file} not found"
|
47
47
|
end
|
@@ -50,7 +50,8 @@ module RobustExcelOle
|
|
50
50
|
@workbook = workbooks.Item(File.basename(file)) rescue nil
|
51
51
|
if @workbook then
|
52
52
|
blocked_by_other_book = (File.basename(file) == File.basename(@workbook.Fullname)) &&
|
53
|
-
(not (file == @workbook.Fullname
|
53
|
+
(not (absolute_path(file) == @workbook.Fullname))
|
54
|
+
#(not (file == @workbook.Fullname.gsub("\\","/")))
|
54
55
|
if blocked_by_other_book then
|
55
56
|
case @options[:if_unsaved_other_book]
|
56
57
|
when :raise
|
@@ -60,8 +61,8 @@ module RobustExcelOle
|
|
60
61
|
when :forget
|
61
62
|
@workbook.Close
|
62
63
|
when :new_app
|
63
|
-
|
64
|
-
@excel_app = ExcelApp.new(
|
64
|
+
excel_app_options[:reuse] = false
|
65
|
+
@excel_app = ExcelApp.new(excel_app_options)
|
65
66
|
@workbook = nil
|
66
67
|
else
|
67
68
|
raise ExcelErrorOpen, ":if_unsaved_other_book: invalid option"
|
@@ -81,8 +82,8 @@ module RobustExcelOle
|
|
81
82
|
old_displayalerts = @excel_app.DisplayAlerts # :nodoc:
|
82
83
|
@excel_app.DisplayAlerts = true # :nodoc:
|
83
84
|
when :new_app
|
84
|
-
|
85
|
-
@excel_app = ExcelApp.new(
|
85
|
+
excel_app_options[:reuse] = false
|
86
|
+
@excel_app = ExcelApp.new(excel_app_options)
|
86
87
|
@workbook = nil
|
87
88
|
else
|
88
89
|
raise ExcelErrorOpen, ":if_unsaved: invalid option"
|
@@ -119,17 +120,13 @@ module RobustExcelOle
|
|
119
120
|
#
|
120
121
|
# options:
|
121
122
|
# :if_unsaved if book is unsaved
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
def close(opts={ })
|
127
|
-
@options
|
128
|
-
:if_unsaved
|
129
|
-
}.merge(opts)
|
130
|
-
if ((alive?) && (not @workbook.Saved)) then
|
131
|
-
#puts "book not saved"
|
132
|
-
case @options[:if_unsaved]
|
123
|
+
# :raise -> raise an exception (default)
|
124
|
+
# :save -> save the book before it is closed
|
125
|
+
# :forget -> close the book
|
126
|
+
# :excel -> give control to excel
|
127
|
+
def close(opts = {:if_unsaved => :raise})
|
128
|
+
if ((alive?) && (not @workbook.Saved) && (not @options[:read_only])) then
|
129
|
+
case opts[:if_unsaved]
|
133
130
|
when :raise
|
134
131
|
raise ExcelErrorClose, "book is unsaved (#{File.basename(filename)})"
|
135
132
|
when :save
|
@@ -146,9 +143,9 @@ module RobustExcelOle
|
|
146
143
|
begin
|
147
144
|
@workbook.Close if alive?
|
148
145
|
@workbook = nil unless alive?
|
149
|
-
raise ExcelUserCanceled, "close: canceled by user" if alive? &&
|
146
|
+
raise ExcelUserCanceled, "close: canceled by user" if alive? && opts[:if_unsaved] == :excel && (not @workbook.Saved)
|
150
147
|
ensure
|
151
|
-
if
|
148
|
+
if opts[:if_unsaved] == :excel then
|
152
149
|
@excel_app.DisplayAlerts = old_displayalerts # :nodoc:
|
153
150
|
end
|
154
151
|
end
|
@@ -173,7 +170,7 @@ module RobustExcelOle
|
|
173
170
|
@workbook.Fullname.tr('\\','/') rescue nil
|
174
171
|
end
|
175
172
|
|
176
|
-
#returns true, if the full book names and excel appications are identical, false, otherwise
|
173
|
+
# returns true, if the full book names and excel appications are identical, false, otherwise
|
177
174
|
def == other_book
|
178
175
|
other_book.is_a?(Book) &&
|
179
176
|
@excel_app == other_book.excel_app &&
|
@@ -186,7 +183,7 @@ module RobustExcelOle
|
|
186
183
|
# saves a book.
|
187
184
|
# returns true, if successfully saved, nil otherwise
|
188
185
|
def save
|
189
|
-
raise
|
186
|
+
raise ExcelErrorSave, "Not opened for writing (opened with :read_only option)" if @options[:read_only]
|
190
187
|
if @workbook then
|
191
188
|
@workbook.Save
|
192
189
|
true
|
@@ -215,8 +212,14 @@ module RobustExcelOle
|
|
215
212
|
if File.exist?(file) then
|
216
213
|
case opts[:if_exists]
|
217
214
|
when :overwrite
|
218
|
-
|
219
|
-
|
215
|
+
# if a book is open with the name of file, then raise error
|
216
|
+
open_workbook = ExcelApp.reuse_if_possible.Workbooks(basename) rescue nil
|
217
|
+
#workbook_file = @excel_app.Workbooks(basename) rescue nil
|
218
|
+
if open_workbook == nil then
|
219
|
+
File.delete(file)
|
220
|
+
else
|
221
|
+
raise ExcelErrorSave, "book is open and used in Excel"
|
222
|
+
end
|
220
223
|
when :excel
|
221
224
|
old_displayalerts = @excel_app.DisplayAlerts # :nodoc:
|
222
225
|
@excel_app.DisplayAlerts = true # :nodoc:
|
@@ -285,23 +288,23 @@ module RobustExcelOle
|
|
285
288
|
|
286
289
|
end
|
287
290
|
|
288
|
-
class ExcelUserCanceled < RuntimeError
|
291
|
+
class ExcelUserCanceled < RuntimeError # :nodoc: #
|
289
292
|
end
|
290
293
|
|
291
|
-
class ExcelError < RuntimeError
|
294
|
+
class ExcelError < RuntimeError # :nodoc: #
|
292
295
|
end
|
293
296
|
|
294
|
-
class ExcelErrorSave < ExcelError
|
297
|
+
class ExcelErrorSave < ExcelError # :nodoc: #
|
295
298
|
end
|
296
299
|
|
297
|
-
class ExcelErrorSaveFailed < ExcelErrorSave
|
300
|
+
class ExcelErrorSaveFailed < ExcelErrorSave # :nodoc: #
|
298
301
|
end
|
299
302
|
|
300
|
-
class ExcelErrorSaveUnknown < ExcelErrorSave
|
303
|
+
class ExcelErrorSaveUnknown < ExcelErrorSave # :nodoc: #
|
301
304
|
end
|
302
305
|
|
303
|
-
class ExcelErrorOpen < ExcelError
|
306
|
+
class ExcelErrorOpen < ExcelError # :nodoc: #
|
304
307
|
end
|
305
308
|
|
306
|
-
class ExcelErrorClose < ExcelError
|
309
|
+
class ExcelErrorClose < ExcelError # :nodoc: #
|
307
310
|
end
|
@@ -58,9 +58,9 @@ module RobustExcelOle
|
|
58
58
|
#trc_info :obj_Parent, o.Parent rescue nil
|
59
59
|
begin
|
60
60
|
o.ole_free
|
61
|
-
puts "olefree OK"
|
61
|
+
#puts "olefree OK"
|
62
62
|
rescue
|
63
|
-
puts "olefree_error: #{$!}"
|
63
|
+
#puts "olefree_error: #{$!}"
|
64
64
|
#puts $!.backtrace.first(9).join "\n"
|
65
65
|
end
|
66
66
|
end
|
data/spec/book_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe RobustExcelOle::Book do
|
|
10
10
|
|
11
11
|
before(:all) do
|
12
12
|
excel_app = RobustExcelOle::ExcelApp.new(:reuse => true)
|
13
|
-
open_books = excel_app == nil ? 0 : excel_app.Workbooks.Count
|
13
|
+
open_books = excel_app == nil ? 0 : excel_app.Workbooks.Count
|
14
14
|
puts "*** open books *** : #{open_books}" if open_books > 0
|
15
15
|
RobustExcelOle::ExcelApp.close_all
|
16
16
|
end
|
@@ -156,7 +156,7 @@ describe RobustExcelOle::Book do
|
|
156
156
|
it "should belong to the same Excel application" do
|
157
157
|
@new_book.excel_app.should == @book.excel_app
|
158
158
|
@different_book.excel_app.should == @book.excel_app
|
159
|
-
end
|
159
|
+
end
|
160
160
|
end
|
161
161
|
end
|
162
162
|
end
|
@@ -166,7 +166,7 @@ describe RobustExcelOle::Book do
|
|
166
166
|
before do
|
167
167
|
@book = RobustExcelOle::Book.open(@simple_file)
|
168
168
|
@sheet = @book[0]
|
169
|
-
@book.add_sheet(@sheet, :as => '
|
169
|
+
@book.add_sheet(@sheet, :as => 'a_name')
|
170
170
|
end
|
171
171
|
|
172
172
|
after do
|
@@ -207,7 +207,7 @@ describe RobustExcelOle::Book do
|
|
207
207
|
|
208
208
|
it "should open the new book and close the unsaved book, if user answers 'yes'" do
|
209
209
|
# "Yes" is the default. --> language independent
|
210
|
-
@key_sender.puts "{enter}"
|
210
|
+
@key_sender.puts "{enter}"
|
211
211
|
@new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :excel)
|
212
212
|
@book.should_not be_alive
|
213
213
|
@new_book.should be_alive
|
@@ -258,7 +258,7 @@ describe RobustExcelOle::Book do
|
|
258
258
|
simple_file_other_path = @dir + '/more_data/simple.xls'
|
259
259
|
@book = RobustExcelOle::Book.open(simple_file_other_path)
|
260
260
|
@sheet = @book[0]
|
261
|
-
@book.add_sheet(@sheet, :as => '
|
261
|
+
@book.add_sheet(@sheet, :as => 'a_name')
|
262
262
|
end
|
263
263
|
|
264
264
|
after do
|
@@ -322,17 +322,35 @@ describe RobustExcelOle::Book do
|
|
322
322
|
|
323
323
|
it "should close book" do
|
324
324
|
expect{
|
325
|
-
@book.close
|
326
|
-
}.to_not raise_error
|
327
|
-
@book.should_not be_alive
|
325
|
+
@book.close
|
326
|
+
}.to_not raise_error
|
327
|
+
@book.should_not be_alive
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
context "with unsaved book and with :read_only" do
|
332
|
+
before do
|
333
|
+
@book = RobustExcelOle::Book.open(@simple_file, :read_only => true)
|
334
|
+
@sheet_count = @book.workbook.Worksheets.Count
|
335
|
+
@book.add_sheet(@sheet, :as => 'a_name')
|
336
|
+
end
|
337
|
+
|
338
|
+
it "should close the unsaved book without error and without saving" do
|
339
|
+
expect{
|
340
|
+
@book.close
|
341
|
+
}.to_not raise_error
|
342
|
+
new_book = RobustExcelOle::Book.open(@simple_file)
|
343
|
+
new_book.workbook.Worksheets.Count.should == @sheet_count
|
344
|
+
new_book.close
|
328
345
|
end
|
346
|
+
|
329
347
|
end
|
330
348
|
|
331
349
|
context "with unsaved book" do
|
332
350
|
before do
|
333
351
|
@book = RobustExcelOle::Book.open(@simple_file)
|
334
352
|
@sheet_count = @book.workbook.Worksheets.Count
|
335
|
-
@book.add_sheet(@sheet, :as => '
|
353
|
+
@book.add_sheet(@sheet, :as => 'a_name')
|
336
354
|
@sheet = @book[0]
|
337
355
|
end
|
338
356
|
|
@@ -397,7 +415,7 @@ describe RobustExcelOle::Book do
|
|
397
415
|
# "Yes" is the default. "No" is right of "Yes", "Cancel" is right of "No" --> language independent
|
398
416
|
@key_sender.puts "{right}" * position + "{enter}"
|
399
417
|
ole_workbook = @book.workbook
|
400
|
-
excel_app = @book.excel_app
|
418
|
+
excel_app = @book.excel_app
|
401
419
|
displayalert_value = @book.excel_app.DisplayAlerts
|
402
420
|
if answer == :cancel then
|
403
421
|
expect {
|
@@ -440,11 +458,11 @@ describe RobustExcelOle::Book do
|
|
440
458
|
end
|
441
459
|
|
442
460
|
describe "save" do
|
443
|
-
|
461
|
+
|
444
462
|
context "with simple save" do
|
445
463
|
it "should save for a file opened without :read_only" do
|
446
464
|
@book = RobustExcelOle::Book.open(@simple_file)
|
447
|
-
@book.add_sheet(@sheet, :as => '
|
465
|
+
@book.add_sheet(@sheet, :as => 'a_name')
|
448
466
|
@new_sheet_count = @book.workbook.Worksheets.Count
|
449
467
|
expect {
|
450
468
|
@book.save
|
@@ -457,8 +475,7 @@ describe RobustExcelOle::Book do
|
|
457
475
|
@book = RobustExcelOle::Book.open(@simple_file, :read_only => true)
|
458
476
|
expect {
|
459
477
|
@book.save
|
460
|
-
}.to raise_error(
|
461
|
-
"Not opened for writing(open with :read_only option)")
|
478
|
+
}.to raise_error(ExcelErrorSave, "Not opened for writing (opened with :read_only option)")
|
462
479
|
@book.close
|
463
480
|
end
|
464
481
|
end
|
@@ -527,6 +544,17 @@ describe RobustExcelOle::Book do
|
|
527
544
|
@book.close
|
528
545
|
end
|
529
546
|
|
547
|
+
it "should raise an error if the book is open" do
|
548
|
+
File.delete @simple_save_file rescue nil
|
549
|
+
FileUtils.copy @simple_file, @simple_save_file
|
550
|
+
# fails with :reuse => false
|
551
|
+
book_save = RobustExcelOle::Book.open(@simple_save_file, :reuse => false)
|
552
|
+
expect{
|
553
|
+
@book.save_as(@simple_save_file, :if_exists => :overwrite)
|
554
|
+
}.to raise_error(ExcelErrorSave, "book is open and used in Excel")
|
555
|
+
book_save.close
|
556
|
+
end
|
557
|
+
|
530
558
|
it "should save to simple_save_file.xls with :if_exists => :overwrite" do
|
531
559
|
File.delete @simple_save_file rescue nil
|
532
560
|
File.open(@simple_save_file,"w") do | file |
|
@@ -621,7 +649,6 @@ describe RobustExcelOle::Book do
|
|
621
649
|
@book.excel_app.DisplayAlerts.should == displayalert_value
|
622
650
|
end
|
623
651
|
|
624
|
-
|
625
652
|
end
|
626
653
|
|
627
654
|
it "should save to 'simple_save_file.xls' with :if_exists => nil" do
|
@@ -658,12 +685,12 @@ describe RobustExcelOle::Book do
|
|
658
685
|
end
|
659
686
|
|
660
687
|
after do
|
661
|
-
@book.close
|
688
|
+
@book.close
|
662
689
|
end
|
663
690
|
|
664
691
|
it "should return right absoute path name" do
|
665
692
|
@book.absolute_path(@simple_file).gsub("\\","/").should == @book.filename
|
666
|
-
end
|
693
|
+
end
|
667
694
|
end
|
668
695
|
|
669
696
|
context "with alive?" do
|
@@ -673,7 +700,7 @@ describe RobustExcelOle::Book do
|
|
673
700
|
end
|
674
701
|
|
675
702
|
after do
|
676
|
-
@book.close
|
703
|
+
@book.close
|
677
704
|
end
|
678
705
|
|
679
706
|
it "should return true, if book is alive" do
|
Binary file
|
Binary file
|
data/spec/data/simple.xls
CHANGED
Binary file
|
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
|
-
hash:
|
4
|
+
hash: 83
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 0.2.0.
|
10
|
+
- 6
|
11
|
+
version: 0.2.0.6
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- traths
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2014-09-
|
19
|
+
date: 2014-09-28 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -132,6 +132,11 @@ files:
|
|
132
132
|
- LICENSE
|
133
133
|
- README.rdoc
|
134
134
|
- Rakefile
|
135
|
+
- examples/example1.rb
|
136
|
+
- examples/example2.rb
|
137
|
+
- examples/example3.rb
|
138
|
+
- examples/example4.rb
|
139
|
+
- examples/example5.rb
|
135
140
|
- lib/robust_excel_ole.rb
|
136
141
|
- lib/robust_excel_ole/book.rb
|
137
142
|
- lib/robust_excel_ole/cell.rb
|