robust_excel_ole 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +13 -0
  3. data/README.rdoc +107 -73
  4. data/{README_excel.rdoc → docs/README_excel.rdoc} +2 -0
  5. data/{README_open.rdoc → docs/README_open.rdoc} +12 -16
  6. data/{README_ranges.rdoc → docs/README_ranges.rdoc} +1 -1
  7. data/{README_save_close.rdoc → docs/README_save_close.rdoc} +0 -0
  8. data/{README_sheet.rdoc → docs/README_sheet.rdoc} +0 -0
  9. data/examples/open_save_close/example_control_to_excel.rb +3 -3
  10. data/examples/open_save_close/example_default_excel.rb +2 -2
  11. data/examples/open_save_close/example_force_excel.rb +2 -2
  12. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +2 -2
  13. data/examples/open_save_close/example_if_obstructed_forget.rb +2 -2
  14. data/examples/open_save_close/example_if_obstructed_save.rb +2 -2
  15. data/examples/open_save_close/example_if_unsaved_accept.rb +3 -3
  16. data/examples/open_save_close/example_if_unsaved_forget.rb +7 -3
  17. data/examples/open_save_close/example_if_unsaved_forget_more.rb +1 -1
  18. data/examples/open_save_close/example_read_only.rb +2 -2
  19. data/examples/open_save_close/example_rename_cells.rb +2 -2
  20. data/examples/open_save_close/example_reuse.rb +2 -2
  21. data/examples/open_save_close/example_simple.rb +1 -1
  22. data/examples/open_save_close/example_unobtrusively.rb +2 -2
  23. data/lib/robust_excel_ole/book.rb +75 -76
  24. data/lib/robust_excel_ole/bookstore.rb +9 -9
  25. data/lib/robust_excel_ole/excel.rb +5 -5
  26. data/lib/robust_excel_ole/general.rb +11 -4
  27. data/lib/robust_excel_ole/reo_common.rb +3 -0
  28. data/lib/robust_excel_ole/version.rb +1 -1
  29. data/robust_excel_ole.gemspec +2 -2
  30. data/spec/book_spec.rb +67 -63
  31. data/spec/book_specs/book_close_spec.rb +12 -10
  32. data/spec/book_specs/book_misc_spec.rb +279 -279
  33. data/spec/book_specs/book_open_spec.rb +258 -105
  34. data/spec/book_specs/book_save_spec.rb +28 -28
  35. data/spec/book_specs/book_unobtr_spec.rb +862 -164
  36. data/spec/bookstore_spec.rb +22 -22
  37. data/spec/data/another_workbook.xls +0 -0
  38. data/spec/data/different_workbook.xls +0 -0
  39. data/spec/data/workbook.xls +0 -0
  40. data/spec/excel_spec.rb +256 -254
  41. data/spec/general_spec.rb +7 -7
  42. data/spec/ruby1.8.6_rspec2.14/book_spec.rb +1421 -0
  43. data/spec/ruby1.8.6_rspec2.14/book_specs/book_all_spec.rb +22 -0
  44. data/spec/ruby1.8.6_rspec2.14/book_specs/book_close_spec.rb +252 -0
  45. data/spec/ruby1.8.6_rspec2.14/book_specs/book_misc_spec.rb +1070 -0
  46. data/spec/ruby1.8.6_rspec2.14/book_specs/book_open_spec.rb +1855 -0
  47. data/spec/ruby1.8.6_rspec2.14/book_specs/book_save_spec.rb +514 -0
  48. data/spec/ruby1.8.6_rspec2.14/book_specs/book_sheet_spec.rb +395 -0
  49. data/spec/ruby1.8.6_rspec2.14/book_specs/book_subclass_spec.rb +51 -0
  50. data/spec/ruby1.8.6_rspec2.14/book_specs/book_unobtr_spec.rb +1737 -0
  51. data/spec/ruby1.8.6_rspec2.14/bookstore_spec.rb +495 -0
  52. data/spec/ruby1.8.6_rspec2.14/cell_spec.rb +76 -0
  53. data/spec/ruby1.8.6_rspec2.14/cygwin_spec.rb +42 -0
  54. data/spec/ruby1.8.6_rspec2.14/excel_spec.rb +1820 -0
  55. data/spec/ruby1.8.6_rspec2.14/general_spec.rb +212 -0
  56. data/spec/ruby1.8.6_rspec2.14/range_spec.rb +131 -0
  57. data/spec/ruby1.8.6_rspec2.14/reo_common_spec.rb +130 -0
  58. data/spec/ruby1.8.6_rspec2.14/sheet_spec.rb +663 -0
  59. data/spec/ruby1.8.6_rspec2.14/spec_helper.rb +35 -0
  60. data/spec/sheet_spec.rb +2 -2
  61. metadata +28 -12
  62. data/README_detail.rdoc +0 -852
  63. data/README_development.rdoc +0 -5
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name1 = dir + 'workbook.xls'
@@ -44,6 +44,6 @@ begin
44
44
  book3.close
