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.
Files changed (34) hide show
  1. data/README.rdoc +266 -71
  2. data/TodoList.md +33 -0
  3. data/examples/edit_sheets/example_adding_sheets.rb +7 -0
  4. data/examples/open_save_close/example_control_to_excel.rb +4 -4
  5. data/examples/open_save_close/example_default_excel.rb +49 -0
  6. data/examples/open_save_close/example_force_excel.rb +34 -0
  7. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +1 -1
  8. data/examples/open_save_close/example_if_obstructed_forget.rb +3 -3
  9. data/examples/open_save_close/example_if_unsaved_accept.rb +1 -1
  10. data/examples/open_save_close/example_if_unsaved_forget.rb +3 -3
  11. data/examples/open_save_close/example_if_unsaved_forget_more.rb +3 -3
  12. data/examples/open_save_close/example_read_only.rb +1 -1
  13. data/examples/open_save_close/example_rename_cells.rb +69 -0
  14. data/examples/open_save_close/example_reuse.rb +8 -8
  15. data/examples/open_save_close/example_simple.rb +3 -3
  16. data/examples/open_save_close/example_unobtrusively.rb +28 -0
  17. data/examples/save_sheets/example_save_sheets.rb +2 -6
  18. data/lib/robust_excel_ole.rb +18 -0
  19. data/lib/robust_excel_ole/book.rb +356 -123
  20. data/lib/robust_excel_ole/book_store.rb +75 -0
  21. data/lib/robust_excel_ole/excel.rb +105 -53
  22. data/lib/robust_excel_ole/robustexcelole.sublime-project +8 -8
  23. data/lib/robust_excel_ole/sheet.rb +29 -1
  24. data/lib/robust_excel_ole/version.rb +1 -1
  25. data/robust_excel_ole.gemspec +3 -3
  26. data/spec/book_spec.rb +1031 -247
  27. data/spec/book_store_spec.rb +306 -0
  28. data/spec/data/book_with_blank.xls +0 -0
  29. data/spec/data/merge_cells.xls +0 -0
  30. data/spec/data/more_simple.xls +0 -0
  31. data/spec/data/simple.xls +0 -0
  32. data/spec/excel_spec.rb +145 -90
  33. data/spec/sheet_spec.rb +31 -7
  34. metadata +15 -7
@@ -0,0 +1,75 @@
1
+
2
+ # -*- coding: utf-8 -*-
3
+
4
+ module RobustExcelOle
5
+
6
+ class BookStore
7
+
8
+ def initialize
9
+ @filename2books = Hash.new {|hash, key| hash[key] = [] }
10
+ end
11
+
12
+ # returns a book with the given filename, if it was open once
13
+ # preference order: writable book, readonly unsaved book, readonly book (the last one), closed book
14
+ # options:
15
+ # :readonly_excel => <instance> -> return the book that was open in the given excel instance,
16
+ # even if it is not writable, if such a book exists
17
+ # prefer the writable book as described above, otherwise
18
+ def fetch(filename, options = { })
19
+ filename_key = RobustExcelOle::canonize(filename)
20
+ readonly_book = readonly_unsaved_book = closed_book = result = nil
21
+ books = @filename2books[filename_key]
22
+ return nil unless books
23
+ books.each do |book|
24
+ return book if options[:readonly_excel] && book.excel == options[:readonly_excel]
25
+ if book.alive?
26
+ if (not book.ReadOnly)
27
+ if options[:readonly_excel]
28
+ result = book
29
+ else
30
+ return book
31
+ end
32
+ else
33
+ book.Saved ? readonly_book = book : readonly_unsaved_book = book
34
+ end
35
+ else
36
+ closed_book = book
37
+ end
38
+ end
39
+ result ? result : (readonly_unsaved_book ? readonly_unsaved_book : (readonly_book ? readonly_book : closed_book))
40
+ end
41
+
42
+ # stores a book
43
+ def store(book)
44
+ filename_key = RobustExcelOle::canonize(book.filename)
45
+ if book.stored_filename
46
+ old_filename_key = RobustExcelOle::canonize(book.stored_filename)
47
+ @filename2books[old_filename_key].delete(book)
48
+ end
49
+ @filename2books[filename_key] |= [book]
50
+ book.stored_filename = book.filename
51
+ end
52
+
53
+ # prints the book store
54
+ def print
55
+ p "@filename2books:"
56
+ if @filename2books
57
+ @filename2books.each do |filename,books|
58
+ p " filename: #{filename}"
59
+ p " books:"
60
+ p " []" if books == []
61
+ books.each do |book|
62
+ p "#{book}"
63
+ p "excel: #{book.excel}"
64
+ p "alive: #{book.alive?}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ class BookStoreError < WIN32OLERuntimeError
73
+ end
74
+
75
+ end
@@ -4,11 +4,11 @@ module RobustExcelOle
4
4
 
5
5
  class Excel
6
6
 
7
- attr_writer :ole_app
7
+ attr_writer :excel
8
8
 
9
- @@hwnd2app = {}
9
+ @@hwnd2excel = {}
10
10
 
11
- # closes all Excel applications
11
+ # closes all Excel instances
12
12
  def self.close_all
13
13
  while current_excel do
14
14
  close_one_excel
@@ -18,54 +18,48 @@ module RobustExcelOle
18
18
  end
19
19
  end
20
20
 
21
- # creates a new Excel application
21
+ # creates a new Excel instance
22
22
  def self.create
23
23
  new(:reuse => false)
24
24
  end
25
25
 
26
- # uses the current Excel application (connects), if such a running Excel application exists
26
+ # uses the current Excel instance (connects), if such a running Excel instance exists
27
27
  # creates a new one, otherwise
28
28
  def self.current
29
29
  new(:reuse => true)
30
30
  end
31
31
 
32
- # returns an Excel application
32
+ # returns an Excel instance
33
33
  # options:
34
- # :reuse use an already running Excel application (default: true)
35
- # :displayalerts allow display alerts in Excel (default: false)
36
- # :visible make visible in Excel (default: false)
34
+ # :reuse use an already running Excel instance (default: true)
35
+ # :displayalerts allow display alerts in Excel (default: false)
36
+ # :visible make visible in Excel (default: false)
37
+ # if :reuse => true, then DisplayAlerts and Visible are set only if they are given
37
38
  def self.new(options= {})
38
39
  options = {:reuse => true}.merge(options)
39
-
40
- ole_app = nil
41
40
  if options[:reuse] then
42
- ole_app = options[:excel] ? options[:excel] : current_excel
43
- if ole_app
44
- ole_app.DisplayAlerts = options[:displayalerts] unless options[:displayalerts]==nil
45
- ole_app.Visible = options[:visible] unless options[:visible]==nil
46
- end
41
+ excel = current_excel
47
42
  end
48
-
49
- options = {
50
- :displayalerts => false,
51
- :visible => false,
52
- }.merge(options)
53
- unless ole_app
54
- ole_app = WIN32OLE.new('Excel.application')
55
- ole_app.DisplayAlerts = options[:displayalerts]
56
- ole_app.Visible = options[:visible]
43
+ if not (excel)
44
+ excel = WIN32OLE.new('Excel.Application')
45
+ options = {
46
+ :displayalerts => false,
47
+ :visible => false,
48
+ }.merge(options)
57
49
  end
50
+ excel.DisplayAlerts = options[:displayalerts] unless options[:displayalerts].nil?
51
+ excel.Visible = options[:visible] unless options[:visible].nil?
58
52
 
59
- hwnd = ole_app.HWnd
60
- stored = @@hwnd2app[hwnd]
53
+ hwnd = excel.HWnd
54
+ stored = hwnd2excel(hwnd)
61
55
 
62
56
  if stored
63
57
  result = stored
64
58
  else
65
- WIN32OLE.const_load(ole_app, RobustExcelOle) unless RobustExcelOle.const_defined?(:CONSTANTS)
59
+ WIN32OLE.const_load(excel, RobustExcelOle) unless RobustExcelOle.const_defined?(:CONSTANTS)
66
60
  result = super(options)
67
- result.ole_app = ole_app
68
- @@hwnd2app[hwnd] = result
61
+ result.excel = excel
62
+ @@hwnd2excel[hwnd] = result
69
63
  end
70
64
  result
71
65
  end
@@ -73,24 +67,73 @@ module RobustExcelOle
73
67
  def initialize(options= {}) # :nodoc:
74
68
  end
75
69
 
76
- # returns true, if the Excel applications are identical, false otherwise
70
+ # generate, save and close an empty workbook
71
+ def self.generate_workbook file_name
72
+ excel = create
73
+ excel.Workbooks.Add
74
+ empty_workbook = excel.Workbooks.Item(1)
75
+ empty_workbook.SaveAs(file_name, XlExcel8)
76
+ empty_workbook.Close
77
+ end
78
+
79
+ def self.hwnd2excel(hwnd)
80
+ @@hwnd2excel[hwnd]
81
+ end
82
+
83
+ def hwnd
84
+ self.Hwnd
85
+ end
86
+
87
+ # returns true, if the Excel instances are identical, false otherwise
77
88
  def == other_excel
78
- self.hwnd == other_excel.hwnd if other_excel.is_a?(Excel)
89
+ self.Hwnd == other_excel.Hwnd if other_excel.is_a?(Excel)
79
90
  end
80
91
 
81
- # returns true, if the Excel application is alive, false otherwise
92
+ # returns true, if the Excel instances is alive, false otherwise
82
93
  def alive?
83
- @ole_app.Name
94
+ @excel.Name
84
95
  true
85
96
  rescue
86
97
  puts $!.message
87
98
  false
88
99
  end
89
100
 
