robust_excel_ole 1.1 → 1.1.1
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.
- checksums.yaml +7 -0
- data/Changelog +26 -12
- data/README.rdoc +35 -27
- data/README_detail.rdoc +66 -50
- data/examples/edit_sheets/example_access_sheets_and_cells.rb +4 -4
- data/examples/edit_sheets/example_adding_sheets.rb +1 -1
- data/examples/edit_sheets/example_concating.rb +2 -2
- data/examples/edit_sheets/example_copying.rb +2 -2
- data/examples/edit_sheets/example_expanding.rb +2 -2
- data/examples/edit_sheets/example_naming.rb +2 -2
- data/examples/edit_sheets/example_saving.rb +4 -4
- data/examples/open_save_close/example_default_excel.rb +5 -5
- data/examples/open_save_close/example_force_excel.rb +4 -4
- data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +3 -3
- data/examples/open_save_close/example_if_obstructed_forget.rb +4 -4
- data/examples/open_save_close/example_if_obstructed_save.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_accept.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_forget.rb +3 -3
- data/examples/open_save_close/example_if_unsaved_forget_more.rb +3 -3
- data/examples/open_save_close/example_read_only.rb +1 -1
- data/examples/open_save_close/example_rename_cells.rb +1 -1
- data/examples/open_save_close/example_reuse.rb +4 -4
- data/examples/open_save_close/example_simple.rb +1 -1
- data/examples/open_save_close/example_unobtrusively.rb +2 -2
- data/lib/reo_console.rb +1 -1
- data/lib/robust_excel_ole/book.rb +56 -23
- data/lib/robust_excel_ole/excel.rb +35 -25
- data/lib/robust_excel_ole/general.rb +1 -1
- data/lib/robust_excel_ole/sheet.rb +99 -66
- data/lib/robust_excel_ole/version.rb +1 -1
- data/reo.bat +1 -1
- data/spec/book_spec.rb +7 -1
- data/spec/book_specs/book_misc_spec.rb +0 -4
- data/spec/data/another_workbook.xls +0 -0
- data/spec/data/workbook.xls +0 -0
- data/spec/excel_spec.rb +11 -17
- data/spec/sheet_spec.rb +11 -11
- metadata +38 -60
| @@ -13,7 +13,7 @@ begin | |
| 13 13 | 
             
              simple_file = dir + 'workbook.xls'
         | 
| 14 14 | 
             
              simple_save_file = dir + 'workbook_save.xls'
         | 
| 15 15 | 
             
              File.delete simple_save_file rescue nil
         | 
| 16 | 
            -
              book =  | 
| 16 | 
            +
              book = Workbook.open(simple_file)      # open a book
         | 
| 17 17 | 
             
              sheet = book.sheet(1)                    # access a sheet via integer 
         | 
| 18 18 | 
             
              cell = sheet[1,1]                  # access the first cell
         | 
| 19 19 | 
             
              puts "1st cell: #{cell.Value}"     # put the value of the first cell
         | 
| @@ -30,9 +30,9 @@ begin | |
| 30 30 | 
             
                  i = i + 1
         | 
| 31 31 | 
             
                  item_name = 
         | 
| 32 32 | 
             
                    case enum_method
         | 
| 33 | 
            -
                    when :each         | 
| 34 | 
            -
                    when :each_row     | 
| 35 | 
            -
                    when :each_column  | 
| 33 | 
            +
                    when :each        then "cell"
         | 
| 34 | 
            +
                    when :each_row    then "row"
         | 
| 35 | 
            +
                    when :each_column then "column"
         | 
| 36 36 | 
             
                    end 
         | 
| 37 37 | 
             
                  puts "#{item_name} #{i}: #{item.Value}" # put values of the item of the sheet
         | 
| 38 38 | 
             
                end
         | 
| @@ -13,7 +13,7 @@ begin | |
| 13 13 | 
             
              simple_file = dir + 'workbook.xls'
         | 
| 14 14 | 
             
              simple_save_file = dir + 'workbook_save.xls'
         | 
| 15 15 | 
             
              File.delete simple_save_file rescue nil
         | 
| 16 | 
            -
              @book =  | 
| 16 | 
            +
              @book = Workbook.open(simple_file)      # open a book
         | 
| 17 17 |  | 
| 18 18 | 
             
              def show_sheets 
         | 
| 19 19 | 
             
                @book.each do |sheet|               # access each sheet
         | 
| @@ -9,7 +9,7 @@ require "fileutils" | |
| 9 9 | 
             
            include RobustExcelOle
         | 
| 10 10 |  | 
| 11 11 | 
             
            begin
         | 
| 12 | 
            -
              dir =  | 
| 12 | 
            +
              dir = File.expand_path('../../spec/data', File.dirname(__FILE__))
         | 
| 13 13 | 
             
              workbook_name = 'workbook_named.xls'
         | 
| 14 14 | 
             
              ws = workbook_name.split(".")
         | 
| 15 15 | 
             
              base_name = ws[0,ws.length-1].join(".")
         | 
| @@ -18,7 +18,7 @@ begin | |
| 18 18 | 
             
              extended_file_name = dir + "/" + base_name + "_concat" + "." + suffix
         | 
| 19 19 | 
             
              FileUtils.copy file_name, extended_file_name 
         | 
| 20 20 |  | 
| 21 | 
            -
               | 
