winexcel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in winexcel.gemspec
4
+ gemspec
@@ -0,0 +1,156 @@
1
+ winexcel
2
+ ========
3
+
4
+ [WinExcel] Makes it possible to access MS Excel from Ruby via Excel COM/Win32OLE interface. Enables to read and write from/to Excel files with the use of Excel's API. Requires MS Excel application to be installed.
5
+
6
+ Install
7
+ =======
8
+
9
+ gem install winexcel
10
+
11
+ Usage
12
+ =====
13
+
14
+ Basic example script
15
+
16
+ require 'rubygems'
17
+ require 'winexcel'
18
+ include WinExcel
19
+
20
+ #
21
+ # Opening existing file and reading all data
22
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "fixtures\\a.xls"))
23
+ xls = ExcelFile.new(file_full_path)
24
+ arrRecords = xls.get2DArray('A1')
25
+ puts arrRecords
26
+ #
27
+ # reading specified data
28
+ arrRecords = xls.get2DArray('A1:B2')
29
+ puts arrRecords
30
+ xls.close
31
+ #
32
+ # Writing some data to existing file
33
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "my_basic_file.xls"))
34
+ xls = ExcelFile.new(file_full_path)
35
+ arrRecords = []
36
+ arrRecords << ['ID', 'NAME', 'NICKNAME']
37
+ arrRecords << ['001', 'Fredrick White', 'fred']
38
+ arrRecords << ['002', 'Robert Green', 'bob']
39
+ xls.write2DArray(arrRecords, 'A1')
40
+ xls.save
41
+ xls.close
42
+ #
43
+ # Creating a new file from template
44
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "my_2nd_new_file.xls"))
45
+ template_full_path = File.expand_path(File.join(File.dirname(__FILE__), "fixtures\\template.xls"))
46
+ xls = ExcelFile.new(file_full_path, false, template_full_path)
47
+ arrRecords = []
48
+ arrRecords << ['Patrick Red', 'xyz', 'mail@xyz.com']
49
+ arrRecords << ['Martin Blue', 'abc']
50
+ xls.append2DArray(arrRecords)
51
+ xls.save
52
+ xls.close # not necessary as it would be handled by finalize
53
+ #
54
+ # Close any open file by calling finalize
55
+ ExcelFile.finalize # always call finalize at the end of a script
56
+
57
+ Extended example script
58
+
59
+ require 'rubygems'
60
+ require 'winexcel'
61
+ include WinExcel
62
+
63
+ arrRecords = []
64
+
65
+ # path to already created empty Excel file that we will write to
66
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "my_extended_file.xls"))
67
+
68
+ xls = ExcelFile.new(file_full_path)
69
+
70
+ begin
71
+
72
+ #### writing data
73
+
74
+ # writing array of arrays
75
+ arrRecords << ['ID', 'NAME', 'NICKNAME']
76
+ arrRecords << ['001', 'Fredrick White', 'fred']
77
+ arrRecords << ['002', 'Robert Green', 'bob']
78
+ xls.write2DArray(arrRecords, 'A1')
79
+
80
+ # appending array of arrays
81
+ arrRecords.clear
82
+ arrRecords << ['003', 'Patrick Red', 'pati']
83
+ arrRecords << ['004', 'Martin Blue', 'mati']
84
+ xls.append2DArray(arrRecords)
85
+
86
+ # lets delete the current sheet for now
87
+ xls.addSheet('hashesSheet')
88
+
89
+ # writing array of hashes
90
+ arrRecords.clear
91
+ arrRecords << {'ID'=>'001', 'NAME'=>'Fredrick White', 'NICKNAME'=>'fred'}
92
+ arrRecords << {'ID'=>'002', 'NAME'=>'Robert Green', 'NICKNAME'=>'bob'}
93
+ xls.writeArrayHash(arrRecords, 'A1')
94
+
95
+ # appending array of hashes
96
+ arrRecords.clear
97
+ arrRecords << {'ID'=>'003', 'NAME'=>'Patrick Red', 'NICKNAME'=>'pati'}
98
+ arrRecords << {'ID'=>'004', 'NAME'=>'Martin Blue', 'NICKNAME'=>'mati'}
99
+ xls.appendArrayHash(arrRecords)
100
+
101
+ # lets delete it to switch to the previous, default sheet
102
+ xls.deleteSheet('hashesSheet')
103
+
104
+ #### reading data
105
+
106
+ # we can specify a range of data to retrieve
107
+ arrRecords = xls.get2DArray('A1:C3')
108
+ p arrRecords
109
+
110
+ # if there is no range argument specified, then all occupied rows will be returned,
111
+ # giving us the whole file contents
112
+ arrRecords = xls.get2DArray()
113
+ p arrRecords
114
+
115
+ ensure
116
+ xls.save
117
+ xls.close # not necessary as it would be handled by finalize
118
+ # close any open file by calling finalize
119
+ ExcelFile.finalize # finalize it at the end of a script
120
+ end
121
+
122
+ For more examples please look at Cucumber features.
123
+
124
+ License
125
+ =======
126
+
127
+ Copyright (c) 2011 Kamil Sobieraj, ksob@dslowl.com
128
+
129
+ (New BSD License)
130
+
131
+ New BSD License claims:
132
+ Redistribution and use in source and binary forms, with or without
133
+ modification, are permitted provided that the following conditions
134
+ are met:
135
+
136
+ 1. Redistributions of source code must retain the above copyright notice,
137
+ this list of conditions and the following disclaimer.
138
+
139
+ 2. Redistributions in binary form must reproduce the above copyright notice,
140
+ this list of conditions and the following disclaimer in the documentation
141
+ and/or other materials provided with the distribution.
142
+
143
+ 3. Neither the name of Zend Technologies USA, Inc. nor the names of its
144
+ contributors may be used to endorse or promote products derived from this
145
+ software without specific prior written permission.
146
+
147
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
148
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
149
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
150
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
151
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
152
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
153
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
154
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
155
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
156
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ Dir['tasks/**/*.rake'].each { |f| load f }
5
+
6
+ desc "Run RSpec examples; Run cucumber:ok;"
7
+ task :default => [:spec, :features]
@@ -0,0 +1,49 @@
1
+ #
2
+ # code created for testing purposes
3
+ # and to demonstrate example usage
4
+ #
5
+ # Note: the library assumes that specified Excel file is already created
6
+ #
7
+ # For examples working out of the box please look at Cucumber/RSpec files
8
+ #
9
+
10
+ require 'rubygems'
11
+ require 'winexcel'
12
+ include WinExcel
13
+
14
+ #
15
+ # Opening existing file and reading all data
16
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "fixtures\\a.xls"))
17
+ xls = ExcelFile.new(file_full_path)
18
+ arrRecords = xls.get2DArray('A1')
19
+ puts arrRecords
20
+ #
21
+ # reading specified data
22
+ arrRecords = xls.get2DArray('A1:B2')
23
+ puts arrRecords
24
+ xls.close
25
+ #
26
+ # Writing some data to existing file
27
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "my_basic_file.xls"))
28
+ xls = ExcelFile.new(file_full_path)
29
+ arrRecords = []
30
+ arrRecords << ['ID', 'NAME', 'NICKNAME']
31
+ arrRecords << ['001', 'Fredrick White', 'fred']
32
+ arrRecords << ['002', 'Robert Green', 'bob']
33
+ xls.write2DArray(arrRecords, 'A1')
34
+ xls.save
35
+ xls.close
36
+ #
37
+ # Creating a new file from template
38
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "my_2nd_new_file.xls"))
39
+ template_full_path = File.expand_path(File.join(File.dirname(__FILE__), "fixtures\\template.xls"))
40
+ xls = ExcelFile.new(file_full_path, false, template_full_path)
41
+ arrRecords = []
42
+ arrRecords << ['Patrick Red', 'xyz', 'mail@xyz.com']
43
+ arrRecords << ['Martin Blue', 'abc']
44
+ xls.append2DArray(arrRecords)
45
+ xls.save
46
+ xls.close # not necessary as it would be handled by finalize
47
+ #
48
+ # Close any open file by calling finalize
49
+ ExcelFile.finalize # always call finalize at the end of a script
@@ -0,0 +1,73 @@
1
+ #
2
+ # code created for testing purposes
3
+ # and to demonstrate example usage
4
+ #
5
+ # Note: the library assumes that specified Excel file is already created
6
+ #
7
+ # For examples working out of the box please look at Cucumber/RSpec files
8
+ #
9
+
10
+ require 'rubygems'
11
+ require 'winexcel'
12
+ include WinExcel
13
+
14
+ arrRecords = []
15
+
16
+ # path to already created empty Excel file that we will write to
17
+ file_full_path = File.expand_path(File.join(File.dirname(__FILE__), "my_extended_file.xls"))
18
+
19
+ xls = ExcelFile.new(file_full_path)
20
+
21
+ begin
22
+
23
+ #### writing data
24
+
25
+ # writing array of arrays
26
+ arrRecords << ['ID', 'NAME', 'NICKNAME']
27
+ arrRecords << ['001', 'Fredrick White', 'fred']
28
+ arrRecords << ['002', 'Robert Green', 'bob']
29
+ xls.write2DArray(arrRecords, 'A1')
30
+
31
+ # appending array of arrays
32
+ arrRecords.clear
33
+ arrRecords << ['003', 'Patrick Red', 'pati']
34
+ arrRecords << ['004', 'Martin Blue', 'mati']
35
+ xls.append2DArray(arrRecords)
36
+
37
+ # lets delete the current sheet for now
38
+ xls.addSheet('hashesSheet')
39
+
40
+ # writing array of hashes
41
+ arrRecords.clear
42
+ arrRecords << {'ID'=>'001', 'NAME'=>'Fredrick White', 'NICKNAME'=>'fred'}
43
+ arrRecords << {'ID'=>'002', 'NAME'=>'Robert Green', 'NICKNAME'=>'bob'}
44
+ xls.writeArrayHash(arrRecords, 'A1')
45
+
46
+ # appending array of hashes
47
+ arrRecords.clear
48
+ arrRecords << {'ID'=>'003', 'NAME'=>'Patrick Red', 'NICKNAME'=>'pati'}
49
+ arrRecords << {'ID'=>'004', 'NAME'=>'Martin Blue', 'NICKNAME'=>'mati'}
50
+ xls.appendArrayHash(arrRecords)
51
+
52
+ # lets delete it to switch to the previous, default sheet
53
+ xls.deleteSheet('hashesSheet')
54
+
55
+ #### reading data
56
+
57
+ # we can specify a range of data to retrieve
58
+ arrRecords = xls.get2DArray('A1:C3')
59
+ p arrRecords
60
+
61
+ # if there is no range argument specified, then all occupied rows will be returned,
62
+ # giving us the whole file contents
63
+ arrRecords = xls.get2DArray()
64
+ p arrRecords
65
+
66
+ ensure
67
+ xls.save
68
+ xls.close # not necessary as it would be handled by finalize
69
+ # close any open file by calling finalize
70
+ ExcelFile.finalize # finalize it at the end of a script
71
+ end
72
+
73
+
Binary file
Binary file
@@ -0,0 +1,32 @@
1
+ # scenarios in this file are meant to be used by developers that want to use excel_file.rb functionality
2
+
3
+ Feature: connect_to_open_excel_file
4
+ As a developer
5
+ I want to connect to open Excel file
6
+ So that I can read and write to it
7
+
8
+ # this initial background processing try to simulate state of end-user system
9
+
10
+ Background: let say that the user executed some script using a button supplied in "../fixtures/temp/a.xls"
11
+ so we are sure the Excel workbook is open while that script is processing
12
+ and so we will open it here in background processing and read one cell to ensure the file is
13
+ open correctly before testing scenario that assume it is open correctly
14
+ Given I copy file "../fixtures/a.xls" to "../fixtures/temp" directory
15
+ And I open the "../fixtures/temp/a.xls" file manually
16
+ And I open the same file via script passing "../fixtures/temp/a.xls" as a parameter to ExcelFile.new
17
+ And I call get2DArray method of the returned object passing "A1" as a cell parameter
18
+ And I get [["aaa"]] back
19
+
20
+ Scenario: opening an existing file that has already been opened by the user before
21
+ and reading some data, then finalizing but NOT closing the file
22
+ since it was opened by user
23
+ Given I pass file="../fixtures/temp/a.xls" as parameter to ExcelFile.new
24
+ Then I should see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
25
+ When I call get2DArray method of the returned object passing "A1" as a cell parameter
26
+ Then I should get [["aaa"]] back
27
+ When I call ExcelFile.finalize
28
+ Then I should still see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
29
+
30
+
31
+
32
+
@@ -0,0 +1,35 @@
1
+ # scenarios in this file are meant to be used by developers that want to use excel_file.rb functionality
2
+
3
+ Feature: create_excel_file_from_template_and_open
4
+ As a developer
5
+ I want to create Excel file from template and open it
6
+ So that I can read and write to it
7
+
8
+ # this initial background processing try to simulate state of end-user system
9
+
10
+ Background:
11
+ Given I close "../fixtures/temp/a.xls" Excel file if it exists and is open
12
+ And I do not see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
13
+ And I delete "../fixtures/temp/a.xls" Excel file if it exists
14
+ And I close "../fixtures/temp/b.xls" Excel file if it exists and is open
15
+ And I do not see file "../fixtures/temp/b.xls" in RunningObjectTable (ROT)
16
+ And I delete "../fixtures/temp/b.xls" Excel file if it exists
17
+ And I copy files "../fixtures/a.xls" and "../fixtures/b.xlsx" to "../fixtures/temp" directory
18
+ And I open the "../fixtures/temp/a.xls" file manually
19
+ And I open the same file via script passing "../fixtures/temp/a.xls" as a parameter to ExcelFile.new
20
+ And I call get2DArray method of the returned object passing "A1" as a cell parameter
21
+ And I get [["aaa"]] back
22
+
23
+ Scenario: creating (and then opening) a new file from template when file with the same name exists there already and is open
24
+ Given I pass file="../fixtures/temp/a.xls", debug="false" and template="../fixtures/temp/b.xlsx" as parameters to ExcelFile.new
25
+ Then I should see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
26
+ When I call get2DArray method of the returned object passing "A1" as a cell parameter
27
+ Then I should get [["bbb"]] back
28
+ When I call ExcelFile.finalize
29
+ Then I should not see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
30
+
31
+
32
+
33
+
34
+
35
+
Binary file
Binary file
@@ -0,0 +1,41 @@
1
+ # scenarios in this file are meant to be used by developers that want to use excel_file.rb functionality
2
+
3
+ Feature: open_excel_file
4
+ As a developer
5
+ I want to open Excel file
6
+ So that I can read and write to it
7
+
8
+ # this initial background processing try to simulate state of end-user system
9
+
10
+ Background: let say that the user did NOT opened "../fixtures/temp/a.xls" but he executed
11
+ some script that will use this file
12
+ Given I close "../fixtures/temp/a.xls" Excel file if it exists and is open
13
+ And I do not see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
14
+ And I delete "../fixtures/temp/a.xls" Excel file if it exists
15
+ And I copy file "../fixtures/a.xls" to "../fixtures/temp" directory
16
+
17
+ Scenario: opening an existing file that has NOT already been opened by the user before
18
+ and reading some data, then we DO call close method and then finalize
19
+ to release automationInstance (Excel application)
20
+ Given I pass file="../fixtures/temp/a.xls" as parameter to ExcelFile.new
21
+ Then I should see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
22
+ When I call get2DArray method of the returned object passing "A1" as a cell parameter
23
+ Then I should get [["aaa"]] back
24
+ When I call close method
25
+ Then I should not see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
26
+ And I call ExcelFile.finalize
27
+
28
+ Scenario: opening an existing file that has NOT already been opened by the user before
29
+ and reading some data, then we DO NOT call close method but finalize method
30
+ to close that for us and release automationInstance (Excel application)
31
+ Given I pass file="../fixtures/temp/a.xls" as parameter to ExcelFile.new
32
+ Then I should see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
33
+ When I call get2DArray method of the returned object passing "A1" as a cell parameter
34
+ Then I should get [["aaa"]] back
35
+ When I call ExcelFile.finalize
36
+ Then I should not see file "../fixtures/temp/a.xls" in RunningObjectTable (ROT)
37
+
38
+
39
+
40
+
41
+
@@ -0,0 +1,71 @@
1
+ xls = nil
2
+ res = nil
3
+ rot = WIN32OLE::RunningObjectTable.new
4
+ if rot.nil? then
5
+ throw 'Cannot access WIN32OLE::RunningObjectTable'
6
+ end
7
+
8
+ Given /^I copy files "([^\"]*)" and "([^\"]*)" to "([^\"]*)" directory$/ do |file1, file2, dir|
9
+ FileUtils.createDirIfMissing File.expand_path(File.join(File.dirname(__FILE__), dir))
10
+ FileUtils.cp(File.expand_path(File.join(File.dirname(__FILE__), file1)), File.join(File.expand_path(File.join(File.dirname(__FILE__), dir)), File.basename(file1)))
11
+ FileUtils.cp(File.expand_path(File.join(File.dirname(__FILE__), file2)), File.join(File.expand_path(File.join(File.dirname(__FILE__), dir)), File.basename(file2)))
12
+ end
13
+ Given /^I copy file "([^\"]*)" to "([^\"]*)" directory$/ do |file1, dir|
14
+ FileUtils.createDirIfMissing File.expand_path(File.join(File.dirname(__FILE__), dir))
15
+ FileUtils.cp(File.expand_path(File.join(File.dirname(__FILE__), file1)), File.join(File.expand_path(File.join(File.dirname(__FILE__), dir)), File.basename(file1)))
16
+ end
17
+ When /^I open the "([^\"]*)" file manually$/ do |path|
18
+ full_path = File.expand_path(File.join(File.dirname(__FILE__), path))
19
+ system("start #{full_path}")
20
+ sleep 5
21
+ end
22
+ When /^I open the same file via script passing "([^\"]*)" as a parameter to ExcelFile\.new$/ do |path|
23
+ full_path = File.expand_path(File.join(File.dirname(__FILE__), path))
24
+ xls = WinExcel::ExcelFile.new(full_path)
25
+ end
26
+ When /^I call get2DArray method of the returned object passing "([^\"]*)" as a cell parameter$/ do |cell|
27
+ res = xls.get2DArray(cell)
28
+ end
29
+ When /^I get \[\["([^\"]*)"\]\] back$/ do |value|
30
+ res[0][0].should == value
31
+ end
32
+ res = nil
33
+ Given /^I pass file="([^\"]*)", debug="([^\"]*)" and template="([^\"]*)" as parameters to ExcelFile\.new$/ do |path, debug, template|
34
+ full_path = File.expand_path(File.join(File.dirname(__FILE__), path))
35
+ template_full_path = File.expand_path(File.join(File.dirname(__FILE__), template))
36
+ xls = WinExcel::ExcelFile.new(full_path, debug, template_full_path)
37
+ end
38
+ Given /^I pass file="([^\"]*)" as parameter to ExcelFile\.new$/ do |path|
39
+ full_path = File.expand_path(File.join(File.dirname(__FILE__), path))
40
+ xls = WinExcel::ExcelFile.new(full_path)
41
+ end
42
+ Then /^I (?:should|do)(?: still)? see file "([^\"]*)" in RunningObjectTable \(ROT\)$/ do |file|
43
+ rot.is_running?(File.expand_path(File.join(File.dirname(__FILE__), file))).should == true
44
+ end
45
+ Then /^I (?:should|do) not see file "([^\"]*)" in RunningObjectTable \(ROT\)$/ do |file|
46
+ rot.is_running?(File.expand_path(File.join(File.dirname(__FILE__), file))).should == false
47
+ end
48
+ Then /^I should get \[\["([^\"]*)"\]\] back$/ do |value|
49
+ res[0][0].should == value
50
+ end
51
+ When /^I call ExcelFile\.finalize$/ do
52
+ WinExcel::ExcelFile.finalize
53
+ end
54
+ When /^I call close method$/ do
55
+ xls.close
56
+ end
57
+ Given /^I close "([^\"]*)" Excel file if it exists and is open$/ do |file|
58
+ full_path = File.expand_path(File.join(File.dirname(__FILE__), file))
59
+ if File.exists?(full_path) and rot.is_running?(full_path)
60
+ xls = WinExcel::ExcelFile.new(full_path)
61
+ xls.close(forceClose=true)
62
+ WinExcel::ExcelFile.finalize
63
+ end
64
+ end
65
+ When /^I delete "([^\"]*)" Excel file if it exists$/ do |file|
66
+ full_path = File.expand_path(File.join(File.dirname(__FILE__), file))
67
+ begin
68
+ FileUtils.rm_rf(full_path)
69
+ rescue
70
+ end
71
+ end