robust_excel_ole 0.2.1 → 0.2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/README.rdoc +130 -77
  2. data/examples/edit_sheets/example_print_cells.rb +43 -0
  3. data/examples/open_save_close/example_control_to_excel.rb +10 -6
  4. data/examples/open_save_close/example_if_obstructed_closeifsaved.rb +9 -6
  5. data/examples/open_save_close/example_if_obstructed_forget.rb +9 -6
  6. data/examples/open_save_close/example_if_obstructed_save.rb +7 -5
  7. data/examples/open_save_close/example_if_unsaved_accept.rb +6 -3
  8. data/examples/open_save_close/example_if_unsaved_forget.rb +7 -4
  9. data/examples/open_save_close/example_if_unsaved_forget_more.rb +38 -0
  10. data/examples/open_save_close/example_read_only.rb +8 -6
  11. data/examples/open_save_close/example_reuse.rb +7 -4
  12. data/examples/open_save_close/example_simple.rb +9 -5
  13. data/examples/save_sheets/example_save_sheets.rb +22 -14
  14. data/lib/robust_excel_ole.rb +1 -1
  15. data/lib/robust_excel_ole/book.rb +42 -55
  16. data/lib/robust_excel_ole/cell.rb +1 -1
  17. data/lib/robust_excel_ole/{excel_app.rb → excel.rb} +97 -89
  18. data/lib/robust_excel_ole/range.rb +1 -1
  19. data/lib/robust_excel_ole/robustexcelole.sublime-workspace +347 -347
  20. data/lib/robust_excel_ole/sheet.rb +1 -1
  21. data/lib/robust_excel_ole/version.rb +1 -1
  22. data/lib/spec_helper.rb +3 -3
  23. data/spec/book_spec.rb +53 -67
  24. data/spec/cell_spec.rb +10 -2
  25. data/spec/data/merge_cells.xls +0 -0
  26. data/spec/data/simple.xls +0 -0
  27. data/spec/{excel_app_spec.rb → excel_spec.rb} +52 -36
  28. data/spec/helpers/create_temporary_dir.rb +11 -0
  29. data/spec/helpers/key_sender.rb +1 -1
  30. data/spec/range_spec.rb +9 -1
  31. data/spec/sheet_spec.rb +11 -3
  32. data/spec/spec_helper.rb +3 -3
  33. metadata +9 -7
  34. data/examples/print_cells/example_print_cells.rb +0 -43
  35. data/robust_excel_ole_example.rb +0 -29
data/README.rdoc CHANGED
@@ -30,7 +30,7 @@ This is work in progress.
30
30
 
31
31
  Example:
32
32
 
33
- book = RobustExcelOle::Book.open('./sample.xls')
33
+ book = RobustExcelOle::Book.open('simple.xls')
34
34
 
35
35
  Open a book with a block.
36
36
  The semantics is similar to, e.g., File.open.
@@ -38,93 +38,60 @@ The semantics is similar to, e.g., File.open.
38
38
  RobustExcelOle::Book.open('./sample.xls') do |book|
39
39
  # do something
40
40
  end
41
-
42
41
 
43
42
  Options are the following:
44
43
 
45
- [:reuse] use an already open Excel-application (default: true)
46
- [:read_only] open in read-only mode (default: false)
47
- [:if_unsaved] :raise (default), :accept, :forget, :new_app, :excel
48
- [:if_obstructed] :raise (default), :forget, :save, :close_if_unsaved, :new_app
49
- [:displayalerts] allow display alerts in Excel (default: false)
50
- [:visible] make visibe in Excel (default: false)
44
+ +:read_only+:: open in read-only mode (default: +false+)
45
+ +:if_unsaved+:: +:raise+ (default), +:accept+, +:forget+, +:new_app+, +:alert+
46
+ +:if_obstructed+:: :raise (default), +:forget+, +:save+, +:close_if_unsaved+, +:new_app+
47
+ +:reuse+:: use an already open Excel-application (default: +true+)
48
+ +:excel+:: an Excel appication (default: +nil+)
49
+ +:displayalerts+:: allow display alerts in Excel (default: +false+)
50
+ +:visible+:: make visibe in Excel (default: +false+)
51
51
 
