robust_excel_ole 0.2.0.7 → 0.2.0.8
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 +6 -4
- data/examples/example1.rb +5 -2
- data/examples/example6.rb +34 -0
- data/examples/example7.rb +41 -0
- data/examples/example8.rb +32 -0
- data/examples/example9.rb +13 -0
- data/lib/robust_excel_ole/book.rb +23 -11
- data/lib/robust_excel_ole/version.rb +1 -1
- data/spec/book_spec.rb +42 -15
- metadata +7 -3
data/README.rdoc
CHANGED
@@ -47,7 +47,7 @@ Options are the following:
|
|
47
47
|
[:displayalerts] boolean (default: false). allow display alerts in Excel
|
48
48
|
[:visible] boolean (default: false). make visibe in Excel
|
49
49
|
[:if_unsaved] :raise (default), :accept, :forget, :new_app, :excel
|
50
|
-
[:
|
50
|
+
[:if_blocking_other] :raise (default), :forget, :save_and_close, :close_or_raise, :new_app
|
51
51
|
|
52
52
|
|
53
53
|
The option :if_unsaved :
|
@@ -60,12 +60,13 @@ If an unsaved book with the same name is open, then
|
|
60
60
|
[:new_app] Open the new book in a new Excel application
|
61
61
|
[:excel] Give control to Excel.
|
62
62
|
|
63
|
-
The option :
|
64
|
-
|
63
|
+
The option :if_blocking_other :
|
65
64
|
If a book with same name in a different path is open, then
|
66
65
|
|
67
66
|
[:raise] Raise an exception. Don't open the book.
|
68
67
|
[:forget] Close the old book, open the new book.
|
68
|
+
[:save_and_close] Save the old book, close it, open the new book
|
69
|
+
[:close_or_raise] Close the old book and open the new book, if the old book is saved, raise an exception otherwise
|
69
70
|
[:new_app] Open the new book in a new Excel application.
|
70
71
|
|
71
72
|
|
@@ -111,7 +112,8 @@ The option :if_exists :
|
|
111
112
|
If a book with the file name already exists, then
|
112
113
|
|
113
114
|
[:raise] Raise an exeption. Don't write the file.
|
114
|
-
[:overwrite] Delete the existing file and write the file.
|
115
|
+
[:overwrite] Delete the existing file and write the file.
|
116
|
+
If the book is open in an Excel application, then raise an exeption.
|
115
117
|
[:excel] Give the control to Excel.
|
116
118
|
|
117
119
|
|
data/examples/example1.rb
CHANGED
@@ -6,8 +6,11 @@ module RobustExcelOle
|
|
6
6
|
|
7
7
|
ExcelApp.close_all
|
8
8
|
begin
|
9
|
-
|
10
|
-
|
9
|
+
simple_file = '../spec/data/simple.xls'
|
10
|
+
simple_save_file = '../spec/data/simple_save.xls'
|
11
|
+
File.delete @simple_save_file rescue nil
|
12
|
+
#FileUtils.copy simple_file, simple_save_file
|
13
|
+
book = RobustExcelOle::Book.open(simple_save_file)
|
11
14
|
sheet = book[0]
|
12
15
|
cell = sheet[0,0]
|
13
16
|
i = 0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# example 6: open with :if_unsaved => :forget, :new_app, 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
|
+
ExcelApp.reuse_if_possible.Visible = true # make Excel visible
|
12
|
+
sleep 1
|
13
|
+
sheet = book[0] # access a sheet
|
14
|
+
first_cell = sheet[0,0].value
|
15
|
+
sheet[0,0] = first_cell == "simple" ? "complex" : "simple" # change a cell
|
16
|
+
sleep 1
|
17
|
+
new_book = RobustExcelOle::Book.open(file_name, :if_unsaved => :forget) # open another book with the same file name
|
18
|
+
# and close the unsaved book without saving it
|
19
|
+
sheet_new_book = new_book[0]
|
20
|
+
if (not book.alive?) && new_book.alive? && sheet_new_book[0,0].value == first_cell then # check whether the unsaved book
|
21
|
+
puts "open with :if_unsaved => :forget : the unsaved book is closed and not saved." # is closed and was not saved
|
22
|
+
end
|
23
|
+
sleep 1
|
24
|
+
sheet_new_book[0,0] = sheet_new_book[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
25
|
+
# open another book in a new Excel application, and make Excel visible
|
26
|
+
another_book = RobustExcelOle::Book.open(file_name, :if_unsaved => :new_app, :visible => true)
|
27
|
+
sleep 3 # leaving the unsaved book open
|
28
|
+
new_book.close(:if_unsaved => :forget ) # close the unsaved book without saving it
|
29
|
+
another_book.close
|
30
|
+
ensure
|
31
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# example 7: open, close, save with giving control to Excel
|
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, :visible => true) # open a book
|
11
|
+
ExcelApp.reuse_if_possible.Visible = true # make Excel visible
|
12
|
+
sleep 1
|
13
|
+
sheet = book[0] # access a sheet
|
14
|
+
sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
15
|
+
sleep 1
|
16
|
+
begin
|
17
|
+
new_book = RobustExcelOle::Book.open(file_name, :if_unsaved => :excel) # open another book with the same file name
|
18
|
+
rescue ExcelUserCanceled => msg # if the user chooses not open the book,
|
19
|
+
puts "#{msg.message}" # an exeptions is raised
|
20
|
+
end
|
21
|
+
puts "new book has opened" if new_book
|
22
|
+
ExcelApp.reuse_if_possible.Visible = true
|
23
|
+
begin
|
24
|
+
book.close(:if_unsaved => :excel) # close the unsaved book.
|
25
|
+
rescue ExcelUserCanceled => msg # user is asked whether the unsaved book shall be saved
|
26
|
+
puts "#{msg.message}" # if the user chooses to cancel, then an expeption is raised
|
27
|
+
end
|
28
|
+
if new_book then
|
29
|
+
begin
|
30
|
+
new_book.save_as(file_name, :if_exists => :excel) # save the new book, if it was opened
|
31
|
+
rescue ExcelErrorSave => msg # user is asked, whether the existing file shall be overwritten
|
32
|
+
puts "save_as: #{msg.message}" # if the user chooses "no" or "cancel", an exception is raised
|
33
|
+
end
|
34
|
+
|
35
|
+
new_book.close # close the new book, if the user chose to open it
|
36
|
+
end
|
37
|
+
ensure
|
38
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# example 8: open with :if_blocking_other:
|
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_dir = '../spec/data/more_data/'
|
12
|
+
other_file_name = other_dir + 'simple.xls'
|
13
|
+
book = RobustExcelOle::Book.open(file_name, :visible => true) # open a book, make Excel application visible
|
14
|
+
sleep 3
|
15
|
+
begin
|
16
|
+
new_book = RobustExcelOle::Book.open(other_file_name) # open a book with the same file name in a different path
|
17
|
+
rescue ExcelErrorOpen => msg # by default: raises an exception
|
18
|
+
puts "open: #{msg.message}"
|
19
|
+
end
|
20
|
+
# open a new book with the same file name in a different path. close the old book before.
|
21
|
+
new_book = RobustExcelOle::Book.open(other_file_name, :if_blocking_other => :forget)
|
22
|
+
# open another book with the same file name in a different path. Use a new Excel application
|
23
|
+
sleep 3
|
24
|
+
another_book = RobustExcelOle::Book.open(file_name, :if_blocking_other => :new_app, :visible => true)
|
25
|
+
sleep 3
|
26
|
+
new_book.close # close the books
|
27
|
+
another_book.close
|
28
|
+
ensure
|
29
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# example 9: save the sheets of a book as separate books
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../lib/robust_excel_ole')
|
4
|
+
|
5
|
+
module RobustExcelOle
|
6
|
+
|
7
|
+
ExcelApp.close_all
|
8
|
+
begin
|
9
|
+
ensure
|
10
|
+
ExcelApp.close_all # close workbooks, quit Excel application
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -17,14 +17,17 @@ module RobustExcelOle
|
|
17
17
|
# :displayalerts (boolean) allow display alerts in Excel (default: false)
|
18
18
|
# :visible (boolean) make visibe in Excel (default: false)
|
19
19
|
# :if_unsaved if an unsaved book with the same name is open, then
|
20
|
-
# :raise -> raise an exception (default)
|
20
|
+
# :raise -> raise an exception (default)
|
21
|
+
# :forget -> close the unsaved book, open the new book
|
21
22
|
# :accept -> let the unsaved book open
|
22
|
-
# :forget -> close the unsaved book, open the new book
|
23
23
|
# :excel -> give control to excel
|
24
24
|
# :new_app -> open the new book in a new excel application
|
25
|
-
# :
|
25
|
+
# :if_blocking_other if a book with the same name in a different path is open, then
|
26
26
|
# :raise -> raise an exception (default)
|
27
|
-
# :forget -> close the
|
27
|
+
# :forget -> close the old book, open the new book
|
28
|
+
# :save_and_close -> save the old book, close it, open the new book
|
29
|
+
# :close_or_raise -> close the old book and open the new book, if the old book is saved
|
30
|
+
# raise an exception otherwise
|
28
31
|
# :new_app -> open the new book in a new excel application
|
29
32
|
def open(file, options={ :reuse => true}, &block)
|
30
33
|
new(file, options, &block)
|
@@ -37,10 +40,10 @@ module RobustExcelOle
|
|
37
40
|
:reuse => true,
|
38
41
|
:read_only => false,
|
39
42
|
:if_unsaved => :raise,
|
40
|
-
:
|
43
|
+
:if_blocking_other => :raise
|
41
44
|
}.merge(opts)
|
42
45
|
excel_app_options = {:reuse => true}.merge(opts).delete_if{|k,v|
|
43
|
-
k== :read_only || k== :if_unsaved || k == :
|
46
|
+
k== :read_only || k== :if_unsaved || k == :if_blocking_other}
|
44
47
|
if not File.exist?(file)
|
45
48
|
raise ExcelErrorOpen, "file #{file} not found"
|
46
49
|
end
|
@@ -51,17 +54,26 @@ module RobustExcelOle
|
|
51
54
|
blocked_by_other_book = (File.basename(file) == File.basename(@workbook.Fullname)) &&
|
52
55
|
(not (absolute_path(file) == @workbook.Fullname))
|
53
56
|
if blocked_by_other_book then
|
54
|
-
case @options[:
|
57
|
+
case @options[:if_blocking_other]
|
55
58
|
when :raise
|
56
|
-
raise ExcelErrorOpen, "blocked by
|
59
|
+
raise ExcelErrorOpen, "blocked by a book with the same name in a different path"
|
57
60
|
when :forget
|
58
61
|
@workbook.Close
|
62
|
+
when :save_and_close
|
63
|
+
save unless @workbook.Saved
|
64
|
+
@workbook.Close
|
65
|
+
when :close_or_raise
|
66
|
+
if (not @workbook.Saved) then
|
67
|
+
raise ExcelErrorOpen, "book with the same name in a different path is unsaved"
|
68
|
+
else
|
69
|
+
@workbook.Close
|
70
|
+
end
|
59
71
|
when :new_app
|
60
72
|
excel_app_options[:reuse] = false
|
61
73
|
@excel_app = ExcelApp.new(excel_app_options)
|
62
74
|
@workbook = nil
|
63
75
|
else
|
64
|
-
raise ExcelErrorOpen, ":
|
76
|
+
raise ExcelErrorOpen, ":if_blocking_other: invalid option"
|
65
77
|
end
|
66
78
|
else
|
67
79
|
# book open, not saved, not blocked by other book
|
@@ -70,10 +82,10 @@ module RobustExcelOle
|
|
70
82
|
case @options[:if_unsaved]
|
71
83
|
when :raise
|
72
84
|
raise ExcelErrorOpen, "book is already open but not saved (#{File.basename(file)})"
|
73
|
-
when :accept
|
74
|
-
#nothing
|
75
85
|
when :forget
|
76
86
|
@workbook.Close
|
87
|
+
when :accept
|
88
|
+
#nothing
|
77
89
|
when :excel
|
78
90
|
old_displayalerts = @excel_app.DisplayAlerts # :nodoc:
|
79
91
|
@excel_app.DisplayAlerts = true # :nodoc:
|
data/spec/book_spec.rb
CHANGED
@@ -255,8 +255,11 @@ describe RobustExcelOle::Book do
|
|
255
255
|
context "with a book in a different path" do
|
256
256
|
|
257
257
|
before do
|
258
|
-
simple_file_other_path = @dir + '/more_data/simple.xls'
|
259
|
-
@book = RobustExcelOle::Book.open(simple_file_other_path)
|
258
|
+
@simple_file_other_path = @dir + '/more_data/simple.xls'
|
259
|
+
@book = RobustExcelOle::Book.open(@simple_file_other_path)
|
260
|
+
@sheet_count = @book.workbook.Worksheets.Count
|
261
|
+
@sheet = @book[0]
|
262
|
+
@book.add_sheet(@sheet, :as => 'a_name')
|
260
263
|
end
|
261
264
|
|
262
265
|
after do
|
@@ -264,38 +267,62 @@ describe RobustExcelOle::Book do
|
|
264
267
|
@new_book.close rescue nil
|
265
268
|
end
|
266
269
|
|
267
|
-
it "should raise an error, if :
|
270
|
+
it "should raise an error, if :if_blocking_other is :raise" do
|
268
271
|
expect {
|
269
|
-
@new_book = RobustExcelOle::Book.open(@simple_file, :
|
270
|
-
}.to raise_error(ExcelErrorOpen, "blocked by
|
272
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :raise)
|
273
|
+
}.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should close the other book and open the new book, if :if_blocking_other is :forget" do
|
277
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :forget)
|
278
|
+
@book.should_not be_alive
|
279
|
+
@new_book.should be_alive
|
280
|
+
@new_book.filename.downcase.should == @simple_file.downcase
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should save the old book, close it, and open the new book, if :if_blocking_other is :save_and_close" do
|
284
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :save_and_close)
|
285
|
+
@book.should_not be_alive
|
286
|
+
@new_book.should be_alive
|
287
|
+
@new_book.filename.downcase.should == @simple_file.downcase
|
288
|
+
new_book = RobustExcelOle::Book.open(@simple_file)
|
289
|
+
new_book.workbook.Worksheets.Count.should == @sheet_count
|
290
|
+
new_book.close
|
271
291
|
end
|
272
292
|
|
273
|
-
it "should close the
|
274
|
-
|
293
|
+
it "should raise an error, if the old book is unsaved, and close the old book and open the new book,
|
294
|
+
if :if_blocking_other is :close_or_raise" do
|
295
|
+
expect{
|
296
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :close_or_raise)
|
297
|
+
}.to raise_error(ExcelErrorOpen, "book with the same name in a different path is unsaved")
|
298
|
+
@book.save
|
299
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :close_or_raise)
|
275
300
|
@book.should_not be_alive
|
276
301
|
@new_book.should be_alive
|
277
302
|
@new_book.filename.downcase.should == @simple_file.downcase
|
303
|
+
new_book = RobustExcelOle::Book.open(@simple_file_other_path, :if_blocking_other => :forget)
|
304
|
+
new_book.workbook.Worksheets.Count.should == @sheet_count + 1
|
305
|
+
new_book.close
|
278
306
|
end
|
279
307
|
|
280
|
-
it "should open the book in a new excel application, if :
|
281
|
-
@new_book = RobustExcelOle::Book.open(@simple_file, :
|
308
|
+
it "should open the book in a new excel application, if :if_blocking_other is :new_app" do
|
309
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :new_app)
|
282
310
|
@book.should be_alive
|
283
311
|
@new_book.should be_alive
|
284
312
|
@new_book.filename.should_not == @book.filename
|
285
313
|
@new_book.excel_app.should_not == @book.excel_app
|
286
|
-
@new_book.close
|
287
314
|
end
|
288
315
|
|
289
|
-
it "should raise an error, if :
|
316
|
+
it "should raise an error, if :if_blocking_other is default" do
|
290
317
|
expect {
|
291
318
|
@new_book = RobustExcelOle::Book.open(@simple_file)
|
292
|
-
}.to raise_error(ExcelErrorOpen, "blocked by
|
319
|
+
}.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
|
293
320
|
end
|
294
321
|
|
295
|
-
it "should raise an error, if :
|
322
|
+
it "should raise an error, if :if_blocking_other is invalid option" do
|
296
323
|
expect {
|
297
|
-
@new_book = RobustExcelOle::Book.open(@simple_file, :
|
298
|
-
}.to raise_error(ExcelErrorOpen, ":
|
324
|
+
@new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :invalid_option)
|
325
|
+
}.to raise_error(ExcelErrorOpen, ":if_blocking_other: invalid option")
|
299
326
|
end
|
300
327
|
|
301
328
|
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
|
-
hash:
|
4
|
+
hash: 79
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 0.2.0.
|
10
|
+
- 8
|
11
|
+
version: 0.2.0.8
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- traths
|
@@ -137,6 +137,10 @@ files:
|
|
137
137
|
- examples/example3.rb
|
138
138
|
- examples/example4.rb
|
139
139
|
- examples/example5.rb
|
140
|
+
- examples/example6.rb
|
141
|
+
- examples/example7.rb
|
142
|
+
- examples/example8.rb
|
143
|
+
- examples/example9.rb
|
140
144
|
- lib/robust_excel_ole.rb
|
141
145
|
- lib/robust_excel_ole/book.rb
|
142
146
|
- lib/robust_excel_ole/cell.rb
|