| 21 | 
            +
              Workbook.unobtrusively(extended_file_name) do |book|
         | 
| 22 22 | 
             
                book.each do |sheet|
         | 
| 23 23 | 
             
                  sheet.each do |cell|
         | 
| 24 24 | 
             
                    name = cell.Name.Name rescue nil
         | 
| @@ -10,7 +10,7 @@ require "fileutils" | |
| 10 10 | 
             
            include RobustExcelOle
         | 
| 11 11 |  | 
| 12 12 | 
             
            begin
         | 
| 13 | 
            -
              dir =  | 
| 13 | 
            +
              dir = File.expand_path('../../spec/data', File.dirname(__FILE__))
         | 
| 14 14 | 
             
              workbook_name = 'another_workbook.xls'
         | 
| 15 15 | 
             
              ws = workbook_name.split(".")
         | 
| 16 16 | 
             
              base_name = ws[0,ws.length-1].join(".")
         | 
| @@ -19,7 +19,7 @@ begin | |
| 19 19 | 
             
              extended_file_name = dir + "/" + base_name + "_copied" + "." + suffix
         | 
| 20 20 | 
             
              FileUtils.copy file_name, extended_file_name 
         | 
| 21 21 |  | 
| 22 | 
            -
               | 
| 22 | 
            +
              Workbook.unobtrusively(extended_file_name) do |book|  
         | 
| 23 23 | 
             
                book.extend Enumerable
         | 
| 24 24 | 
             
                sheet_names = book.map { |sheet| sheet.name }
         | 
| 25 25 |  | 
| @@ -12,7 +12,7 @@ require "fileutils" | |
| 12 12 | 
             
            include RobustExcelOle
         | 
| 13 13 |  | 
| 14 14 | 
             
            begin
         | 
| 15 | 
            -
              dir =  | 
| 15 | 
            +
              dir = File.expand_path('../../spec/data', File.dirname(__FILE__))
         | 
| 16 16 | 
             
              workbook_name = 'workbook_named_concat.xls'
         | 
| 17 17 | 
             
              ws = workbook_name.split(".")
         | 
| 18 18 | 
             
              base_name = ws[0,ws.length-1].join(".")
         | 
| @@ -21,7 +21,7 @@ begin | |
| 21 21 | 
             
              extended_file_name = dir + "/" + base_name + "_expanded" + "." + suffix
         | 
| 22 22 | 
             
              FileUtils.copy file_name, extended_file_name 
         | 
| 23 23 |  | 
| 24 | 
            -
               | 
| 24 | 
            +
              Workbook.unobtrusively(extended_file_name) do |book|     
         | 
| 25 25 | 
             
                book.extend Enumerable
         | 
| 26 26 | 
             
                sheet_names = book.map { |sheet| sheet.name }
         | 
| 27 27 |  | 
| @@ -10,7 +10,7 @@ require "fileutils" | |
| 10 10 | 
             
            include RobustExcelOle
         | 
| 11 11 |  | 
| 12 12 | 
             
            begin
         | 
| 13 | 
            -
              dir =  | 
| 13 | 
            +
              dir = File.expand_path('../../spec/data', File.dirname(__FILE__))
         | 
| 14 14 | 
             
              workbook_name = 'workbook.xls'
         | 
| 15 15 | 
             
              ws = workbook_name.split(".")
         | 
| 16 16 | 
             
              base_name = ws[0,ws.length-1].join(".")
         | 
| @@ -25,7 +25,7 @@ begin | |
| 25 25 | 
             
              extended_file_name = dir + "/" + base_name + "_named" + "." + suffix
         | 
| 26 26 | 
             
              FileUtils.copy file_name, extended_file_name 
         | 
| 27 27 |  | 
| 28 | 
            -
               | 
| 28 | 
            +
              Workbook.unobtrusively(extended_file_name) do |book|     
         | 
| 29 29 | 
             
                book.each do |sheet|
         | 
| 30 30 | 
             
                  sheet.each do |cell_orig|
         | 
| 31 31 | 
             
                    contents = cell_orig.Value
         | 
| @@ -9,8 +9,8 @@ include RobustExcelOle | |
| 9 9 |  | 
| 10 10 | 
             
            begin
         | 
| 11 11 |  | 
| 12 | 
            -
              dir =  | 
| 13 | 
            -
              workbook_name = ' | 
| 12 | 
            +
              dir = File.expand_path('../../spec/data', File.dirname(__FILE__))
         | 
| 13 | 
            +
              workbook_name = 'workbook_new.xls'
         | 
| 14 14 | 
             
              ws = workbook_name.split(".")
         | 
| 15 15 | 
             
              base_name = ws[0,ws.length-1].join(".")
         | 
| 16 16 | 
             
              suffix = ws.last  
         | 
| @@ -21,14 +21,14 @@ begin | |
| 21 21 | 
             
                  file_sheet_name = dir + "/" + base_name + "_" + sheet_orig.name + "." + suffix
         | 
| 22 22 | 
             
                  Excel.current.generate_workbook(file_sheet_name)
         | 
| 23 23 | 
             
                  # delete all existing sheets, and add the sheet    
         | 
| 24 | 
            -
                  book =  | 
| 24 | 
            +
                  book = Workbook.open(file_sheet_name)
         | 
| 25 25 | 
             
                  book.add_sheet sheet_orig
         | 
| 26 26 | 
             
                  book.each do |sheet|
         | 
