robust_excel_ole 1.11 → 1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog +11 -0
  3. data/README.rdoc +20 -8
  4. data/docs/README_open.rdoc +1 -0
  5. data/docs/README_ranges.rdoc +5 -11
  6. data/examples/{introducing_examples/example_introducing.rb → introductory_examples/example_introductory.rb} +10 -2
  7. data/examples/{introducing_examples → introductory_examples}/example_open.rb +18 -17
  8. data/examples/{introducing_examples → introductory_examples}/example_range.rb +29 -16
  9. data/examples/modifying_sheets/example_access_sheets_and_cells.rb +8 -7
  10. data/examples/modifying_sheets/example_add_names.rb +4 -8
  11. data/examples/modifying_sheets/example_adding_sheets.rb +7 -6
  12. data/examples/modifying_sheets/example_concating.rb +2 -2
  13. data/examples/modifying_sheets/example_copying.rb +3 -3
  14. data/examples/modifying_sheets/example_expanding.rb +2 -2
  15. data/examples/modifying_sheets/example_naming.rb +2 -2
  16. data/examples/modifying_sheets/example_ranges.rb +4 -4
  17. data/examples/modifying_sheets/example_saving.rb +3 -3
  18. data/examples/open_save_close/example_control_to_excel.rb +10 -11
  19. data/examples/open_save_close/example_default_excel.rb +13 -14
  20. data/examples/open_save_close/example_force_excel.rb +9 -10
  21. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +7 -7
  22. data/examples/open_save_close/example_if_obstructed_forget.rb +5 -5
  23. data/examples/open_save_close/example_if_obstructed_save.rb +7 -7
  24. data/examples/open_save_close/example_if_unsaved_accept.rb +13 -13
  25. data/examples/open_save_close/example_if_unsaved_forget.rb +9 -10
  26. data/examples/open_save_close/example_if_unsaved_forget_more.rb +9 -10
  27. data/examples/open_save_close/example_read_only.rb +6 -6
  28. data/examples/open_save_close/example_rename_cells.rb +4 -5
  29. data/examples/open_save_close/example_reuse.rb +6 -6
  30. data/examples/open_save_close/example_simple.rb +5 -5
  31. data/examples/open_save_close/example_unobtrusively.rb +4 -4
  32. data/lib/robust_excel_ole/address.rb +0 -4
  33. data/lib/robust_excel_ole/bookstore.rb +4 -28
  34. data/lib/robust_excel_ole/excel.rb +17 -22
  35. data/lib/robust_excel_ole/general.rb +11 -18
  36. data/lib/robust_excel_ole/range_owners.rb +17 -27
  37. data/lib/robust_excel_ole/reo_common.rb +7 -3
  38. data/lib/robust_excel_ole/version.rb +1 -1
  39. data/lib/robust_excel_ole/workbook.rb +178 -180
  40. data/lib/robust_excel_ole/worksheet.rb +7 -4
  41. data/robust_excel_ole.gemspec +6 -4
  42. data/spec/bookstore_spec.rb +38 -34
  43. data/spec/data/more_data/workbook.xls +0 -0
  44. data/spec/excel_spec.rb +89 -44
  45. data/spec/general_spec.rb +1 -0
  46. data/spec/range_spec.rb +7 -4
  47. data/spec/workbook_specs/workbook_close_spec.rb +2 -1
  48. data/spec/workbook_specs/workbook_misc_spec.rb +34 -18
  49. data/spec/workbook_specs/workbook_open_spec.rb +112 -71
  50. data/spec/workbook_specs/workbook_save_spec.rb +173 -5
  51. data/spec/workbook_specs/workbook_sheet_spec.rb +6 -42
  52. data/spec/workbook_specs/workbook_unobtr_spec.rb +9 -246
  53. data/spec/worksheet_spec.rb +21 -5
  54. metadata +12 -11