101
+ # set DisplayAlerts in a block
102
+ def with_displayalerts displayalerts_value
103
+ old_displayalerts = @excel.DisplayAlerts
104
+ @excel.DisplayAlerts = displayalerts_value
105
+ begin
106
+ yield self
107
+ ensure
108
+ @excel.DisplayAlerts = old_displayalerts
109
+ end
110
+ end
111
+
112
+ # enable DisplayAlerts in the current Excel instance
113
+ def displayalerts= displayalerts_value
114
+ @excel.DisplayAlerts = displayalerts_value
115
+ end
116
+
117
+ # return if in the current Excel instance DisplayAlerts is enabled
118
+ def displayalerts
119
+ @excel.DisplayAlerts
120
+ end
121
+
122
+ # make the current Excel instance visible or invisible
123
+ def visible= visible_value
124
+ @excel.Visible = visible_value
125
+ end
126
+
127
+ # return if the current Excel instance is visible
128
+ def visible
129
+ @excel.Visible
130
+ end
131
+
132
+
90
133
  private
91
134
 
92
- # closes one Excel application
93
- def self.close_one_excel # :nodoc: #
135
+ # closes one Excel instance
136
+ def self.close_one_excel
94
137
  excel = current_excel
95
138
  if excel then
96
139
  excel.Workbooks.Close
@@ -112,8 +155,8 @@ module RobustExcelOle
112
155
  end
113
156
  end
114
157
 
115
- @@hwnd2app[excel_hwnd].die rescue nil
116
- #@@hwnd2app[excel_hwnd] = nil
158
+ hwnd2excel(excel_hwnd).die rescue nil
159
+ #@@hwnd2excel[excel_hwnd] = nil
117
160
  end
118
161
 
119
162
 
@@ -128,7 +171,7 @@ module RobustExcelOle
128
171
  end
129
172
 
130
173
  # frees all OLE objects in the object space
131
- def self.free_all_ole_objects # :nodoc: #
174
+ def self.free_all_ole_objects
132
175
  anz_objekte = 0
133
176
  ObjectSpace.each_object(WIN32OLE) do |o|
134
177
  anz_objekte += 1
@@ -149,7 +192,7 @@ module RobustExcelOle
149
192
  puts "went through #{anz_objekte} OLE objects"
150
193
  end
151
194
 
152
- # returns the current Excel application, if a running, working Excel appication exists, nil otherwise
195
+ # returns the current Excel instance
153
196
  def self.current_excel # :nodoc: #
154
197
  result = WIN32OLE.connect('Excel.Application') rescue nil
155
198
  if result
@@ -163,26 +206,35 @@ module RobustExcelOle
163
206
  result
164
207
  end
165
208
 
166
- def hwnd_xxxx # :nodoc: #
209
+ def hwnd_xxxx
167
210
  self.HWnd #rescue Win32 nil
168
211
  end
169
212
 
170
- # set this Excel application to nil
171
- def die # :nodoc:
172
- @ole_app = nil
213
+ # set this Excel instance to nil
214
+ def die
215
+ @excel = nil
173
216
  end
174
217
 
175
- def method_missing(name, *args) # :nodoc: #
176
- @ole_app.send(name, *args)
218
+
219
+ def method_missing(name, *args)
220
+ if name.to_s[0,1] =~ /[A-Z]/
221
+ begin
222
+ @excel.send(name, *args)
223
+ rescue WIN32OLERuntimeError => msg
224
+ if msg.message =~ /unknown property or method/
225
+ raise VBAMethodMissingError, "unknown VBA property or method #{name}"
226
+ else
227
+ raise msg
228
+ end
229
+ end
230
+ else
231
+ super
232
+ end
177
233
  end
178
234
 
179
- end
235
+ end
236
+
237
+
180
238
 
181
- def absolute_path(file)
182
- file = File.expand_path(file)
183
- file = RobustExcelOle::Cygwin.cygpath('-w', file) if RUBY_PLATFORM =~ /cygwin/
184
- WIN32OLE.new('Scripting.FileSystemObject').GetAbsolutePathName(file)
185
- end
186
- module_function :absolute_path
187
239
 
188
240
  end
@@ -1,8 +1,8 @@
1
- {
2
- "folders":
3
- [
4
- {
5
- "path": "/C/Dokumente und Einstellungen/Zauberthomas/Eigene Dateien/robust_excel_ole"
6
- }
7
- ]
8
- }
1
+ {
2
+ "folders":
3
+ [
4
+ {
5
+ "path": "/C/Dokumente und Einstellungen/Zauberthomas/Eigene Dateien/robust_excel_ole"
6
+ }
7
+ ]
8
+ }
@@ -81,6 +81,25 @@ module RobustExcelOle
81
81
  RobustExcelOle::Range.new(@sheet.Range(@sheet.Cells(range.min + 1, col + 1), @sheet.Cells(range.max + 1, col + 1)))
82
82
  end
83
83
 
84
+ # returns the contents of a range or cell with given name
85
+ def nvalue(name)
86
+ begin
87
+ item = self.Names.Item(name)
88
+ rescue WIN32OLERuntimeError
89
+ raise SheetErrorNValue, "name #{name} not in sheet"
90
+ end
91
+ begin
92
+ referstorange = item.RefersToRange
93
+ rescue WIN32OLERuntimeError
94
+ raise SheetErrorNValue, "range error in sheet"
95
+ end
96
+ begin
97
+ value = referstorange.Value
98
+ rescue WIN32OLERuntimeError
99
+ raise SheetErrorNValue, "value error in sheet"
100
+ end
101
+ end
102
+
84
103
  def method_missing(id, *args) # :nodoc: #
85
104
  @sheet.send(id, *args)
86
105
  end
@@ -98,6 +117,15 @@ module RobustExcelOle
98
117
  used_last_column = @sheet.UsedRange.Columns.Count
99
118
 
100
119
  special_last_column >= used_last_column ? special_last_column : used_last_column
101
- end
120
+ end
102
121
  end
122
+
123
+ public
124
+
125
+ class SheetError < RuntimeError # :nodoc: #
126
+ end
127
+
128
+ class SheetErrorNValue < WIN32OLERuntimeError # :nodoc: #
129
+ end
130
+
103
131
  end
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -6,10 +6,10 @@ Gem::Specification.new do |s|
6
6
  s.name = "robust_excel_ole"
7
7
  s.version = RobustExcelOle::VERSION
8
8
  s.authors = ["traths"]
9
- s.email = ["traths@cs.uni-potsdam.de"]
9
+ s.email = ["Thomas.Raths@gmx.net"]
10
10
  s.homepage = "https://github.com/Thomas008/robust_excel_ole"
11
- s.summary = "RobustExcelOle is a wrapper library that specializes in the operation of Excel win32ole."
12
- s.description = "RobustExcelOle is to wrap the win32ole, and easy to use Excel operations with ruby. Detailed description please see the README."
11
+ s.summary = "RobustExcelOle wraps the win32ole library and implements various operations in Excel"
12
+ s.description = "RobustExcelOle wraps the win32ole library, and allows to perform various operation in Excel with ruby in a reliable way. Detailed description please see the README."
13
13
 
14
14
  s.rubyforge_project = "robust_excel_ole"
15
15
 
data/spec/book_spec.rb CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  # -*- coding: utf-8 -*-
3
2
 
4
3
  require File.join(File.dirname(__FILE__), './spec_helper')
@@ -6,13 +5,15 @@ require File.join(File.dirname(__FILE__), './spec_helper')
6
5
 
7
6
  $VERBOSE = nil
8
7
 
9
- describe RobustExcelOle::Book do
8
+ include RobustExcelOle
9
+
10
+ describe Book do
10
11
 
11
12
  before(:all) do
12
- excel = RobustExcelOle::Excel.new(:reuse => true)
13
+ excel = Excel.new(:reuse => true)
13
14
  open_books = excel == nil ? 0 : excel.Workbooks.Count
14
15
  puts "*** open books *** : #{open_books}" if open_books > 0
15
- RobustExcelOle::Excel.close_all
16
+ Excel.close_all
16
17
  end
17
18
 
18
19
 
@@ -20,40 +21,90 @@ describe RobustExcelOle::Book do
20
21
  @dir = create_tmpdir
21
22
  @simple_file = @dir + '/simple.xls'
22
23
  @simple_save_file = @dir + '/simple_save.xls'
24
+ @different_file = @dir + '/different_simple.xls'
25
+ @simple_file_other_path = @dir + '/more_data/simple.xls'
26
+ @more_simple_file = @dir + '/more_simple.xls'
23
27
  end
24
28
 
25
29
  after do
26
- #RobustExcelOle::Excel.close_all
30
+ Excel.close_all
27
31
  rm_tmp(@dir)
28
32
  end
29
33
 
30
34
 
31
- context "class methods" do
32
- context "create file" do
35
+ describe "create file" do
36
+ context "with standard" do
33
37
  it "simple file with default" do
34
38
  expect {
35
- @book = RobustExcelOle::Book.new(@simple_file)
36
- }.to_not raise_error
37
- @book.should be_a RobustExcelOle::Book
39
+ @book = Book.new(@simple_file)
40
+ }.to_not raise_error
41
+ @book.should be_a Book
38
42
  @book.close
39
43
  end
40
44
  end
41
45
  end
46
+
42
47
 
43
48
  describe "open" do
44
49
 
45
- context "with non-existing file" do
46
- it "should raise an exception" do
47
- File.delete @simple_save_file rescue nil
48
- expect {
49
- RobustExcelOle::Book.open(@simple_save_file)
50
- }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
50
+ context "standard use cases" do
51
+
52
+ it "should read in a seperate excel instance" do
53
+ first_excel = Excel.new
54
+ book = Book.open(@simple_file, :read_only => true, :force_excel => :new)
55
+ book.should be_a Book
56
+ book.should be_alive
57
+ book.ReadOnly.should be_true
58
+ book.Saved.should be_true
59
+ book.excel.should_not == first_excel
60
+ sheet = book[0]
61
+ sheet[0,0].value.should == "simple"
62
+ book.close
63
+ end
64
+
65
+ it "should read not bothering about excel instances" do
66
+ first_excel = Excel.new
67
+ book = Book.open(@simple_file, :read_only => true)
68
+ book.should be_a Book
69
+ book.should be_alive
70
+ book.ReadOnly.should be_true
71
+ book.Saved.should be_true
72
+ book.excel.should == first_excel
73
+ sheet = book[0]
74
+ sheet[0,0].value.should == "simple"
75
+ book.close
76
+ end
77
+
78
+ it "should open writable" do
79
+ book = Book.open(@simple_file, :if_locked => :take_writable,
80
+ :if_unsaved => :forget, :if_obstructed => :save)
81
+ book.close
82
+ end
83
+
84
+ it "should open unobtrusively" do
85
+ book = Book.open(@simple_file, :if_locked => :take_writable,
86
+ :if_unsaved => :accept, :if_obstructed => :reuse_excel)
87
+ book.close
88
+ end
89
+
90
+ it "should open in a given instance" do
91
+ book1 = Book.open(@simple_file)
92
+ book2 = Book.open(@simple_file, :force_excel => book1.excel, :if_locked => :force_writable)
93
+ book2.close
94
+ book1.close
95
+ end
96
+
97
+ it "should open writable" do
98
+ book = Book.open(@simple_file, :if_locked => :take_writable,
99
+ :if_unsaved => :save, :if_obstructed => :save)
100
+ book.close
51
101
  end
