robust_excel_ole 0.2.0.8 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
+
5
+ require File.join(File.dirname(__FILE__), '../../spec/spec_helper')
6
+
7
+ require "fileutils"
8
+
9
+ include RobustExcelOle
10
+
11
+ ExcelApp.close_all
12
+ begin
13
+ #dir = 'C:/'
14
+ dir = create_tmpdir
15
+ simple_file = dir + 'simple.xls'
16
+ simple_save_file = dir + 'simple_save.xls'
17
+ File.delete simple_save_file rescue nil
18
+ book = Book.open(simple_file)
19
+ sheet = book[0]
20
+ cell = sheet[0,0]
21
+ i = 0
22
+ sheet.each do |cell|
23
+ i = i + 1
24
+ puts "#{i}. cell: #{cell.value}"
25
+ end
26
+ i = 0
27
+ sheet.each_row do |row|
28
+ i = i + 1
29
+ puts "#{i}. row: #{row.value}"
30
+ end
31
+ i = 0
32
+ sheet.each_column do |column|
33
+ i = i + 1
34
+ puts "#{i}. column: #{column.value}"
35
+ end
36
+ sheet[0,0] = "complex"
37
+ book.save
38
+ book.close
39
+ ensure
40
+ ExcelApp.close_all
41
+ end
42
+
43
+
@@ -0,0 +1,30 @@
1
+ # example 11: save the sheets of a book as separate books
2
+
3
+ require File.join(File.dirname(__FILE__), '../../lib/robust_excel_ole')
4
+
5
+ require "fileutils"
6
+
7
+ include RobustExcelOle
8
+
9
+ ExcelApp.close_all
10
+ begin
11
+ dir = 'C:/'
12
+ file_name = dir + 'book_with_blank.xls'
13
+ book = Book.open(file_name) # open a book
14
+ ExcelApp.reuse.Visible = true # make Excel visible
15
+ # 1. Bücher erstmal speichern, um sie öffnen zu können
16
+ i = 0
17
+ book.each do |sheet|
18
+ i = i + 1
19
+ puts "#{i}. sheet:"
20
+ file_name_sheet = file_name + "_sheet#{i}.xls"
21
+ puts "file_name_sheet: #{file_name_sheet}"
22
+ book.save_as(file_name_sheet)
23
+ end
24
+ # generate empty book
25
+ # book_sheet = ExcelApp.Workbooks.Add
26
+
27
+ ensure
28
+ ExcelApp.close_all # close workbooks, quit Excel application
29
+ end
30
+
@@ -12,23 +12,24 @@ module RobustExcelOle
12
12
  # opens a book.
13
13
  #
14
14
  # options:
15
- # :reuse (boolean) use an already open Excel-application (default: true)
16
- # :read_only (boolean) open in read-only mode (default: false)
17
- # :displayalerts (boolean) allow display alerts in Excel (default: false)
18
- # :visible (boolean) make visibe in Excel (default: false)
15
+ # :read_only open in read-only mode (default: false)
19
16
  # :if_unsaved if an unsaved book with the same name is open, then
20
- # :raise -> raise an exception (default)
17
+ # :raise -> raise an exception (default)
21
18
  # :forget -> close the unsaved book, open the new book
22
19
  # :accept -> let the unsaved book open
23
20
  # :excel -> give control to excel
24
21
  # :new_app -> open the new book in a new excel application