| 27 27 | 
             
                    sheet.Delete unless sheet.name == sheet_orig.name 
         | 
| 28 28 | 
             
                  end
         | 
| 29 29 | 
             
                  book.close(:if_unsaved => :save)
         | 
| 30 30 | 
             
                  # alternative: delete all other sheets
         | 
| 31 | 
            -
                  #book =  | 
| 31 | 
            +
                  #book = Workbook.open(file_sheet_name, :force => {:excel => :new}, :visible => true)
         | 
| 32 32 | 
             
                  #book.each do |sheet|
         | 
| 33 33 | 
             
                  #  book.sheet(sheet.Name).Delete() unless sheet.Name == sheet_orig.Name
         | 
| 34 34 | 
             
                  #end
         | 
| @@ -14,27 +14,27 @@ begin | |
| 14 14 | 
             
              file_name2 = dir + 'different_workbook.xls'
         | 
| 15 15 | 
             
              file_name3 = dir + 'book_with_blank.xls'
         | 
| 16 16 | 
             
              file_name4 = dir + 'merge_cells.xls'
         | 
| 17 | 
            -
              book1 =  | 
| 17 | 
            +
              book1 = Workbook.open(file_name1)            # open a book in a new Excel instance since no Excel is open
         | 
| 18 18 | 
             
              book1.excel.visible = true               # make current Excel visible
         | 
| 19 19 | 
             
              sleep 2
         | 
| 20 20 | 
             
              book1.close                               # close the book
         | 
| 21 21 | 
             
              sleep 2
         | 
| 22 | 
            -
              book2 =  | 
| 22 | 
            +
              book2 = Workbook.open(file_name1)            # reopen the book                            
         | 
| 23 23 | 
             
              p "book1 == book2" if book2 == book1      # the books are identical
         | 
| 24 24 | 
             
              sleep 2   
         | 
| 25 25 | 
             
              new_excel = Excel.new(:reuse => false)    # create a new Excel    
         | 
| 26 | 
            -
              book3 =  | 
| 26 | 
            +
              book3 = Workbook.open(file_name2, :default => {:excel => :current}, :visible => true) # open another book
         | 
| 27 27 | 
             
              if book3.excel == book2.excel then     # since this book cannot be reopened, the option :default => {:excel} applies:
         | 
| 28 28 | 
             
                p "book3 opened in the first Excel"  # according to :default => {:excel => :current} the book is opened
         | 
| 29 29 | 
             
              end                                    # in the Excel instance the was created first
         | 
| 30 30 | 
             
              sleep 2                                          
         | 
| 31 31 | 
             
              new_excel = Excel.new(:reuse => false)         
         | 
| 32 | 
            -
              book4 =  | 
| 32 | 
            +
              book4 = Workbook.open(file_name3, :default => {:excel => new_excel}, :visible => true)  # open another book
         | 
| 33 33 | 
             
              if book4.excel == new_excel then       # since this book cannot be reopened, the option :default_excel applies: 
         | 
| 34 34 | 
             
                p "book4 opened in the second Excel" # according to :default_excel => new_excel the book is opened
         | 
| 35 35 | 
             
              end                                    # in the given Excel, namely the second Excel instance new_excel 
         | 
| 36 36 | 
             
              sleep 2   
         | 
| 37 | 
            -
              book5 =  | 
| 37 | 
            +
              book5 = Workbook.open(file_name4, :default => {:excel => :new}, :visible => true)  # open another book
         | 
| 38 38 | 
             
              if ((not book5.excel == book1.excel) && (not book5.excel == new_excel)) then  # since this book cannot be reopened, 
         | 
| 39 39 | 
             
                p "book5 opened in a third Excel" # the option :default_excel applies. according to :default_excel => :new 
         | 
| 40 40 | 
             
              end                                 # the book is opened in a new Excel
         | 
| @@ -11,17 +11,17 @@ Excel.close_all | |
| 11 11 | 
             
            begin
         | 
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              simple_file = dir + 'workbook.xls'
         | 
| 14 | 
            -
              book1 =  | 
| 14 | 
            +
              book1 = Workbook.open(simple_file)            # open a book in a new Excel instance since no Excel is open
         | 
| 15 15 | 
             
              book1.excel.visible = true                # make current Excel visible
         | 
| 16 16 | 
             
              sleep 2
         | 
| 17 | 
            -
              book2 =  | 
| 17 | 
            +
              book2 = Workbook.open(simple_file)             # open a new book in the same Excel instance                            
         | 
| 18 18 | 
             
              p "book1 == book2" if book2 == book1      # the books are identical
         | 
| 19 19 | 
             
              sleep 2       
         | 
| 20 | 
            -
              book3 =  | 
| 20 | 
            +
              book3 = Workbook.open(simple_file, :force => {:excel => :new, :visible => true}) # open another book in a new Excel instance,   
         | 
| 21 21 | 
             
              p "book3 != book1" if (not (book3 == book1))   # the books are not identical 
         | 
| 22 22 | 
             
              sleep 2   
         | 
| 23 23 | 
             
              new_excel = Excel.new(:reuse => false)        # create a third Excel instance
         | 
| 24 | 
            -
              book4 =  | 
| 24 | 
            +
              book4 = Workbook.open(simple_file, :force => {:excel => new_excel, :visible => true})  # open another book in the new Excel instance
         | 