52
- The option :if_unsaved :
52
+ The option +:if_unsaved+ :
53
53
 
54
54
  If an unsaved book with the same name is open, then
55
-
56
- [:raise ] Raise an exeption. Don't open the book.
57
- [:accept] Let the unsaved book open.
58
- [:forget] Close the unsaved book, open the new book.
59
- [:new_app] Open the new book in a new Excel application
60
- [:excel] Give control to Excel.
61
55
 
62
- The option :if_obstructed :
56
+ +:raise+:: Raise an exeption. Don't open the book.
57
+ +:accept+:: Let the unsaved book open.
58
+ +:forget+:: Close the unsaved book, open the new book.
59
+ +:new_app+:: Open the new book in a new Excel application
60
+ +:alert+:: Give control to Excel.
63
61
 
64
- If a book with same name in a different path is open, then
65
-
66
- [:raise] Raise an exception. Don't open the book.
67
- [:forget] Close the old book, open the new book.
68
- [:save] Save the old book, close it, open the new book
69
- [:close_if_saved] Close the old book and open the new book, if the old book is saved, raise an exception otherwise
70
- [:new_app] Open the new book in a new Excel application.
71
-
72
- The values :displayalerts and :visible are reached to the class ExcelApp that controls opening and closing of Excel applications.
73
-
74
- Examples:
75
-
76
- Open a book.
77
-
78
- book = RobustExcelOle::Book.open('./sample.xls')
79
-
80
- Open another book in the same Excel application.
81
-
82
- new_book = RobustExcelOle::Book.open('./different.xls')
83
-
84
- Open another book in a new Excel applications.
62
+ The option +:if_obstructed+ :
85
63
 
86
- new_book = RobustExcelOle::Book.open('./different.xls', :reuse => false)
87
-
88
- Change the book (see below).
89
-
90
- sheet = book[0]
91
- book.add_sheet(sheet, :as => 'copyed_name')
64
+ If a book with same name in a different path is open, then
92
65
 
93
- Let the unsaved book open, when opening a book and a book with the same name is unsaved and open.
66
+ +:raise+:: Raise an exception. Don't open the book.
67
+ +:forget+:: Close the old book, open the new book.
68
+ +:save+:: Save the old book, close it, open the new book
69
+ +:close_if_saved+:: Close the old book and open the new book, if the old book is saved, raise an exception otherwise
70
+ +:new_app+:: Open the new book in a new Excel application.
94
71
 
95
- new_book = RobustExcelOle::Book.open('./sample.xls', :if_unsaved => accept)
72
+ The values :displayalerts and :visible are reached to the class Excel that controls opening and closing of Excel applications.
96
73
 
97
74
  === Close a book.
98
75
 
99
- Example:
76
+ Simple close.
100
77
 
101
78
  book.close
102
79
 
103
80
  Options are the following:
104
81
 
105
- [:if_unsaved] :raise (default), :save, :forget, :excel
106
- if the book is unsaved
82
+ +:if_unsaved+, +:raise+ (default), +:save+, +:forget+, +:alert+
107
83
 
108
- :raise
84
+ If the book is unsaved, then
109
85
 
110
- Raise an exception. Don't close the book.
111
-
112
- :save
113
-
114
- Save the book before closing it.
115
-
116
- :forget
117
-
118
- Close the book.
119
-
120
- :excel
121
-
122
- Give control to Excel.
86
+ +:raise+:: Raise an exception. Don't close the book.
87
+ +:save+:: Save the book before closing it.
88
+ +:forget+:: Close the book.
89
+ +:alert+:: Give control to Excel.
123
90
 
124
91
 
125
92
  === Save a book.
126
93
 
127
- Simple save:
94
+ Simple save.
128
95
 
129
96
  book.save
130
97
 
@@ -134,23 +101,16 @@ Save a book with a file name.
134
101
 