52
102
  end
53
103
 
104
+
54
105
  context "with standard options" do
55
106
  before do
56
- @book = RobustExcelOle::Book.open(@simple_file)
107
+ @book = Book.open(@simple_file)
57
108
  end
58
109
 
59
110
  after do
@@ -65,48 +116,549 @@ describe RobustExcelOle::Book do
65
116
  end
66
117
  end
67
118
 
68
- context "with attr_reader excel" do
119
+ context "with transperency identity" do
120
+
69
121
  before do
70
- @new_book = RobustExcelOle::Book.open(@simple_file)
122
+ @book = Book.open(@simple_file)
71
123
  end
124
+
72
125
  after do
73
- @new_book.close
126
+ @book.close
74
127
  end
75
- it "should provide the excel application of the book" do
76
- excel = @new_book.excel
77
- excel.class.should == RobustExcelOle::Excel
78
- excel.should be_a RobustExcelOle::Excel
79
- end
80
- end
81
-
82
- context "with :excel" do
83
- it "should reuse the given excel application of the book" do
84
- RobustExcelOle::Excel.close_all
85
- book1 = RobustExcelOle::Book.open(@simple_file)
86
- excel1 = book1.excel
87
- book2 = RobustExcelOle::Book.open(@simple_file, :reuse => false)
88
- excel2 = book2.excel
89
- excel2.should_not == excel1
90
- book3 = RobustExcelOle::Book.open(@simple_file)
91
- excel3 = book3.excel
92
- book4 = RobustExcelOle::Book.open(@simple_file, :excel => excel2)
93
- excel4 = book4.excel
94
- excel3.should == excel1
95
- excel4.should == excel2
96
- excel4.class.should == RobustExcelOle::Excel
97
- excel4.should be_a RobustExcelOle::Excel
128
+
129
+ it "should yield identical Book objects for identical Excel books" do
130
+ book2 = Book.open(@simple_file)
131
+ book2.should == @book
132
+ book2.close
133
+ end
134
+
135
+ it "should yield different Book objects for different Excel books" do
136
+ book2 = Book.open(@different_file)
137
+ book2.should_not == @book
138
+ book2.close
139
+ end
140
+
141
+ it "should yield different Book objects when opened the same file in different Excel instances" do
142
+ book2 = Book.open(@simple_file, :force_excel => :new)
143
+ book2.should_not == @book
144
+ book2.close
145
+ end
146
+
147
+ it "should yield identical Book objects for identical Excel books when reopening" do
148
+ @book.should be_alive
149
+ @book.close
150
+ book2 = Book.open(@simple_file)
151
+ book2.should == @book
152
+ book2.should be_alive
153
+ book2.close
154
+ end
155
+ end
156
+
157
+ context "with :force_excel" do
158
+
159
+ before do
160
+ @book = Book.open(@simple_file)
161
+ end
162
+
163
+ after do
164
+ @book.close
165
+ end
166
+
167
+ it "should open in a new Excel" do
168
+ book2 = Book.open(@simple_file, :force_excel => :new)
169
+ book2.should be_alive
170
+ book2.should be_a Book
171
+ book2.excel.should_not == @book.excel
172
+ book2.should_not == @book
173
+ @book.Readonly.should be_false
174
+ book2.Readonly.should be_true
175
+ end
176
+
177
+ it "should open in a given Excel" do
178
+ book2 = Book.open(@simple_file, :force_excel => :new)
179
+ book2.excel.should_not == @book.excel
180
+ book3 = Book.open(@simple_file, :force_excel => :new)
181
+ book3.excel.should_not == book2.excel
182
+ book3.excel.should_not == @book.excel
183
+ book2.close
184
+ book4 = Book.open(@simple_file, :force_excel => book2.excel)
185
+ book4.should be_alive
186
+ book4.should be_a Book
187
+ book4.excel.should == book2.excel
98
188
  book4.close
99
189
  book3.close
190
+ end
191
+
192
+ it "should do force_excel even if both force_ and default_excel is given" do
193
+ book2 = Book.open(@simple_file, :default_excel => @book.excel, :force_excel => :new)
194
+ book2.should be_alive
195
+ book2.should be_a Book
196
+ book2.excel.should_not == @book.excel
197
+ book2.should_not == @book
198
+ end
199
+ end
200
+
201
+ context "with :default_excel" do
202
+
203
+ before do
204
+ @book = Book.open(@simple_file)
205
+ end
206
+
207
+ after do
208
+ @book.close rescue nil
209
+ end
210
+
211
+ it "should reuse an open book" do
212
+ book2 = Book.open(@simple_file, :default_excel => :reuse)
213
+ book2.excel.should == @book.excel
214
+ book2.should be_alive
215
+ book2.should be_a Book
216
+ book2.should == @book
100
217
  book2.close
101
- book1.close
218
+ end
219
+
220
+ it "should reopen a book" do
221
+ @book.close
222
+ book2 = Book.open(@simple_file, :default_excel => :reuse)
223
+ book2.should be_alive
224
+ book2.should be_a Book
225
+ book2.excel.should == @book.excel
226
+ book2.filename.should == @book.filename
227
+ @book.should be_alive
228
+ book2.should == @book
229
+ book2.close
230
+ end
231
+
232
+ it "should connect to the first opened excel, if the book cannot be reopened" do
233
+ @book.close
234
+ Excel.close_all
235
+ excel1 = Excel.new(:reuse => false)
236
+ excel2 = Excel.new(:reuse => false)
237
+ book2 = Book.open(@different_file, :default_excel => :reuse)
238
+ book2.should be_alive
239
+ book2.should be_a Book
240
+ book2.excel.should == excel1
241
+ book2.excel.should_not == excel2
242
+ book2.close
243
+ end
244
+
245
+ it "should open a new excel, if the book cannot be reopened" do
246
+ @book.close
247
+ new_excel = Excel.new(:reuse => false)
248
+ book2 = Book.open(@different_file, :default_excel => :new)
249
+ book2.should be_alive
250
+ book2.should be_a Book
251
+ book2.excel.should_not == new_excel
252
+ book2.excel.should_not == @book.excel
253
+ book2.close
254
+ end
255
+
256
+ it "should open a given excel, if the book cannot be reopened" do
257
+ @book.close
258
+ new_excel = Excel.new(:reuse => false)
259
+ book2 = Book.open(@different_file, :default_excel => @book.excel)
260
+ book2.should be_alive
261
+ book2.should be_a Book
262
+ book2.excel.should_not == new_excel
263
+ book2.excel.should == @book.excel
264
+ book2.close
265
+ end
266
+
267
+ it "should reuse an open book by default" do
268
+ book2 = Book.open(@simple_file)
269
+ book2.excel.should == @book.excel
270
+ book2.should == @book
271
+ end
272
+
273
+ end
274
+
275
+ context "with :if_locked" do
276
+
277
+ context "with an writable book first" do
278
+
279
+ before do
280
+ @book = Book.open(@simple_file)
281
+ end
282
+
283
+ after do
284
+ @book.close
285
+ end
286
+
287
+ it "should use the already open Excel and book" do
288
+ @book.ReadOnly.should be_false
289
+ new_book1 = Book.open(@simple_file, :if_locked => :readonly)
290
+ new_book1.ReadOnly.should be_true
291
+ new_book1.excel.should_not == @book.excel
292
+ new_book1.should_not == @book
293
+ new_book1.close
294
+ new_book2 = Book.open(@simple_file, :if_locked => :take_writable)
295
+ new_book2.ReadOnly.should be_false
296
+ new_book2.excel == @book.excel
297
+ new_book2.should == @book
298
+ new_book2.close
299
+ new_book3 = Book.open(@simple_file)
300
+ new_book3.ReadOnly.should be_false
301
+ new_book3.excel == @book.excel
302
+ new_book3.should == @book
303
+ new_book3.close
304
+ end
305
+
306
+ it "should open in a new Excel as read_only" do
307
+ new_book = Book.open(@simple_file, :force_excel => :new, :if_locked => :readonly)
308
+ @book.ReadOnly.should be_false
309
+ new_book.ReadOnly.should be_true
310
+ new_book.excel.should_not == @book.excel
311
+ new_book.close
312
+ new_excel = Excel.new(:reuse => false)
313
+ new_book2 = Book.open(@simple_file, :force_excel => new_excel, :if_locked => :readonly)
314
+ new_book2.ReadOnly.should be_true
315
+ new_book2.excel.should_not == @book.excel
316
+ new_book2.close
317
+ end
318
+
319
+ it "should open in the Excel where the book is writable" do
320
+ new_book = Book.open(@simple_file, :force_excel => :new, :if_locked => :take_writable)
321
+ @book.ReadOnly.should be_false
322
+ new_book.ReadOnly.should be_false
323
+ new_book.excel.should == @book.excel
324
+ new_book.should == @book
325
+ new_book.close
326
+ new_excel = Excel.new(:reuse => false)
327
+ new_book2 = Book.open(@simple_file, :force_excel => new_excel, :if_locked => :take_writable)
328
+ new_book2.ReadOnly.should be_false
329
+ new_book2.excel.should == @book.excel
330
+ new_book2.close
331
+ end
332
+
333
+ it "should open in a new Excel as writable" do
334
+ new_book = Book.open(@simple_file, :force_excel => :new, :if_locked => :force_writability)
335
+ @book.ReadOnly.should be_false
336
+ new_book.ReadOnly.should be_false
337
+ new_book.excel.should_not == @book.excel
338
+ new_book.close
339
+ new_excel = Excel.new(:reuse => false)
340
+ new_book2 = Book.open(@simple_file, :force_excel => new_excel, :if_locked => :force_writability)
341
+ new_book2.ReadOnly.should be_false
342
+ new_book2.excel.should_not == @book.excel
343
+ new_book2.close
344
+ end
345
+
346
+ it "should open in a new Excel as read_only by default" do
347
+ new_book = Book.open(@simple_file, :force_excel => :new)
348
+ @book.ReadOnly.should be_false
349
+ new_book.ReadOnly.should be_true
350
+ new_book.excel.should_not == @book.excel
351
+ new_book.close
352
+ new_excel = Excel.new(:reuse => false)
353
+ new_book2 = Book.open(@simple_file, :force_excel => new_excel)
354
+ new_book2.ReadOnly.should be_true
355
+ new_book2.excel.should_not == @book.excel
356
+ new_book2.close
357
+ end
358
+ end
359
+
360
+ context "with read_only book first" do
361
+
362
+ before do
363
+ @book = Book.open(@simple_file, :read_only => true)
364
+ end
365
+
366
+ after do
367
+ @book.close
368
+ end
369
+
370
+ it "should use the already open Excel and book" do
371
+ @book.ReadOnly.should be_true
372
+ new_book1 = Book.open(@simple_file, :if_locked => :readonly)
373
+ new_book1.ReadOnly.should be_true
374
+ new_book1.excel.should == @book.excel
375
+ new_book1.should == @book
376
+ new_book1.close
377
+ new_book2 = Book.open(@simple_file, :if_locked => :take_writable)
378
+ new_book2.ReadOnly.should be_true
379
+ new_book2.excel.should == @book.excel
380
+ new_book2.should == @book
381
+ new_book2.close
382
+ new_book3 = Book.open(@simple_file)
383
+ new_book3.ReadOnly.should be_true
384
+ new_book3.excel.should == @book.excel
385
+ new_book3.should == @book
386
+ new_book3.close
387
+ end
388
+
389
+ it "should open in the already open Excel" do
390
+ new_book = Book.open(@simple_file, :if_locked => :force_writability)
391
+ @book.ReadOnly.should be_true
392
+ new_book.ReadOnly.should be_false
393
+ new_book.excel.should_not == @book.excel
394
+ new_book.should_not == @book
395
+ new_book.close
396
+ end
397
+
398
+ it "should open in a new Excel as writable" do
399
+ @book.ReadOnly.should be_true
400
+ new_book1 = Book.open(@simple_file, :force_excel => :new, :if_locked => :readonly)
401
+ new_book1.ReadOnly.should be_false
402
+ new_book1.excel.should_not == @book.excel
403
+ new_book1.close
404
+ new_book2 = Book.open(@simple_file, :force_excel => :new, :if_locked => :take_writable)
405
+ new_book2.ReadOnly.should be_false
406
+ new_book2.excel.should_not == @book.excel
407
+ new_book2.close
408
+ new_book3 = Book.open(@simple_file, :force_excel => :new, :if_locked => :force_writability)
409
+ new_book3.ReadOnly.should be_false
410
+ new_book3.excel.should_not == @book.excel
411
+ new_book3.close
412
+ new_book4 = Book.open(@simple_file, :force_excel => :new)
413
+ new_book4.ReadOnly.should be_false
414
+ new_book4.excel.should_not == @book.excel
415
+ new_book4.close
416
+ end
102
417
  end