| 25 25 | 
             
              p "book4 != book3 && book4 != book1" if (not (book4 == book3) && (not (book4 == book1)))
         | 
| 26 26 | 
             
              sleep 2                                     # (Excel chooses the first Excel application)        
         | 
| 27 27 | 
             
              book4.close                                 # close the books
         | 
| @@ -12,19 +12,19 @@ begin | |
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls'
         | 
| 14 14 | 
             
              other_file_name = dir + 'more_data/workbook.xls'
         | 
| 15 | 
            -
              book =  | 
| 15 | 
            +
              book = Workbook.open(file_name, :visible => true)  # open a book, make Excel visible
         | 
| 16 16 | 
             
              sleep 1
         | 
| 17 17 | 
             
              sheet = book.sheet(1)
         | 
| 18 18 | 
             
              first_cell = sheet[1,1].Value                                   # access a sheet
         | 
| 19 19 | 
             
              sheet[1,1] = first_cell == "simple" ? "complex" : "simple"      # change a cell
         | 
| 20 20 | 
             
              sleep 1
         | 
| 21 21 | 
             
              begin
         | 
| 22 | 
            -
                new_book =  | 
| 22 | 
            +
                new_book = Workbook.open(other_file_name, :if_obstructed => :close_if_saved) # raises an exception since the file is not saved
         | 
| 23 23 | 
             
                rescue WorkbookNotSaved => msg                                             
         | 
| 24 24 | 
             
                puts "error: open: #{msg.message}"
         | 
| 25 25 | 
             
              end                                                        
         | 
| 26 26 | 
             
              book.save                                                           # save the unsaved book
         | 
| 27 | 
            -
              new_book =  | 
| 27 | 
            +
              new_book = Workbook.open(file_name, :if_obstructed => :close_if_saved)  # open the new book, close the saved book    
         | 
| 28 28 | 
             
              sleep 1
         | 
| 29 29 | 
             
              new_sheet = new_book.sheet(1)
         | 
| 30 30 | 
             
              new_first_cell = new_sheet[1,1].Value
         | 
| @@ -12,18 +12,18 @@ begin | |
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls'
         | 
| 14 14 | 
             
              other_file_name = dir + 'more_data/workbook.xls'
         | 
| 15 | 
            -
              book =  | 
| 15 | 
            +
              book = Workbook.open(file_name, :visible => true)  # open a book, make Excel application visible
         | 
| 16 16 | 
             
              sleep 3 
         | 
| 17 17 | 
             
              begin
         | 
| 18 | 
            -
                new_book =  | 
| 18 | 
            +
                new_book = Workbook.open(other_file_name)        # open a book with the same file name in a different path
         | 
| 19 19 | 
             
              rescue WorkbookBlocked => msg                   # by default: raises an exception 
         | 
| 20 20 | 
             
              	puts "error: open: #{msg.message}"
         | 
| 21 21 | 
             
              end
         | 
| 22 22 | 
             
              # open a new book with the same file name in a different path. close the old book before.
         | 
| 23 | 
            -
              new_book =  | 
| 23 | 
            +
              new_book = Workbook.open(other_file_name, :if_obstructed => :forget) 
         | 
| 24 24 | 
             
              sleep 3
         | 
| 25 25 | 
             
              # open another book with the same file name in a different path. Use a new Excel application
         | 
| 26 | 
            -
              another_book =  | 
| 26 | 
            +
              another_book = Workbook.open(file_name, :if_obstructed => :new_excel, :visible => true)                                         
         | 
| 27 27 | 
             
              sleep 3
         | 
| 28 28 | 
             
              new_book.close                                 # close the books                      
         | 
| 29 29 | 
             
              another_book.close
         | 
| @@ -12,15 +12,15 @@ begin | |
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls'
         | 
| 14 14 | 
             
              other_file_name = dir + 'more_data/workbook.xls'
         | 
| 15 | 
            -
              book =  | 
| 15 | 
            +
              book = Workbook.open(file_name, :visible => true)  # open a book, make Excel visible
         | 
| 16 16 | 
             
              sleep 1
         | 
| 17 17 | 
             
              sheet = book.sheet(1)
         | 
| 18 18 | 
             
              first_cell = sheet[1,1].value                                   # access a sheet
         | 
| 19 19 | 
             
              sheet[1,1] = first_cell == "simple" ? "complex" : "simple"      # change a cell
         | 
| 20 20 | 
             
              sleep 1
         | 
| 21 | 
            -
              new_book =  | 
| 21 | 
            +
              new_book = Workbook.open(other_file_name, :if_obstructed => :save)  # open a book with the same file name in a different path
         | 
| 22 22 | 
             
              sleep 1                                                         #save the old book, close it, before
         | 
| 23 | 
            -
              old_book =  | 
| 23 | 
            +
              old_book = Workbook.open(file_name, :if_obstructed => :forget ,:visible => true) # open the old book    
         | 
| 24 24 | 
             
              sleep 1
         | 
| 25 25 | 
             
              old_sheet = old_book.sheet(1)
         | 
| 26 26 | 
             
              old_first_cell = old_sheet[1,1].value
         | 
| @@ -11,15 +11,15 @@ Excel.close_all | |
| 11 11 | 
             
            begin
         | 
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls' 
         | 
| 14 | 
            -
              book =  | 
| 14 | 
            +
              book = Workbook.open(file_name)                      # open a book 
         | 
