Class: RobustExcelOle::Workbook

Inherits:
RangeOwners show all
Defined in:
lib/robust_excel_ole/workbook.rb

Overview

This class essentially wraps a Win32Ole Workbook object. You can apply all VBA methods (starting with a capital letter) that you would apply for a Workbook object. See docs.microsoft.com/en-us/office/vba/api/excel.workbook#methods

Defined Under Namespace

Classes: AlreadyManaged

Constant Summary collapse

CORE_DEFAULT_OPEN_OPTS =
{
  default: {excel: :current}, 
  force: {},
  update_links: :never 
}.freeze
DEFAULT_OPEN_OPTS =
{
  if_unsaved: :raise,
  if_obstructed: :raise,
  if_absent: :raise,
  if_exists: :raise
}.merge(CORE_DEFAULT_OPEN_OPTS).freeze
ABBREVIATIONS =
[
  [:default,:d],
  [:force, :f],
  [:excel, :e],
  [:visible, :v],
  [:if_obstructed, :if_blocked]
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RangeOwners

#add_name, #delete_name, #name2range, #names, #namevalue_global, #rename_name, #set_namevalue_global

Methods inherited from VbaObjects

#to_reo

Constructor Details

#initialize(file_or_workbook, opts) ⇒ Workbook

creates a new Workbook object, if a file name is given Promotes the win32ole workbook to a Workbook object, if a win32ole-workbook is given

Parameters:

  • file_or_workbook (Variant)

    file name or workbook

  • opts (Hash)

Options Hash (opts):

  • see (Symbol)

    above



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/robust_excel_ole/workbook.rb', line 158

def initialize(file_or_workbook, opts)
  if file_or_workbook.is_a? WIN32OLE
    @ole_workbook = file_or_workbook
    ole_excel = begin 
      @ole_workbook.Application
    rescue WIN32OLERuntimeError
      raise ExcelREOError, "could not determine the Excel instance\n#{$!.message}"
    end
    @excel = excel_class.new(ole_excel)
    file_name = @ole_workbook.Fullname.tr('\\','/') 
  else
    file_name = file_or_workbook
    ensure_workbook(file_name, opts)        
  end      
  apply_options(file_name, opts)
  store_myself
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

Raises:

  • (ObjectNotAlive)


1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'lib/robust_excel_ole/workbook.rb', line 1184

def method_missing(name, *args) 
  super unless name.to_s[0,1] =~ /[A-Z]/
  raise ObjectNotAlive, 'method missing: workbook not alive' unless alive?
  if ::ERRORMESSAGE_JRUBY_BUG 
    begin
      @ole_workbook.send(name, *args)
    rescue Java::OrgRacobCom::ComFailException 
      raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
    end
  else
    begin
      @ole_workbook.send(name, *args)
    rescue NoMethodError 
      raise VBAMethodMissingError, "unknown VBA property or method #{name.inspect}"
    end
  end
end

Instance Attribute Details

#excelObject (readonly)

Returns the value of attribute excel



17
18
19
# File 'lib/robust_excel_ole/workbook.rb', line 17

def excel
  @excel
end

#ole_workbookObject (readonly)

Returns the value of attribute ole_workbook



16
17
18
# File 'lib/robust_excel_ole/workbook.rb', line 16

def ole_workbook
  @ole_workbook
end

#stored_filenameObject (readonly)

Returns the value of attribute stored_filename



18
19
20
# File 'lib/robust_excel_ole/workbook.rb', line 18

def stored_filename
  @stored_filename
end

Class Method Details

.close(file, opts = {if_unsaved: :raise}) ⇒ Object

closes a given file if it is open



850
851
852
853
854
855
856
857
# File 'lib/robust_excel_ole/workbook.rb', line 850

def self.close(file, opts = {if_unsaved: :raise})
  book = begin
    bookstore.fetch(file)
    rescue
      nil
    end
  book.close(opts) if book && book.alive?
end

.create(file_name, opts = { }) ⇒ Object

creates, i.e., opens a new, empty workbook, and saves it under a given filename

Parameters:

  • filename (String)

    the filename under which the new workbook should be saved

  • opts (Hash) (defaults to: { })

    the options as in Workbook::open



555
556
557
# File 'lib/robust_excel_ole/workbook.rb', line 555

def self.create(file_name, opts = { })
  open(file_name, if_absent: :create)
end

.for_modifying(arg, opts = { }, &block) ⇒ Object



629
630
631
# File 'lib/robust_excel_ole/workbook.rb', line 629

def self.for_modifying(arg, opts = { }, &block)
  unobtrusively(arg, {writable: true}.merge(opts), &block)
end

.for_reading(arg, opts = { }, &block) ⇒ Object



625
626
627
# File 'lib/robust_excel_ole/workbook.rb', line 625

def self.for_reading(arg, opts = { }, &block)
  unobtrusively(arg, {writable: false}.merge(opts), &block)
end

.new(file_or_workbook, opts = { }) ⇒ Workbook

opens a workbook. options: :default : if the workbook was already open before, then use (unchange) its properties,

otherwise, i.e. if the workbook cannot be reopened, use the properties stated in :default

:force : no matter whether the workbook was already open before, use the properties stated in :force :default and :force contain: :excel

:excel   :current (or :active or :reuse)
                  -> connects to a running (the first opened) Excel instance,
                     excluding the hidden Excel instance, if it exists,
                     otherwise opens in a new Excel instance.
         :new     -> opens in a new Excel instance
         <excel-instance> -> opens in the given Excel instance
:visible true, false, or nil (default)
alternatives: :default_excel, :force_excel, :visible, :d, :f, :e, :v

:if_unsaved if an unsaved workbook with the same name is open, then

:raise               -> raise an exception
:forget              -> close the unsaved workbook, re-open the workbook
:accept              -> let the unsaved workbook open
:alert or :excel     -> give control to Excel
:new_excel           -> open the workbook in a new Excel instance

:if_obstructed if a workbook with the same name in a different path is open, then or :raise -> raise an exception :if_blocked :forget -> close the workbook, re-open the workbook

:accept              -> let the blocked workbook open
:save                -> save the blocked workbook, close it, re-open the workbook
:close_if_saved      -> close the blocked workbook and re-open the workbook, if the blocked workbook is saved,
                        otherwise raise an exception.
:new_excel           -> open the workbook in a new Excel instance

:if_absent :raise -> raise an exception , if the file does not exists

:create              -> create a new Excel file, if it does not exists

:read_only true -> open in read-only mode :visible true -> make the workbook visible :check_compatibility true -> check compatibility when saving :update_links true -> user is being asked how to update links, false -> links are never updated

Parameters:

  • file_or_workbook (String, Pathname)

    a file name (string or pathname) or WIN32OLE workbook

  • opts (Hash) (defaults to: { })

    the options

Options Hash (opts):

  • :default (Hash)

    or :d

  • :force (Hash)

    or :f

  • :if_unsaved (Symbol)

    :raise (default), :forget, :save, :accept, :alert, :excel, or :new_excel

  • :if_blocked (Symbol)

    :raise (default), :forget, :save, :close_if_saved, or _new_excel

  • :if_absent (Symbol)

    :raise (default) or :create

  • :read_only (Boolean)

    true (default) or false

  • :update_links (Boolean)

    :never (default), :always, :alert

  • :calculation (Boolean)

    :manual, :automatic, or nil (default)

Returns:

  • (Workbook)

    a representation of a workbook



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/robust_excel_ole/workbook.rb', line 91

def self.new(file_or_workbook, opts = { })
  process_options(opts)
  case file_or_workbook
  when NilClass
    raise FileNameNotGiven, "filename is nil"
  when WIN32OLE
    begin
      file_or_workbook.send(:LinkSources)
      file = file_or_workbook.Fullname.tr('\\','/')
    rescue
      raise TypeREOError, "given win32ol object is not a workbook"
    end
  when Workbook
    file = file_or_workbook.Fullname.tr('\\','/')
  when String
    file = file_or_workbook
    raise FileNotFound, "file #{General.absolute_path(file).inspect} is a directory" if File.directory?(file)
  when ->(n){ n.respond_to? :to_path }
    file = file_or_workbook.to_path
    raise FileNotFound, "file #{General.absolute_path(file).inspect} is a directory" if File.directory?(file)
  else
    raise TypeREOError, "given object is neither a filename, a Win32ole, nor a Workbook object"
  end
  # try to fetch the workbook from the bookstore
  set_was_open opts, file_or_workbook.is_a?(WIN32OLE)
  book = nil
  if opts[:force][:excel] != :new
    # if readonly is true, then prefer a book that is given in force_excel if this option is set              
    forced_excel = begin
      (opts[:force][:excel].nil? || opts[:force][:excel] == :current) ? 
        (excel_class.new(reuse: true) if !::CONNECT_JRUBY_BUG) : opts[:force][:excel].to_reo.excel
    rescue NoMethodError
      raise TypeREOError, "provided Excel option value is neither an Excel object nor a valid option"
    end
    begin
      book = if File.exist?(file)
        bookstore.fetch(file, prefer_writable: !(opts[:read_only]),
                              prefer_excel: (opts[:read_only] ? forced_excel : nil))
      end
    rescue
      raise
      #trace "#{$!.message}"
    end
    if book 
      set_was_open opts, book.alive?
      # drop the fetched workbook if it shall be opened in another Excel instance
      # or the workbook is an unsaved workbook that should not be accepted
      if (opts[:force][:excel].nil? || opts[:force][:excel] == :current || forced_excel == book.excel) &&
        !(book.alive? && !book.saved && (opts[:if_unsaved] != :accept))
        opts[:force][:excel] = book.excel if book.excel && book.excel.alive?
        book.ensure_workbook(file,opts)
        book.send :apply_options, file, opts
        return book
      end
    end
  end        
  super(file_or_workbook, opts)
end

.process_options(opts, proc_opts = {use_defaults: true}) ⇒ Object



193
194
195
196
197
198
# File 'lib/robust_excel_ole/workbook.rb', line 193

def self.process_options(opts, proc_opts = {use_defaults: true})
  translate(opts)
  default_opts = (proc_opts[:use_defaults] ? DEFAULT_OPEN_OPTS : CORE_DEFAULT_OPEN_OPTS).dup
  translate(default_opts)
  opts.merge!(default_opts) { |key, v1, v2| !v2.is_a?(Hash) ? v1 : v2.merge(v1 || {}) }
end

.save(file) ⇒ Object

saves a given file if it is open



860
861
862
863
# File 'lib/robust_excel_ole/workbook.rb', line 860

def self.save(file)
  book = bookstore.fetch(file) rescue nil
  book.save if book && book.alive?
end

.save_as(file, new_file, opts = { }) ⇒ Object

saves a given file under a new name if it is open



866
867
868
869
870
871
872
873
# File 'lib/robust_excel_ole/workbook.rb', line 866

def self.save_as(file, new_file, opts = { })
  book = begin
    bookstore.fetch(file)
  rescue 
    nil
  end
  book.save_as(new_file, opts) if book && book.alive?
end

.set_was_open(hash, value) ⇒ Object



185
186
187
# File 'lib/robust_excel_ole/workbook.rb', line 185

def self.set_was_open(hash, value)
  hash[:was_open] = value if hash.has_key?(:was_open)
end

.translate(opts) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/robust_excel_ole/workbook.rb', line 200

def self.translate(opts)
  erg = {}
  opts.each do |key,value|
    new_key = key
    ABBREVIATIONS.each { |long,short| new_key = long if key == short }
    if value.is_a?(Hash)
      erg[new_key] = {}
      value.each do |k,v|
        new_k = k
        ABBREVIATIONS.each { |l,s| new_k = l if k == s }
        erg[new_key][new_k] = v
      end
    else
      erg[new_key] = value
    end
  end
  opts.merge!(erg)
  opts[:default] ||= {}
  opts[:force] ||= {}
  force_list = [:visible, :excel]
  opts.each { |key,value| opts[:force][key] = value if force_list.include?(key) }
  opts[:default][:excel] = opts[:default_excel] unless opts[:default_excel].nil?
  opts[:force][:excel] = opts[:force_excel] unless opts[:force_excel].nil?
  opts[:default][:excel] = :current if opts[:default][:excel] == :reuse || opts[:default][:excel] == :active
  opts[:force][:excel] = :current if opts[:force][:excel] == :reuse || opts[:force][:excel] == :active
end

.unobtrusively(file_or_workbook, opts = { }, &block) ⇒ Workbook

allows to read or modify a workbook such that its state remains unchanged state comprises: open, saved, writable, visible, calculation mode, check compatibility

Parameters:

  • file_or_workbook (String)

    a file name or WIN32OLE workbook

  • opts (Hash) (defaults to: { })

    the options

Options Hash (opts):

  • :read_only (Boolean)

    true/false (default), force to open the workbook in read-only/read-write mode

  • :writable (Boolean)

    true (default)/false changes of the workbook shall be saved/not saved, and the workbook is being opened in read-only/read-write mode by default (when the workbook was not open before)

  • :keep_open (Boolean)

    whether the workbook shall be kept open after unobtrusively opening (default: false)

  • :if_closed (Variant)

    :current (default), :new or an Excel instance

Returns:



644
645
646
647
# File 'lib/robust_excel_ole/workbook.rb', line 644

def self.unobtrusively(file_or_workbook, opts = { }, &block)
  file = (file_or_workbook.is_a? WIN32OLE) ? file_or_workbook.Fullname.tr('\\','/') : file_or_workbook
  unobtrusively_opening(file, opts, nil, &block)
end

Instance Method Details

#==(other_book) ⇒ Boolean

Returns true, if the full workbook names and excel Instances are identical, false otherwise.

Returns:

  • (Boolean)

    true, if the full workbook names and excel Instances are identical, false otherwise



1109
1110
1111
1112
1113
# File 'lib/robust_excel_ole/workbook.rb', line 1109

def == other_book
  other_book.is_a?(Workbook) &&
    @excel == other_book.excel &&
    self.filename == other_book.filename
end

#[](name) ⇒ Object

returns the value of a range

Parameters:

  • name (String)

    the name of a range



1000
1001
1002
# File 'lib/robust_excel_ole/workbook.rb', line 1000

def [] name
  namevalue_global(name)
end

#[]=(name, value) ⇒ Object

sets the value of a range

Parameters:

  • name (String)

    the name of the range

  • value (Variant)

    the contents of the range



1007
1008
1009
# File 'lib/robust_excel_ole/workbook.rb', line 1007

def []= (name, value)
  set_namevalue_global(name, value)   
end

#add_or_copy_sheet(sheet = nil, opts = { }) ⇒ Worksheet

copies a sheet to another position if a sheet is given, or adds an empty sheet default: copied or empty sheet is appended, i.e. added behind the last sheet

Parameters:

  • sheet (Worksheet) (defaults to: nil)

    a sheet that shall be copied (optional)

  • opts (Hash) (defaults to: { })

    the options

Options Hash (opts):

  • :as (Symbol)

    new name of the copied or added sheet

  • :before (Symbol)

    a sheet before which the sheet shall be inserted

  • :after (Symbol)

    a sheet after which the sheet shall be inserted

Returns:



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'lib/robust_excel_ole/workbook.rb', line 915

def add_or_copy_sheet(sheet = nil, opts = { })
  if sheet.is_a? Hash
    opts = sheet
    sheet = nil
  end
  begin
    sheet = sheet.to_reo unless sheet.nil?
    new_sheet_name = opts.delete(:as)
    last_sheet_local = last_sheet
    after_or_before, base_sheet = opts.to_a.first || [:after, last_sheet_local]
    base_sheet_ole = base_sheet.to_reo.ole_worksheet
    if !::COPYSHEETS_JRUBY_BUG          
      add_or_copy_sheet_simple(sheet, { after_or_before.to_s => base_sheet_ole })
    else
      if after_or_before == :before 
        add_or_copy_sheet_simple(sheet, base_sheet_ole)
      else
        if base_sheet.name != last_sheet_local.name
          add_or_copy_sheet_simple(sheet, base_sheet.Next)
        else
          add_or_copy_sheet_simple(sheet, base_sheet_ole)
          base_sheet.Move(ole_workbook.Worksheets.Item(ole_workbook.Worksheets.Count-1))
          ole_workbook.Worksheets.Item(ole_workbook.Worksheets.Count).Activate
        end
      end
    end
  rescue # WIN32OLERuntimeError, NameNotFound, Java::OrgRacobCom::ComFailException
    raise WorksheetREOError, "could not add given worksheet #{sheet.inspect}\n#{$!.message}"
  end
  new_sheet = worksheet_class.new(ole_workbook.Activesheet)
  new_sheet.name = new_sheet_name if new_sheet_name
  new_sheet
end

#add_sheet(sheet = nil, opts = { }) ⇒ Object

for compatibility to older versions



962
963
964
# File 'lib/robust_excel_ole/workbook.rb', line 962

def add_sheet(sheet = nil, opts = { })  # :deprecated: #
  add_or_copy_sheet(sheet, opts)
end

#alive?Boolean

returns true, if the workbook reacts to methods, false otherwise

Returns:

  • (Boolean)


1027
1028
1029
1030
1031
1032
1033
# File 'lib/robust_excel_ole/workbook.rb', line 1027

def alive?
  @ole_workbook.Name
  true
rescue
  @ole_workbook = nil  # dead object won't be alive again
  false
end

#calculationObject



1072
1073
1074
# File 'lib/robust_excel_ole/workbook.rb', line 1072

def calculation
  @excel.properties[:calculation] if @ole_workbook
end

#close(opts = {if_unsaved: :raise}) ⇒ Object

closes the workbook, if it is alive options:

:if_unsaved    if the workbook is unsaved
                    :raise           -> raise an exception
                    :save            -> save the workbook before it is closed
                    :forget          -> close the workbook
                    :keep_open       -> keep the workbook open
                    :alert or :excel -> give control to excel

Parameters:

  • opts (Hash) (defaults to: {if_unsaved: :raise})

    the options

Options Hash (opts):

  • :if_unsaved (Symbol)

    :raise (default), :save, :forget, :keep_open, or :alert

Raises:

  • WorkbookNotSaved if the option :if_unsaved is :raise and the workbook is unsaved

  • OptionInvalid if the options is invalid



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/robust_excel_ole/workbook.rb', line 571

def close(opts = {if_unsaved: :raise})
  return close_workbook unless (alive? && !@ole_workbook.Saved && writable)
  case opts[:if_unsaved]
  when :raise
    raise WorkbookNotSaved, "workbook is unsaved: #{File.basename(self.stored_filename).inspect}" +
    "\nHint: Use option :save or :forget to close the workbook with or without saving"
  when :save
    save
    close_workbook
  when :forget
    @excel.with_displayalerts(false) { close_workbook }
  when :keep_open
    # nothing
  when :alert, :excel
    @excel.with_displayalerts(true) { close_workbook }
  else
    raise OptionInvalid, ":if_unsaved: invalid option: #{opts[:if_unsaved].inspect}" +
    "\nHint: Valid values are :raise, :save, :keep_open, :alert, :excel"
  end
end

#copy_sheet(sheet, opts = { }) ⇒ Object

for compatibility to older versions



967
968
969
# File 'lib/robust_excel_ole/workbook.rb', line 967

def copy_sheet(sheet, opts = { })       # :deprecated: #
  add_or_copy_sheet(sheet, opts)
end

#eachEnumerator

Returns traversing all worksheet objects.

Returns:

  • (Enumerator)

    traversing all worksheet objects



889
890
891
892
893
894
895
896
897
# File 'lib/robust_excel_ole/workbook.rb', line 889

def each
  if block_given?
    @ole_workbook.Worksheets.lazy.each do |ole_worksheet|
      yield worksheet_class.new(ole_worksheet)
    end
  else
    to_enum(:each).lazy
  end
end

#each_with_index(offset = 0) ⇒ Object



899
900
901
902
903
904
905
# File 'lib/robust_excel_ole/workbook.rb', line 899

def each_with_index(offset = 0)
  i = offset
  @ole_workbook.Worksheets.lazy.each do |sheet|
    yield worksheet_class.new(sheet), i
    i += 1
  end
end

#filenameObject

returns the full file name of the workbook



1036
1037
1038
# File 'lib/robust_excel_ole/workbook.rb', line 1036

def filename
  General.canonize(@ole_workbook.Fullname.tr('\\','/')) rescue nil
end

#first_sheetObject



975
976
977
# File 'lib/robust_excel_ole/workbook.rb', line 975

def first_sheet
  worksheet_class.new(@ole_workbook.Worksheets.Item(1))
end

#focusObject

brings workbook to foreground, makes it available for heyboard inputs, makes the Excel instance visible



1020
1021
1022
1023
1024
# File 'lib/robust_excel_ole/workbook.rb', line 1020

def focus
  self.visible = true
  @excel.focus
  @ole_workbook.Activate
end

#for_modifying(opts = { }, &block) ⇒ Object



621
622
623
# File 'lib/robust_excel_ole/workbook.rb', line 621

def for_modifying(opts = { }, &block)
  unobtrusively({writable: true}.merge(opts), &block)
end

#for_reading(opts = { }, &block) ⇒ Object



617
618
619
# File 'lib/robust_excel_ole/workbook.rb', line 617

def for_reading(opts = { }, &block)
  unobtrusively({writable: false}.merge(opts), &block)
end

#for_this_workbook(opts) ⇒ Object

sets options

Parameters:

  • opts (Hash)


1013
1014
1015
1016
1017
# File 'lib/robust_excel_ole/workbook.rb', line 1013

def for_this_workbook(opts)
  return unless alive?
  self.class.process_options(opts, use_defaults: false)
  self.send :apply_options, @stored_filename, opts
end

#last_sheetObject



971
972
973
# File 'lib/robust_excel_ole/workbook.rb', line 971

def last_sheet
  worksheet_class.new(@ole_workbook.Worksheets.Item(@ole_workbook.Worksheets.Count))
end

#range(name_or_worksheet, name_or_address = :__not_provided, address2 = :__not_provided) ⇒ Range

creates a range from a given defined name or from a given worksheet and address

Returns:



983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/robust_excel_ole/workbook.rb', line 983

def range(name_or_worksheet, name_or_address = :__not_provided, address2 = :__not_provided)
  if name_or_worksheet.respond_to?(:gsub)
    name = name_or_worksheet
    RobustExcelOle::Range.new(get_name_object(name).RefersToRange)
  else 
    begin 
      worksheet = name_or_worksheet.to_reo
      worksheet.range(name_or_address, address2)
    rescue
      raise RangeNotCreated, "argument error: a defined name or a worksheet and an address must be provided"
    end          
  end
end

#retain_savedObject

keeps the saved-status unchanged



608
609
610
611
612
613
614
615
# File 'lib/robust_excel_ole/workbook.rb', line 608

def retain_saved
  saved = self.Saved
  begin
    yield self
  ensure
    self.Saved = saved
  end
end

#set_was_open(hash, value) ⇒ Object



189
190
191
# File 'lib/robust_excel_ole/workbook.rb', line 189

def set_was_open(hash, value)
  self.class.set_was_open(hash, value)
end

#sheet(name) ⇒ Object

returns a sheet, if a sheet name or a number is given

Parameters:

Raises:

  • (NameNotFound)


878
879
880
881
882
# File 'lib/robust_excel_ole/workbook.rb', line 878

def sheet(name)
  worksheet_class.new(@ole_workbook.Worksheets.Item(name))
rescue WIN32OLERuntimeError, Java::OrgRacobCom::ComFailException => msg
  raise NameNotFound, "could not return a sheet with name #{name.inspect}"
end

#unobtrusively(opts = { }, &block) ⇒ Object



649
650
651
652
# File 'lib/robust_excel_ole/workbook.rb', line 649

def unobtrusively(opts = { }, &block)
  file = @stored_filename
  self.class.unobtrusively_opening(file, opts, alive?, &block)
end

#visibleObject

returns true, if the workbook is visible, false otherwise



1082
1083
1084
# File 'lib/robust_excel_ole/workbook.rb', line 1082

def visible
  @excel.Visible && @ole_workbook.Windows(@ole_workbook.Name).Visible
end

#visible=(visible_value) ⇒ Object

makes both the Excel instance and the window of the workbook visible, or the window invisible does not do anything if geben visible_value is nil

Parameters:

  • visible_value (Boolean)

    determines whether the workbook shall be visible



1089
1090
1091
1092
1093
# File 'lib/robust_excel_ole/workbook.rb', line 1089

def visible= visible_value
  return if visible_value.nil?
  @excel.visible = true if visible_value
  self.window_visible = @excel.Visible ? visible_value : true
end

#window_visibleObject

returns true, if the window of the workbook is set to visible, false otherwise



1096
1097
1098
# File 'lib/robust_excel_ole/workbook.rb', line 1096

def window_visible
  @ole_workbook.Windows(@ole_workbook.Name).Visible
end

#window_visible=(visible_value) ⇒ Object

makes the window of the workbook visible or invisible

Parameters:

  • visible_value (Boolean)

    determines whether the window of the workbook shall be visible



1102
1103
1104
1105
1106
# File 'lib/robust_excel_ole/workbook.rb', line 1102

def window_visible= visible_value
  retain_saved do
    @ole_workbook.Windows(@ole_workbook.Name).Visible = visible_value if @ole_workbook.Windows.Count > 0
  end
end

#worksheets_countObject



884
885
886
# File 'lib/robust_excel_ole/workbook.rb', line 884

def worksheets_count
  @ole_workbook.Worksheets.Count
end

#writableObject



1041
1042
1043
# File 'lib/robust_excel_ole/workbook.rb', line 1041

def writable   
  !@ole_workbook.ReadOnly if @ole_workbook
end

#writable=(value_and_opts) ⇒ Object

sets the writable mode

Parameters:

  • writable (Bool)

    mode (true: read-write-mode, false: read-only mode)



1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
# File 'lib/robust_excel_ole/workbook.rb', line 1052

def writable=(value_and_opts)
  writable_value, unsaved_opts = *value_and_opts
  if @ole_workbook && !value_and_opts.nil?
    options = {:if_unsaved => :raise}
    options = options.merge(unsaved_opts) if unsaved_opts
    options = {:read_only => !writable_value}.merge(options)
    if options[:read_only] != @ole_workbook.ReadOnly
      manage_changing_readonly_mode(filename, options) 
      manage_unsaved_workbook(filename,options) if !@ole_workbook.Saved && unsaved_opts
    end
  end
  writable_value
end