@@ -2,8 +2,8 @@
2
2
  # each named cell gets the value of cell right to it appended to its own value
3
3
  # the new workbook's name is extended by the suffix "_concat"
4
4
 
5
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
6
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
5
+ require_relative '../../lib/robust_excel_ole'
6
+ require_relative '../../spec/helpers/create_temporary_dir'
7
7
  require "fileutils"
8
8
 
9
9
  include RobustExcelOle
@@ -1,10 +1,10 @@
1
1
  # example_copying.rb:
2
- # each named cell is to be copied into another sheet
2
+ # each named cell is to be copied into another worksheet
3
3
  # unnamed cells shall not be copied
4
4
  # if a sheet does not contain any named cell, then the sheet shall not be copied
5
5
 
6
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
7
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
6
+ require_relative '../../lib/robust_excel_ole'
7
+ require_relative '../../spec/helpers/create_temporary_dir'
8
8
  require "fileutils"
9
9
 
10
10
  include RobustExcelOle
@@ -5,8 +5,8 @@
5
5
  # the sheet's name shall be the name of the Excel name
6
6
  # in addition to that, the cell B2 shall be named "name" and get the sheet name as its value
7
7
 
8
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
9
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
8
+ require_relative '../../lib/robust_excel_ole'
9
+ require_relative '../../spec/helpers/create_temporary_dir'
10
10
  require "fileutils"
11
11
 
12
12
  include RobustExcelOle
@@ -3,8 +3,8 @@
3
3
  # the contents of each cell is copied
4
4
  # the new workbook's name is extended by the suffix "_named"
5
5
 
6
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
7
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
6
+ require_relative '../../lib/robust_excel_ole'
7
+ require_relative '../../spec/helpers/create_temporary_dir'
8
8
  require "fileutils"
9
9
 
10
10
  include RobustExcelOle
@@ -1,8 +1,8 @@
1
1
  # example_ranges.rb:
2
2
  # access row and column ranges of a sheet.
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -13,8 +13,8 @@ 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 = Workbook.open(simple_file) # open a book
17
- sheet = book.sheet('Sheet1') # access a sheet via the name
16
+ book = Workbook.open(simple_file, :visible => true) # open a workbook
17
+ sheet = book.sheet('Sheet1') # access a worksheet via the name
18
18
  row_r = sheet.row_range(1) # access the whole range of the first row
19
19
  col_r = sheet.col_range(1, 1..2) # access the first two cells of the range of the first column
20
20
  cell = col_r[0] # access the first cell of these cells
@@ -1,8 +1,8 @@
1
1
  # example_saving.rb:
2
2
  # save the sheets of a book as separate workbooks
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -27,7 +27,7 @@ begin
27
27
  sheet.Delete unless sheet.name == sheet_orig.name
28
28
  end
29
29
  book.close(:if_unsaved => :save)
30
- # alternative: delete all other sheets
30
+ # alternative: delete all other worksheets
31
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
@@ -1,8 +1,8 @@
1
1
  # example_give_control_to_excel.rb:
2
2
  # open, close, save with giving control to Excel
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -11,32 +11,31 @@ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
14
- book = Workbook.open(file_name) # open a book
15
- book.excel.visible = true # make current Excel visible
14
+ book = Workbook.open(file_name, :visible => true) # open a book
16
15
  sleep 1
17
- sheet = book.sheet(1) # access a sheet
16
+ sheet = book.sheet(1) # access a worksheet
18
17
  sheet[1,1] = sheet[1,1].Value == "simple" ? "complex" : "simple" # change a cell
19
18
  sleep 1
20
19
  begin
21
- new_book = Workbook.open(file_name, :if_unsaved => :alert) # open another book with the same file name
22
- rescue WorkbookREOError => msg # if the user chooses not open the book,
20
+ new_book = Workbook.open(file_name, :visible => true, :if_unsaved => :alert) # open another workbook with the same file name
21
+ rescue WorkbookREOError => msg # if the user chooses not open the workbook,
23
22
  puts "#{msg.message}" # an exeptions is raised