| 15 15 | 
             
              sheet = book.sheet(1)                                                  # access a sheet
         | 
| 16 16 | 
             
              sheet[1,1] = sheet[1,1].value == "simple" ? "complex" : "simple" # change a cell
         | 
| 17 17 | 
             
              begin
         | 
| 18 | 
            -
                new_book =  | 
| 18 | 
            +
                new_book = Workbook.open(file_name)                # open another book with the same file name
         | 
| 19 19 | 
             
              rescue WorkbookBeingUsed => 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 | 
            -
              new_book =  | 
| 22 | 
            +
              new_book = Workbook.open(file_name, :if_unsaved => :accept) # open another book with the same file name 
         | 
| 23 23 | 
             
                                                                      # and let the unsaved book open
         | 
| 24 24 | 
             
              if book.alive? && new_book.alive? then                  # check whether the referenced workbooks
         | 
| 25 25 | 
             
              	puts "open with :if_unsaved => :accept : the two books are alive." # respond to methods
         | 
| @@ -11,14 +11,14 @@ Excel.close_all | |
| 11 11 | 
             
            begin
         | 
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls' 
         | 
| 14 | 
            -
              book =  | 
| 14 | 
            +
              book = Workbook.open(file_name)                      # open a book
         | 
| 15 15 | 
             
              book.excel.visible = true                        # make current Excel visible 
         | 
| 16 16 | 
             
              sleep 1
         | 
| 17 17 | 
             
              sheet = book.sheet(1)                                            # access a sheet
         | 
| 18 18 | 
             
              first_cell = sheet[1,1].value
         | 
| 19 19 | 
             
              sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
         | 
| 20 20 | 
             
              sleep 1
         | 
| 21 | 
            -
              new_book =  | 
| 21 | 
            +
              new_book = Workbook.open(file_name, :if_unsaved => :forget) # open another book with the same file name 
         | 
| 22 22 | 
             
                                                                      # and close the unsaved book without saving it
         | 
| 23 23 | 
             
              sheet_new_book = new_book.sheet(1)
         | 
| 24 24 | 
             
              if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].value == first_cell then # check whether the unsaved book 
         | 
| @@ -27,7 +27,7 @@ begin | |
| 27 27 | 
             
              sleep 1
         | 
| 28 28 | 
             
              sheet_new_book[1,1] = sheet_new_book[1,1].value == "simple" ? "complex" : "simple" # change a cell
         | 
| 29 29 | 
             
              # open another book in a new Excel application, and make Excel visible, leaving the unsaved book open
         | 
| 30 | 
            -
              another_book =  | 
| 30 | 
            +
              another_book = Workbook.open(file_name, :if_unsaved => :new_excel, :visible => true)  
         | 
| 31 31 | 
             
              sleep 3                                                                  # leaving the unsaved book open  
         | 
| 32 32 | 
             
              new_book.close(:if_unsaved => :forget )                                
         | 
| 33 33 | 
             
              another_book.close
         | 
| @@ -11,13 +11,13 @@ Excel.kill_all | |
| 11 11 | 
             
            begin
         | 
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls' 
         | 
| 14 | 
            -
              book =  | 
| 14 | 
            +
              book = Workbook.open(file_name)                      # open a book
         | 
| 15 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
         | 
| 19 19 | 
             
              sleep 1
         | 
| 20 | 
            -
              new_book =  | 
| 20 | 
            +
              new_book = Workbook.open(file_name, :if_unsaved => :new_excel, :visible => true) # open another book with the same file name in a new Excel
         | 
| 21 21 | 
             
              sheet_new_book = new_book.sheet(1)
         | 
| 22 22 | 
             
              if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].value == first_cell then # check whether the unsaved book 
         | 
| 23 23 | 
             
                puts "open with :if_unsaved => :forget : the unsaved book is closed and not saved."     # is closed and was not saved
         | 
| @@ -25,7 +25,7 @@ begin | |
| 25 25 | 
             
              sleep 1
         | 
| 26 26 | 
             
              sheet_new_book[1,1] = sheet_new_book[1,1].value == "simple" ? "complex" : "simple" # change a cell
         | 
| 27 27 | 
             
              # open another book in the running Excel application, and make Excel visible, closing the unsaved book
         | 
| 28 | 
            -
              another_book =  | 
| 28 | 
            +
              another_book = Workbook.open(file_name, :if_unsaved => :forget, :visible => true)  
         | 
| 29 29 | 
             
              sleep 1
         | 
| 30 30 | 
             
              sheet_another_book = another_book.sheet(1)
         | 
| 31 31 | 
             
              sheet_another_book[1,1] = sheet_another_book[1,1].value == "simple" ? "complex" : "simple" # change a cell                                                                   
         | 
| @@ -11,7 +11,7 @@ begin | |
| 11 11 | 
             
              dir = create_tmpdir
         | 
| 12 12 | 
             
              file_name = dir + 'workbook.xls'
         | 
| 13 13 | 
             
              other_file_name = dir + 'different_workbook.xls'
         | 
| 14 | 
            -
              book =  | 
| 14 | 
            +
              book = Workbook.open(file_name, :read_only => true, :visible => true) # open a book with read_only and make Excel visible
         | 
| 15 15 | 
             
              sheet = book.sheet(1)                                     			        # access a sheet
         | 
| 16 16 | 
             
              sleep 1     
         | 