25
- # :if_blocking_other if a book with the same name in a different path is open, then
26
- # :raise -> raise an exception (default)
27
- # :forget -> close the old book, open the new book
28
- # :save_and_close -> save the old book, close it, open the new book
29
- # :close_or_raise -> close the old book and open the new book, if the old book is saved
30
- # raise an exception otherwise
31
- # :new_app -> open the new book in a new excel application
22
+ # :if_obstructed if a book with the same name in a different path is open, then
23
+ # :raise -> raise an exception (default)
24
+ # :forget -> close the old book, open the new book
25
+ # :save -> save the old book, close it, open the new book
26
+ # :close_if_saved -> close the old book and open the new book, if the old book is saved
27
+ # raise an exception otherwise
28
+ # :new_app -> open the new book in a new excel application
29
+ # :reuse use a running Excel-application (default: true)
30
+ # :excel_app an Excel application (default: nil)
31
+ # :displayalerts allow display alerts in Excel (default: false)
32
+ # :visible make visibe in Excel (default: false)
32
33
  def open(file, options={ :reuse => true}, &block)
33
34
  new(file, options, &block)
34
35
  end
@@ -38,31 +39,33 @@ module RobustExcelOle
38
39
  def initialize(file, opts={ }, &block)
39
40
  @options = {
40
41
  :reuse => true,
42
+ :excel_app => nil,
41
43
  :read_only => false,
42
44
  :if_unsaved => :raise,
43
- :if_blocking_other => :raise
45
+ :if_obstructed => :raise
44
46
  }.merge(opts)
45
47
  excel_app_options = {:reuse => true}.merge(opts).delete_if{|k,v|
46
- k== :read_only || k== :if_unsaved || k == :if_blocking_other}
48
+ k== :read_only || k== :if_unsaved || k == :if_obstructed}
47
49
  if not File.exist?(file)
48
50
  raise ExcelErrorOpen, "file #{file} not found"
49
51
  end
50
- @excel_app = ExcelApp.new(excel_app_options) # :nodoc:
52
+ @excel_app = @options[:excel_app] ? @options[:excel_app] : ExcelApp.new(excel_app_options)
51
53
  workbooks = @excel_app.Workbooks
52
54
  @workbook = workbooks.Item(File.basename(file)) rescue nil
53
55
  if @workbook then
54
- blocked_by_other_book = (File.basename(file) == File.basename(@workbook.Fullname)) &&
55
- (not (absolute_path(file) == @workbook.Fullname))
56
- if blocked_by_other_book then
57
- case @options[:if_blocking_other]
56
+ obstructed_by_other_book = (File.basename(file) == File.basename(@workbook.Fullname)) &&
57
+ (not (absolute_path(file) == @workbook.Fullname))
58
+ if obstructed_by_other_book then
59
+ # @workbook is not the desired workbook
60
+ case @options[:if_obstructed]
58
61
  when :raise
59
62
  raise ExcelErrorOpen, "blocked by a book with the same name in a different path"
60
63
  when :forget
61
64
  @workbook.Close
62
- when :save_and_close
65
+ when :save
63
66
  save unless @workbook.Saved
64
67
  @workbook.Close
65
- when :close_or_raise
68
+ when :close_if_saved
66
69
  if (not @workbook.Saved) then
67
70
  raise ExcelErrorOpen, "book with the same name in a different path is unsaved"
68
71
  else
@@ -73,10 +76,10 @@ module RobustExcelOle
73
76
  @excel_app = ExcelApp.new(excel_app_options)
74
77
  @workbook = nil
75
78
  else
76
- raise ExcelErrorOpen, ":if_blocking_other: invalid option"
79
+ raise ExcelErrorOpen, ":if_obstructed: invalid option"
77
80
  end
78
81
  else
79
- # book open, not saved, not blocked by other book
82
+ # book open, not saved, not obstructed by an other book
80
83
  if (not @workbook.Saved) then
81
84
  #p "book not saved"
82
85
  case @options[:if_unsaved]
@@ -221,13 +224,19 @@ module RobustExcelOle
221
224
  case opts[:if_exists]
222
225
  when :overwrite
223
226
  # if a book is open with the name of file, then raise error
