robust_excel_ole 1.11 → 1.12
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.
- checksums.yaml +4 -4
- data/Changelog +11 -0
- data/README.rdoc +20 -8
- data/docs/README_open.rdoc +1 -0
- data/docs/README_ranges.rdoc +5 -11
- data/examples/{introducing_examples/example_introducing.rb → introductory_examples/example_introductory.rb} +10 -2
- data/examples/{introducing_examples → introductory_examples}/example_open.rb +18 -17
- data/examples/{introducing_examples → introductory_examples}/example_range.rb +29 -16
- data/examples/modifying_sheets/example_access_sheets_and_cells.rb +8 -7
- data/examples/modifying_sheets/example_add_names.rb +4 -8
- data/examples/modifying_sheets/example_adding_sheets.rb +7 -6
- data/examples/modifying_sheets/example_concating.rb +2 -2
- data/examples/modifying_sheets/example_copying.rb +3 -3
- data/examples/modifying_sheets/example_expanding.rb +2 -2
- data/examples/modifying_sheets/example_naming.rb +2 -2
- data/examples/modifying_sheets/example_ranges.rb +4 -4
- data/examples/modifying_sheets/example_saving.rb +3 -3
- data/examples/open_save_close/example_control_to_excel.rb +10 -11
- data/examples/open_save_close/example_default_excel.rb +13 -14
- data/examples/open_save_close/example_force_excel.rb +9 -10
- data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +7 -7
- data/examples/open_save_close/example_if_obstructed_forget.rb +5 -5
- data/examples/open_save_close/example_if_obstructed_save.rb +7 -7
- data/examples/open_save_close/example_if_unsaved_accept.rb +13 -13
- data/examples/open_save_close/example_if_unsaved_forget.rb +9 -10
- data/examples/open_save_close/example_if_unsaved_forget_more.rb +9 -10
- data/examples/open_save_close/example_read_only.rb +6 -6
- data/examples/open_save_close/example_rename_cells.rb +4 -5
- data/examples/open_save_close/example_reuse.rb +6 -6
- data/examples/open_save_close/example_simple.rb +5 -5
- data/examples/open_save_close/example_unobtrusively.rb +4 -4
- data/lib/robust_excel_ole/address.rb +0 -4
- data/lib/robust_excel_ole/bookstore.rb +4 -28
- data/lib/robust_excel_ole/excel.rb +17 -22
- data/lib/robust_excel_ole/general.rb +11 -18
- data/lib/robust_excel_ole/range_owners.rb +17 -27
- data/lib/robust_excel_ole/reo_common.rb +7 -3
- data/lib/robust_excel_ole/version.rb +1 -1
- data/lib/robust_excel_ole/workbook.rb +178 -180
- data/lib/robust_excel_ole/worksheet.rb +7 -4
- data/robust_excel_ole.gemspec +6 -4
- data/spec/bookstore_spec.rb +38 -34
- data/spec/data/more_data/workbook.xls +0 -0
- data/spec/excel_spec.rb +89 -44
- data/spec/general_spec.rb +1 -0
- data/spec/range_spec.rb +7 -4
- data/spec/workbook_specs/workbook_close_spec.rb +2 -1
- data/spec/workbook_specs/workbook_misc_spec.rb +34 -18
- data/spec/workbook_specs/workbook_open_spec.rb +112 -71
- data/spec/workbook_specs/workbook_save_spec.rb +173 -5
- data/spec/workbook_specs/workbook_sheet_spec.rb +6 -42
- data/spec/workbook_specs/workbook_unobtr_spec.rb +9 -246
- data/spec/worksheet_spec.rb +21 -5
- metadata +12 -11
@@ -2,8 +2,8 @@
|
|
2
2
|
# each named cell gets the value of cell right to it appended to its own value
|
3
3
|
# the new workbook's name is extended by the suffix "_concat"
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
require_relative '../../lib/robust_excel_ole'
|
6
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
7
7
|
require "fileutils"
|
8
8
|
|
9
9
|
include RobustExcelOle
|
@@ -1,10 +1,10 @@
|
|
1
1
|
# example_copying.rb:
|
2
|
-
# each named cell is to be copied into another
|
2
|
+
# each named cell is to be copied into another worksheet
|
3
3
|
# unnamed cells shall not be copied
|
4
4
|
# if a sheet does not contain any named cell, then the sheet shall not be copied
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require_relative '../../lib/robust_excel_ole'
|
7
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
8
8
|
require "fileutils"
|
9
9
|
|
10
10
|
include RobustExcelOle
|
@@ -5,8 +5,8 @@
|
|
5
5
|
# the sheet's name shall be the name of the Excel name
|
6
6
|
# in addition to that, the cell B2 shall be named "name" and get the sheet name as its value
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
require_relative '../../lib/robust_excel_ole'
|
9
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
10
10
|
require "fileutils"
|
11
11
|
|
12
12
|
include RobustExcelOle
|
@@ -3,8 +3,8 @@
|
|
3
3
|
# the contents of each cell is copied
|
4
4
|
# the new workbook's name is extended by the suffix "_named"
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
require_relative '../../lib/robust_excel_ole'
|
7
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
8
8
|
require "fileutils"
|
9
9
|
|
10
10
|
include RobustExcelOle
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_ranges.rb:
|
2
2
|
# access row and column ranges of a sheet.
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -13,8 +13,8 @@ begin
|
|
13
13
|
simple_file = dir + 'workbook.xls'
|
14
14
|
simple_save_file = dir + 'workbook_save.xls'
|
15
15
|
File.delete simple_save_file rescue nil
|
16
|
-
book = Workbook.open(simple_file) # open a
|
17
|
-
sheet = book.sheet('Sheet1') # access a
|
16
|
+
book = Workbook.open(simple_file, :visible => true) # open a workbook
|
17
|
+
sheet = book.sheet('Sheet1') # access a worksheet via the name
|
18
18
|
row_r = sheet.row_range(1) # access the whole range of the first row
|
19
19
|
col_r = sheet.col_range(1, 1..2) # access the first two cells of the range of the first column
|
20
20
|
cell = col_r[0] # access the first cell of these cells
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_saving.rb:
|
2
2
|
# save the sheets of a book as separate workbooks
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -27,7 +27,7 @@ begin
|
|
27
27
|
sheet.Delete unless sheet.name == sheet_orig.name
|
28
28
|
end
|
29
29
|
book.close(:if_unsaved => :save)
|
30
|
-
# alternative: delete all other
|
30
|
+
# alternative: delete all other worksheets
|
31
31
|
#book = Workbook.open(file_sheet_name, :force => {:excel => :new}, :visible => true)
|
32
32
|
#book.each do |sheet|
|
33
33
|
# book.sheet(sheet.Name).Delete() unless sheet.Name == sheet_orig.Name
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_give_control_to_excel.rb:
|
2
2
|
# open, close, save with giving control to Excel
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -11,32 +11,31 @@ Excel.kill_all
|
|
11
11
|
begin
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'workbook.xls'
|
14
|
-
book = Workbook.open(file_name) # open a book
|
15
|
-
book.excel.visible = true # make current Excel visible
|
14
|
+
book = Workbook.open(file_name, :visible => true) # open a book
|
16
15
|
sleep 1
|
17
|
-
sheet = book.sheet(1) # access a
|
16
|
+
sheet = book.sheet(1) # access a worksheet
|
18
17
|
sheet[1,1] = sheet[1,1].Value == "simple" ? "complex" : "simple" # change a cell
|
19
18
|
sleep 1
|
20
19
|
begin
|
21
|
-
new_book = Workbook.open(file_name, :if_unsaved => :alert) # open another
|
22
|
-
rescue WorkbookREOError => msg # if the user chooses not open the
|
20
|
+
new_book = Workbook.open(file_name, :visible => true, :if_unsaved => :alert) # open another workbook with the same file name
|
21
|
+
rescue WorkbookREOError => msg # if the user chooses not open the workbook,
|
23
22
|
puts "#{msg.message}" # an exeptions is raised
|
24
23
|
end
|
25
24
|
puts "new book has opened" if new_book
|
26
25
|
Excel.current.visible = true
|
27
26
|
begin
|
28
|
-
book.close(:if_unsaved => :alert) # close the unsaved
|
29
|
-
rescue WorkbookREOError => msg # user is asked whether the unsaved
|
27
|
+
book.close(:if_unsaved => :alert) # close the unsaved workbook.
|
28
|
+
rescue WorkbookREOError => msg # user is asked whether the unsaved workbook shall be saved
|
30
29
|
puts "#{msg.message}" # if the user chooses to cancel, then an expeption is raised
|
31
30
|
end
|
32
31
|
if new_book then
|
33
32
|
begin
|
34
|
-
new_book.save_as(file_name, :if_exists => :alert) # save the new
|
33
|
+
new_book.save_as(file_name, :if_exists => :alert) # save the new workbook, if it was opened
|
35
34
|
rescue WorkbookREOError => msg # user is asked, whether the existing file shall be overwritten
|
36
35
|
puts "save_as: #{msg.message}" # if the user chooses "no" or "cancel", an exception is raised
|
37
36
|
end
|
38
37
|
|
39
|
-
new_book.close # close the new
|
38
|
+
new_book.close # close the new workbook, if the user chose to open it
|
40
39
|
end
|
41
40
|
ensure
|
42
41
|
Excel.kill_all # close ALL workbooks, quit Excel application
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_default_excel.rb:
|
2
2
|
# reopening books using :default_excel
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -14,30 +14,29 @@ begin
|
|
14
14
|
file_name2 = dir + 'different_workbook.xls'
|
15
15
|
file_name3 = dir + 'book_with_blank.xls'
|
16
16
|
file_name4 = dir + 'merge_cells.xls'
|
17
|
-
book1 = Workbook.open(file_name1)
|
18
|
-
book1.excel.visible = true # make current Excel visible
|
17
|
+
book1 = Workbook.open(file_name1, :visible => true) # open a workbook in a new Excel instance since no Excel is open
|
19
18
|
sleep 2
|
20
|
-
book1.close # close the
|
19
|
+
book1.close # close the workbook
|
21
20
|
sleep 2
|
22
|
-
book2 = Workbook.open(file_name1) # reopen the
|
21
|
+
book2 = Workbook.open(file_name1, :visible => true) # reopen the workbook
|
23
22
|
p "book1 == book2" if book2 == book1 # the books are identical
|
24
23
|
sleep 2
|
25
24
|
new_excel = Excel.new(:reuse => false) # create a new Excel
|
26
25
|
book3 = Workbook.open(file_name2, :default => {:excel => :current}, :visible => true) # open another book
|
27
|
-
if book3.excel == book2.excel then # since this
|
28
|
-
p "book3 opened in the first Excel" # according to :default => {:excel => :current} the
|
26
|
+
if book3.excel == book2.excel then # since this workbook cannot be reopened, the option :default => {:excel} applies:
|
27
|
+
p "book3 opened in the first Excel" # according to :default => {:excel => :current} the workbook is opened
|
29
28
|
end # in the Excel instance the was created first
|
30
29
|
sleep 2
|
31
30
|
new_excel = Excel.new(:reuse => false)
|
32
|
-
book4 = Workbook.open(file_name3, :default => {:excel => new_excel}, :visible => true) # open another
|
33
|
-
if book4.excel == new_excel then # since this
|
34
|
-
p "book4 opened in the second Excel" # according to :default_excel => new_excel the
|
31
|
+
book4 = Workbook.open(file_name3, :default => {:excel => new_excel}, :visible => true) # open another workbook
|
32
|
+
if book4.excel == new_excel then # since this workbook cannot be reopened, the option :default_excel applies:
|
33
|
+
p "book4 opened in the second Excel" # according to :default_excel => new_excel the workbook is opened
|
35
34
|
end # in the given Excel, namely the second Excel instance new_excel
|
36
35
|
sleep 2
|
37
|
-
book5 = Workbook.open(file_name4, :default => {:excel => :new}, :visible => true) # open another
|
38
|
-
if ((not book5.excel == book1.excel) && (not book5.excel == new_excel)) then # since this
|
36
|
+
book5 = Workbook.open(file_name4, :default => {:excel => :new}, :visible => true) # open another workbook
|
37
|
+
if ((not book5.excel == book1.excel) && (not book5.excel == new_excel)) then # since this workbook cannot be reopened,
|
39
38
|
p "book5 opened in a third Excel" # the option :default_excel applies. according to :default_excel => :new
|
40
|
-
end # the
|
39
|
+
end # the workbook is opened in a new Excel
|
41
40
|
sleep 2
|
42
41
|
book5.close
|
43
42
|
book4.close
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_force_excel.rb:
|
2
2
|
# opening books in new or given Excel instances using :force_excel
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -11,20 +11,19 @@ Excel.kill_all
|
|
11
11
|
begin
|
12
12
|
dir = create_tmpdir
|
13
13
|
simple_file = dir + 'workbook.xls'
|
14
|
-
book1 = Workbook.open(simple_file)
|
15
|
-
book1.excel.visible = true # make current Excel visible
|
14
|
+
book1 = Workbook.open(simple_file, :visible => true) # open a workbook in a new Excel instance since no Excel is open
|
16
15
|
sleep 2
|
17
|
-
book2 = Workbook.open(simple_file) # open a new
|
18
|
-
p "book1 == book2" if book2 == book1 # the
|
16
|
+
book2 = Workbook.open(simple_file) # open a new workbook in the same Excel instance
|
17
|
+
p "book1 == book2" if book2 == book1 # the workbooks are identical
|
19
18
|
sleep 2
|
20
|
-
book3 = Workbook.open(simple_file, :force => {:excel => :new, :visible => true}) # open another
|
21
|
-
p "book3 != book1" if (not (book3 == book1)) # the
|
19
|
+
book3 = Workbook.open(simple_file, :force => {:excel => :new, :visible => true}) # open another workbook in a new Excel instance,
|
20
|
+
p "book3 != book1" if (not (book3 == book1)) # the workbooks are not identical
|
22
21
|
sleep 2
|
23
22
|
new_excel = Excel.new(:reuse => false) # create a third Excel instance
|
24
|
-
book4 = Workbook.open(simple_file, :force => {:excel => new_excel, :visible => true}) # open another
|
23
|
+
book4 = Workbook.open(simple_file, :force => {:excel => new_excel, :visible => true}) # open another workbook in the new Excel instance
|
25
24
|
p "book4 != book3 && book4 != book1" if (not (book4 == book3) && (not (book4 == book1)))
|
26
25
|
sleep 2 # (Excel chooses the first Excel application)
|
27
|
-
book4.close # close the
|
26
|
+
book4.close # close the workbooks
|
28
27
|
book3.close
|
29
28
|
book2.close
|
30
29
|
book1.close
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_if_obstructed_close_if_saved.rb:
|
2
2
|
# open with :if_obstructed: :close_if_saved
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -12,10 +12,10 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'workbook.xls'
|
14
14
|
other_file_name = dir + 'more_data/workbook.xls'
|
15
|
-
book = Workbook.open(file_name, :visible => true) # open a
|
15
|
+
book = Workbook.open(file_name, :visible => true) # open a workbook, make Excel visible
|
16
16
|
sleep 1
|
17
17
|
sheet = book.sheet(1)
|
18
|
-
first_cell = sheet[1,1].Value # access a
|
18
|
+
first_cell = sheet[1,1].Value # access a worksheet
|
19
19
|
sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
|
20
20
|
sleep 1
|
21
21
|
begin
|
@@ -23,13 +23,13 @@ begin
|
|
23
23
|
rescue WorkbookBlocked => msg
|
24
24
|
puts "error: open: #{msg.message}"
|
25
25
|
end
|
26
|
-
book.save # save the unsaved
|
27
|
-
new_book = Workbook.open(file_name, :if_obstructed => :close_if_saved) # open the new
|
26
|
+
book.save # save the unsaved workbook
|
27
|
+
new_book = Workbook.open(file_name, :if_obstructed => :close_if_saved) # open the new workbook, close the saved workbook
|
28
28
|
sleep 1
|
29
29
|
new_sheet = new_book.sheet(1)
|
30
30
|
new_first_cell = new_sheet[1,1].Value
|
31
31
|
puts "the old book was saved" unless new_first_cell == first_cell
|
32
|
-
new_book.close # close the
|
32
|
+
new_book.close # close the workbook
|
33
33
|
ensure
|
34
34
|
Excel.kill_all # close all workbooks, quit Excel application
|
35
35
|
#rm_tmp(dir)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_if_obstructed_forget.rb:
|
2
2
|
# open with :if_obstructed: :forget, :new_excel
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -12,10 +12,10 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'workbook.xls'
|
14
14
|
other_file_name = dir + 'more_data/workbook.xls'
|
15
|
-
book = Workbook.open(file_name, :visible => true) # open a
|
15
|
+
book = Workbook.open(file_name, :visible => true) # open a workbook, make Excel application visible
|
16
16
|
sleep 3
|
17
17
|
begin
|
18
|
-
new_book = Workbook.open(other_file_name) # open a
|
18
|
+
new_book = Workbook.open(other_file_name) # open a workbook with the same file name in a different path
|
19
19
|
rescue WorkbookBlocked => msg # by default: raises an exception
|
20
20
|
puts "error: open: #{msg.message}"
|
21
21
|
end
|
@@ -25,7 +25,7 @@ begin
|
|
25
25
|
# open another book with the same file name in a different path. Use a new Excel application
|
26
26
|
another_book = Workbook.open(file_name, :if_obstructed => :new_excel, :visible => true)
|
27
27
|
sleep 3
|
28
|
-
new_book.close # close the
|
28
|
+
new_book.close # close the workbooks
|
29
29
|
another_book.close
|
30
30
|
ensure
|
31
31
|
Excel.kill_all # close all workbooks, quit Excel application
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_if_obstructed_save.rb:
|
2
2
|
# open with :if_obstructed: :save
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -12,20 +12,20 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'workbook.xls'
|
14
14
|
other_file_name = dir + 'more_data/workbook.xls'
|
15
|
-
book = Workbook.open(file_name, :visible => true) # open a
|
15
|
+
book = Workbook.open(file_name, :visible => true) # open a workbook, make Excel visible
|
16
16
|
sleep 1
|
17
17
|
sheet = book.sheet(1)
|
18
|
-
first_cell = sheet[1,1].Value # access a
|
18
|
+
first_cell = sheet[1,1].Value # access a worksheet
|
19
19
|
sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
|
20
20
|
sleep 1
|
21
|
-
new_book = Workbook.open(other_file_name, :if_obstructed => :save) # open a
|
22
|
-
sleep 1 #save the old
|
21
|
+
new_book = Workbook.open(other_file_name, :if_obstructed => :save) # open a workbook with the same file name in a different path
|
22
|
+
sleep 1 #save the old workbook, close it, before
|
23
23
|
old_book = Workbook.open(file_name, :if_obstructed => :forget ,:visible => true) # open the old book
|
24
24
|
sleep 1
|
25
25
|
old_sheet = old_book.sheet(1)
|
26
26
|
old_first_cell = old_sheet[1,1].Value
|
27
27
|
puts "the old book was saved" unless old_first_cell == first_cell
|
28
|
-
new_book.close # close the
|
28
|
+
new_book.close # close the workbooks
|
29
29
|
old_book.close
|
30
30
|
ensure
|
31
31
|
Excel.kill_all # close all workbooks, quit Excel application
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_ifunsaved_accept.rb:
|
2
2
|
# open with :if_unsaved => :accept, close with :if_unsaved => :save
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -11,30 +11,30 @@ Excel.kill_all
|
|
11
11
|
begin
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'workbook.xls'
|
14
|
-
book = Workbook.open(file_name) # open a
|
15
|
-
sheet = book.sheet(1) # access a
|
14
|
+
book = Workbook.open(file_name, :visible => true) # open a workbook
|
15
|
+
sheet = book.sheet(1) # access a worksheet
|
16
16
|
sheet[1,1] = sheet[1,1].Value == "simple" ? "complex" : "simple" # change a cell
|
17
17
|
begin
|
18
|
-
new_book = Workbook.open(file_name)
|
19
|
-
rescue WorkbookNotSaved => msg
|
20
|
-
puts "error: open: #{msg.message}" # a
|
18
|
+
new_book = Workbook.open(file_name) # open another workbook with the same file name
|
19
|
+
rescue WorkbookNotSaved => msg # by default: raises an exception:
|
20
|
+
puts "error: open: #{msg.message}" # a workbook with the same name is already open and unsaved
|
21
21
|
end
|
22
|
-
new_book = Workbook.open(file_name, :if_unsaved => :accept) # open another
|
23
|
-
# and let the unsaved
|
22
|
+
new_book = Workbook.open(file_name, :if_unsaved => :accept) # open another workbook with the same file name
|
23
|
+
# and let the unsaved workbook open
|
24
24
|
if book.alive? && new_book.alive? then # check whether the referenced workbooks
|
25
25
|
puts "open with :if_unsaved => :accept : the two books are alive." # respond to methods
|
26
26
|
end
|
27
|
-
if book == new_book then # check whether the
|
27
|
+
if book == new_book then # check whether the workbook are equal
|
28
28
|
puts "both books are equal"
|
29
29
|
end
|
30
30
|
begin
|
31
|
-
book.close # close the
|
32
|
-
rescue WorkbookNotSaved => msg #
|
31
|
+
book.close # close the workbook. by default: raises an exception:
|
32
|
+
rescue WorkbookNotSaved => msg # workbook is unsaved
|
33
33
|
puts "close error: #{msg.message}"
|
34
34
|
end
|
35
35
|
book.close(:if_unsaved => :save) # save the book before closing it
|
36
36
|
puts "closed the book successfully with option :if_unsaved => :save"
|
37
|
-
new_book.close # close the other
|
37
|
+
new_book.close # close the other workbook. It is already saved.
|
38
38
|
ensure
|
39
39
|
Excel.kill_all # close workbooks, quit Excel application
|
40
40
|
rm_tmp(dir)
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# example_if_unsaved_forget.rb:
|
2
2
|
# open with :if_unsaved => :forget, :new_excel, close with :if_unsaved => :save
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
require_relative '../../lib/robust_excel_ole'
|
5
|
+
require_relative '../../spec/helpers/create_temporary_dir'
|
6
6
|
require "fileutils"
|
7
7
|
|
8
8
|
include RobustExcelOle
|
@@ -11,28 +11,27 @@ Excel.kill_all
|
|
11
11
|
begin
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'workbook.xls'
|
14
|
-
book = Workbook.open(file_name)
|
14
|
+
book = Workbook.open(file_name, :visible => true) # open a workbook
|
15
15
|
puts "file_name: #{file_name}"
|
16
|
-
#book.excel.visible = true # make current Excel visible
|
17
16
|
sleep 1
|
18
|
-
sheet = book.sheet(1)
|
17
|
+
sheet = book.sheet(1) # access a worksheet
|
19
18
|
first_cell = sheet[1,1].Value
|
20
19
|
sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
|
21
20
|
sleep 1
|
22
21
|
puts "new_book: before"
|
23
22
|
puts "file_name: #{file_name}"
|
24
|
-
new_book = Workbook.open(file_name, :if_unsaved => :forget) # open another
|
25
|
-
# and close the unsaved
|
23
|
+
new_book = Workbook.open(file_name, :if_unsaved => :forget) # open another workbook with the same file name
|
24
|
+
# and close the unsaved workbook without saving it
|
26
25
|
puts "new_book: after"
|
27
26
|
sheet_new_book = new_book.sheet(1)
|
28
|
-
if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].Value == first_cell then # check whether the unsaved
|
27
|
+
if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].Value == first_cell then # check whether the unsaved workbook
|
29
28
|
puts "open with :if_unsaved => :forget : the unsaved book is closed and not saved." # is closed and was not saved
|
30
29
|
end
|
31
30
|
sleep 1
|
32
31
|
sheet_new_book[1,1] = sheet_new_book[1,1].Value == "simple" ? "complex" : "simple" # change a cell
|
33
|
-
# open another
|
32
|
+
# open another workbook in a new Excel application, and make Excel visible, leaving the unsaved workbook open
|
34
33
|
another_book = Workbook.open(file_name, :if_unsaved => :new_excel, :visible => true)
|
35
|
-
sleep 3 # leaving the unsaved
|
34
|
+
sleep 3 # leaving the unsaved workbook open
|
36
35
|
new_book.close(:if_unsaved => :forget )
|
37
36
|
another_book.close
|
38
37
|
ensure
|