| 17 17 | 
             
              sheet[1,1] = sheet[1,1].value == "simple" ? "complex" : "simple" # change a cell
         | 
| @@ -11,7 +11,7 @@ Excel.close_all | |
| 11 11 | 
             
            begin
         | 
| 12 12 | 
             
              dir = create_tmpdir
         | 
| 13 13 | 
             
              file_name = dir + 'workbook.xls'
         | 
| 14 | 
            -
              book =  | 
| 14 | 
            +
              book = Workbook.open(file_name)                                # open a book.  default:  :read_only => false
         | 
| 15 15 | 
             
              book.excel.visible = true                                          # make current Excel visible
         | 
| 16 16 | 
             
              sheet = book.sheet(1)
         | 
| 17 17 | 
             
              workbook = book.ole_workbook
         | 
| @@ -13,14 +13,14 @@ begin | |
| 13 13 | 
             
              file_name2 = dir + 'different_workbook.xls'
         | 
| 14 14 | 
             
              file_name3 = dir + 'different_workbook.xls'
         | 
| 15 15 | 
             
              file_name4 = dir + 'book_with_blank.xls'
         | 
| 16 | 
            -
              book1 =  | 
| 16 | 
            +
              book1 = Workbook.open(file_name1)             # open a book in a new Excel instance since no Excel is open
         | 
| 17 17 | 
             
              book1.excel.visible = true                # make current Excel visible
         | 
| 18 18 | 
             
              sleep 2
         | 
| 19 | 
            -
              book2 =  | 
| 19 | 
            +
              book2 = Workbook.open(file_name2)             # open a new book in the same Excel instance
         | 
| 20 20 | 
             
              sleep 2                                   
         | 
| 21 | 
            -
              book3 =  | 
| 21 | 
            +
              book3 = Workbook.open(file_name3, :force_excel => :new, :visible => true) # open another book in a new Excel instance, 
         | 
| 22 22 | 
             
              sleep 2                                                          # make Excel visible
         | 
| 23 | 
            -
              book4 =  | 
| 23 | 
            +
              book4 = Workbook.open(file_name4, :visible => true)  # open anther book, and use a running Excel application
         | 
| 24 24 | 
             
              sleep 2                                          # (Excel chooses the first Excel application)        
         | 
| 25 25 | 
             
              book1.close                               # close the books
         | 
| 26 26 | 
             
              book2.close                                             
         | 
| @@ -16,7 +16,7 @@ begin | |
| 16 16 | 
             
              dir = create_tmpdir
         | 
| 17 17 | 
             
              file_name = dir + 'workbook.xls'
         | 
| 18 18 | 
             
              other_file_name = dir + 'different_workbook.xls'
         | 
| 19 | 
            -
              book =  | 
| 19 | 
            +
              book = Workbook.open(file_name)                                # open a book.  default:  :read_only => false
         | 
| 20 20 | 
             
              book.excel.visible = true                                  # make current Excel visible
         | 
| 21 21 | 
             
              sheet = book.sheet(1)                                            # access a sheet
         | 
| 22 22 | 
             
              sleep 1     
         | 
| @@ -10,11 +10,11 @@ Excel.close_all | |
| 10 10 | 
             
            begin
         | 
| 11 11 | 
             
              dir = create_tmpdir
         | 
| 12 12 | 
             
              simple_file = dir + 'workbook.xls'
         | 
| 13 | 
            -
              book =  | 
| 13 | 
            +
              book = Workbook.open(simple_file, :visible => true)  # open a book, make Excel visible
         | 
| 14 14 | 
             
              old_sheet = book.sheet(1)
         | 
| 15 15 | 
             
              p "1st cell: #{old_sheet[1,1].value}"
         | 
| 16 16 | 
             
              sleep 2
         | 
| 17 | 
            -
               | 
| 17 | 
            +
              Workbook.unobtrusively(simple_file) do |book|   # modify the book and keep its status unchanged
         | 
| 18 18 | 
             
                sheet = book.sheet(1)
         | 
| 19 19 | 
             
                sheet[1,1] = sheet[1,1].value == "simple" ? "complex" : "simple"
         | 
| 20 20 | 
             
              end
         | 
    
        data/lib/reo_console.rb
    CHANGED
    
    
| @@ -541,11 +541,13 @@ module RobustExcelOle | |
| 541 541 | 
             
                end
         | 
| 542 542 |  | 
| 543 543 | 
             
                # simple save of a workbook.
         | 
| 544 | 
            +
                # @option opts [Boolean]  states, whether colored ranges shall be discolored
         | 
| 544 545 | 
             
                # @return [Boolean] true, if successfully saved, nil otherwise
         | 
| 545 | 
            -
                def save      
         | 
| 546 | 
            +
                def save(opts = {:discoloring => false})      
         | 
| 546 547 | 
             
                  raise ObjectNotAlive, "workbook is not alive" if (not alive?)
         | 
| 547 548 | 
             
                  raise WorkbookReadOnly, "Not opened for writing (opened with :read_only option)" if @ole_workbook.ReadOnly
         | 
| 548 549 | 
             
                  begin
         | 
| 550 | 
            +
                    discoloring if opts[:discoloring] 
         | 
| 549 551 | 
             
                    @ole_workbook.Save 
         | 
| 550 552 | 
             
                  rescue WIN32OLERuntimeError => msg
         | 