103
418
  end
104
419
 
420
+ context "with :if_unsaved" do
421
+
422
+ before do
423
+ @book = Book.open(@simple_file)
424
+ @sheet = @book[0]
425
+ @book.add_sheet(@sheet, :as => 'a_name')
426
+ end
427
+
428
+ after do
429
+ @book.close(:if_unsaved => :forget)
430
+ @new_book.close rescue nil
431
+ end
432
+
433
+ it "should raise an error, if :if_unsaved is :raise" do
434
+ expect {
435
+ @new_book = Book.open(@simple_file, :if_unsaved => :raise)
436
+ }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
437
+ end
438
+
439
+ it "should let the book open, if :if_unsaved is :accept" do
440
+ expect {
441
+ @new_book = Book.open(@simple_file, :if_unsaved => :accept)
442
+ }.to_not raise_error
443
+ @book.should be_alive
444
+ @new_book.should be_alive
445
+ @new_book.should == @book
446
+ end
447
+
448
+ it "should open book and close old book, if :if_unsaved is :forget" do
449
+ @new_book = Book.open(@simple_file, :if_unsaved => :forget)
450
+ @book.should_not be_alive
451
+ @new_book.should be_alive
452
+ @new_book.filename.downcase.should == @simple_file.downcase
453
+ end
454
+
455
+ context "with :if_unsaved => :alert" do
456
+ before do
457
+ @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
458
+ end
459
+
460
+ after do
461
+ @key_sender.close
462
+ end
463
+
464
+ it "should open the new book and close the unsaved book, if user answers 'yes'" do
465
+ # "Yes" is the default. --> language independent
466
+ @key_sender.puts "{enter}"
467
+ @new_book = Book.open(@simple_file, :if_unsaved => :alert)
468
+ @new_book.should be_alive
469
+ @new_book.filename.downcase.should == @simple_file.downcase
470
+ @book.should_not be_alive
471
+ end
472
+
473
+ it "should not open the new book and not close the unsaved book, if user answers 'no'" do
474
+ # "No" is right to "Yes" (the default). --> language independent
475
+ # strangely, in the "no" case, the question will sometimes be repeated three times
476
+ #@book.excel.Visible = true
477
+ @key_sender.puts "{right}{enter}"
478
+ @key_sender.puts "{right}{enter}"
479
+ @key_sender.puts "{right}{enter}"
480
+ expect{
481
+ Book.open(@simple_file, :if_unsaved => :alert)
482
+ }.to raise_error(ExcelUserCanceled, "open: canceled by user")
483
+ @book.should be_alive
484
+ end
485
+ end
486
+
487
+ it "should open the book in a new excel instance, if :if_unsaved is :new_excel" do
488
+ @new_book = Book.open(@simple_file, :if_unsaved => :new_excel)
489
+ @book.should be_alive
490
+ @new_book.should be_alive
491
+ @new_book.filename.should == @book.filename
492
+ @new_book.excel.should_not == @book.excel
493
+ @new_book.close
494
+ end
495
+
496
+ it "should raise an error, if :if_unsaved is default" do
497
+ expect {
498
+ @new_book = Book.open(@simple_file)
499
+ }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
500
+ end
501
+
502
+ it "should raise an error, if :if_unsaved is invalid option" do
503
+ expect {
504
+ @new_book = Book.open(@simple_file, :if_unsaved => :invalid_option)
505
+ }.to raise_error(ExcelErrorOpen, ":if_unsaved: invalid option")
506
+ end
507
+ end
508
+
509
+ context "with :if_obstructed" do
510
+
511
+ for i in 1..2 do
512
+
513
+ context "with and without reopen" do
514
+
515
+ before do
516
+ if i == 1 then
517
+ book_before = Book.open(@simple_file)
518
+ book_before.close
519
+ end
520
+ @book = Book.open(@simple_file_other_path)
521
+ @sheet_count = @book.workbook.Worksheets.Count
522
+ @sheet = @book[0]
523
+ @book.add_sheet(@sheet, :as => 'a_name')
524
+ end
525
+
526
+ after do
527
+ @book.close(:if_unsaved => :forget)
528
+ @new_book.close rescue nil
529
+ end
530
+
531
+ it "should raise an error, if :if_obstructed is :raise" do
532
+ expect {
533
+ @new_book = Book.open(@simple_file, :if_obstructed => :raise)
534
+ }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
535
+ end
536
+
537
+ it "should close the other book and open the new book, if :if_obstructed is :forget" do
538
+ @new_book = Book.open(@simple_file, :if_obstructed => :forget)
539
+ @book.should_not be_alive
540
+ @new_book.should be_alive
541
+ @new_book.filename.downcase.should == @simple_file.downcase
542
+ end
543
+
544
+ it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
545
+ @new_book = Book.open(@simple_file, :if_obstructed => :save)
546
+ @book.should_not be_alive
547
+ @new_book.should be_alive
548
+ @new_book.filename.downcase.should == @simple_file.downcase
549
+ old_book = Book.open(@simple_file_other_path, :if_obstructed => :forget)
550
+ old_book.workbook.Worksheets.Count.should == @sheet_count + 1
551
+ old_book.close
552
+ end
553
+
554
+ it "should raise an error, if the old book is unsaved, and close the old book and open the new book,
555
+ if :if_obstructed is :close_if_saved" do
556
+ expect{
557
+ @new_book = Book.open(@simple_file, :if_obstructed => :close_if_saved)
558
+ }.to raise_error(ExcelErrorOpen, "book with the same name in a different path is unsaved")
559
+ @book.save
560
+ @new_book = Book.open(@simple_file, :if_obstructed => :close_if_saved)
561
+ @book.should_not be_alive
562
+ @new_book.should be_alive
563
+ @new_book.filename.downcase.should == @simple_file.downcase
564
+ old_book = Book.open(@simple_file_other_path, :if_obstructed => :forget)
565
+ old_book.workbook.Worksheets.Count.should == @sheet_count + 1
566
+ old_book.close
567
+ end
568
+
569
+ it "should open the book in a new excel instance, if :if_obstructed is :new_excel" do
570
+ @new_book = Book.open(@simple_file, :if_obstructed => :new_excel)
571
+ @book.should be_alive
572
+ @new_book.should be_alive
573
+ @new_book.filename.should_not == @book.filename
574
+ @new_book.excel.should_not == @book.excel
575
+ end
576
+
577
+ it "should raise an error, if :if_obstructed is default" do
578
+ expect {
579
+ @new_book = Book.open(@simple_file)
580
+ }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
581
+ end
582
+
583
+ it "should raise an error, if :if_obstructed is invalid option" do
584
+ expect {
585
+ @new_book = Book.open(@simple_file, :if_obstructed => :invalid_option)
586
+ }.to raise_error(ExcelErrorOpen, ":if_obstructed: invalid option")
587
+ end
588
+ end
589
+ end
590
+ end
591
+
592
+ context "with an already saved book" do
593
+ before do
594
+ @book = Book.open(@simple_file)
595
+ end
596
+
597
+ after do
598
+ @book.close
599
+ end
600
+
601
+ possible_options = [:read_only, :raise, :accept, :forget, nil]
602
+ possible_options.each do |options_value|
603
+ context "with :if_unsaved => #{options_value} and in the same and different path" do
604
+ before do
605
+ @new_book = Book.open(@simple_file, :reuse=> true, :if_unsaved => options_value)
606
+ @different_book = Book.new(@different_file, :reuse=> true, :if_unsaved => options_value)
607
+ end
608
+ after do
609
+ @new_book.close
610
+ @different_book.close
611
+ end
612
+ it "should open without problems " do
613
+ @new_book.should be_a Book
614
+ @different_book.should be_a Book
615
+ end
616
+ it "should belong to the same Excel instance" do
617
+ @new_book.excel.should == @book.excel
618
+ @different_book.excel.should == @book.excel
619
+ end
620
+ end
621
+ end
622
+ end
623
+
624
+
625
+ context "with non-existing file" do
626
+ it "should raise an exception" do
627
+ File.delete @simple_save_file rescue nil
628
+ expect {
629
+ Book.open(@simple_save_file)
630
+ }.to raise_error(ExcelErrorOpen, "file #{@simple_save_file} not found")
631
+ end
632
+ end
633
+
634
+ context "with attr_reader excel" do
635
+ before do
636
+ @new_book = Book.open(@simple_file)
637
+ end
638
+ after do
639
+ @new_book.close
640
+ end
641
+ it "should provide the excel instance of the book" do
642
+ excel = @new_book.excel
643
+ excel.class.should == Excel
644
+ excel.should be_a Excel
645
+ end
646
+ end
105
647
 