24
23
  end
25
24
  puts "new book has opened" if new_book
26
25
  Excel.current.visible = true
27
26
  begin
28
- book.close(:if_unsaved => :alert) # close the unsaved book.
29
- rescue WorkbookREOError => msg # user is asked whether the unsaved book shall be saved
27
+ book.close(:if_unsaved => :alert) # close the unsaved workbook.
28
+ rescue WorkbookREOError => msg # user is asked whether the unsaved workbook shall be saved
30
29
  puts "#{msg.message}" # if the user chooses to cancel, then an expeption is raised
31
30
  end
32
31
  if new_book then
33
32
  begin
34
- new_book.save_as(file_name, :if_exists => :alert) # save the new book, if it was opened
33
+ new_book.save_as(file_name, :if_exists => :alert) # save the new workbook, if it was opened
35
34
  rescue WorkbookREOError => msg # user is asked, whether the existing file shall be overwritten
36
35
  puts "save_as: #{msg.message}" # if the user chooses "no" or "cancel", an exception is raised
37
36
  end
38
37
 
39
- new_book.close # close the new book, if the user chose to open it
38
+ new_book.close # close the new workbook, if the user chose to open it
40
39
  end
41
40
  ensure
42
41
  Excel.kill_all # close ALL workbooks, quit Excel application
@@ -1,8 +1,8 @@
1
1
  # example_default_excel.rb:
2
2
  # reopening books using :default_excel
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -14,30 +14,29 @@ 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 = Workbook.open(file_name1) # open a book in a new Excel instance since no Excel is open
18
- book1.excel.visible = true # make current Excel visible
17
+ book1 = Workbook.open(file_name1, :visible => true) # open a workbook in a new Excel instance since no Excel is open
19
18
  sleep 2
20
- book1.close # close the book
19
+ book1.close # close the workbook
21
20
  sleep 2
22
- book2 = Workbook.open(file_name1) # reopen the book
21
+ book2 = Workbook.open(file_name1, :visible => true) # reopen the workbook
23
22
  p "book1 == book2" if book2 == book1 # the books are identical
24
23
  sleep 2
25
24
  new_excel = Excel.new(:reuse => false) # create a new Excel
26
25
  book3 = Workbook.open(file_name2, :default => {:excel => :current}, :visible => true) # open another book
27
- if book3.excel == book2.excel then # since this book cannot be reopened, the option :default => {:excel} applies:
28
- p "book3 opened in the first Excel" # according to :default => {:excel => :current} the book is opened
26
+ if book3.excel == book2.excel then # since this workbook cannot be reopened, the option :default => {:excel} applies:
27
+ p "book3 opened in the first Excel" # according to :default => {:excel => :current} the workbook is opened
29
28
  end # in the Excel instance the was created first
30
29
  sleep 2
31
30
  new_excel = Excel.new(:reuse => false)
32
- book4 = Workbook.open(file_name3, :default => {:excel => new_excel}, :visible => true) # open another book
33
- if book4.excel == new_excel then # since this book cannot be reopened, the option :default_excel applies:
34
- p "book4 opened in the second Excel" # according to :default_excel => new_excel the book is opened
31
+ book4 = Workbook.open(file_name3, :default => {:excel => new_excel}, :visible => true) # open another workbook
32
+ if book4.excel == new_excel then # since this workbook cannot be reopened, the option :default_excel applies:
33
+ p "book4 opened in the second Excel" # according to :default_excel => new_excel the workbook is opened
35
34
  end # in the given Excel, namely the second Excel instance new_excel
36
35
  sleep 2
