Class: RobustExcelOle::Excel
- Inherits:
-
RangeOwners
- Object
- REOCommon
- RangeOwners
- RobustExcelOle::Excel
- Defined in:
- lib/robust_excel_ole/excel.rb
Overview
This class essentially wraps a Win32Ole Application object. You can apply all VBA methods (starting with a capital letter) that you would apply for an Application object. See docs.microsoft.com/en-us/office/vba/api/excel.application(object)#methods
Constant Summary collapse
- @@hwnd2excel =
{}
Instance Attribute Summary collapse
-
#calculation ⇒ Object
Returns the value of attribute calculation.
-
#created ⇒ Object
Returns the value of attribute created.
-
#displayalerts ⇒ Object
Returns the value of attribute displayalerts.
-
#ole_excel ⇒ Object
Returns the value of attribute ole_excel.
-
#screenupdating ⇒ Object
Returns the value of attribute screenupdating.
-
#visible ⇒ Object
setter methods are implemented below.
-
#workbook ⇒ Object
Returns the value of attribute workbook.
Class Method Summary collapse
-
.close_all(options = { :if_unsaved => :raise }, &blk) ⇒ Integer
closes all Excel instances remark: the returned number of closed Excel instances is valid only for known Excel instances if there are unknown Excel instances (opened not via this class), then they are counted as 1 options: :if_unsaved if unsaved workbooks are open in an Excel instance :raise (default) -> raises an exception :save -> saves the workbooks before closing :forget -> closes the excel instance without saving the workbooks :alert -> give control to Excel.
- .contains_unsaved_workbooks? ⇒ Boolean
-
.create(options = {}) ⇒ Excel
creates a new Excel instance.
-
.current(options = {}) ⇒ Excel
connects to the current (first opened) Excel instance, if such a running Excel instance exists returns a new Excel instance, otherwise.
- .excels_number ⇒ Object
-
.free_all_ole_objects ⇒ Object
frees all OLE objects in the object space.
-
.hwnd2excel(hwnd) ⇒ Object
:nodoc:.
- .init ⇒ Object
-
.kill_all ⇒ Integer
kill all Excel instances.
-
.known_excel_instances ⇒ Object
returns all Excel objects for all Excel instances opened with RobustExcelOle,.
-
.new(win32ole_excel = nil, options = {}) ⇒ Excel
returns an Excel instance options: :reuse connects to an already running Excel instance (true) or creates a new Excel instance (false) (default: true) :visible makes the Excel visible (default: false) :displayalerts enables or disables DisplayAlerts (true, false, :if_visible (default)) :calculation calculation mode is being forced to be manual (:manual) or automatic (:automtic) or is not being forced (default: nil) :screenupdating turns on or off screen updating (default: true).
-
.print_hwnd2excel ⇒ Object
:nodoc:.
-
.unsaved_known_workbooks ⇒ Object
returns unsaved workbooks in known (not opened by user) Excel instances.
-
.workbook_class ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#==(other_excel) ⇒ Object
returns true, if the Excel instances are alive and identical, false otherwise.
-
#[](name) ⇒ Object
returns the value of a range.
-
#[]=(name, value) ⇒ Object
sets the value of a range.
-
#alive? ⇒ Boolean
returns true, if the Excel instances responds to VBA methods, false otherwise.
-
#Calculation=(calculation_vba_mode) ⇒ Object
VBA method overwritten.
-
#close(options = { :if_unsaved => :raise }) ⇒ Object
closes the Excel.
-
#close_workbooks(options = { :if_unsaved => :raise }) ⇒ Object
closes workbooks.
-
#excel ⇒ Object
:nodoc:.
- #focus ⇒ Object
-
#for_all_workbooks(options) ⇒ Object
set options in all workbooks.
-
#for_this_instance(options) ⇒ Object
set options in this Excel instance.
-
#generate_workbook(file_name) ⇒ Object
generates, saves, and closes empty workbook.
-
#hwnd ⇒ Object
:nodoc:.
-
#initialize(options = {}) ⇒ Excel
constructor
A new instance of Excel.
-
#inspect ⇒ Object
:nodoc:.
-
#print_workbooks ⇒ Object
:nodoc:.
-
#recreate(opts = {}) ⇒ Excel
reopens a closed Excel instance options: reopen_workbooks (default: false): reopen the workbooks in the Excel instances :visible (default: false), :displayalerts (default: :if_visible), :calculation (default: false).
-
#retain_saved_workbooks ⇒ Object
retain the saved status of all workbooks.
- #set_options(options) ⇒ Object
-
#to_s ⇒ Object
:nodoc:.
-
#unsaved_workbooks ⇒ Object
returns unsaved workbooks (win32ole objects).
-
#with_calculation(calculation_mode) ⇒ Object
sets calculation mode in a block.
-
#with_displayalerts(displayalerts_value) ⇒ Object
sets DisplayAlerts in a block.
-
#workbook_class ⇒ Object
:nodoc:.
Methods inherited from RangeOwners
#add_name, #delete_name, #name2range, #nameval, #namevalue, #namevalue_glob, #range, #rangeval, #rename_range, #set_name, #set_nameval, #set_namevalue, #set_namevalue_glob, #set_rangeval
Methods inherited from REOCommon
#own_methods, puts_hash, tr1, trace
Constructor Details
#initialize(options = {}) ⇒ Excel
Returns a new instance of Excel.
107 |
# File 'lib/robust_excel_ole/excel.rb', line 107 def initialize( = {}) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
:nodoc:
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/robust_excel_ole/excel.rb', line 664 def method_missing(name, *args) # :nodoc: if name.to_s[0,1] =~ /[A-Z]/ begin raise ObjectNotAlive, 'method missing: Excel not alive' unless alive? @ole_excel.send(name, *args) rescue WIN32OLERuntimeError => msg if msg. =~ /unknown property or method/ raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}" else raise msg end end else super end end |
Instance Attribute Details
#calculation ⇒ Object
Returns the value of attribute calculation
25 26 27 |
# File 'lib/robust_excel_ole/excel.rb', line 25 def calculation @calculation end |
#created ⇒ Object
Returns the value of attribute created
19 20 21 |
# File 'lib/robust_excel_ole/excel.rb', line 19 def created @created end |
#displayalerts ⇒ Object
Returns the value of attribute displayalerts
24 25 26 |
# File 'lib/robust_excel_ole/excel.rb', line 24 def displayalerts @displayalerts end |
#ole_excel ⇒ Object
Returns the value of attribute ole_excel
18 19 20 |
# File 'lib/robust_excel_ole/excel.rb', line 18 def ole_excel @ole_excel end |
#screenupdating ⇒ Object
Returns the value of attribute screenupdating
26 27 28 |
# File 'lib/robust_excel_ole/excel.rb', line 26 def screenupdating @screenupdating end |
#visible ⇒ Object
setter methods are implemented below
23 24 25 |
# File 'lib/robust_excel_ole/excel.rb', line 23 def visible @visible end |
#workbook ⇒ Object
Returns the value of attribute workbook
20 21 22 |
# File 'lib/robust_excel_ole/excel.rb', line 20 def workbook @workbook end |
Class Method Details
.close_all(options = { :if_unsaved => :raise }, &blk) ⇒ Integer
closes all Excel instances remark: the returned number of closed Excel instances is valid only for known Excel instances if there are unknown Excel instances (opened not via this class), then they are counted as 1 options:
:if_unsaved if unsaved workbooks are open in an Excel instance
:raise (default) -> raises an exception
:save -> saves the workbooks before closing
:forget -> closes the excel instance without saving the workbooks
:alert -> give control to Excel
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/robust_excel_ole/excel.rb', line 250 def self.close_all( = { :if_unsaved => :raise }, &blk) [:if_unsaved] = blk if blk finished_number = error_number = overall_number = 0 first_error = nil finishing_action = proc do |excel| if excel begin overall_number += 1 finished_number += excel.close(:if_unsaved => [:if_unsaved]) rescue first_error = $! #trace "error when finishing #{$!}" error_number += 1 end end end # known Excel-instances @@hwnd2excel.each do |hwnd, wr_excel| if wr_excel.weakref_alive? excel = wr_excel.__getobj__ if excel.alive? excel.displayalerts = false finishing_action.call(excel) end else @@hwnd2excel.delete(hwnd) end end # unknown Excel-instances old_error_number = error_number 9.times do |_index| sleep 0.1 excel = begin new(WIN32OLE.connect('Excel.Application')) rescue nil end finishing_action.call(excel) if excel free_all_ole_objects unless (error_number > 0) && ([:if_unsaved] == :raise) break unless excel break if error_number > old_error_number # + 3 end raise first_error if (([:if_unsaved] == :raise) && first_error) || (first_error.class == OptionInvalid) [finished_number, error_number] end |
.contains_unsaved_workbooks? ⇒ Boolean
181 182 183 |
# File 'lib/robust_excel_ole/excel.rb', line 181 def self.contains_unsaved_workbooks? !Excel.current.unsaved_workbooks.empty? end |
.create(options = {}) ⇒ Excel
creates a new Excel instance
39 40 41 |
# File 'lib/robust_excel_ole/excel.rb', line 39 def self.create( = {}) new(.merge(:reuse => false)) end |
.current(options = {}) ⇒ Excel
connects to the current (first opened) Excel instance, if such a running Excel instance exists returns a new Excel instance, otherwise
50 51 52 |
# File 'lib/robust_excel_ole/excel.rb', line 50 def self.current( = {}) new(.merge(:reuse => true)) end |
.excels_number ⇒ Object
397 398 399 400 |
# File 'lib/robust_excel_ole/excel.rb', line 397 def self.excels_number processes = WIN32OLE.connect('winmgmts:\\\\.').InstancesOf('win32_process') processes.select { |p| p.name == 'EXCEL.EXE' }.size end |
.free_all_ole_objects ⇒ Object
frees all OLE objects in the object space
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/robust_excel_ole/excel.rb', line 355 def self.free_all_ole_objects anz_objekte = 0 ObjectSpace.each_object(WIN32OLE) do |o| anz_objekte += 1 # trace "#{anz_objekte} name: #{(o.Name rescue (o.Count rescue "no_name"))} ole_object_name: #{(o.ole_object_name rescue nil)} type: #{o.ole_type rescue nil}" # trace [:Name, (o.Name rescue (o.Count rescue "no_name"))] # trace [:ole_object_name, (o.ole_object_name rescue nil)] # trace [:methods, (o.ole_methods rescue nil)] unless (o.Name rescue false) # trace o.ole_type rescue nil begin o.ole_free # trace "olefree OK" rescue # trace "olefree_error: #{$!}" # trace $!.backtrace.first(9).join "\n" end end # trace "went through #{anz_objekte} OLE objects" end |
.hwnd2excel(hwnd) ⇒ Object
:nodoc:
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/robust_excel_ole/excel.rb', line 437 def self.hwnd2excel(hwnd) # :nodoc: excel_weakref = @@hwnd2excel[hwnd] if excel_weakref if excel_weakref.weakref_alive? excel_weakref.__getobj__ else trace 'dead reference to an Excel' begin @@hwnd2excel.delete(hwnd) nil rescue trace "Warning: deleting dead reference failed! (hwnd: #{hwnd.inspect})" end end end end |
.init ⇒ Object
375 376 377 |
# File 'lib/robust_excel_ole/excel.rb', line 375 def self.init @@hwnd2excel = {} end |
.kill_all ⇒ Integer
kill all Excel instances
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/robust_excel_ole/excel.rb', line 381 def self.kill_all number = 0 WIN32OLE.connect('winmgmts:\\\\.').InstancesOf('win32_process').each do |p| begin if p.name == 'EXCEL.EXE' Process.kill('KILL', p.processid) number += 1 end rescue # trace "kill error: #{$!}" end end init number end |
.known_excel_instances ⇒ Object
returns all Excel objects for all Excel instances opened with RobustExcelOle,
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/robust_excel_ole/excel.rb', line 403 def self.known_excel_instances pid2excel = {} @@hwnd2excel.each do |hwnd,wr_excel| next unless wr_excel.weakref_alive? excel = wr_excel.__getobj__ process_id = Win32API.new('user32', 'GetWindowThreadProcessId', %w[I P], 'I') pid_puffer = ' ' * 32 process_id.call(hwnd, pid_puffer) pid = pid_puffer.unpack('L')[0] pid2excel[pid] = excel end processes = WIN32OLE.connect('winmgmts:\\\\.').InstancesOf('win32_process') # excel_processes = processes.select{ |p| p.name == "EXCEL.EXE" && pid2excel.include?(p.processid)} # excel_processes.map{ |p| Excel.new(pid2excel[p.processid]) } processes.select { |p| Excel.new(pid2excel[p.processid]) if p.name == 'EXCEL.EXE' && pid2excel.include?(p.processid) } result = [] processes.each do |p| next unless p.name == 'EXCEL.EXE' if pid2excel.include?(p.processid) excel = pid2excel[p.processid] result << excel end # how to connect to an (interactively opened) Excel instance and get a WIN32OLE object? # after that, lift it to an Excel object end result end |
.new(win32ole_excel = nil, options = {}) ⇒ Excel
returns an Excel instance options:
:reuse connects to an already running Excel instance (true) or
creates a new Excel instance (false) (default: true)
:visible makes the Excel visible (default: false)
:displayalerts enables or disables DisplayAlerts (true, false, :if_visible (default))
:calculation calculation mode is being forced to be manual (:manual) or automatic (:automtic)
or is not being forced (default: nil)
:screenupdating turns on or off screen updating (default: true)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/robust_excel_ole/excel.rb', line 71 def self.new(win32ole_excel = nil, = {}) if win32ole_excel.is_a? Hash = win32ole_excel win32ole_excel = nil end ole_xl = win32ole_excel unless win32ole_excel.nil? = { :reuse => true }.merge() ole_xl = current_excel if [:reuse] == true ole_xl ||= WIN32OLE.new('Excel.Application') hwnd = ole_xl.HWnd stored = hwnd2excel(hwnd) if stored && stored.alive? result = stored else result = super() result.instance_variable_set(:@ole_excel, ole_xl) WIN32OLE.const_load(ole_xl, RobustExcelOle) unless RobustExcelOle.const_defined?(:CONSTANTS) @@hwnd2excel[hwnd] = WeakRef.new(result) end unless .is_a? WIN32OLE begin reused = [:reuse] && stored && stored.alive? unless reused = { :displayalerts => :if_visible, :visible => false, :screenupdating => true }.merge() end result.visible = [:visible] unless [:visible].nil? result.displayalerts = [:displayalerts] unless [:displayalerts].nil? result.calculation = [:calculation] unless [:calculation].nil? result.screenupdating = [:screenupdating] unless [:screenupdating].nil? result.created = !reused end end result end |
.print_hwnd2excel ⇒ Object
:nodoc:
460 461 462 463 464 465 466 |
# File 'lib/robust_excel_ole/excel.rb', line 460 def self.print_hwnd2excel @@hwnd2excel.each do |hwnd,wr_excel| excel_string = (wr_excel.weakref_alive? ? wr_excel.__getobj__.to_s : 'weakref not alive') printf("hwnd: %8i => excel: %s\n", hwnd, excel_string) end @@hwnd2excel.size end |
.unsaved_known_workbooks ⇒ Object
returns unsaved workbooks in known (not opened by user) Excel instances
483 484 485 486 487 488 489 490 |
# File 'lib/robust_excel_ole/excel.rb', line 483 def self.unsaved_known_workbooks result = [] @@hwnd2excel.each do |_hwnd,wr_excel| excel = wr_excel.__getobj__ if wr_excel.weakref_alive? result << excel.unsaved_workbooks end result end |
.workbook_class ⇒ Object
:nodoc:
647 648 649 650 651 652 653 654 |
# File 'lib/robust_excel_ole/excel.rb', line 647 def self.workbook_class @workbook_class ||= begin module_name = parent_name "#{module_name}::Workbook".constantize rescue NameError => e Workbook end end |
Instance Method Details
#==(other_excel) ⇒ Object
returns true, if the Excel instances are alive and identical, false otherwise
469 470 471 |
# File 'lib/robust_excel_ole/excel.rb', line 469 def == other_excel self.Hwnd == other_excel.Hwnd if other_excel.is_a?(Excel) && alive? && other_excel.alive? end |
#[](name) ⇒ Object
returns the value of a range
628 629 630 |
# File 'lib/robust_excel_ole/excel.rb', line 628 def [] name namevalue_glob(name) end |
#[]=(name, value) ⇒ Object
sets the value of a range
635 636 637 |
# File 'lib/robust_excel_ole/excel.rb', line 635 def []=(name, value) set_namevalue_glob(name,value, :color => 42) # 42 - aqua-marin, 7-green end |
#alive? ⇒ Boolean
returns true, if the Excel instances responds to VBA methods, false otherwise
474 475 476 477 478 479 480 |
# File 'lib/robust_excel_ole/excel.rb', line 474 def alive? @ole_excel.Name true rescue # trace $!.message false end |
#Calculation=(calculation_vba_mode) ⇒ Object
VBA method overwritten
568 569 570 571 572 573 574 575 576 |
# File 'lib/robust_excel_ole/excel.rb', line 568 def Calculation= calculation_vba_mode case calculation_vba_mode when XlCalculationManual @calculation = :manual when XlCalculationAutomatic @calculation = :automatic end @ole_excel.Calculation = calculation_vba_mode end |
#close(options = { :if_unsaved => :raise }) ⇒ Object
closes the Excel
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/robust_excel_ole/excel.rb', line 309 def close( = { :if_unsaved => :raise }) finishing_living_excel = alive? if finishing_living_excel hwnd = (begin @ole_excel.HWnd rescue nil end) close_workbooks(:if_unsaved => [:if_unsaved]) @ole_excel.Quit if false && defined?(weak_wkbks) && weak_wkbks.weakref_alive? weak_wkbks.ole_free end weak_xl = WeakRef.new(@ole_excel) else weak_xl = nil end @ole_excel = nil GC.start sleep 0.1 if finishing_living_excel if hwnd process_id = Win32API.new('user32', 'GetWindowThreadProcessId', %w[I P], 'I') pid_puffer = ' ' * 32 process_id.call(hwnd, pid_puffer) pid = pid_puffer.unpack('L')[0] begin Process.kill('KILL', pid) rescue # trace "kill_error: #{$!}" end end @@hwnd2excel.delete(hwnd) if weak_xl.weakref_alive? # if WIN32OLE.ole_reference_count(weak_xlapp) > 0 begin weak_xl.ole_free rescue # trace "weakref_probl_olefree" end end end weak_xl ? 1 : 0 end |
#close_workbooks(options = { :if_unsaved => :raise }) ⇒ Object
closes workbooks
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/robust_excel_ole/excel.rb', line 203 def close_workbooks( = { :if_unsaved => :raise }) return unless alive? weak_wkbks = @ole_excel.Workbooks unless unsaved_workbooks.empty? case [:if_unsaved] when Proc [:if_unsaved].call(self, unsaved_workbooks) when :raise raise UnsavedWorkbooks, 'Excel contains unsaved workbooks' when :alert # nothing when :forget unsaved_workbooks.each { |m| m.Saved = true } when :save unsaved_workbooks.each { |m| m.Save } else raise OptionInvalid, ":if_unsaved: invalid option: #{options[:if_unsaved].inspect}" end end begin @ole_excel.Workbooks.Close rescue WIN32OLERuntimeError => msg if msg. =~ /800A03EC/ raise ExcelREOError, 'user canceled or runtime error' else raise UnexpectedREOError, "unknown WIN32OLERuntimeError: #{msg.message}" end end weak_wkbks = nil weak_wkbks = @ole_excel.Workbooks weak_wkbks = nil end |
#excel ⇒ Object
:nodoc:
433 434 435 |
# File 'lib/robust_excel_ole/excel.rb', line 433 def excel self end |
#focus ⇒ Object
616 617 618 619 620 621 622 623 |
# File 'lib/robust_excel_ole/excel.rb', line 616 def focus self.visible = true # if not Windows10 then Win32API.new('user32','SetForegroundWindow','I','I').call(@ole_excel.Hwnd) # else # Win32API.new("user32","SetForegroundWindow","","I").call # end end |
#for_all_workbooks(options) ⇒ Object
set options in all workbooks
601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/robust_excel_ole/excel.rb', line 601 def for_all_workbooks() ole_workbooks = begin @ole_excel.Workbooks rescue WIN32OLERuntimeError => msg if msg. =~ /failed to get Dispatch Interface/ raise ExcelDamaged, 'Excel instance not alive or damaged' else raise ExcelREOError, 'workbooks could not be determined' end end ole_workbooks.each do |ole_workbook| workbook_class.open(ole_workbook).for_this_workbook() end end |
#for_this_instance(options) ⇒ Object
set options in this Excel instance
592 593 594 |
# File 'lib/robust_excel_ole/excel.rb', line 592 def for_this_instance() self.class.new(@ole_excel, ) end |
#generate_workbook(file_name) ⇒ Object
generates, saves, and closes empty workbook
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/robust_excel_ole/excel.rb', line 497 def generate_workbook file_name raise FileNameNotGiven, 'filename is nil' if file_name.nil? self.Workbooks.Add empty_workbook = self.Workbooks.Item(self.Workbooks.Count) filename = General.absolute_path(file_name).tr('/','\\') unless File.exist?(filename) begin empty_workbook.SaveAs(filename) rescue WIN32OLERuntimeError => msg # if msg.message =~ /SaveAs/ and msg.message =~ /Workbook/ then raise FileNotFound, "could not save workbook with filename #{file_name.inspect}" # else # # todo some time: find out when this occurs : # raise UnexpectedREOError, "unknown WIN32OLERuntimeError with filename #{file_name.inspect}: \n#{msg.message}" # end end end empty_workbook end |
#hwnd ⇒ Object
:nodoc:
454 455 456 457 458 |
# File 'lib/robust_excel_ole/excel.rb', line 454 def hwnd self.Hwnd rescue nil end |
#inspect ⇒ Object
:nodoc:
643 644 645 |
# File 'lib/robust_excel_ole/excel.rb', line 643 def inspect to_s end |
#print_workbooks ⇒ Object
:nodoc:
492 493 494 |
# File 'lib/robust_excel_ole/excel.rb', line 492 def print_workbooks self.Workbooks.each { |w| trace "#{w.Name} #{w}" } end |
#recreate(opts = {}) ⇒ Excel
reopens a closed Excel instance options: reopen_workbooks (default: false): reopen the workbooks in the Excel instances :visible (default: false), :displayalerts (default: :if_visible), :calculation (default: false)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/robust_excel_ole/excel.rb', line 118 def recreate(opts = {}) unless alive? opts = { :visible => @visible || false, :displayalerts => @displayalerts || :if_visible }.merge(opts) @ole_excel = WIN32OLE.new('Excel.Application') self.visible = opts[:visible] self.displayalerts = opts[:displayalerts] self.calculation = opts[:calculation] if opts[:reopen_workbooks] books = workbook_class.books books.each do |book| book.reopen if !book.alive? && book.excel.alive? && book.excel == self end end end self end |
#retain_saved_workbooks ⇒ Object
retain the saved status of all workbooks
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/robust_excel_ole/excel.rb', line 167 def retain_saved_workbooks saved = [] @ole_excel.Workbooks.each { |w| saved << w.Saved } begin yield self ensure i = 0 @ole_excel.Workbooks.each do |w| w.Saved = saved[i] if saved[i] i += 1 end end end |
#set_options(options) ⇒ Object
596 597 598 |
# File 'lib/robust_excel_ole/excel.rb', line 596 def () for_this_instance() end |
#to_s ⇒ Object
:nodoc:
639 640 641 |
# File 'lib/robust_excel_ole/excel.rb', line 639 def to_s '#<Excel: ' + hwnd.to_s + ('not alive' unless alive?).to_s + '>' end |
#unsaved_workbooks ⇒ Object
returns unsaved workbooks (win32ole objects)
186 187 188 189 190 191 192 193 194 |
# File 'lib/robust_excel_ole/excel.rb', line 186 def unsaved_workbooks unsaved_workbooks = [] begin @ole_excel.Workbooks.each { |w| unsaved_workbooks << w unless w.Saved || w.ReadOnly } rescue RuntimeError => msg raise ExcelDamaged, 'Excel instance not alive or damaged' if msg. =~ /failed to get Dispatch Interface/ end unsaved_workbooks end |
#with_calculation(calculation_mode) ⇒ Object
sets calculation mode in a block
579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/robust_excel_ole/excel.rb', line 579 def with_calculation(calculation_mode) return unless calculation_mode old_calculation_mode = @ole_excel.Calculation begin self.calculation = calculation_mode yield self ensure @ole_excel.Calculation = old_calculation_mode if @ole_excel.Calculation.is_a?(Integer) end end |
#with_displayalerts(displayalerts_value) ⇒ Object
sets DisplayAlerts in a block
519 520 521 522 523 524 525 526 527 |
# File 'lib/robust_excel_ole/excel.rb', line 519 def with_displayalerts displayalerts_value old_displayalerts = displayalerts self.displayalerts = displayalerts_value begin yield self ensure self.displayalerts = old_displayalerts if alive? end end |
#workbook_class ⇒ Object
:nodoc:
656 657 658 |
# File 'lib/robust_excel_ole/excel.rb', line 656 def workbook_class self.class.workbook_class end |