106
648
  context "with :read_only" do
649
+
650
+ it "should open the second book in another Excel as writable" do
651
+ book = Book.open(@simple_file, :read_only => true)
652
+ book.ReadOnly.should be_true
653
+ new_book = Book.open(@simple_file, :force_excel => :new, :read_only => false)
654
+ new_book.ReadOnly.should be_false
655
+ new_book.close
656
+ book.close
657
+ end
658
+
107
659
  it "should be able to save, if :read_only => false" do
108
- book = RobustExcelOle::Book.open(@simple_file, :read_only => false)
109
- book.should be_a RobustExcelOle::Book
660
+ book = Book.open(@simple_file, :read_only => false)
661
+ book.should be_a Book
110
662
  expect {
111
663
  book.save_as(@simple_save_file, :if_exists => :overwrite)
112
664
  }.to_not raise_error
@@ -114,8 +666,8 @@ describe RobustExcelOle::Book do
114
666
  end
115
667
 
116
668
  it "should be able to save, if :read_only is set to default value" do
117
- book = RobustExcelOle::Book.open(@simple_file)
118
- book.should be_a RobustExcelOle::Book
669
+ book = Book.open(@simple_file)
670
+ book.should be_a Book
119
671
  expect {
120
672
  book.save_as(@simple_save_file, :if_exists => :overwrite)
121
673
  }.to_not raise_error
@@ -123,8 +675,8 @@ describe RobustExcelOle::Book do
123
675
  end
124
676
 
125
677
  it "should raise an error, if :read_only => true" do
126
- book = RobustExcelOle::Book.open(@simple_file, :read_only => true)
127
- book.should be_a RobustExcelOle::Book
678
+ book = Book.open(@simple_file, :read_only => true)
679
+ book.should be_a Book
128
680
  expect {
129
681
  book.save_as(@simple_save_file, :if_exists => :overwrite)
130
682
  }.to raise_error
@@ -133,9 +685,9 @@ describe RobustExcelOle::Book do
133
685
  end
134
686
 
135
687
  context "with block" do
136
- it 'block parameter should be instance of RobustExcelOle::Book' do
137
- RobustExcelOle::Book.open(@simple_file) do |book|
138
- book.should be_a RobustExcelOle::Book
688
+ it 'block parameter should be instance of Book' do
689
+ Book.open(@simple_file) do |book|
690
+ book.should be_a Book
139
691
  end
140
692
  end
141
693
  end
@@ -145,219 +697,413 @@ describe RobustExcelOle::Book do
145
697
  path = '~/Abrakadabra.xlsx'
146
698
  expected_path = Regexp.new(File.expand_path(path).gsub(/\//, "."))
147
699
  expect {
148
- RobustExcelOle::Book.open(path)
700
+ Book.open(path)
149
701
  }.to raise_error(ExcelErrorOpen, "file #{path} not found")
150
702
  end
151
703
  end
704
+ end
152
705
 
153
- context "with an already opened book" do
706
+ describe "send methods to workbook" do
154
707
 
708
+ context "with standard" do
155
709
  before do
156
- @book = RobustExcelOle::Book.open(@simple_file)
710
+ @book = Book.open(@simple_file)
157
711
  end
158
712
 
159
713
  after do
160
714
  @book.close
161
715
  end
162
716
 
163
- context "with an already saved book" do
164
- possible_options = [:read_only, :raise, :accept, :forget, nil]
165
- possible_options.each do |options_value|
166
- context "with :if_unsaved => #{options_value} and in the same and different path" do
167
- before do
168
- @new_book = RobustExcelOle::Book.open(@simple_file, :reuse=> true, :if_unsaved => options_value)
169
- different_file = @dir + '/different_simple.xls'
170
- @different_book = RobustExcelOle::Book.new(different_file, :reuse=> true, :if_unsaved => options_value)
171
- end
172
- after do
173
- @new_book.close
174
- @different_book.close
175
- end
176
- it "should open without problems " do
177
- @new_book.should be_a RobustExcelOle::Book
178
- @different_book.should be_a RobustExcelOle::Book
179
- end
180
- it "should belong to the same Excel application" do
181
- @new_book.excel.should == @book.excel
182
- @different_book.excel.should == @book.excel
183
- end
184
- end
185
- end
717
+ it "should send Saved to workbook" do
718
+ @book.Saved.should be_true
186
719
  end
187
720
 
188
- context "with an unsaved book" do
721
+ it "should send Fullname to workbook" do
722
+ @book.Fullname.tr('\\','/').should == @simple_file
723
+ end
724
+ end
725
+ end
189
726
 
190
- before do
191
- @book = RobustExcelOle::Book.open(@simple_file)
192
- @sheet = @book[0]
193
- @book.add_sheet(@sheet, :as => 'a_name')
194
- end
727
+ describe "unobtrusively" do
195
728
 
196
- after do
197
- @book.close(:if_unsaved => :forget)
198
- @new_book.close rescue nil
199
- end
729
+ def unobtrusively_ok? # :nodoc: #
730
+ Book.unobtrusively(@simple_file) do |book|
731
+ book.should be_a Book
732
+ sheet = book[0]
733
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
734
+ book.should be_alive
735
+ book.Saved.should be_false
736
+ end
737
+ end
200
738
 
201
- it "should raise an error, if :if_unsaved is :raise" do
202
- expect {
203
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :raise)
204
- }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
205
- end
739
+ context "with an open book" do
206
740
 
207
- it "should let the book open, if :if_unsaved is :accept" do
208
- expect {
209
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :accept)
210
- }.to_not raise_error
211
- @book.should be_alive
212
- @new_book.should be_alive
213
- @new_book.should == @book
214
- end
741
+ before do
742
+ @book = Book.open(@simple_file)
743
+ end
215
744
 
216
- it "should open book and close old book, if :if_unsaved is :forget" do
217
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :forget)
218
- @book.should_not be_alive
219
- @new_book.should be_alive
220
- @new_book.filename.downcase.should == @simple_file.downcase
221
- end
745
+ after do
746
+ @book.close(:if_unsaved => :forget)
747
+ end
222
748
 
223
- context "with :if_unsaved => :alert" do
224
- before do
225
- @key_sender = IO.popen 'ruby "' + File.join(File.dirname(__FILE__), '/helpers/key_sender.rb') + '" "Microsoft Office Excel" ' , "w"
226
- end
749
+ it "should let a saved book saved" do
750
+ @book.Saved.should be_true
751
+ @book.should be_alive
752
+ sheet = @book[0]
753
+ old_cell_value = sheet[0,0].value
754
+ unobtrusively_ok?
755
+ @book.Saved.should be_true
756
+ @book.should be_alive
757
+ sheet = @book[0]
758
+ sheet[0,0].value.should_not == old_cell_value
759
+ end
227
760
 
228
- after do
229
- @key_sender.close
230
- end
761
+ it "should let an unsaved book unsaved" do
762
+ sheet = @book[0]
763
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
764
+ @book.Saved.should be_false
765
+ @book.should be_alive
766
+ old_cell_value = sheet[0,0].value
767
+ unobtrusively_ok?
768
+ @book.Saved.should be_false
769
+ @book.should be_alive
770
+ sheet = @book[0]
771
+ sheet[0,0].value.should_not == old_cell_value
772
+ end
773
+ end
774
+
775
+ context "with a closed book" do
776
+
777
+ before do
778
+ @book = Book.open(@simple_file)
779
+ end
231
780
 
232
- it "should open the new book and close the unsaved book, if user answers 'yes'" do
233
- # "Yes" is the default. --> language independent
234
- @key_sender.puts "{enter}"
235
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :alert)
236
- @book.should_not be_alive
237
- @new_book.should be_alive
238
- @new_book.filename.downcase.should == @simple_file.downcase
239
- end
781
+ after do
782
+ @book.close(:if_unsaved => :forget)
783
+ Excel.close_all
784
+ end
240
785
 