45
45
  book2.close
46
46
  ensure
47
- Excel.close_all # close all workbooks, quit Excel instances
47
+ Excel.kill_all # close all workbooks, quit Excel instances
48
48
  rm_tmp(dir)
49
49
  end
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  simple_file = dir + 'workbook.xls'
@@ -29,6 +29,6 @@ begin
29
29
  book2.close
30
30
  book1.close
31
31
  ensure
32
- Excel.close_all # close all workbooks, quit Excel instances
32
+ Excel.kill_all # close all workbooks, quit Excel instances
33
33
  rm_tmp(dir)
34
34
  end
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
@@ -31,6 +31,6 @@ begin
31
31
  puts "the old book was saved" unless new_first_cell == first_cell
32
32
  new_book.close # close the books
33
33
  ensure
34
- Excel.close_all # close all workbooks, quit Excel application
34
+ Excel.kill_all # close all workbooks, quit Excel application
35
35
  rm_tmp(dir)
36
36
  end
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
@@ -28,6 +28,6 @@ begin
28
28
  new_book.close # close the books
29
29
  another_book.close
30
30
  ensure
31
- Excel.close_all # close all workbooks, quit Excel application
31
+ Excel.kill_all # close all workbooks, quit Excel application
32
32
  rm_tmp(dir)
33
33
  end
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
@@ -28,6 +28,6 @@ begin
28
28
  new_book.close # close the books
29
29
  old_book.close
30
30
  ensure
31
- Excel.close_all # close all workbooks, quit Excel application
31
+ Excel.kill_all # close all workbooks, quit Excel application
32
32
  rm_tmp(dir)
33
33
  end
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
@@ -16,7 +16,7 @@ begin
16
16
  sheet[1,1] = sheet[1,1].value == "simple" ? "complex" : "simple" # change a cell
17
17
  begin
18
18
  new_book = Workbook.open(file_name) # open another book with the same file name
19
- rescue WorkbookBeingUsed => msg # by default: raises an exception:
19
+ rescue WorkbookNotSaved => msg # by default: raises an exception:
20
20
  puts "error: open: #{msg.message}" # a book with the same name is already open and unsaved
21
21
  end
22
22
  new_book = Workbook.open(file_name, :if_unsaved => :accept) # open another book with the same file name
@@ -36,6 +36,6 @@ begin
36
36
  puts "closed the book successfully with option :if_unsaved => :save"
37
37
  new_book.close # close the other book. It is already saved.
38
38
  ensure
39
- Excel.close_all # close workbooks, quit Excel application
39
+ Excel.kill_all # close workbooks, quit Excel application
40
40
  rm_tmp(dir)
41
41
  end
@@ -7,19 +7,23 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
14
14
  book = Workbook.open(file_name) # open a book