37
- book5 = Workbook.open(file_name4, :default => {:excel => :new}, :visible => true) # open another book
38
- if ((not book5.excel == book1.excel) && (not book5.excel == new_excel)) then # since this book cannot be reopened,
36
+ book5 = Workbook.open(file_name4, :default => {:excel => :new}, :visible => true) # open another workbook
37
+ if ((not book5.excel == book1.excel) && (not book5.excel == new_excel)) then # since this workbook cannot be reopened,
39
38
  p "book5 opened in a third Excel" # the option :default_excel applies. according to :default_excel => :new
40
- end # the book is opened in a new Excel
39
+ end # the workbook is opened in a new Excel
41
40
  sleep 2
42
41
  book5.close
43
42
  book4.close
@@ -1,8 +1,8 @@
1
1
  # example_force_excel.rb:
2
2
  # opening books in new or given Excel instances using :force_excel
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -11,20 +11,19 @@ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  simple_file = dir + 'workbook.xls'
14
- book1 = Workbook.open(simple_file) # open a book in a new Excel instance since no Excel is open
15
- book1.excel.visible = true # make current Excel visible
14
+ book1 = Workbook.open(simple_file, :visible => true) # open a workbook in a new Excel instance since no Excel is open
16
15
  sleep 2
17
- book2 = Workbook.open(simple_file) # open a new book in the same Excel instance
18
- p "book1 == book2" if book2 == book1 # the books are identical
16
+ book2 = Workbook.open(simple_file) # open a new workbook in the same Excel instance
17
+ p "book1 == book2" if book2 == book1 # the workbooks are identical
19
18
  sleep 2
20
- book3 = Workbook.open(simple_file, :force => {:excel => :new, :visible => true}) # open another book in a new Excel instance,
21
- p "book3 != book1" if (not (book3 == book1)) # the books are not identical
19
+ book3 = Workbook.open(simple_file, :force => {:excel => :new, :visible => true}) # open another workbook in a new Excel instance,
20
+ p "book3 != book1" if (not (book3 == book1)) # the workbooks are not identical
22
21
  sleep 2
23
22
  new_excel = Excel.new(:reuse => false) # create a third Excel instance
24
- book4 = Workbook.open(simple_file, :force => {:excel => new_excel, :visible => true}) # open another book in the new Excel instance
23
+ book4 = Workbook.open(simple_file, :force => {:excel => new_excel, :visible => true}) # open another workbook in the new Excel instance
25
24
  p "book4 != book3 && book4 != book1" if (not (book4 == book3) && (not (book4 == book1)))
26
25
  sleep 2 # (Excel chooses the first Excel application)
27
- book4.close # close the books
26
+ book4.close # close the workbooks
28
27
  book3.close
29
28
  book2.close
30
29
  book1.close
@@ -1,8 +1,8 @@
1
1
  # example_if_obstructed_close_if_saved.rb:
2
2
  # open with :if_obstructed: :close_if_saved
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -12,10 +12,10 @@ 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 = Workbook.open(file_name, :visible => true) # open a book, make Excel visible
15
+ book = Workbook.open(file_name, :visible => true) # open a workbook, make Excel visible
16
16
  sleep 1
17
17
  sheet = book.sheet(1)
18
- first_cell = sheet[1,1].Value # access a sheet
18
+ first_cell = sheet[1,1].Value # access a worksheet
19
19
  sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
20
20
  sleep 1
21
21
  begin
@@ -23,13 +23,13 @@ begin
23
23
  rescue WorkbookBlocked => msg
24
24
  puts "error: open: #{msg.message}"
25
25
  end
26
- book.save # save the unsaved book
27
- new_book = Workbook.open(file_name, :if_obstructed => :close_if_saved) # open the new book, close the saved book
26
+ book.save # save the unsaved workbook
27
+ new_book = Workbook.open(file_name, :if_obstructed => :close_if_saved) # open the new workbook, close the saved workbook
28
28
  sleep 1
29
29
  new_sheet = new_book.sheet(1)
30
30
  new_first_cell = new_sheet[1,1].Value