241
- it "should not open the new book and not close the unsaved book, if user answers 'no'" do
242
- # "No" is right to "Yes" (the default). --> language independent
243
- # strangely, in the "no" case, the question will sometimes be repeated three times
244
- #@book.excel.Visible = true
245
- @key_sender.puts "{right}{enter}"
246
- @key_sender.puts "{right}{enter}"
247
- @key_sender.puts "{right}{enter}"
248
- expect{
249
- RobustExcelOle::Book.open(@simple_file, :if_unsaved => :alert)
250
- }.to raise_error(ExcelUserCanceled, "open: canceled by user")
251
- @book.should be_alive
252
- end
253
- end
786
+ it "should let the closed book closed by default" do
787
+ sheet = @book[0]
788
+ old_cell_value = sheet[0,0].value
789
+ @book.close
790
+ @book.should_not be_alive
791
+ unobtrusively_ok?
792
+ @book.should_not be_alive
793
+ @book = Book.open(@simple_file)
794
+ sheet = @book[0]
795
+ sheet[0,0].value.should_not == old_cell_value
796
+ end
254
797
 
255
- it "should open the book in a new excel application, if :if_unsaved is :new_app" do
256
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :new_app)
257
- @book.should be_alive
258
- @new_book.should be_alive
259
- @new_book.filename.should == @book.filename
260
- @new_book.excel.should_not == @book.excel
261
- @new_book.close
798
+ # The bold reanimation of the @book
799
+ it "should keep open the book" do
800
+ sheet = @book[0]
801
+ old_cell_value = sheet[0,0].value
802
+ @book.close
803
+ @book.should_not be_alive
804
+ Book.unobtrusively(@simple_file, :keep_open => true) do |book|
805
+ book.should be_a Book
806
+ sheet = book[0]
807
+ cell = sheet[0,0]
808
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
809
+ book.Saved.should be_false
262
810
  end
811
+ @book.should be_alive
812
+ @book.close
813
+ @book = Book.open(@simple_file)
814
+ sheet = @book[0]
815
+ sheet[0,0].value.should_not == old_cell_value
816
+ end
817
+ end
263
818
 
264
- it "should raise an error, if :if_unsaved is default" do
265
- expect {
266
- @new_book = RobustExcelOle::Book.open(@simple_file)
267
- }.to raise_error(ExcelErrorOpen, "book is already open but not saved (#{File.basename(@simple_file)})")
268
- end
819
+ context "with two books" do
269
820
 
270
- it "should raise an error, if :if_unsaved is invalid option" do
271
- expect {
272
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :invalid_option)
273
- }.to raise_error(ExcelErrorOpen, ":if_unsaved: invalid option")
821
+ before do
822
+ @book = Book.open(@simple_file)
823
+ end
824
+
825
+ after do
826
+ @book.close(:if_unsaved => :forget)
827
+ @book2.close(:if_unsaved => :forget)
828
+ end
829
+
830
+ it "should let the unsaved book unsaved" do
831
+ sheet = @book[0]
832
+ old_cell_value = sheet[0,0].value
833
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
834
+ old_cell_value = sheet[0,0].value
835
+ @book.Saved.should be_false
836
+ unobtrusively_ok?
837
+ @book.should be_alive
838
+ @book.Saved.should be_false
839
+ @book.close(:if_unsaved => :forget)
840
+ @book2 = Book.open(@simple_file)
841
+ sheet2 = @book2[0]
842
+ sheet2[0,0].value.should_not == old_cell_value
843
+ end
844
+
845
+ it "should modify unobtrusively the second, writable book" do
846
+ @book2 = Book.open(@simple_file, :force_excel => :new)
847
+ @book.ReadOnly.should be_false
848
+ @book2.ReadOnly.should be_true
849
+ sheet = @book2[0]
850
+ old_cell_value = sheet[0,0].value
851
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
852
+ unobtrusively_ok?
853
+ @book2.should be_alive
854
+ @book2.Saved.should be_false
855
+ @book2.close(:if_unsaved => :forget)
856
+ @book.close
857
+ @book = Book.open(@simple_file)
858
+ sheet2 = @book[0]
859
+ sheet2[0,0].value.should_not == old_cell_value
860
+ end
861
+ end
862
+
863
+ context "with read_only book" do
864
+
865
+ before do
866
+ @book = Book.open(@simple_file, :read_only => true)
867
+ end
868
+
869
+ after do
870
+ @book.close
871
+ end
872
+
873
+ it "should let the saved book saved" do
874
+ @book.ReadOnly.should be_true
875
+ @book.Saved.should be_true
876
+ sheet = @book[0]
877
+ old_cell_value = sheet[0,0].value
878
+ unobtrusively_ok?
879
+ @book.should be_alive
880
+ @book.Saved.should be_true
881
+ @book.ReadOnly.should be_true
882
+ @book.close
883
+ book2 = Book.open(@simple_file)
884
+ sheet2 = book2[0]
885
+ sheet2[0,0].value.should_not == old_cell_value
886
+ end
887
+
888
+ it "should let the unsaved book unsaved" do
889
+ @book.ReadOnly.should be_true
890
+ sheet = @book[0]
891
+ sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple"
892
+ @book.Saved.should be_false
893
+ @book.should be_alive
894
+ cell_value = sheet[0,0].value
895
+ unobtrusively_ok?
896
+ @book.should be_alive
897
+ @book.Saved.should be_false
898
+ @book.ReadOnly.should be_true
899
+ @book.close
900
+ book2 = Book.open(@simple_file)
901
+ sheet2 = book2[0]
902
+ # modifies unobtrusively the saved version, not the unsaved version
903
+ sheet2[0,0].value.should == cell_value
904
+ end
905
+ end
906
+
907
+ context "with a virgin Book class" do
908
+ before do
909
+ class Book
910
+ @@bookstore = nil
274
911
  end
912
+ end
913
+ it "should work" do
914
+ expect{ unobtrusively_ok? }.to_not raise_error
915
+ end
916
+ end
275
917
 
918
+ context "with a book never opened before" do
919
+ before do
920
+ class Book
921
+ @@bookstore = nil
922
+ end
923
+ other_book = Book.open(@different_file)
924
+ end
925
+ it "should open the book" do
926
+ expect{ unobtrusively_ok? }.to_not raise_error
276
927
  end
277
928
  end
278
929
 
279
- context "with a book in a different path" do
930
+ context "with a saved book" do
280
931
 
281
932
  before do
282
- @simple_file_other_path = @dir + '/more_data/simple.xls'
283
- @book = RobustExcelOle::Book.open(@simple_file_other_path)
284
- @sheet_count = @book.workbook.Worksheets.Count
285
- @sheet = @book[0]
286
- @book.add_sheet(@sheet, :as => 'a_name')
933
+ @book1 = Book.open(@simple_file)
287
934
  end
288
935
 
289
936
  after do
290
- @book.close(:if_unsaved => :forget)
291
- @new_book.close rescue nil
937
+ @book1.close(:if_unsaved => :forget)
938
+ end
939
+
940
+ it "should save if the book was modified during unobtrusively" do
941
+ m_time = File.mtime(@book1.stored_filename)
942
+ Book.unobtrusively(@simple_file) do |book|
943
+ @book1.Saved.should be_true
944
+ book.Saved.should be_true
945
+ sheet = book[0]
946
+ cell = sheet[0,0]
947
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
948
+ @book1.Saved.should be_false
949
+ book.Saved.should be_false
950
+ sleep 1
951
+ end
952
+ m_time2 = File.mtime(@book1.stored_filename)
953
+ m_time2.should_not == m_time
954
+ end
955
+
956
+ it "should not save the book was not modified during unobtrusively" do
957
+ m_time = File.mtime(@book1.stored_filename)
958
+ Book.unobtrusively(@simple_file) do |book|
959
+ @book1.Saved.should be_true
960
+ book.Saved.should be_true
961
+ sleep 1
962
+ end
963
+ m_time2 = File.mtime(@book1.stored_filename)
964
+ m_time2.should == m_time
965
+ end
966
+ end
967
+
968
+ context "with block result" do
969
+ before do
970
+ @book1 = Book.open(@simple_file)
292
971
  end
293
972
 
294
- it "should raise an error, if :if_obstructed is :raise" do
295
- expect {
296
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :raise)
297
- }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
973
+ after do
974
+ @book1.close(:if_unsaved => :forget)
975
+ end
976
+
977
+ it "should yield the block result true" do
978
+ result =
979
+ Book.unobtrusively(@simple_file) do |book|
980
+ @book1.Saved.should be_true
981
+ end
982
+ result.should == true
298
983
  end
299
984
 
300
- it "should close the other book and open the new book, if :if_obstructed is :forget" do
301
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :forget)
302
- @book.should_not be_alive
303
- @new_book.should be_alive
304
- @new_book.filename.downcase.should == @simple_file.downcase
985
+ it "should yield the block result nil" do
986
+ result =
987
+ Book.unobtrusively(@simple_file) do |book|
988
+ end
989
+ result.should == nil
305
990
  end
306
991
 
307
- it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
308
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :save)
309
- @book.should_not be_alive
310
- @new_book.should be_alive
311
- @new_book.filename.downcase.should == @simple_file.downcase
312
- old_book = RobustExcelOle::Book.open(@simple_file_other_path, :if_obstructed => :forget)
313
- old_book.workbook.Worksheets.Count.should == @sheet_count + 1
314
- old_book.close
992
+ it "should yield the block result with an unmodified book" do
993
+ sheet1 = @book1[0]
994
+ cell1 = sheet1[0,0].value
995
+ result =
996
+ Book.unobtrusively(@simple_file) do |book|
997
+ sheet = book[0]
998
+ cell = sheet[0,0].value
999
+ end
1000
+ result.should == cell1
1001
+ end
1002
+
1003
+ it "should yield the block result even if the book gets saved" do
1004
+ sheet1 = @book1[0]
1005
+ @book1.save
1006
+ result =
1007
+ Book.unobtrusively(@simple_file) do |book|
1008
+ sheet = book[0]
1009
+ sheet[0,0].value = 22
1010
+ @book1.Saved.should be_false
1011
+ 42
1012
+ end
1013
+ result.should == 42
1014
+ @book1.Saved.should be_true
315
1015
  end
1016
+ end
316
1017
 