135
102
  Options are the following:
136
103
 
137
- [:if_exists] :raise (default), :overwrite, :excel
104
+ +:if_exists+:: +:raise+ (default), +:overwrite+, +:alert+
138
105
 
139
- The option :if_exists :
106
+ The option +:if_exists+ :
140
107
 
141
108
  If a book with the file name already exists, then
142
109
 
143
- [:raise] Raise an exeption. Don't write the file.
144
- [:overwrite] Delete the existing file and write the file.
145
- If the book is open in an Excel application, then raise an exeption.
146
- [:excel] Give the control to Excel.
147
-
148
-
149
- Example:
150
-
151
- Overwrite a book with the same name.
152
-
153
- book.save('./another_sample.xls', :if_exists => :overwrite)
110
+ +:raise+:: Raise an exeption. Don't write the file.
111
+ +:overwrite+:: Delete the existing file and write the file. If the book is open in an Excel application, then raise an exception.
112
+ +:alert+:: Give the control to Excel.
113
+
154
114
 
155
115
  === ==
156
116
 
@@ -174,7 +134,7 @@ A sheet object can be accessed with a Book#[] method.
174
134
 
175
135
  Access a sheet object with the sheet name.
176
136
 
177
- book['Sheet1']
137
+ book['Sheet1']
178
138
 
179
139
  === Access a row or a column.
180
140
 
@@ -204,6 +164,99 @@ Read a cell from a range object.
204
164
  row_range[0] => first cell in row_range
205
165
  column_range[1] => second cell in column_range
206
166
 
167
+ = Examples
168
+
169
+ Include robust_excel_ole.
170
+
171
+ include RobustExcelOle
172
+
173
+ === Example 1
174
+
175
+ Open a book.
176
+
177
+ book = Book.open('simple.xls')
178
+
179
+ Access a sheet.
180
+
181
+ sheet = book[0]
182
+
183
+ Change a cell.
184
+
185
+ sheet[0,0] = "new"
186
+
187
+ Save the book.
188
+
189
+ book.save
190
+
191
+ Save it with a different name, and overwrite if a file with this name exists.
192
+
193
+ book.save_as('different_simple.xls', :if_exists => :overwrite)
194
+
195
+ Close the book.
196
+
197
+ book.close
198
+
199
+ === Example 2
200
+
201
+ Open the book again in a new Excel application and make it visible.
202
+
203
+ book1.open('simple.xls', :reuse => false, :visible => true)
204
+
205
+ Add a sheet.
206
+
207
+ book1.add_sheet
208
+
209
+ Open a new book with the same name in a new Excel. Leave the unsaved book in the old Excel.
210
+
211
+ book2.open('simple.xls', :if_unsaved => :new_app)
212
+
213
+ Access a sheet and Change a cell.
214
+
215
+ sheet = book2[0]
216
+ sheet[0,0] = "another"
217
+
218
+ Open another book with the same name in the running Excel. Close the unsaved book.
219
+
220
+ book3.open('simple.xls', :if_unsaved => :forget)
221
+
222
+ Add a sheet.
223
+
224
+ book3.add_sheet
225
+
226
+ Close the book without saving it.
227
+
228
+ book3.close(:if_unsaved => :forget)
229
+
230
+ Close the first book and save it before.
231
+
232
+ book1.close(:if_unsaved => :save)
233
+
234
+ === Example 3
235
+
236
+ Open a book.
237
+
238
+ book1 = Book.open('simple.xls')
239
+
240
+ Open a book with the same name in a different path. Close the old book.
241
+
242
+ book2 = Book.open('more/simple.xls', :if_obstructed => :forget)
243
+
244
+ Change its cell.
245
+
246
+ sheet = book2[0]
247
+ sheet[0,0] = "new"
248
+
249
+ Open a book with the same name in a different path. Save and close the unsaved book before.
250
+
251
+ book3 = Book.open('simple.xls', :if_obstructed => :save)
252
+
253
+ Open a book with the same name in a different path. The other book is closed, since it is saved.
254
+
255
+ book4 = Book.open('more/simple.xls', :if_obstructed => :close_if_unsaved)
256
+
257
+ Close the book.
258
+
259
+ book4.close
207
260
 