15
- book.excel.visible = true # make current Excel visible
15
+ puts "file_name: #{file_name}"
16
+ #book.excel.visible = true # make current Excel visible
16
17
  sleep 1
17
18
  sheet = book.sheet(1) # access a sheet
18
19
  first_cell = sheet[1,1].value
19
20
  sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
20
21
  sleep 1
22
+ puts "new_book: before"
23
+ puts "file_name: #{file_name}"
21
24
  new_book = Workbook.open(file_name, :if_unsaved => :forget) # open another book with the same file name
22
25
  # and close the unsaved book without saving it
26
+ puts "new_book: after"
23
27
  sheet_new_book = new_book.sheet(1)
24
28
  if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].value == first_cell then # check whether the unsaved book
25
29
  puts "open with :if_unsaved => :forget : the unsaved book is closed and not saved." # is closed and was not saved
@@ -32,6 +36,6 @@ begin
32
36
  new_book.close(:if_unsaved => :forget )
33
37
  another_book.close
34
38
  ensure
35
- Excel.close_all # close all workbooks, quit Excel application
39
+ Excel.kill_all # close all workbooks, quit Excel application
36
40
  rm_tmp(dir)
37
41
  end
@@ -12,7 +12,7 @@ begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
14
14
  book = Workbook.open(file_name) # open a book
15
- book.excel.visible = true # make current Excel visible
15
+ #book.excel.visible = true # make current Excel visible
16
16
  sheet = book.sheet(1) # access a sheet
17
17
  first_cell = sheet[1,1].value
18
18
  sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
@@ -6,7 +6,7 @@ require "fileutils"
6
6
 
7
7
  include RobustExcelOle
8
8
 
9
- Excel.close_all
9
+ Excel.kill_all
10
10
  begin
11
11
  dir = create_tmpdir
12
12
  file_name = dir + 'workbook.xls'
@@ -23,7 +23,7 @@ begin
23
23
  end
24
24
  book.close # close the book without saving it
25
25
  ensure
26
- Excel.close_all # close workbooks, quit Excel application
26
+ Excel.kill_all # close workbooks, quit Excel application
27
27
  rm_tmp(dir)
28
28
  end
29
29
 
@@ -7,7 +7,7 @@ require "fileutils"
7
7
 
8
8
  include RobustExcelOle
9
9
 
10
- Excel.close_all
10
+ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
@@ -51,7 +51,7 @@ begin
51
51
  book.close(:if_unsaved => :forget) # close the book
52
52
 
53
53
  ensure
54
- Excel.close_all # close workbooks, quit Excel application
54
+ Excel.kill_all # close workbooks, quit Excel application
55
55
  rm_tmp(dir)
56
56
  end
57
57
 
@@ -6,7 +6,7 @@ require "fileutils"
6
6
 
7
7
  include RobustExcelOle
8
8
 
9
- Excel.close_all
9
+ Excel.kill_all
10
10
  begin
11
11
  dir = create_tmpdir
12
12
  file_name1 = dir + 'workbook.xls'
@@ -27,7 +27,7 @@ begin
27
27
  book3.close
28
28
  book4.close
29
29
  ensure
30
- Excel.close_all # close all workbooks, quit Excel application
30
+ Excel.kill_all # close all workbooks, quit Excel application
31
31
  rm_tmp(dir)
32
32
  end
33
33
 
@@ -32,7 +32,7 @@ begin
32
32
  puts "save_as: saved successfully with option :if_exists => :overwrite"
33
33
  book.close # close the book
34
34
  ensure
35
- Excel.close_all # close workbooks, quit Excel application
35
+ Excel.kill_all # close workbooks, quit Excel application
36
36
  #rm_tmp(dir)
37
37
  end
38
38
 
@@ -6,7 +6,7 @@ require "fileutils"
6
6
 
7
7
  include RobustExcelOle
8
8
 
9
- Excel.close_all
9
+ Excel.kill_all
10
10
  begin
11
11
  dir = create_tmpdir