31
31
  puts "the old book was saved" unless new_first_cell == first_cell
32
- new_book.close # close the books
32
+ new_book.close # close the workbook
33
33
  ensure
34
34
  Excel.kill_all # close all workbooks, quit Excel application
35
35
  #rm_tmp(dir)
@@ -1,8 +1,8 @@
1
1
  # example_if_obstructed_forget.rb:
2
2
  # open with :if_obstructed: :forget, :new_excel
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -12,10 +12,10 @@ 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 = Workbook.open(file_name, :visible => true) # open a book, make Excel application visible
15
+ book = Workbook.open(file_name, :visible => true) # open a workbook, make Excel application visible
16
16
  sleep 3
17
17
  begin
18
- new_book = Workbook.open(other_file_name) # open a book with the same file name in a different path
18
+ new_book = Workbook.open(other_file_name) # open a workbook 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
@@ -25,7 +25,7 @@ begin
25
25
  # open another book with the same file name in a different path. Use a new Excel application
26
26
  another_book = Workbook.open(file_name, :if_obstructed => :new_excel, :visible => true)
27
27
  sleep 3
28
- new_book.close # close the books
28
+ new_book.close # close the workbooks
29
29
  another_book.close
30
30
  ensure
31
31
  Excel.kill_all # close all workbooks, quit Excel application
@@ -1,8 +1,8 @@
1
1
  # example_if_obstructed_save.rb:
2
2
  # open with :if_obstructed: :save
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -12,20 +12,20 @@ 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 = Workbook.open(file_name, :visible => true) # open a book, make Excel visible
15
+ book = Workbook.open(file_name, :visible => true) # open a workbook, make Excel visible
16
16
  sleep 1
17
17
  sheet = book.sheet(1)
18
- first_cell = sheet[1,1].Value # access a sheet
18
+ first_cell = sheet[1,1].Value # access a worksheet
19
19
  sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
20
20
  sleep 1
21
- new_book = Workbook.open(other_file_name, :if_obstructed => :save) # open a book with the same file name in a different path
22
- sleep 1 #save the old book, close it, before
21
+ new_book = Workbook.open(other_file_name, :if_obstructed => :save) # open a workbook with the same file name in a different path
22
+ sleep 1 #save the old workbook, close it, before
23
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
27
27
  puts "the old book was saved" unless old_first_cell == first_cell
28
- new_book.close # close the books
28
+ new_book.close # close the workbooks
29
29
  old_book.close
30
30
  ensure
31
31
  Excel.kill_all # close all workbooks, quit Excel application
@@ -1,8 +1,8 @@
1
1
  # example_ifunsaved_accept.rb:
2
2
  # open with :if_unsaved => :accept, close with :if_unsaved => :save
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -11,30 +11,30 @@ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
14
- book = Workbook.open(file_name) # open a book
15
- sheet = book.sheet(1) # access a sheet
14
+ book = Workbook.open(file_name, :visible => true) # open a workbook
15
+ sheet = book.sheet(1) # access a worksheet
16
16
  sheet[1,1] = sheet[1,1].Value == "simple" ? "complex" : "simple" # change a cell
17
17
  begin
18
- new_book = Workbook.open(file_name) # open another book with the same file name
19
- rescue WorkbookNotSaved => msg # by default: raises an exception:
20
- puts "error: open: #{msg.message}" # a book with the same name is already open and unsaved
18
+ new_book = Workbook.open(file_name) # open another workbook with the same file name
19
+ rescue WorkbookNotSaved => msg # by default: raises an exception:
20
+ puts "error: open: #{msg.message}" # a workbook with the same name is already open and unsaved
21
21
  end
22
- new_book = Workbook.open(file_name, :if_unsaved => :accept) # open another book with the same file name
23
- # and let the unsaved book open
22
+ new_book = Workbook.open(file_name, :if_unsaved => :accept) # open another workbook with the same file name
23
+ # and let the unsaved workbook 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
26
26
  end