317
- it "should raise an error, if the old book is unsaved, and close the old book and open the new book,
318
- if :if_obstructed is :close_if_saved" do
319
- expect{
320
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :close_if_saved)
321
- }.to raise_error(ExcelErrorOpen, "book with the same name in a different path is unsaved")
322
- @book.save
323
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :close_if_saved)
324
- @book.should_not be_alive
325
- @new_book.should be_alive
326
- @new_book.filename.downcase.should == @simple_file.downcase
327
- old_book = RobustExcelOle::Book.open(@simple_file_other_path, :if_obstructed => :forget)
328
- old_book.workbook.Worksheets.Count.should == @sheet_count + 1
329
- old_book.close
1018
+ context "with visible" do
1019
+ before do
1020
+ @book1 = Book.open(@simple_file)
330
1021
  end
331
1022
 
332
- it "should open the book in a new excel application, if :if_obstructed is :new_app" do
333
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :new_app)
334
- @book.should be_alive
335
- @new_book.should be_alive
336
- @new_book.filename.should_not == @book.filename
337
- @new_book.excel.should_not == @book.excel
1023
+ after do
1024
+ @book1.close(:if_unsaved => :forget)
1025
+ end
1026
+
1027
+ it "should unobtrusively use a book invisibe" do
1028
+ @book1.excel.Visible.should be_false
1029
+ Book.unobtrusively(@simple_file, :visible => false) do |book|
1030
+ @book1.excel.Visible.should be_false
1031
+ book.excel.Visible.should be_false
1032
+ sheet = book[0]
1033
+ cell = sheet[0,0]
1034
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1035
+ sheet = book[0]
1036
+ end
1037
+ @book1.excel.Visible.should be_false
1038
+ end
1039
+
1040
+ it "should unobtrusively use a book visibe" do
1041
+ @book1.excel.Visible.should be_false
1042
+ Book.unobtrusively(@simple_file, :visible => true) do |book|
1043
+ @book1.excel.Visible.should be_true
1044
+ book.excel.Visible.should be_true
1045
+ sheet = book[0]
1046
+ cell = sheet[0,0]
1047
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1048
+ sheet = book[0]
1049
+ end
1050
+ @book1.excel.Visible.should be_false
1051
+ end
1052
+
1053
+ it "should unobtrusively use a book invisibe by default" do
1054
+ @book1.excel.Visible.should be_false
1055
+ Book.unobtrusively(@simple_file) do |book|
1056
+ @book1.excel.Visible.should be_false
1057
+ book.excel.Visible.should be_false
1058
+ sheet = book[0]
1059
+ cell = sheet[0,0]
1060
+ sheet[0,0] = cell.value == "simple" ? "complex" : "simple"
1061
+ sheet = book[0]
1062
+ end
1063
+ @book1.excel.Visible.should be_false
338
1064
  end
1065
+ end
1066
+ end
1067
+
1068
+ describe "nvalue" do
1069
+ context "with standard" do
1070
+ before do
1071
+ @book1 = Book.open(@more_simple_file)
1072
+ end
1073
+
1074
+ after do
1075
+ @book1.close
1076
+ end
339
1077
 
340
- it "should raise an error, if :if_obstructed is default" do
1078
+ it "should return value of a range" do
1079
+ @book1.nvalue("new").should == "foo"
1080
+ @book1.nvalue("one").should == 1
1081
+ @book1.nvalue("firstrow").should == [[1,2]]
1082
+ @book1.nvalue("four").should == [[1,2],[3,4]]
1083
+ @book1.nvalue("firstrow").should_not == "12"
1084
+ end
1085
+
1086
+ it "should raise an error if name not defined" do
341
1087
  expect {
342
- @new_book = RobustExcelOle::Book.open(@simple_file)
343
- }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
1088
+ value = @book1.nvalue("foo")
1089
+ }.to raise_error(ExcelErrorNValue, "name foo not in more_simple.xls")
344
1090
  end
345
1091
 
346
- it "should raise an error, if :if_obstructed is invalid option" do
1092
+ it "should raise an error if name was defined but contents is calcuated" do
347
1093
  expect {
348
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :invalid_option)
349
- }.to raise_error(ExcelErrorOpen, ":if_obstructed: invalid option")
1094
+ value = @book1.nvalue("named_formula")
1095
+ }.to raise_error(ExcelErrorNValue, "range error in more_simple.xls")
350
1096
  end
351
1097
 
1098
+
352
1099
  end
353
1100
  end
354
1101
 
355
-
356
1102
  describe "close" do
357
1103
 
358
1104
  context "with saved book" do
359
1105
  before do
360
- @book = RobustExcelOle::Book.open(@simple_file)
1106
+ @book = Book.open(@simple_file)
361
1107
  end
362
1108
 
363
1109
  it "should close book" do
@@ -368,9 +1114,9 @@ describe RobustExcelOle::Book do
368
1114
  end
369
1115
  end
370
1116
 
371
- context "with unsaved book and with :read_only" do
1117
+ context "with unsaved read_only book" do
372
1118
  before do
373
- @book = RobustExcelOle::Book.open(@simple_file, :read_only => true)
1119
+ @book = Book.open(@simple_file, :read_only => true)
374
1120
  @sheet_count = @book.workbook.Worksheets.Count
375
1121
  @book.add_sheet(@sheet, :as => 'a_name')
376
1122
  end
@@ -379,7 +1125,7 @@ describe RobustExcelOle::Book do
379
1125
  expect{
380
1126
  @book.close
381
1127
  }.to_not raise_error
382
- new_book = RobustExcelOle::Book.open(@simple_file)
1128
+ new_book = Book.open(@simple_file)
383
1129
  new_book.workbook.Worksheets.Count.should == @sheet_count
384
1130
  new_book.close
385
1131
  end
@@ -388,7 +1134,7 @@ describe RobustExcelOle::Book do
388
1134
 
389
1135
  context "with unsaved book" do
390
1136
  before do
391
- @book = RobustExcelOle::Book.open(@simple_file)
1137
+ @book = Book.open(@simple_file)
392
1138
  @sheet_count = @book.workbook.Worksheets.Count
393
1139
  @book.add_sheet(@sheet, :as => 'a_name')
394
1140
  @sheet = @book[0]
@@ -414,7 +1160,7 @@ describe RobustExcelOle::Book do
414
1160
  @book.should_not be_alive