12
12
  simple_file = dir + 'workbook.xls'
@@ -23,6 +23,6 @@ begin
23
23
  p "book saved" if book.Saved
24
24
  book.close # close the book
25
25
  ensure
26
- Excel.close_all # close all workbooks, quit Excel application
26
+ Excel.kill_all # close all workbooks, quit Excel application
27
27
  rm_tmp(dir)
28
28
  end
@@ -73,9 +73,9 @@ module RobustExcelOle
73
73
  # :visible true -> makes the workbook visible
74
74
  # :check_compatibility true -> check compatibility when saving
75
75
  # :update_links true -> user is being asked how to update links, false -> links are never updated
76
- # @return [Book] a workbook
76
+ # @return [Book] a representation of a workbook
77
77
  def open(file, opts={ }, &block)
78
- options = process_options(opts)
78
+ options = @options = process_options(opts)
79
79
  book = nil
80
80
  if (not (options[:force][:excel] == :new))
81
81
  # if readonly is true, then prefer a book that is given in force_excel if this option is set
@@ -86,16 +86,20 @@ module RobustExcelOle
86
86
  :prefer_writable => (not options[:read_only]),
87
87
  :prefer_excel => (options[:read_only] ? forced_excel : nil)) rescue nil
88
88
  if book
89
+ #if forced_excel != book.excel &&
90
+ # (not (book.alive? && (not book.saved) && (not options[:if_unsaved] == :accept)))
89
91
  if (((not options[:force][:excel]) || (forced_excel == book.excel)) &&
90
- (not (book.alive? && (not book.saved) && (not options[:if_unsaved] == :accept))))
92
+ (not (book.alive? && (not book.saved) && (not options[:if_unsaved] == :accept))))
91
93
  book.options = options
92
94
  book.ensure_excel(options) # unless book.excel.alive?
93
- # if the ReadOnly status shall be changed, then save, close and reopen it
94
- book.close(:if_unsaved => :save) if (book.alive? &&
95
- (((not book.writable) and (not options[:read_only])) or
96
- (book.writable and options[:read_only])))
97
- # reopens the book
98
- book.ensure_workbook(file,options) unless book.alive?
95
+ # if the ReadOnly status shall be changed, save, close and reopen it
96
+ if book.alive? and (((not book.writable) and (not options[:read_only])) or
97
+ (book.writable and options[:read_only]))
98
+ book.save if book.writable && (not book.saved)
99
+ book.close(:if_unsaved => :forget)
100
+ end
101
+ # reopens the book if it was closed
102
+ book.ensure_workbook(file,options) unless book.alive?
99
103
  book.visible = options[:force][:visible] unless options[:force][:visible].nil?
100
104
  book.CheckCompatibility = options[:check_compatibility] unless options[:check_compatibility].nil?
101
105
  book.excel.calculation = options[:calculation] unless options[:calculation].nil?
@@ -107,6 +111,24 @@ module RobustExcelOle
107
111
  end
108
112
  end
109
113
 
114
+ # here is what is different and works
115
+ =begin
116
+ if book
117
+ if (((not options[:force][:excel]) || (forced_excel == book.excel)) &&
118
+ (not (book.alive? && (not book.saved) && (not options[:if_unsaved] == :accept))))
119
+ book.options = options
120
+ book.ensure_excel(options) # unless book.excel.alive?
121
+ # if the ReadOnly status shall be changed, close and reopen it; save before, if it is writable
122
+ if book.alive? and (((not book.writable) and (not options[:read_only])) or
123
+ (book.writable and options[:read_only]))
124
+ book.save if book.writable
125
+ book.close(:if_unsaved => :forget)
126
+ end
127
+ # reopens the book
128
+ book.ensure_workbook(file,options) unless book.alive?
129
+ =end
130
+
131
+
110
132
  # creates a Book object by opening an Excel file given its filename workbook or
111
133
  # by lifting a Win32OLE object representing an Excel file