27
- if book == new_book then # check whether the book are equal
27
+ if book == new_book then # check whether the workbook are equal
28
28
  puts "both books are equal"
29
29
  end
30
30
  begin
31
- book.close # close the book. by default: raises an exception:
32
- rescue WorkbookNotSaved => msg # book is unsaved
31
+ book.close # close the workbook. by default: raises an exception:
32
+ rescue WorkbookNotSaved => msg # workbook is unsaved
33
33
  puts "close error: #{msg.message}"
34
34
  end
35
35
  book.close(:if_unsaved => :save) # save the book before closing it
36
36
  puts "closed the book successfully with option :if_unsaved => :save"
37
- new_book.close # close the other book. It is already saved.
37
+ new_book.close # close the other workbook. It is already saved.
38
38
  ensure
39
39
  Excel.kill_all # close workbooks, quit Excel application
40
40
  rm_tmp(dir)
@@ -1,8 +1,8 @@
1
1
  # example_if_unsaved_forget.rb:
2
2
  # open with :if_unsaved => :forget, :new_excel, close with :if_unsaved => :save
3
3
 
4
- require File.expand_path('../../lib/robust_excel_ole', File.dirname(__FILE__))
5
- require File.join(File.dirname(File.expand_path(__FILE__)), '../../spec/helpers/create_temporary_dir')
4
+ require_relative '../../lib/robust_excel_ole'
5
+ require_relative '../../spec/helpers/create_temporary_dir'
6
6
  require "fileutils"
7
7
 
8
8
  include RobustExcelOle
@@ -11,28 +11,27 @@ Excel.kill_all
11
11
  begin
12
12
  dir = create_tmpdir
13
13
  file_name = dir + 'workbook.xls'
14
- book = Workbook.open(file_name) # open a book
14
+ book = Workbook.open(file_name, :visible => true) # open a workbook
15
15
  puts "file_name: #{file_name}"
16
- #book.excel.visible = true # make current Excel visible
17
16
  sleep 1
18
- sheet = book.sheet(1) # access a sheet
17
+ sheet = book.sheet(1) # access a worksheet
19
18
  first_cell = sheet[1,1].Value
20
19
  sheet[1,1] = first_cell == "simple" ? "complex" : "simple" # change a cell
21
20
  sleep 1
22
21
  puts "new_book: before"
23
22
  puts "file_name: #{file_name}"
24
- new_book = Workbook.open(file_name, :if_unsaved => :forget) # open another book with the same file name
25
- # and close the unsaved book without saving it
23
+ new_book = Workbook.open(file_name, :if_unsaved => :forget) # open another workbook with the same file name
24
+ # and close the unsaved workbook without saving it
26
25
  puts "new_book: after"
27
26
  sheet_new_book = new_book.sheet(1)
28
- if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].Value == first_cell then # check whether the unsaved book
27
+ if (not book.alive?) && new_book.alive? && sheet_new_book[1,1].Value == first_cell then # check whether the unsaved workbook
29
28
  puts "open with :if_unsaved => :forget : the unsaved book is closed and not saved." # is closed and was not saved
30
29
  end
31
30
  sleep 1
32
31
  sheet_new_book[1,1] = sheet_new_book[1,1].Value == "simple" ? "complex" : "simple" # change a cell
33
- # open another book in a new Excel application, and make Excel visible, leaving the unsaved book open
32
+ # open another workbook in a new Excel application, and make Excel visible, leaving the unsaved workbook open
34
33
  another_book = Workbook.open(file_name, :if_unsaved => :new_excel, :visible => true)
35
- sleep 3 # leaving the unsaved book open
34
+ sleep 3 # leaving the unsaved workbook open
36
35
  new_book.close(:if_unsaved => :forget )
37
36
  another_book.close
38
37
  ensure