415
1161
  expect{
416
1162
  ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
417
- new_book = RobustExcelOle::Book.open(@simple_file)
1163
+ new_book = Book.open(@simple_file)
418
1164
  begin
419
1165
  new_book.workbook.Worksheets.Count.should == @sheet_count
420
1166
  ensure
@@ -432,7 +1178,7 @@ describe RobustExcelOle::Book do
432
1178
  @book.should_not be_alive
433
1179
  expect{
434
1180
  ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
435
- new_book = RobustExcelOle::Book.open(@simple_file)
1181
+ new_book = Book.open(@simple_file)
436
1182
  begin
437
1183
  new_book.workbook.Worksheets.Count.should == @sheet_count + 1
438
1184
  ensure
@@ -472,7 +1218,7 @@ describe RobustExcelOle::Book do
472
1218
  @book.should_not be_alive
473
1219
  expect{ole_workbook.Name}.to raise_error(WIN32OLERuntimeError)
474
1220
  end
475
- new_book = RobustExcelOle::Book.open(@simple_file, :if_unsaved => :forget)
1221
+ new_book = Book.open(@simple_file, :if_unsaved => :forget)
476
1222
  begin
477
1223
  new_book.workbook.Worksheets.Count.should == @sheet_count + (answer==:yes ? 1 : 0)
478
1224
  new_book.excel.DisplayAlerts.should == displayalert_value
@@ -501,7 +1247,7 @@ describe RobustExcelOle::Book do
501
1247
 
502
1248
  context "with simple save" do
503
1249
  it "should save for a file opened without :read_only" do
504
- @book = RobustExcelOle::Book.open(@simple_file)
1250
+ @book = Book.open(@simple_file)
505
1251
  @book.add_sheet(@sheet, :as => 'a_name')
506
1252
  @new_sheet_count = @book.workbook.Worksheets.Count
507
1253
  expect {
@@ -512,7 +1258,7 @@ describe RobustExcelOle::Book do
512
1258
  end
513
1259
 
514
1260
  it "should raise error with read_only" do
515
- @book = RobustExcelOle::Book.open(@simple_file, :read_only => true)
1261
+ @book = Book.open(@simple_file, :read_only => true)
516
1262
  expect {
517
1263
  @book.save
518
1264
  }.to raise_error(ExcelErrorSave, "Not opened for writing (opened with :read_only option)")
@@ -522,7 +1268,7 @@ describe RobustExcelOle::Book do
522
1268
 
523
1269
  context "with open with read only" do
524
1270
  before do
525
- @book = RobustExcelOle::Book.open(@simple_file, :read_only => true)
1271
+ @book = Book.open(@simple_file, :read_only => true)
526
1272
  end
527
1273
 
528
1274
  after do
@@ -539,7 +1285,7 @@ describe RobustExcelOle::Book do
539
1285
 
540
1286
  context "with argument" do
541
1287
  before do
542
- RobustExcelOle::Book.open(@simple_file) do |book|
1288
+ Book.open(@simple_file) do |book|
543
1289
  book.save_as(@simple_save_file, :if_exists => :overwrite)
544
1290
  end
545
1291
  end
@@ -551,7 +1297,7 @@ describe RobustExcelOle::Book do
551
1297
 
552
1298
  context "with different extensions" do
553
1299
  before do
554
- @book = RobustExcelOle::Book.open(@simple_file)
1300
+ @book = Book.open(@simple_file)
555
1301
  end
556
1302
 
557
1303
  after do
@@ -565,8 +1311,8 @@ describe RobustExcelOle::Book do
565
1311
  File.delete simple_save_file rescue nil
566
1312
  @book.save_as(simple_save_file, :if_exists => :overwrite)
567
1313
  File.exist?(simple_save_file).should be_true
568
- new_book = RobustExcelOle::Book.open(simple_save_file)
569
- new_book.should be_a RobustExcelOle::Book
1314
+ new_book = Book.open(simple_save_file)
1315
+ new_book.should be_a Book
570
1316
  new_book.close
571
1317
  end
572
1318
  end
@@ -577,7 +1323,7 @@ describe RobustExcelOle::Book do
577
1323
  possible_displayalerts.each do |displayalert_value|
578
1324
  context "with displayalerts=#{displayalert_value}" do
579
1325
  before do
580
- @book = RobustExcelOle::Book.open(@simple_file, :displayalerts => displayalert_value)
1326
+ @book = Book.open(@simple_file, :displayalerts => displayalert_value)
581
1327
  end
582
1328
 
583
1329
  after do
@@ -587,7 +1333,7 @@ describe RobustExcelOle::Book do
587
1333
  it "should raise an error if the book is open" do
588
1334
  File.delete @simple_save_file rescue nil
589
1335
  FileUtils.copy @simple_file, @simple_save_file
590
- book_save = RobustExcelOle::Book.open(@simple_save_file, :reuse => false)
1336
+ book_save = Book.open(@simple_save_file, :excel => :new)
591
1337
  expect{
592
1338
  @book.save_as(@simple_save_file, :if_exists => :overwrite)
593
1339
  }.to raise_error(ExcelErrorSave, "book is open and used in Excel")
@@ -601,11 +1347,10 @@ describe RobustExcelOle::Book do
601
1347
  end
602
1348
  @book.save_as(@simple_save_file, :if_exists => :overwrite)
603
1349
  File.exist?(@simple_save_file).should be_true
604
- new_book = RobustExcelOle::Book.open(@simple_save_file)
605
- new_book.should be_a RobustExcelOle::Book
1350
+ new_book = Book.open(@simple_save_file)
1351
+ new_book.should be_a Book
606
1352
  new_book.close
607
1353
  end
608
-
609
1354
  it "should save to 'simple_save_file.xls' with :if_exists => :raise" do
610
1355
  dirname, basename = File.split(@simple_save_file)
611
1356
  File.delete @simple_save_file rescue nil
@@ -618,7 +1363,7 @@ describe RobustExcelOle::Book do
618
1363
  @book.save_as(@simple_save_file, :if_exists => :raise)
619
1364
  }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
620
1365
  File.exist?(@simple_save_file).should be_true
621
- (File.size?(@simple_save_file) == booklength).should be_true
1366
+ File.size?(@simple_save_file).should == booklength
622
1367
  end
623
1368
 
624
1369
  context "with :if_exists => :alert" do
@@ -641,10 +1386,11 @@ describe RobustExcelOle::Book do
641
1386
  @book.save_as(@simple_save_file, :if_exists => :alert)
642
1387
  File.exist?(@simple_save_file).should be_true
643
1388
  File.size?(@simple_save_file).should > @garbage_length
644
- new_book = RobustExcelOle::Book.open(@simple_save_file)
645
- new_book.should be_a RobustExcelOle::Book
646
1389
  @book.excel.DisplayAlerts.should == displayalert_value
1390
+ new_book = Book.open(@simple_save_file, :excel => :new)
1391
+ new_book.should be_a Book
647
1392
  new_book.close
1393
+ @book.excel.DisplayAlerts.should == displayalert_value
648
1394
  end
649
1395
 
650
1396
  it "should not save if user answers 'no'" do
@@ -702,7 +1448,7 @@ describe RobustExcelOle::Book do
702
1448
  @book.save_as(@simple_save_file)
703
1449
  }.to raise_error(ExcelErrorSave, 'book already exists: ' + basename)
704
1450
  File.exist?(@simple_save_file).should be_true
705
- (File.size?(@simple_save_file) == booklength).should be_true
1451
+ File.size?(@simple_save_file).should == booklength
706
1452
  end
707
1453
 
708
1454
  it "should save to 'simple_save_file.xls' with :if_exists => :invalid_option" do
@@ -716,12 +1462,12 @@ describe RobustExcelOle::Book do
716
1462
  end
717
1463
  end
718
1464
 
719
- describe "== , alive?, filename" do
1465
+ describe "== , alive?, filename, visible, empty_workbook" do
720
1466
 
721
1467
  context "with alive?" do
722
1468
 
723
1469
  before do
724
- @book = RobustExcelOle::Book.open(@simple_file)
1470
+ @book = Book.open(@simple_file)
725
1471
  end
726
1472
 
727
1473
  after do
@@ -742,7 +1488,7 @@ describe RobustExcelOle::Book do
742
1488
  context "with filename" do
743
1489
 
744
1490
  before do
745
- @book = RobustExcelOle::Book.open(@simple_file)
1491
+ @book = Book.open(@simple_file)
746
1492
  end
747
1493
 
748
1494
  after do
@@ -763,7 +1509,7 @@ describe RobustExcelOle::Book do
763
1509
  context "with ==" do
764
1510
 
765
1511
  before do
766
- @book = RobustExcelOle::Book.open(@simple_file)
1512
+ @book = Book.open(@simple_file)
767
1513
  end
768
1514
 
769
1515
  after do
@@ -772,24 +1518,22 @@ describe RobustExcelOle::Book do
772
1518
  end
773
1519
 
774
1520
  it "should be true with two identical books" do
775
- @new_book = RobustExcelOle::Book.open(@simple_file)
1521
+ @new_book = Book.open(@simple_file)
776
1522
  @new_book.should == @book
777
1523
  end
778
1524
 
779
1525
  it "should be false with two different books" do
780
- different_file = @dir + '/different_simple.xls'
781
- @new_book = RobustExcelOle::Book.new(different_file)
1526
+ @new_book = Book.new(@different_file)
782
1527
  @new_book.should_not == @book
783
1528
  end
784
1529
 
785
- it "should be false with same book names but different paths" do
786
- simple_file_other_path = @dir + '/more_data/simple.xls'
787
- @new_book = RobustExcelOle::Book.new(simple_file_other_path, :reuse => false)
1530
+ it "should be false with same book names but different paths" do
1531
+ @new_book = Book.new(@simple_file_other_path, :excel => :new)
788
1532
  @new_book.should_not == @book
789
1533
  end
790
1534
 
791
- it "should be false with same book names but different excel apps" do
792
- @new_book = RobustExcelOle::Book.new(@simple_file, :reuse => false)
1535
+ it "should be false with same book names but different excel instances" do
1536
+ @new_book = Book.new(@simple_file, :excel => :new)
793
1537
  @new_book.should_not == @book
794
1538
  end
795
1539
 
@@ -800,11 +1544,41 @@ describe RobustExcelOle::Book do
800
1544
  end
801
1545
  end
802
1546
 
1547
+ context "with visible and displayalerts" do
1548
+
1549
+ before do
1550
+ @book = Book.open(@simple_file)
1551
+ end
1552
+
1553
+ after do
1554
+ @book.close
1555
+ end
1556
+
1557
+ it "should make Excel visible" do
1558
+ @book.visible = false
1559
+ @book.excel.visible.should be_false
1560
+ @book.visible.should be_false
1561
+ @book.visible = true
1562
+ @book.excel.visible.should be_true
1563
+ @book.visible.should be_true
1564
+ end
1565
+
1566
+ it "should enable DisplayAlerts in Excel" do
1567
+ @book.displayalerts = false
1568
+ @book.excel.displayalerts.should be_false
1569
+ @book.displayalerts.should be_false
1570
+ @book.displayalerts = true
1571
+ @book.excel.displayalerts.should be_true
1572
+ @book.displayalerts.should be_true
1573
+ end
1574
+
1575
+ end
1576
+
803
1577
  end
804
1578
 
805
1579
  describe "#add_sheet" do
806
1580
  before do
807
- @book = RobustExcelOle::Book.open(@simple_file)
1581
+ @book = Book.open(@simple_file)
808
1582
  @sheet = @book[0]
809
1583
  end
810
1584
 
@@ -869,7 +1643,6 @@ describe RobustExcelOle::Book do
869
1643
  @book.add_sheet(:after => @sheet).name.should eq @book[1].name
870
1644
  end
871
1645
  end
872
-
873
1646
  end
874
1647
 
875
1648
  context "without argument" do
@@ -883,11 +1656,22 @@ describe RobustExcelOle::Book do
883
1656
  sheet.name.should eq copyed_sheet.name
884
1657
  end
885
1658
  end
1659
+
1660
+ context "should raise error if the sheet name already exists" do
1661
+ it "should raise error with giving a name that already exists" do
1662
+ @book.add_sheet(@sheet, :as => 'new_sheet')
1663
+ expect{
1664
+ @book.add_sheet(@sheet, :as => 'new_sheet')
1665
+ }.to raise_error(ExcelErrorSheet, "sheet name already exists")
1666
+ end
1667
+ end
1668
+
1669
+
886
1670
  end
887
1671
 
888
1672
  describe 'access sheet' do
889
1673
  before do
890
- @book = RobustExcelOle::Book.open(@simple_file)
1674
+ @book = Book.open(@simple_file)
891
1675
  end
892
1676
 
893
1677
  after do
@@ -895,23 +1679,23 @@ describe RobustExcelOle::Book do
895
1679
  end
896
1680
 
897
1681
  it 'with sheet name' do
898
- @book['Sheet1'].should be_kind_of RobustExcelOle::Sheet
1682
+ @book['Sheet1'].should be_kind_of Sheet
899
1683
  end
900
1684
 
901
1685
  it 'with integer' do
902
- @book[0].should be_kind_of RobustExcelOle::Sheet
1686
+ @book[0].should be_kind_of Sheet
903
1687
  end
904
1688
 
905
1689
  it 'with block' do
906
1690
  @book.each do |sheet|
907
- sheet.should be_kind_of RobustExcelOle::Sheet
1691
+ sheet.should be_kind_of Sheet
908
1692
  end
909
1693
  end
910
1694
 
911
1695
  context 'open with block' do
912
1696
  it {
913
- RobustExcelOle::Book.open(@simple_file) do |book|
914
- book['Sheet1'].should be_a RobustExcelOle::Sheet
1697
+ Book.open(@simple_file) do |book|
1698
+ book['Sheet1'].should be_a Sheet
915
1699
  end
916
1700
  }
917
1701
  end