robust_excel_ole 0.2.3 → 0.3.0
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 +266 -71
- data/TodoList.md +33 -0
- data/examples/edit_sheets/example_adding_sheets.rb +7 -0
- data/examples/open_save_close/example_control_to_excel.rb +4 -4
- data/examples/open_save_close/example_default_excel.rb +49 -0
- data/examples/open_save_close/example_force_excel.rb +34 -0
- data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +1 -1
- data/examples/open_save_close/example_if_obstructed_forget.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_accept.rb +1 -1
- data/examples/open_save_close/example_if_unsaved_forget.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_forget_more.rb +3 -3
- data/examples/open_save_close/example_read_only.rb +1 -1
- data/examples/open_save_close/example_rename_cells.rb +69 -0
- data/examples/open_save_close/example_reuse.rb +8 -8
- data/examples/open_save_close/example_simple.rb +3 -3
- data/examples/open_save_close/example_unobtrusively.rb +28 -0
- data/examples/save_sheets/example_save_sheets.rb +2 -6
- data/lib/robust_excel_ole.rb +18 -0
- data/lib/robust_excel_ole/book.rb +356 -123
- data/lib/robust_excel_ole/book_store.rb +75 -0
- data/lib/robust_excel_ole/excel.rb +105 -53
- data/lib/robust_excel_ole/robustexcelole.sublime-project +8 -8
- data/lib/robust_excel_ole/sheet.rb +29 -1
- data/lib/robust_excel_ole/version.rb +1 -1
- data/robust_excel_ole.gemspec +3 -3
- data/spec/book_spec.rb +1031 -247
- data/spec/book_store_spec.rb +306 -0
- data/spec/data/book_with_blank.xls +0 -0
- data/spec/data/merge_cells.xls +0 -0
- data/spec/data/more_simple.xls +0 -0
- data/spec/data/simple.xls +0 -0
- data/spec/excel_spec.rb +145 -90
- data/spec/sheet_spec.rb +31 -7
- metadata +15 -7
data/TodoList.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
### immediately
|
3
|
+
|
4
|
+
--- 2015-04-09 unobtrusively: save book only if it saved: Test
|
5
|
+
--- 2015-04-09 unobtrusively: return the block result: implementation and test
|
6
|
+
--- 2015-04-09 unobtrusively: namval: return the contents of cells or region with a given name
|
7
|
+
--- 2015-04-09 unobtrusively: visible, displayalerts
|
8
|
+
--- 2015-04-17 unobtrusively: readonly
|
9
|
+
|
10
|
+
|
11
|
+
### quick
|
12
|
+
|
13
|
+
--- 2015-04-02 if_unlocked
|
14
|
+
--- 2015-04-02 standard use cases
|
15
|
+
--- 2015-04-02 set options: make it dry
|
16
|
+
|
17
|
+
### rather quick
|
18
|
+
|
19
|
+
### medium
|
20
|
+
|
21
|
+
### slow
|
22
|
+
|
23
|
+
### done
|
24
|
+
|
25
|
+
--- 2015-04-01 examples for unobtrusively
|
26
|
+
--- 2015-04-01 further tests for unobtrusively
|
27
|
+
--- 2015-03-26 connnect: check test outputs
|
28
|
+
--- 2015-01-26 simpler implementation of connect
|
29
|
+
--- 2015-01-26 reopen with permenent storage
|
30
|
+
--- 2015-01-26 tests for unobtrusively
|
31
|
+
--- 2015-01-26 unobtrusively
|
32
|
+
--- 2015-01-26 option keep_open
|
33
|
+
--- 2015-01-26 separate class for book management
|
@@ -53,6 +53,13 @@ begin
|
|
53
53
|
@book.add_sheet(@book[3], :as => 'sheet_copy', :after => @book[6])
|
54
54
|
show_sheets
|
55
55
|
|
56
|
+
puts"adding a copy of the 2nd sheet and name it again 'second_sheet_copy'"
|
57
|
+
begin
|
58
|
+
@book.add_sheet(sheet, :as => 'second_sheet_copy')
|
59
|
+
rescue ExcelErrorSheet => msg
|
60
|
+
puts "error: add_sheet: #{msg.message}"
|
61
|
+
end
|
62
|
+
|
56
63
|
@book.close(:if_unsaved => :forget) # close the book without saving it
|
57
64
|
|
58
65
|
ensure
|
@@ -12,26 +12,26 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'simple.xls'
|
14
14
|
book = Book.open(file_name) # open a book
|
15
|
-
|
15
|
+
book.visible = true # make current Excel visible
|
16
16
|
sleep 1
|
17
17
|
sheet = book[0] # access a sheet
|
18
18
|
sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
19
19
|
sleep 1
|
20
20
|
begin
|
21
|
-
new_book = Book.open(file_name, :if_unsaved => :
|
21
|
+
new_book = Book.open(file_name, :if_unsaved => :alert) # open another book with the same file name
|
22
22
|
rescue ExcelUserCanceled => msg # if the user chooses not open the book,
|
23
23
|
puts "#{msg.message}" # an exeptions is raised
|
24
24
|
end
|
25
25
|
puts "new book has opened" if new_book
|
26
26
|
Excel.current.Visible = true
|
27
27
|
begin
|
28
|
-
book.close(:if_unsaved => :
|
28
|
+
book.close(:if_unsaved => :alert) # close the unsaved book.
|
29
29
|
rescue ExcelUserCanceled => msg # user is asked whether the unsaved book shall be saved
|
30
30
|
puts "#{msg.message}" # if the user chooses to cancel, then an expeption is raised
|
31
31
|
end
|
32
32
|
if new_book then
|
33
33
|
begin
|
34
|
-
new_book.save_as(file_name, :if_exists => :
|
34
|
+
new_book.save_as(file_name, :if_exists => :alert) # save the new book, if it was opened
|
35
35
|
rescue ExcelErrorSave => msg # user is asked, whether the existing file shall be overwritten
|
36
36
|
puts "save_as: #{msg.message}" # if the user chooses "no" or "cancel", an exception is raised
|
37
37
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# example_default_excel.rb:
|
2
|
+
# reopening books using :default_excel
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
5
|
+
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
6
|
+
require "fileutils"
|
7
|
+
|
8
|
+
include RobustExcelOle
|
9
|
+
|
10
|
+
Excel.close_all
|
11
|
+
begin
|
12
|
+
dir = create_tmpdir
|
13
|
+
file_name1 = dir + 'simple.xls'
|
14
|
+
file_name2 = dir + 'different_simple.xls'
|
15
|
+
file_name3 = dir + 'book_with_blank.xls'
|
16
|
+
file_name4 = dir + 'merge_cells.xls'
|
17
|
+
book1 = Book.open(file_name1) # open a book in a new Excel instance since no Excel is open
|
18
|
+
book1.visible = true # make current Excel visible
|
19
|
+
sleep 2
|
20
|
+
book1.close # close the book
|
21
|
+
sleep 2
|
22
|
+
book2 = Book.open(file_name1) # reopen the book
|
23
|
+
p "book1 == book2" if book2 == book1 # the books are identical
|
24
|
+
sleep 2
|
25
|
+
new_excel = Excel.new(:reuse => false) # create a new Excel
|
26
|
+
book3 = Book.open(file_name2, :default_excel => :reuse, :visible => true) # open another book
|
27
|
+
if book3.excel == book2.excel then # since this book cannot be reopened, the option :default_excel applies:
|
28
|
+
p "book3 opened in the first Excel" # according to :default_excel => :reuse the book is opened
|
29
|
+
end # in the Excel instance the was created first
|
30
|
+
sleep 2
|
31
|
+
new_excel = Excel.new(:reuse => false)
|
32
|
+
book4 = Book.open(file_name3, :default_excel => new_excel, :visible => true) # open another book
|
33
|
+
if book4.excel == new_excel then # since this book cannot be reopened, the option :default_excel applies:
|
34
|
+
p "book4 opened in the second Excel" # according to :default_excel => new_excel the book is opened
|
35
|
+
end # in the given Excel, namely the second Excel instance new_excel
|
36
|
+
sleep 2
|
37
|
+
book5 = Book.open(file_name4, :default_excel => :new, :visible => true) # open another book
|
38
|
+
if ((not book5.excel == book1.excel) && (not book5.excel == new_excel)) then # since this book cannot be reopened,
|
39
|
+
p "book5 opened in a third Excel" # the option :default_excel applies. according to :default_excel => :new
|
40
|
+
end # the book is opened in a new Excel
|
41
|
+
sleep 2
|
42
|
+
book5.close
|
43
|
+
book4.close
|
44
|
+
book3.close
|
45
|
+
book2.close
|
46
|
+
ensure
|
47
|
+
Excel.close_all # close all workbooks, quit Excel instances
|
48
|
+
rm_tmp(dir)
|
49
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# example_force_excel.rb:
|
2
|
+
# opening books in new or given Excel instances using :force_excel
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
5
|
+
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
6
|
+
require "fileutils"
|
7
|
+
|
8
|
+
include RobustExcelOle
|
9
|
+
|
10
|
+
Excel.close_all
|
11
|
+
begin
|
12
|
+
dir = create_tmpdir
|
13
|
+
simple_file = dir + 'simple.xls'
|
14
|
+
book1 = Book.open(simple_file) # open a book in a new Excel instance since no Excel is open
|
15
|
+
book1.visible = true # make current Excel visible
|
16
|
+
sleep 2
|
17
|
+
book2 = Book.open(simple_file) # open a new book in the same Excel instance
|
18
|
+
p "book1 == book2" if book2 == book1 # the books are identical
|
19
|
+
sleep 2
|
20
|
+
book3 = Book.open(simple_file, :force_excel => :new, :visible => true) # open another book in a new Excel instance, # make Excel visible
|
21
|
+
p "book3 != book1" if (not (book3 == book1)) # the books are not identical
|
22
|
+
sleep 2
|
23
|
+
new_excel = Excel.new(:reuse => false) # create a third Excel instance
|
24
|
+
book4 = Book.open(simple_file, :force_excel => new_excel, :visible => true) # open another book in the new Excel instance
|
25
|
+
p "book4 != book3 && book4 != book1" if (not (book4 == book3) && (not (book4 == book1)))
|
26
|
+
sleep 2 # (Excel chooses the first Excel application)
|
27
|
+
book4.close # close the books
|
28
|
+
book3.close
|
29
|
+
book2.close
|
30
|
+
book1.close
|
31
|
+
ensure
|
32
|
+
Excel.close_all # close all workbooks, quit Excel instances
|
33
|
+
rm_tmp(dir)
|
34
|
+
end
|
@@ -21,7 +21,7 @@ begin
|
|
21
21
|
begin
|
22
22
|
new_book = Book.open(other_file_name, :if_obstructed => :close_if_saved) # raises an exception since the file is not saved
|
23
23
|
rescue ExcelErrorOpen => msg
|
24
|
-
puts "open: #{msg.message}"
|
24
|
+
puts "error: open: #{msg.message}"
|
25
25
|
end
|
26
26
|
book.save # save the unsaved book
|
27
27
|
new_book = Book.open(file_name, :if_obstructed => :close_if_saved) # open the new book, close the saved book
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# example_if_obstructed_forget.rb:
|
2
|
-
# open with :if_obstructed: :forget, :
|
2
|
+
# open with :if_obstructed: :forget, :new_excel
|
3
3
|
|
4
4
|
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
5
5
|
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
@@ -17,13 +17,13 @@ begin
|
|
17
17
|
begin
|
18
18
|
new_book = Book.open(other_file_name) # open a book with the same file name in a different path
|
19
19
|
rescue ExcelErrorOpen => msg # by default: raises an exception
|
20
|
-
puts "open: #{msg.message}"
|
20
|
+
puts "error: open: #{msg.message}"
|
21
21
|
end
|
22
22
|
# open a new book with the same file name in a different path. close the old book before.
|
23
23
|
new_book = Book.open(other_file_name, :if_obstructed => :forget)
|
24
24
|
sleep 3
|
25
25
|
# open another book with the same file name in a different path. Use a new Excel application
|
26
|
-
another_book = Book.open(file_name, :if_obstructed => :
|
26
|
+
another_book = Book.open(file_name, :if_obstructed => :new_excel, :visible => true)
|
27
27
|
sleep 3
|
28
28
|
new_book.close # close the books
|
29
29
|
another_book.close
|
@@ -17,7 +17,7 @@ begin
|
|
17
17
|
begin
|
18
18
|
new_book = Book.open(file_name) # open another book with the same file name
|
19
19
|
rescue ExcelErrorOpen => msg # by default: raises an exception:
|
20
|
-
puts "
|
20
|
+
puts "error: open: #{msg.message}" # a book with the same name is already open and unsaved
|
21
21
|
end
|
22
22
|
new_book = Book.open(file_name, :if_unsaved => :accept) # open another book with the same file name
|
23
23
|
# and let the unsaved book open
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# example_ifunsaved_forget.rb:
|
2
|
-
# open with :if_unsaved => :forget, :
|
2
|
+
# open with :if_unsaved => :forget, :new_excel, close with :if_unsaved => :save
|
3
3
|
|
4
4
|
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
5
5
|
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'simple.xls'
|
14
14
|
book = Book.open(file_name) # open a book
|
15
|
-
|
15
|
+
book.visible = true # make current Excel visible
|
16
16
|
sleep 1
|
17
17
|
sheet = book[0] # access a sheet
|
18
18
|
first_cell = sheet[0,0].value
|
@@ -27,7 +27,7 @@ begin
|
|
27
27
|
sleep 1
|
28
28
|
sheet_new_book[0,0] = sheet_new_book[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
29
29
|
# open another book in a new Excel application, and make Excel visible, leaving the unsaved book open
|
30
|
-
another_book = Book.open(file_name, :if_unsaved => :
|
30
|
+
another_book = Book.open(file_name, :if_unsaved => :new_excel, :visible => true)
|
31
31
|
sleep 3 # leaving the unsaved book open
|
32
32
|
new_book.close(:if_unsaved => :forget )
|
33
33
|
another_book.close
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# example_ifunsaved_forget_more.rb:
|
2
|
-
# open with :if_unsaved => :forget, :
|
2
|
+
# open with :if_unsaved => :forget, :new_excel, close with :if_unsaved => :save
|
3
3
|
|
4
4
|
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
5
5
|
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
@@ -12,13 +12,13 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'simple.xls'
|
14
14
|
book = Book.open(file_name) # open a book
|
15
|
-
|
15
|
+
book.visible = true # make current Excel visible
|
16
16
|
sleep 1
|
17
17
|
sheet = book[0] # access a sheet
|
18
18
|
first_cell = sheet[0,0].value
|
19
19
|
sheet[0,0] = first_cell == "simple" ? "complex" : "simple" # change a cell
|
20
20
|
sleep 1
|
21
|
-
new_book = Book.open(file_name, :if_unsaved => :
|
21
|
+
new_book = Book.open(file_name, :if_unsaved => :new_excel, :visible => true) # open another book with the same file name in a new Excel
|
22
22
|
sheet_new_book = new_book[0]
|
23
23
|
if (not book.alive?) && new_book.alive? && sheet_new_book[0,0].value == first_cell then # check whether the unsaved book
|
24
24
|
puts "open with :if_unsaved => :forget : the unsaved book is closed and not saved." # is closed and was not saved
|
@@ -19,7 +19,7 @@ begin
|
|
19
19
|
begin
|
20
20
|
book.save # simple save.
|
21
21
|
rescue ExcelErrorSave => msg # raises an exception because book is opened in read_only mode
|
22
|
-
puts "
|
22
|
+
puts "error: save_as: #{msg.message}"
|
23
23
|
end
|
24
24
|
book.close # close the book without saving it
|
25
25
|
ensure
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# example_simple.rb:
|
2
|
+
# open a book, simple save, save_as, close
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
5
|
+
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
6
|
+
require "fileutils"
|
7
|
+
|
8
|
+
include RobustExcelOle
|
9
|
+
|
10
|
+
Excel.close_all
|
11
|
+
begin
|
12
|
+
dir = create_tmpdir
|
13
|
+
file_name = dir + 'simple.xls'
|
14
|
+
book = Book.open(file_name) # open a book. default: :read_only => false
|
15
|
+
book.visible = true # make current Excel visible
|
16
|
+
sheet = book[0]
|
17
|
+
#sheet.Names.Add("Wert","$A$1"
|
18
|
+
|
19
|
+
workbook = book.workbook
|
20
|
+
fullname = workbook.Fullname
|
21
|
+
puts "fullname: #{fullname}"
|
22
|
+
workbook.Names.Add("a_name", "=$A$1") # rename cell A1 to "a_name"
|
23
|
+
number = workbook.Names.Count
|
24
|
+
puts "number of name objects :#{number}"
|
25
|
+
name_object = workbook.Names("a_name")
|
26
|
+
name = name_object.Name
|
27
|
+
puts "name: #{name}"
|
28
|
+
value = name_object.Value # definition of the cell
|
29
|
+
puts "definition: #{value}"
|
30
|
+
reference = name_object.RefersTo
|
31
|
+
puts "reference: #{reference}"
|
32
|
+
visible = name_object.Visible
|
33
|
+
puts "visible: #{visible}"
|
34
|
+
sleep 2
|
35
|
+
workbook.Names("a_name").Name = "new_name"
|
36
|
+
new_name_object = workbook.Names("new_name")
|
37
|
+
puts "name: #{new_name_object.Name}"
|
38
|
+
puts "definition: #{new_name_object.Value}"
|
39
|
+
puts "reference: #{new_name_object.RefersTo}"
|
40
|
+
puts "visible: #{new_name_object.Visible}"
|
41
|
+
sleep 2
|
42
|
+
new_name_object.RefersTo = "=$A$2"
|
43
|
+
puts "name: #{new_name_object.Name}"
|
44
|
+
puts "definition: #{new_name_object.Value}"
|
45
|
+
puts "reference: #{new_name_object.RefersTo}"
|
46
|
+
puts "visible: #{new_name_object.Visible}"
|
47
|
+
sleep 2
|
48
|
+
new_name_object.Visible = false
|
49
|
+
puts "visible: #{new_name_object.Visible}"
|
50
|
+
sleep 2
|
51
|
+
|
52
|
+
# to do:
|
53
|
+
# read, write contents of the cell (under the old name)
|
54
|
+
# read, write contents in the cell under the new name
|
55
|
+
|
56
|
+
#sheet.Cells
|
57
|
+
# sheet[0,0].value
|
58
|
+
# Worksheets("sheet1").Cells
|
59
|
+
|
60
|
+
new_name_object.Delete
|
61
|
+
|
62
|
+
sleep 2
|
63
|
+
book.close(:if_unsaved => :forget) # close the book
|
64
|
+
|
65
|
+
ensure
|
66
|
+
Excel.close_all # close workbooks, quit Excel application
|
67
|
+
rm_tmp(dir)
|
68
|
+
end
|
69
|
+
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# example_reuse.rb: open a book in a
|
1
|
+
# example_reuse.rb: open a book in a new Excel and a running Excel instance. make visible
|
2
2
|
|
3
3
|
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
4
4
|
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
@@ -13,15 +13,15 @@ begin
|
|
13
13
|
file_name2 = dir + 'different_simple.xls'
|
14
14
|
file_name3 = dir + 'different_simple.xls'
|
15
15
|
file_name4 = dir + 'book_with_blank.xls'
|
16
|
-
book1 = Book.open(file_name1) # open a book in a new Excel
|
17
|
-
|
16
|
+
book1 = Book.open(file_name1) # open a book in a new Excel instance since no Excel is open
|
17
|
+
book1.visible = true # make current Excel visible
|
18
18
|
sleep 2
|
19
|
-
book2 = Book.open(file_name2) # open a new book in the same Excel
|
20
|
-
sleep 2
|
21
|
-
book3 = Book.open(file_name3, :
|
19
|
+
book2 = Book.open(file_name2) # open a new book in the same Excel instance
|
20
|
+
sleep 2
|
21
|
+
book3 = Book.open(file_name3, :force_excel => :new, :visible => true) # open another book in a new Excel instance,
|
22
22
|
sleep 2 # make Excel visible
|
23
|
-
book4 = Book.open(file_name4, :
|
24
|
-
sleep 2
|
23
|
+
book4 = Book.open(file_name4, :visible => true) # open anther book, and use a running Excel application
|
24
|
+
sleep 2 # (Excel chooses the first Excel application)
|
25
25
|
book1.close # close the books
|
26
26
|
book2.close
|
27
27
|
book3.close
|
@@ -12,8 +12,8 @@ begin
|
|
12
12
|
dir = create_tmpdir
|
13
13
|
file_name = dir + 'simple.xls'
|
14
14
|
other_file_name = dir + 'different_simple.xls'
|
15
|
-
book = Book.open(file_name)
|
16
|
-
|
15
|
+
book = Book.open(file_name) # open a book. default: :read_only => false
|
16
|
+
book.visible = true # make current Excel visible
|
17
17
|
sheet = book[0] # access a sheet
|
18
18
|
sleep 1
|
19
19
|
sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
|
@@ -22,7 +22,7 @@ begin
|
|
22
22
|
begin
|
23
23
|
book.save_as(other_file_name) # save_as : default :if_exists => :raise
|
24
24
|
rescue ExcelErrorSave => msg
|
25
|
-
puts "
|
25
|
+
puts "error: save_as: #{msg.message}"
|
26
26
|
end
|
27
27
|
book.save_as(other_file_name, :if_exists => :overwrite) # save_as with :if_exists => :overwrite
|
28
28
|
puts "save_as: saved successfully with option :if_exists => :overwrite"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# example_unobtrusively.rb:
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
|
4
|
+
require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
|
5
|
+
require "fileutils"
|
6
|
+
|
7
|
+
include RobustExcelOle
|
8
|
+
|
9
|
+
Excel.close_all
|
10
|
+
begin
|
11
|
+
dir = create_tmpdir
|
12
|
+
simple_file = dir + 'simple.xls'
|
13
|
+
book = Book.open(simple_file, :visible => true) # open a book, make Excel visible
|
14
|
+
old_sheet = book[0]
|
15
|
+
p "1st cell: #{old_sheet[0,0].value}"
|
16
|
+
sleep 2
|
17
|
+
Book.unobtrusively(simple_file) do |book| # modify the book and keep its status unchanged
|
18
|
+
sheet = book[0]
|
19
|
+
sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
|
20
|
+
end
|
21
|
+
new_sheet = book[0]
|
22
|
+
p "1st cell: #{new_sheet[0,0].value}"
|
23
|
+
p "book saved" if book.Saved
|
24
|
+
book.close # close the book
|
25
|
+
ensure
|
26
|
+
Excel.close_all # close all workbooks, quit Excel application
|
27
|
+
rm_tmp(dir)
|
28
|
+
end
|
@@ -13,18 +13,14 @@ begin
|
|
13
13
|
suffix = '.xls'
|
14
14
|
book_name = dir + 'book_with_blank'
|
15
15
|
book = Book.open(book_name + suffix) # open a book with several sheets
|
16
|
-
|
16
|
+
book.visible = true # make Excel visible
|
17
17
|
i = 0
|
18
18
|
book.each do |sheet|
|
19
19
|
i = i + 1
|
20
20
|
puts "#{i}. sheet:"
|
21
21
|
sheet_name = book_name + "_sheet#{i}"
|
22
22
|
puts "sheet_name: #{sheet_name}"
|
23
|
-
|
24
|
-
excel.Workbooks.Add # generate an empty workbook
|
25
|
-
empty_workbook = excel.Workbooks.Item(1) # save it
|
26
|
-
empty_workbook.SaveAs(absolute_path(sheet_name), XlExcel8) # close it
|
27
|
-
empty_workbook.Close
|
23
|
+
Excel.generate_workbook absolute_path(sheet_name) # generate an empty workbook
|
28
24
|
sheet_book = Book.open(absolute_path(sheet_name) + suffix) # open the book
|
29
25
|
sheet_book.add_sheet sheet # add the sheet
|
30
26
|
sheet_book.save # save it
|
data/lib/robust_excel_ole.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "win32ole"
|
2
2
|
require File.join(File.dirname(__FILE__), 'robust_excel_ole/excel')
|
3
|
+
require File.join(File.dirname(__FILE__), 'robust_excel_ole/book_store')
|
3
4
|
require File.join(File.dirname(__FILE__), 'robust_excel_ole/book')
|
4
5
|
require File.join(File.dirname(__FILE__), 'robust_excel_ole/sheet')
|
5
6
|
require File.join(File.dirname(__FILE__), 'robust_excel_ole/cell')
|
@@ -10,4 +11,21 @@ require File.join(File.dirname(__FILE__), 'robust_excel_ole/version')
|
|
10
11
|
|
11
12
|
module RobustExcelOle
|
12
13
|
|
14
|
+
def absolute_path(file)
|
15
|
+
file = File.expand_path(file)
|
16
|
+
file = RobustExcelOle::Cygwin.cygpath('-w', file) if RUBY_PLATFORM =~ /cygwin/
|
17
|
+
WIN32OLE.new('Scripting.FileSystemObject').GetAbsolutePathName(file)
|
18
|
+
end
|
19
|
+
|
20
|
+
def canonize(filename)
|
21
|
+
raise "No string given to canonize, but #{filename.inspect}" unless filename.is_a?(String)
|
22
|
+
filename.downcase rescue nil
|
23
|
+
end
|
24
|
+
|
25
|
+
module_function :absolute_path, :canonize
|
26
|
+
|
27
|
+
|
28
|
+
class VBAMethodMissingError < RuntimeError # :nodoc: #
|
29
|
+
end
|
30
|
+
|
13
31
|
end
|