112
134
  # @param [WIN32OLE] workbook a workbook
@@ -114,14 +136,13 @@ module RobustExcelOle
114
136
  # @option opts [Symbol] see above
115
137
  # @return [Book] a workbook
116
138
  def self.new(workbook, opts={ }, &block)
139
+ opts = process_options(opts)
117
140
  if workbook && (workbook.is_a? WIN32OLE)
118
- opts = process_options(opts)
119
141
  filename = workbook.Fullname.tr('\\','/') rescue nil
120
142
  if filename
121
143
  book = bookstore.fetch(filename)
122
144
  if book && book.alive?
123
- book.visible = opts[:force][:visible] unless opts[:force][:visible].nil?
124
- #book.excel.calculation = opts[:calculation].nil? ? book.excel.calculation : opts[:calculation]
145
+ book.visible = opts[:force][:visible] unless opts[:force].nil? or opts[:force][:visible].nil?
125
146
  book.excel.calculation = opts[:calculation] unless opts[:calculation].nil?
126
147
  return book
127
148
  end
@@ -139,7 +160,7 @@ module RobustExcelOle
139
160
  # @return [Book] a workbook
140
161
  #def initialize(file_or_workbook, opts={ }, &block)
141
162
  def initialize(file_or_workbook, options={ }, &block)
142
- #options = self.class.process_options(opts)
163
+ #options = @options = self.class.process_options(options) if options.empty?
143
164
  if file_or_workbook.is_a? WIN32OLE
144
165
  workbook = file_or_workbook
145
166
  @ole_workbook = workbook
@@ -152,7 +173,7 @@ module RobustExcelOle
152
173
  else
153
174
  file = file_or_workbook
154
175
  ensure_excel(options)
155
- ensure_workbook(file, options)
176
+ ensure_workbook(file, options)
156
177
  end
157
178
  bookstore.store(self)
158
179
  @modified_cells = []
@@ -179,7 +200,7 @@ module RobustExcelOle
179
200
  erg[new_key] = {}
180
201
  value.each do |k,v|
181
202
  new_k = k
182
- ABBREVIATIONS.each{|long,short| new_k = long if k == short}
203
+ ABBREVIATIONS.each{|l,s| new_k = l if k == s}
183
204
  erg[new_key][new_k] = v
184
205
  end
185
206
  else
@@ -228,40 +249,17 @@ module RobustExcelOle
228
249
 
229
250
  public
230
251
 
231
- =begin
232
- # work in progress#
233
- def self.open_in_current_excel(file, opts = { }) # :nodoc: #
234
- options = DEFAULT_OPEN_OPTS.merge(opts)
235
- filename = General::absolute_path(file)
236
- ole_workbook = WIN32OLE.connect(filename)
237
- workbook = Book.new(ole_workbook)
238
- workbook.visible = options[:force][:visible] unless options[:force][:visible].nil?
239
- update_links_opt =
240
- case options[:update_links]
241
- when :alert; RobustExcelOle::XlUpdateLinksUserSetting
242
- when :never; RobustExcelOle::XlUpdateLinksNever
243
- when :always; RobustExcelOle::XlUpdateLinksAlways
244
- else RobustExcelOle::XlUpdateLinksNever
245
- end
246
- workbook.UpdateLinks = update_links_opt
247
- workbook.CheckCompatibility = options[:check_compatibility]
248
- workbook
249
- end
250
- =end
251
-
252
252
  def ensure_excel(options) # :nodoc: #
253
- if @excel && @excel.alive?
253
+ #if (excel && @excel.alive? && (options[:force].nil? or options[:force][:excel].nil? or
254
+ # options[:force][:excel]==@excel))
255
+ if excel && @excel.alive?
254
256
  @excel.created = false
255
257
  return
256
258
  end
257
259
  excel_option = (options[:force].nil? or options[:force][:excel].nil?) ? options[:default][:excel] : options[:force][:excel]