| 551 553 | 
             
                    if msg.message =~ /SaveAs/ and msg.message =~ /Workbook/ then
         | 
| @@ -553,7 +555,7 @@ module RobustExcelOle | |
| 553 555 | 
             
                    else
         | 
| 554 556 | 
             
                      raise UnexpectedError, "unknown WIN32OLERuntimeError:\n#{msg.message}"
         | 
| 555 557 | 
             
                    end       
         | 
| 556 | 
            -
                  end
         | 
| 558 | 
            +
                  end      
         | 
| 557 559 | 
             
                  true
         | 
| 558 560 | 
             
                end
         | 
| 559 561 |  | 
| @@ -573,6 +575,7 @@ module RobustExcelOle | |
| 573 575 | 
             
                #                  :save                -> saves the blocking workbook and closes it
         | 
| 574 576 | 
             
                #                  :close_if_saved      -> closes the blocking workbook, if it is saved, 
         | 
| 575 577 | 
             
                #                                          otherwise raises an exception
         | 
| 578 | 
            +
                # :discoloring     states, wheter colored ranges shall be discolored
         | 
| 576 579 | 
             
                # @return [Book], the book itself, if successfully saved, raises an exception otherwise
         | 
| 577 580 | 
             
                def save_as(file, opts = { } )
         | 
| 578 581 | 
             
                  raise FileNameNotGiven, "filename is nil" if file.nil?
         | 
| @@ -586,7 +589,7 @@ module RobustExcelOle | |
| 586 589 | 
             
                    case options[:if_exists]
         | 
| 587 590 | 
             
                    when :overwrite
         | 
| 588 591 | 
             
                      if file == self.filename
         | 
| 589 | 
            -
                        save
         | 
| 592 | 
            +
                        save({:discoloring => opts[:discoloring]})
         | 
| 590 593 | 
             
                        return self
         | 
| 591 594 | 
             
                      else
         | 
| 592 595 | 
             
                        begin
         | 
| @@ -628,6 +631,10 @@ module RobustExcelOle | |
| 628 631 |  | 
| 629 632 | 
             
              private
         | 
| 630 633 |  | 
| 634 | 
            +
                def discoloring
         | 
| 635 | 
            +
                  self.each{|sheet| sheet.each{|cell| cell.Interior.ColorIndex = XlNone}}
         | 
| 636 | 
            +
                end
         | 
| 637 | 
            +
             | 
| 631 638 | 
             
                def save_as_workbook(file, options)   # :nodoc: #
         | 
| 632 639 | 
             
                  begin
         | 
| 633 640 | 
             
                    dirname, basename = File.split(file)
         | 
| @@ -637,6 +644,7 @@ module RobustExcelOle | |
| 637 644 | 
             
                        when '.xlsx'; RobustExcelOle::XlOpenXMLWorkbook
         | 
| 638 645 | 
             
                        when '.xlsm'; RobustExcelOle::XlOpenXMLWorkbookMacroEnabled
         | 
| 639 646 | 
             
                      end
         | 
| 647 | 
            +
                    discoloring if options[:discoloring]  
         | 
| 640 648 | 
             
                    @ole_workbook.SaveAs(General::absolute_path(file), file_format)
         | 
| 641 649 | 
             
                    bookstore.store(self)
         | 
| 642 650 | 
             
                  rescue WIN32OLERuntimeError => msg
         | 
| @@ -653,7 +661,15 @@ module RobustExcelOle | |
| 653 661 |  | 
| 654 662 | 
             
                # closes a given file if it is open
         | 
| 655 663 | 
             
                def self.close(file, opts = {:if_unsaved => :raise})
         | 
| 664 | 
            +
                  #puts "self.close:"
         | 
| 665 | 
            +
                  #puts "file: #{file}"
         | 
| 666 | 
            +
                  #begin
         | 
| 667 | 
            +
                  #  bookstore.fetch(file)
         | 
| 668 | 
            +
                  #rescue
         | 
| 669 | 
            +
                  #  puts "#{$!.message}"
         | 
| 670 | 
            +
                  #end
         | 
| 656 671 | 
             
                  book = bookstore.fetch(file) rescue nil
         | 
| 672 | 
            +
                  #puts "book after fetch: #{book.inspect}"
         | 
| 657 673 | 
             
                  book.close(opts) if book && book.alive?
         | 
| 658 674 | 
             
                end
         | 
| 659 675 |  | 
| @@ -685,6 +701,14 @@ module RobustExcelOle | |
| 685 701 | 
             
                    yield sheet_class.new(sheet)
         | 
| 686 702 | 
             
                  end
         | 
| 687 703 | 
             
                end
         | 
| 704 | 
            +
             | 
| 705 | 
            +
                def each_with_index(offset = 0)
         | 
| 706 | 
            +
                  i = offset
         | 
| 707 | 
            +
                  @ole_workbook.Worksheets.each do |sheet|
         | 
| 708 | 
            +
                    yield sheet_class.new(sheet), i
         | 
| 709 | 
            +
                    i += 1
         | 
| 710 | 
            +
                  end
         | 
| 711 | 
            +
                end
         | 
| 688 712 |  | 
| 689 713 | 
             
                # copies a sheet to another position
         | 
| 690 714 | 
             
                # default: copied sheet is appended
         | 
| @@ -761,7 +785,7 @@ module RobustExcelOle | |
| 761 785 | 
             
                # @param [String]  name  the name of the range
         | 