224
- open_workbook = ExcelApp.reuse_if_possible.Workbooks(basename) rescue nil
225
- #workbook_file = @excel_app.Workbooks(basename) rescue nil
226
- if open_workbook == nil then
227
+ # bräuchte alle Excel-Applikationen.
228
+ # mit ExcelApp.reuse bzw. running_app bzw. connect bekomme ich nur die 1. Excel-Applikation
229
+ #open_workbook = ExcelApp.reuse.Workbooks(basename) rescue nil
230
+ begin
227
231
  File.delete(file)
228
- else
232
+ rescue
229
233
  raise ExcelErrorSave, "book is open and used in Excel"
230
234
  end
235
+ #if open_workbook == nil then
236
+ # File.delete(file)
237
+ #else
238
+ # raise ExcelErrorSave, "book is open and used in Excel"
239
+ #end
231
240
  when :excel
232
241
  old_displayalerts = @excel_app.DisplayAlerts # :nodoc:
233
242
  @excel_app.DisplayAlerts = true # :nodoc:
@@ -46,6 +46,7 @@ module RobustExcelOle
46
46
  Process.kill("KILL", pid)
47
47
  end
48
48
 
49
+ # frees all OLE objects in the object space
49
50
  def self.free_all_ole_objects
50
51
  anz_objekte = 0
51
52
  ObjectSpace.each_object(WIN32OLE) do |o|
@@ -96,24 +97,23 @@ module RobustExcelOle
96
97
  new(:reuse => false)
97
98
  end
98
99
 
99
- # uses a running Excel application, if such an application exists
100
+ # uses a running Excel application (connects), if such an Excel application exists
100
101
  # creates a new one, otherwise
101
- def self.reuse_if_possible
102
+ def self.reuse
102
103
  new(:reuse => true)
103
104
  end
104
105
 
105
106
  # returns an Excel application
106
- #
107
107
  # options:
108
- # :reuse (boolean) use an already running Excel application (default: true)
109
- # :displayalerts (boolean) allow display alerts in Excel (default: false)
110
- # :visible (boolean) make visible in Excel (default: false)
108
+ # :reuse use an already running Excel application (default: true)
109
+ # :displayalerts allow display alerts in Excel (default: false)
110
+ # :visible make visible in Excel (default: false)
111
111
  def self.new(options= {})
112
112
  options = {:reuse => true}.merge(options)
113
113
 
114
114
  ole_app = nil
115
115
  if options[:reuse] then
116
- ole_app = running_app
116
+ ole_app = options[:excel_app] ? options[:excel_app] : running_app
117
117
  if ole_app
118
118
  ole_app.DisplayAlerts = options[:displayalerts] unless options[:displayalerts]==nil
119
119
  ole_app.Visible = options[:visible] unless options[:visible]==nil
@@ -1,3 +1,3 @@
1
1
  module RobustExcelOle
2
- VERSION = "0.2.0.8"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -29,4 +29,5 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency "wdm", '>= 0.0.3'
30
30
  s.add_development_dependency "win32console", '>= 1.3.2'
31
31
  s.add_development_dependency "guard-rspec", '>= 2.1.1'
32
+ s.required_ruby_version = '>= 1.8.6'
32
33
  end
data/spec/book_spec.rb CHANGED
@@ -65,7 +65,7 @@ describe RobustExcelOle::Book do
65
65
  end
66
66
  end
67
67
 
68
- context "with excel_app" do
68
+ context "with attr_reader excel_app" do
69
69
  before do
70
70
  @new_book = RobustExcelOle::Book.open(@simple_file)
71
71
  end
@@ -79,6 +79,29 @@ describe RobustExcelOle::Book do
79
79
  end
80
80
  end
81
81
 