258
260
  @excel = self.class.excel_of(excel_option) unless (excel_option == :current || excel_option == :new)
259
261
  @excel = excel_class.new(:reuse => (excel_option == :current)) unless (@excel && @excel.alive?)
260
-
261
- #options[:excel] = options[:force_excel] ? options[:force_excel] : options[:default_excel]
262
- #options[:excel] = :current if (options[:excel] == :reuse || options[:excel] == :active)
263
- #@excel = self.class.excel_of(options[:excel]) unless (options[:excel] == :current || options[:excel] == :new)
264
- #@excel = excel_class.new(:reuse => (options[:excel] == :current)) unless (@excel && @excel.alive?)
262
+ @excel
265
263
  end
266
264
 
267
265
  def ensure_workbook(file, options) # :nodoc: #
@@ -474,7 +472,7 @@ module RobustExcelOle
474
472
  def self.for_reading(*args, &block)
475
473
  args = args.dup
476
474
  opts = args.last.is_a?(Hash) ? args.pop : {}
477
- opts = {:read_only => true}.merge(opts)
475
+ opts = {:writable => false}.merge(opts)
478
476
  args.push opts
479
477
  unobtrusively(*args, &block)
480
478
  end
@@ -482,7 +480,7 @@ module RobustExcelOle
482
480
  def self.for_modifying(*args, &block)
483
481
  args = args.dup
484
482
  opts = args.last.is_a?(Hash) ? args.pop : {}
485
- opts = {:read_only => false}.merge(opts)
483
+ opts = {:writable => true}.merge(opts)
486
484
  args.push opts
487
485
  unobtrusively(*args, &block)
488
486
  end
@@ -492,21 +490,22 @@ module RobustExcelOle
492
490
  # @param [String] file the file name
493
491
  # @param [Hash] opts the options
494
492
  # @option opts [Variant] :if_closed :current (default), :new or an Excel instance
495
- # @option opts [Boolean] :read_only whether the workbook is opened for read-only
496
- # @option opts [Boolean] :readonly_excel behaviour when workbook is opened read-only and shall be modified
497
- # true: closes it and open it as writable in the Excel instance where it was open so far
498
- # false (default) opens it as writable in another running excel instance, if it exists,
499
- # otherwise open in a new Excel instance.
493
+ # @option opts [Boolean] :read_only true/false, open the workbook in read-only/read-write modus (save changes)
494
+ # @option opts [Boolean] :writable true/false changes of the workbook shall be saved/not saved
495
+ # @option opts [Boolean] :rw_change_excel Excel instance in which the workbook with the new
496
+ # write permissions shall be opened :current (default), :new or an Excel instance
500
497
  # @option opts [Boolean] :keep_open whether the workbook shall be kept open after unobtrusively opening
501
498
  # @return [Book] a workbook
502
-
503
499
  # state = [:open, :saved, :writable, :visible, :calculation, :check_compatibility]
504
500
  def self.unobtrusively(file, opts = { }, &block)
505
- options = {:if_closed => :current,
506
- :read_only => false,
507
- :readonly_excel => false,
508
- :keep_open => false}.merge(opts)
509
- book = bookstore.fetch(file, :prefer_writable => (not options[:read_only]))
501
+ opts = {:if_closed => :current,
502
+ :rw_change_excel => :current,
503
+ :keep_open => false}.merge(opts)
504
+ raise OptionInvalid, "contradicting options" if (opts[:writable] && opts[:read_only])
505
+ prefer_writable = (((not opts[:read_only]) || opts[:writable]==true) &&
506
+ (not (opts[:read_only].nil?) && opts[:writable]==false))
507
+ do_not_write = (opts[:read_only] or (opts[:read_only].nil? && opts[:writable]==false))
508
+ book = bookstore.fetch(file, :prefer_writable => prefer_writable)
510
509
  was_open = book && book.alive?
511
510
  if was_open
512
511
  was_saved = book.saved