208
261
 
209
262
  === Want to do more things
@@ -0,0 +1,43 @@
1
+ # example 1: open a book, print the cells, rows, and columns of a sheet
2
+
3
+ require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
4
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
5
+ require "fileutils"
6
+
7
+ include RobustExcelOle
8
+
9
+ Excel.close_all
10
+ begin
11
+ dir = create_tmpdir
12
+ simple_file = dir + 'simple.xls'
13
+ simple_save_file = dir + 'simple_save.xls'
14
+ File.delete simple_save_file rescue nil
15
+ book = Book.open(simple_file)
16
+ sheet = book[0]
17
+ cell = sheet[0,0]
18
+
19
+ sheet_enum = proc do |enum_method|
20
+ i = 0
21
+ sheet.send(enum_method) do |cell|
22
+ i = i + 1
23
+ puts "sheet.#{enum_method} #{i}: #{cell.value}"
24
+ end
25
+ end
26
+
27
+ sheet_enum[:each]
28
+ sheet_enum[:each_row]
29
+ sheet_enum[:each_column]
30
+
31
+ col_r = sheet.col_range(0,1..2).values
32
+ row_r = sheet.row_range(0,1..2).values
33
+ puts "row range of 1st row, 1..2: #{row_r}"
34
+ puts "column range of 1st column, 1..2: #{col_r}"
35
+ sheet[0,0] = "complex"
36
+ book.save
37
+ book.close
38
+ ensure
39
+ Excel.close_all
40
+ rm_tmp(dir)
41
+ end
42
+
43
+
@@ -1,15 +1,18 @@
1
- # example_gibe_control_to_excel.rb: open, close, save with giving control to Excel
1
+ # example_give_control_to_excel.rb:
2
+ # open, close, save with giving control to Excel
2
3
 
3
4
  require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
4
7
 
5
8
  include RobustExcelOle
6
9
 
7
- ExcelApp.close_all
10
+ Excel.close_all
8
11
  begin
9
- dir = 'c:/'
12
+ dir = create_tmpdir
10
13
  file_name = dir + 'simple.xls'
11
14
  book = Book.open(file_name) # open a book
12
- ExcelApp.reuse.Visible = true # make Excel visible
15
+ Excel.current.Visible = true # make Excel visible
13
16
  sleep 1
14
17
  sheet = book[0] # access a sheet
15
18
  sheet[0,0] = sheet[0,0].value == "simple" ? "complex" : "simple" # change a cell
@@ -20,7 +23,7 @@ begin
20
23
  puts "#{msg.message}" # an exeptions is raised
21
24
  end
22
25
  puts "new book has opened" if new_book
23
- ExcelApp.reuse.Visible = true
26
+ Excel.current.Visible = true
24
27
  begin
25
28
  book.close(:if_unsaved => :excel) # close the unsaved book.
26
29
  rescue ExcelUserCanceled => msg # user is asked whether the unsaved book shall be saved
@@ -36,5 +39,6 @@ begin
36
39
  new_book.close # close the new book, if the user chose to open it
37
40
  end
38
41
  ensure
39
- ExcelApp.close_all # close ALL workbooks, quit Excel application
42
+ Excel.close_all # close ALL workbooks, quit Excel application
43
+ rm_tmp(dir)
40
44
  end
@@ -1,15 +1,17 @@
1
- # example 10: open with :if_obstructed: :close_if_saved
1
+ # example_if_obstructed_close_if_saved.rb:
2
+ # open with :if_obstructed: :close_if_saved
2
3
 
3
4
  require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
4
7
 
5
8
  include RobustExcelOle
6
9
 
7
- ExcelApp.close_all
10
+ Excel.close_all
8
11
  begin
9
- dir = 'C:/'
12
+ dir = create_tmpdir
10
13
  file_name = dir + 'simple.xls'