82
+ context "with :excel_app" do
83
+ it "should reuse the given excel application of the book" do
84
+ book1 = RobustExcelOle::Book.open(@simple_file, :reuse => false)
85
+ excel_app1 = book1.excel_app
86
+ book2 = RobustExcelOle::Book.open(@simple_file, :reuse => false)
87
+ excel_app2 = book2.excel_app
88
+ excel_app2.should_not == excel_app1
89
+ book3 = RobustExcelOle::Book.open(@simple_file)
90
+ excel_app3 = book3.excel_app
91
+ book4 = RobustExcelOle::Book.open(@simple_file, :excel_app => excel_app2)
92
+ excel_app4 = book4.excel_app
93
+ excel_app3.should == excel_app1
94
+ excel_app4.should == excel_app2
95
+ excel_app4.class.should == RobustExcelOle::ExcelApp
96
+ excel_app4.should be_a RobustExcelOle::ExcelApp
97
+ book1.close
98
+ book2.close
99
+ book3.close
100
+ book4.close
101
+ end
102
+ end
103
+
104
+
82
105
  context "with :read_only" do
83
106
  it "should be able to save, if :read_only => false" do
84
107
  book = RobustExcelOle::Book.open(@simple_file, :read_only => false)
@@ -186,7 +209,7 @@ describe RobustExcelOle::Book do
186
209
  }.to_not raise_error
187
210
  @book.should be_alive
188
211
  @new_book.should be_alive
189
- @new_book.filename.should == @book.filename
212
+ @new_book.should == @book
190
213
  end
191
214
 
192
215
  it "should open book and close old book, if :if_unsaved is :forget" do
@@ -267,62 +290,62 @@ describe RobustExcelOle::Book do
267
290
  @new_book.close rescue nil
268
291
  end
269
292
 
270
- it "should raise an error, if :if_blocking_other is :raise" do
293
+ it "should raise an error, if :if_obstructed is :raise" do
271
294
  expect {
272
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :raise)
295
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :raise)
273
296
  }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
274
297
  end
275
298
 
276
- it "should close the other book and open the new book, if :if_blocking_other is :forget" do
277
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :forget)
299
+ it "should close the other book and open the new book, if :if_obstructed is :forget" do
300
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :forget)
278
301
  @book.should_not be_alive
279
302
  @new_book.should be_alive
280
303
  @new_book.filename.downcase.should == @simple_file.downcase
281
304
  end
282
305
 
283
- it "should save the old book, close it, and open the new book, if :if_blocking_other is :save_and_close" do
284
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :save_and_close)
306
+ it "should save the old book, close it, and open the new book, if :if_obstructed is :save" do
307
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :save)
285
308
  @book.should_not be_alive
286
309
  @new_book.should be_alive
287
310
  @new_book.filename.downcase.should == @simple_file.downcase
288
- new_book = RobustExcelOle::Book.open(@simple_file)
289
- new_book.workbook.Worksheets.Count.should == @sheet_count
290
- new_book.close
311
+ old_book = RobustExcelOle::Book.open(@simple_file_other_path, :if_obstructed => :forget)
312
+ old_book.workbook.Worksheets.Count.should == @sheet_count + 1
313
+ old_book.close
291
314
  end
292
315
 
293
316
  it "should raise an error, if the old book is unsaved, and close the old book and open the new book,
294
- if :if_blocking_other is :close_or_raise" do
317
+ if :if_obstructed is :close_if_saved" do
295
318
  expect{
296
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :close_or_raise)
319
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :close_if_saved)
297
320
  }.to raise_error(ExcelErrorOpen, "book with the same name in a different path is unsaved")
298
321
  @book.save
299
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :close_or_raise)
322
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :close_if_saved)
300
323
  @book.should_not be_alive
301
324
  @new_book.should be_alive
302
325
  @new_book.filename.downcase.should == @simple_file.downcase
303
- new_book = RobustExcelOle::Book.open(@simple_file_other_path, :if_blocking_other => :forget)
304
- new_book.workbook.Worksheets.Count.should == @sheet_count + 1
305
- new_book.close
326
+ old_book = RobustExcelOle::Book.open(@simple_file_other_path, :if_obstructed => :forget)
327
+ old_book.workbook.Worksheets.Count.should == @sheet_count + 1
328
+ old_book.close
306
329
  end
307
330
 