@@ -514,37 +513,39 @@ module RobustExcelOle
514
513
  was_visible = book.visible
515
514
  was_calculation = book.calculation
516
515
  was_check_compatibility = book.check_compatibility
517
- end
518
- begin
516
+ if ((opts[:writable] && (not was_writable) && (not was_saved)) ||
517
+ (opts[:read_only] && was_writable && (not was_saved)))
518
+ raise NotImplementedREOError, "unsaved read-only workbook shall be written"
519
+ end
520
+ opts[:rw_change_excel] = book.excel if opts[:rw_change_excel]==:current
521
+ end
522
+ change_rw_mode = ((opts[:read_only] && was_writable) or (opts[:writable] && (not was_writable)))
523
+ begin
519
524
  book =
520
525
  if was_open
521
- if (not was_writable) && (not options[:read_only])
522
- open(file, :force => {:excel => (options[:readonly_excel] ? book.excel : :new)}, :read_only => false)
526
+ if change_rw_mode
527
+ open(file, :force => {:excel => opts[:rw_change_excel]}, :read_only => do_not_write)
523
528
  else
524
529
  book
525
530
  end
526
531
  else
527
- open(file, :force => {:excel => options[:if_closed]}, :read_only => false)
532
+ open(file, :force => {:excel => opts[:if_closed]}, :read_only => do_not_write)
528
533
  end
529
534
  yield book
530
535
  ensure
531
536
  if book && book.alive?
532
- unless book.saved
533
- book.save unless options[:read_only]
534
- book.Saved = true if (was_saved || (not was_open)) && options[:read_only]
535
- book.Saved = false if (not was_saved) && (not options[:read_only]) && was_open
536
- end
537
+ book.save unless book.saved || do_not_write || book.ReadOnly
537
538
  if was_open
538
- if (not was_writable) && (not options[:read_only]) && options[:readonly_excel]
539
+ if opts[:rw_change_excel]==book.excel && change_rw_mode
539
540
  book.close
540
- open(file, :force => {:excel => book.excel}, :if_obstructed => :new_excel, :read_only => true)
541
- end
541
+ book = open(file, :force => {:excel => opts[:rw_change_excel]}, :read_only => (not was_writable))
542
+ end
542
543
  book.excel.calculation = was_calculation
543
544
  book.CheckCompatibility = was_check_compatibility
544
545
  #book.visible = was_visible # not necessary
545
- end
546
-
547
- book.close unless was_open || options[:keep_open]
546
+ end
547
+ book.Saved = (was_saved || (not was_open))
548
+ book.close unless was_open || opts[:keep_open]
548
549
  end
549
550
  end
550
551
  end
@@ -552,7 +553,9 @@ module RobustExcelOle
552
553
  # reopens a closed workbook
553
554
  # @options options
554
555
  def reopen(options = { })
555
- self.class.open(@stored_filename, options)
556
+ book = self.class.open(@stored_filename, options)
557
+ raise WorkbookREOError("cannot reopen book") unless book && book.alive?
558
+ book
556
559
  end
557
560
 
558
561
  # simple save of a workbook.
@@ -825,10 +828,6 @@ module RobustExcelOle
825
828
  unless opts[:read_only].nil?
826
829
  # if the ReadOnly status shall be changed, then close and reopen it
827
830
  if ((not writable) and (not opts[:read_only])) or (writable and opts[:read_only])
828
- #opts[:check_compatibility] = opts[:check_compatibility].nil? ? check_compatibility :
829
- # DEFAULT_OPEN_OPTS[:check_compatibility]
830
- #opts[:check_compatibility] = opts[:check_compatibility].nil? ? check_compatibility :
831
- # opts[:check_compatibility]
832
831
  opts[:check_compatibility] = check_compatibility if opts[:check_compatibility].nil?
833
832
  close(:if_unsaved => true)
834
833
  open_or_create_workbook(@stored_filename, opts)