| 762 786 | 
             
                # @param [Variant] value the contents of the range
         | 
| 763 787 | 
             
                def []= (name, value)
         | 
| 764 | 
            -
                  set_nameval(name,value)
         | 
| 788 | 
            +
                  set_nameval(name,value, :color => 42)   # 42 - aqua-marin, 4-green
         | 
| 765 789 | 
             
                end
         | 
| 766 790 |  | 
| 767 791 | 
             
                # returns the contents of a range with given name
         | 
| @@ -774,46 +798,55 @@ module RobustExcelOle | |
| 774 798 | 
             
                # @option opts [Symbol] :default  the default value that is provided if no contents could be returned
         | 
| 775 799 | 
             
                # @return [Variant] the contents of a range with given name
         | 
| 776 800 | 
             
                def nameval(name, opts = {:default => nil})
         | 
| 777 | 
            -
                   | 
| 778 | 
            -
                  begin
         | 
| 779 | 
            -
                    name_obj | 
| 780 | 
            -
                  rescue WIN32OLERuntimeError
         | 
| 781 | 
            -
                    return default_val if default_val
         | 
| 782 | 
            -
                    raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"
         | 
| 783 | 
            -
                  end
         | 
| 784 | 
            -
                  begin
         | 
| 785 | 
            -
                    value = name_obj.RefersToRange.Value
         | 
| 801 | 
            +
                  name_obj = name_object(name)
         | 
| 802 | 
            +
                  value = begin
         | 
| 803 | 
            +
                    name_obj.RefersToRange.Value
         | 
| 786 804 | 
             
                  rescue  WIN32OLERuntimeError
         | 
| 787 805 | 
             
                    begin
         | 
| 788 | 
            -
                       | 
| 806 | 
            +
                      self.sheet(1).Evaluate(name_obj.Name)
         | 
| 789 807 | 
             
                    rescue WIN32OLERuntimeError
         | 
| 790 | 
            -
                      return  | 
| 808 | 
            +
                      return opts[:default] if opts[:default]
         | 
| 791 809 | 
             
                      raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
         | 
| 792 810 | 
             
                    end
         | 
| 793 811 | 
             
                  end
         | 
| 794 812 | 
             
                  if value.is_a?(Bignum)  #RobustExcelOle::XlErrName  
         | 
| 795 | 
            -
                    return  | 
| 813 | 
            +
                    return opts[:default] if opts[:default]
         | 
| 796 814 | 
             
                    raise RangeNotEvaluatable, "cannot evaluate range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"
         | 
| 797 815 | 
             
                  end 
         | 
| 798 | 
            -
                  return  | 
| 816 | 
            +
                  return opts[:default] if opts[:default] && value.nil?
         | 
| 799 817 | 
             
                  value      
         | 
| 800 818 | 
             
                end
         | 
| 801 819 |  | 
| 802 820 | 
             
                # sets the contents of a range
         | 
| 803 821 | 
             
                # @param [String]  name  the name of a range
         | 
| 804 822 | 
             
                # @param [Variant] value the contents of the range
         | 
| 805 | 
            -
                 | 
| 823 | 
            +
                # @param [FixNum]  color the color when setting a value
         | 
| 824 | 
            +
                # @param [Hash]    opts :color [FixNum]  the color when setting the contents
         | 
| 825 | 
            +
                def set_nameval(name, value, opts = {:color => 0})
         | 
| 806 826 | 
             
                  begin
         | 
| 807 | 
            -
                     | 
| 827 | 
            +
                    cell = name_object(name).RefersToRange
         | 
| 828 | 
            +
                    cell.Interior.ColorIndex = opts[:color] 
         | 
| 829 | 
            +
                    cell.Value = value
         | 
| 808 830 | 
             
                  rescue WIN32OLERuntimeError
         | 
| 809 | 
            -
                    raise  | 
| 831 | 
            +
                    raise RangeNotEvaluatable, "cannot assign value to range named #{name.inspect} in #{File.basename(self.stored_filename).inspect}"    
         | 
| 810 832 | 
             
                  end
         | 
| 833 | 
            +
                end
         | 
| 834 | 
            +
             | 
| 835 | 
            +
              private        
         | 
| 836 | 
            +
             | 
| 837 | 
            +
                def name_object(name)
         | 
| 811 838 | 
             
                  begin
         | 
| 812 | 
            -
                     | 
| 839 | 
            +
                    self.Parent.Names.Item(name)
         | 
| 813 840 | 
             
                  rescue WIN32OLERuntimeError
         | 
| 814 | 
            -
                     | 
| 841 | 
            +
                    begin
         | 
| 842 | 
            +
                      self.Names.Item(name)
         | 
| 843 | 
            +
                    rescue WIN32OLERuntimeError
         | 
| 844 | 
            +
                      raise NameNotFound, "name #{name.inspect} not in #{File.basename(self.stored_filename).inspect}"  
         | 
| 845 | 
            +
                    end
         | 
| 815 846 | 
             
                  end
         | 
| 816 | 
            -
                end | 
| 847 | 
            +
                end
         | 
| 848 | 
            +
             | 
| 849 | 
            +
              public    
         | 
| 817 850 |  | 
| 818 851 | 
             
                # renames a range
         | 
| 819 852 | 
             
                # @param [String] name     the previous range name
         |