308
- it "should open the book in a new excel application, if :if_blocking_other is :new_app" do
309
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :new_app)
331
+ it "should open the book in a new excel application, if :if_obstructed is :new_app" do
332
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :new_app)
310
333
  @book.should be_alive
311
334
  @new_book.should be_alive
312
335
  @new_book.filename.should_not == @book.filename
313
336
  @new_book.excel_app.should_not == @book.excel_app
314
337
  end
315
338
 
316
- it "should raise an error, if :if_blocking_other is default" do
339
+ it "should raise an error, if :if_obstructed is default" do
317
340
  expect {
318
341
  @new_book = RobustExcelOle::Book.open(@simple_file)
319
342
  }.to raise_error(ExcelErrorOpen, "blocked by a book with the same name in a different path")
320
343
  end
321
344
 
322
- it "should raise an error, if :if_blocking_other is invalid option" do
345
+ it "should raise an error, if :if_obstructed is invalid option" do
323
346
  expect {
324
- @new_book = RobustExcelOle::Book.open(@simple_file, :if_blocking_other => :invalid_option)
325
- }.to raise_error(ExcelErrorOpen, ":if_blocking_other: invalid option")
347
+ @new_book = RobustExcelOle::Book.open(@simple_file, :if_obstructed => :invalid_option)
348
+ }.to raise_error(ExcelErrorOpen, ":if_obstructed: invalid option")
326
349
  end
327
350
 
328
351
  end
Binary file
Binary file
data/spec/data/simple.xls CHANGED
Binary file
@@ -7,16 +7,6 @@ $VERBOSE = nil
7
7
  module RobustExcelOle
8
8
 
9
9
  describe ExcelApp do
10
- before do
11
- @dir = create_tmpdir
12
- @simple_file = @dir + '/simple.xls'
13
- end
14
-
15
- after do
16
- rm_tmp(@dir)
17
- end
18
-
19
- save_path = "C:" + "/" + "simple_save.xls"
20
10
 
21
11
  context "app creation" do
22
12
  after do
@@ -68,7 +58,7 @@ module RobustExcelOle
68
58
  end
69
59
 
70
60
  it "should reuse existing app" do
71
- app2 = ExcelApp.reuse_if_possible
61
+ app2 = ExcelApp.reuse
72
62
  #puts "@app1 #{@app1.Hwnd}"
73
63
  #puts "app2 #{app2.Hwnd}"
74
64
  app2.Hwnd.should == @app1.Hwnd
@@ -114,7 +104,7 @@ module RobustExcelOle
114
104
  end
115
105
 
116
106
  it "should be true with two identical excel applications" do
117
- app2 = ExcelApp.reuse_if_possible
107
+ app2 = ExcelApp.reuse
118
108
  app2.should == @app1
119
109
  end
120
110
 
@@ -131,6 +121,28 @@ module RobustExcelOle
131
121
 
132
122
  end
133
123
 
124
+
125
+ context "with :excel_app" do
126
+
127
+ before do
128
+ ExcelApp.close_all
129
+ end
130
+
131
+ after (:each) do
132
+ ExcelApp.close_all
133
+ end
134
+
135
+ it "should reuse in given excel app" do
136
+ app1 = ExcelApp.new(:reuse => false)
137
+ app2 = ExcelApp.new(:reuse => false)
138
+ app3 = ExcelApp.new(:excel_app => app1)
139
+ app4 = ExcelApp.new(:excel_app => app2)
140
+ app3.should == app1
141
+ app4.should == app2
142
+ end
143
+
144
+ end
145
+
134
146
  context "with Visible and DisplayAlerts" do
135
147
 
136
148
  before do
data/version.rb CHANGED
@@ -1,5 +1 @@
1
- require 'lib/robust_excel_ole/verson'
2
-
3
- module RobustExcelOle
4
- VERSION = "0.2.0"
5
- end
1
+ require File.expand_path('../lib/robust_excel_ole/version', __FILE__)