11
- other_dir = 'C:/more_data/'
12
- other_file_name = other_dir + 'simple.xls'
14
+ other_file_name = dir + 'more_data/simple.xls'
13
15
  book = Book.open(file_name, :visible => true) # open a book, make Excel visible
14
16
  sleep 1
15
17
  sheet = book[0]
@@ -29,5 +31,6 @@ begin
29
31
  puts "the old book was saved" unless new_first_cell == first_cell
30
32
  new_book.close # close the books
31
33
  ensure
32
- ExcelApp.close_all # close all workbooks, quit Excel application
34
+ Excel.close_all # close all workbooks, quit Excel application
35
+ rm_tmp(dir)
33
36
  end
@@ -1,15 +1,17 @@
1
- # example_if_obstructed_forget.rb: open with :if_obstructed: :forget, :new_app
1
+ # example_if_obstructed_forget.rb:
2
+ # open with :if_obstructed: :forget, :new_app
2
3
 
3
4
  require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
4
7
 
5
8
  include RobustExcelOle
6
9
 
7
- ExcelApp.close_all
10
+ Excel.close_all
8
11
  begin
9
- dir = 'C:/'
12
+ dir = create_tmpdir
10
13
  file_name = dir + 'simple.xls'
11
- other_dir = 'C:/more_data/'
12
- other_file_name = other_dir + 'simple.xls'
14
+ other_file_name = dir + 'more_data/simple.xls'
13
15
  book = Book.open(file_name, :visible => true) # open a book, make Excel application visible
14
16
  sleep 3
15
17
  begin
@@ -26,5 +28,6 @@ begin
26
28
  new_book.close # close the books
27
29
  another_book.close
28
30
  ensure
29
- ExcelApp.close_all # close all workbooks, quit Excel application
31
+ Excel.close_all # close all workbooks, quit Excel application
32
+ rm_tmp(dir)
30
33
  end
@@ -2,15 +2,16 @@
2
2
  # open with :if_obstructed: :save
3
3
 
4
4
  require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
5
7
 
6
8
  include RobustExcelOle
7
9
 
8
- ExcelApp.close_all
10
+ Excel.close_all
9
11
  begin
10
- dir = 'C:/'
12
+ dir = create_tmpdir
11
13
  file_name = dir + 'simple.xls'
12
- other_dir = 'C:/more_data/'
13
- other_file_name = other_dir + 'simple.xls'
14
+ other_file_name = dir + 'more_data/simple.xls'
14
15
  book = Book.open(file_name, :visible => true) # open a book, make Excel visible
15
16
  sleep 1
16
17
  sheet = book[0]
@@ -27,5 +28,6 @@ begin
27
28
  new_book.close # close the books
28
29
  old_book.close
29
30
  ensure
30
- ExcelApp.close_all # close all workbooks, quit Excel application
31
+ Excel.close_all # close all workbooks, quit Excel application
32
+ rm_tmp(dir)
31
33
  end
@@ -2,12 +2,14 @@
2
2
  # open with :if_unsaved => :accept, close with :if_unsaved => :save
3
3
 
4
4
  require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
5
+ require File.join(File.dirname(__FILE__), '../../spec/helpers/create_temporary_dir')
6
+ require "fileutils"
5
7
 
6
8
  include RobustExcelOle
7
9
 
8
- ExcelApp.close_all
10
+ Excel.close_all
9
11
  begin
10
- dir = 'C:/'
12
+ dir = create_tmpdir
11
13
  file_name = dir + 'simple.xls'
12
14
  book = Book.open(file_name) # open a book
13
15
  sheet = book[0] # access a sheet
@@ -34,5 +36,6 @@ begin
34
36
  puts "closed the book successfully with option :if_unsaved => :save"
35
37
  new_book.close # close the other book. It is already saved.
36
38
  ensure
37
- ExcelApp.close_all # close workbooks, quit Excel application
39
+ Excel.close_all # close workbooks, quit Excel application
40
+ rm_tmp(dir)
38
41
  end