excelgrip 0.0.2
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/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +174 -0
- data/Rakefile +2 -0
- data/excelgrip.gemspec +23 -0
- data/lib/excelgrip/GripWrapper.rb +54 -0
- data/lib/excelgrip/version.rb +3 -0
- data/lib/excelgrip.rb +463 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 46b0f4cce294025f901a90db691d4b404f1409c1
|
4
|
+
data.tar.gz: bed79d72b0b89e9bc32c77dc9f6bf3e18e89f3e5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 67feb7837e2707b358409bd111d2a08e45e90c2de0de3a5740a20704242ef09f8a965a11851b00ef5869dbb8d580d1086d6c0510e774c01badd8cd76946289ea
|
7
|
+
data.tar.gz: a2e875ca9560e730bcc5e849851238141ba92889e732321d2c80c79eeb3b2ea3640e6d6e0b870a89632490a4dc210078328479c58c552f39e84b1e1524313db2
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 yac4423
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 yac4423
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# Excelgrip
|
2
|
+
Excelgrip is Ruby library to handle all Excel classes.
|
3
|
+
* Windows Only
|
4
|
+
* Necessary Microsoft Excel
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
# gem install excelgrip
|
8
|
+
|
9
|
+
= Usage
|
10
|
+
require 'excelgrip'
|
11
|
+
excel = Excelgrip::Excel.new
|
12
|
+
book = excel.open_book("./sample.xls")
|
13
|
+
sheet = book.sheets(1)
|
14
|
+
puts sheet["A1:B1"].value
|
15
|
+
sheet["A1:B1"].value = ["TEST", "Yap"]
|
16
|
+
|
17
|
+
= Documentation
|
18
|
+
In RubyDoc.Info, all Excelgrip classes are listed.
|
19
|
+
http://rubydoc.info/gems/excelgrip/
|
20
|
+
|
21
|
+
== Excelgrip::GripWrapper
|
22
|
+
Base class of all Excelgrip classes.
|
23
|
+
This class wraps a various Excel objects.
|
24
|
+
=== Methods
|
25
|
+
[raw]
|
26
|
+
Return raw Excel object which is wrapped by Excelgrip object.
|
27
|
+
|
28
|
+
== Excelgrip::Excel class
|
29
|
+
Wrap Excel object to get all classes of Excel class library.
|
30
|
+
excel = Excelgrip::Excel.new
|
31
|
+
book = excel.open_book("./sample.xls") # Workbook
|
32
|
+
sheet = book.sheets(1) # Worksheet
|
33
|
+
range = sheet["A1:B1"] # Range
|
34
|
+
=== Additional Methods
|
35
|
+
[open_book(filename)]
|
36
|
+
Open excel file. Return Workbook object. Modify workbook and save it, overwrite the excel file.
|
37
|
+
[copy_book(filename)]
|
38
|
+
Open excel file as a template file. Return Workbook object.
|
39
|
+
If you modify workbook and save, the excel file is not change.
|
40
|
+
[workbooks(index=nil)]
|
41
|
+
Return the workbook object specified by index.
|
42
|
+
When omit index, return Workbooks object.
|
43
|
+
|
44
|
+
== Excelgrip::Workbooks
|
45
|
+
Wrap Workbooks object to to use all properties and methods of Workbooks class.
|
46
|
+
[[index]]
|
47
|
+
Return the workbook object specified by index.
|
48
|
+
[size]
|
49
|
+
Return count of Workbook objects opened by Excel.
|
50
|
+
[each {|workbook| block}]
|
51
|
+
Calls the given block for each Workbook object in Excel.
|
52
|
+
excel.workbooks.each {|book|
|
53
|
+
p book
|
54
|
+
}
|
55
|
+
|
56
|
+
== Excelgrip::Workbook
|
57
|
+
Wrap Workbook object to to use all properties and methods of Workbook class.
|
58
|
+
[[sheetname]]
|
59
|
+
Return the worksheet object specified by sheetname in the book.
|
60
|
+
[worksheets(index=nil)]
|
61
|
+
Return the worksheet object specified by index.
|
62
|
+
When omit index, return Worksheets object.
|
63
|
+
|
64
|
+
sheets() is alias of worksheets().
|
65
|
+
[add_sheet(source_sheet)]
|
66
|
+
Add new worksheet which is copy of source_sheet.
|
67
|
+
[save(filename)]
|
68
|
+
Save the workbook to file.
|
69
|
+
[each_sheet {|sheet| block}]
|
70
|
+
Calls the given block for each Worksheet object in the Workbook.
|
71
|
+
book = excel.copy_book("./sample.xls")
|
72
|
+
book.each_sheet {|sheet|
|
73
|
+
p sheet.name
|
74
|
+
}
|
75
|
+
|
76
|
+
=== Excelgrip::Worksheets
|
77
|
+
Wrap Worksheets object to to use all properties and methods of Worksheets class.
|
78
|
+
[[index]]
|
79
|
+
Return Worksheet object specified by index.
|
80
|
+
[size]
|
81
|
+
Return count of Worksheet.
|
82
|
+
[each {|worksheet| block}]
|
83
|
+
Calls the given block for each Worksheet object in Worksheets.
|
84
|
+
book.worksheets.each {|book|
|
85
|
+
p book
|
86
|
+
}
|
87
|
+
|
88
|
+
|
89
|
+
== Excelgrip::Worksheet
|
90
|
+
Wrap Worksheet object to to use all properties and methods of Worksheet class.
|
91
|
+
[range(rangename)]
|
92
|
+
Return Range object specified by rangename.
|
93
|
+
(ex)
|
94
|
+
* sheet.range("A1")
|
95
|
+
* sheet.range("A1:N1")
|
96
|
+
* sheet.range("Title") # "Title" is a name of cell.
|
97
|
+
[range(cell1, cell2)]
|
98
|
+
Return Range object between cell1 and cell2.
|
99
|
+
(ex)
|
100
|
+
cell1=sheet.range("A1")
|
101
|
+
cell2=sheet.range("N1")
|
102
|
+
sheet.range(cell1, cell2)
|
103
|
+
[cells(v_pos, h_pos)]
|
104
|
+
Return Range object that location is (v_pos, h_pos). The upper left cell is cells(1, 1).
|
105
|
+
[[rangename]]
|
106
|
+
Return Range object specified by rangename.
|
107
|
+
range01 = sheet["A1:N1"]
|
108
|
+
[name]
|
109
|
+
Name of this sheet.
|
110
|
+
|
111
|
+
== Excelgrip::Range
|
112
|
+
Wrap Range ojject to use all properties and methods of Range class.
|
113
|
+
[value]
|
114
|
+
Return the value of the cell. When this Range object is composed by multi cells, this method return Array of values.
|
115
|
+
[text]
|
116
|
+
Alias of value method.
|
117
|
+
[value=]
|
118
|
+
Modify value of this cell. When this Range object is composed by multi cells, set Array to modify multi cells at once.
|
119
|
+
[name]
|
120
|
+
Return name of this cell or cells.
|
121
|
+
[v_pos]
|
122
|
+
Return virtical position of this cell or cells.
|
123
|
+
[h_pos]
|
124
|
+
Return horizontal position of this cell or cells.
|
125
|
+
[position]
|
126
|
+
Return virtical and horizontal position of this cell or cells.
|
127
|
+
[v_size]
|
128
|
+
Return virtical count of cells in the Range.
|
129
|
+
[h_size]
|
130
|
+
Return horizontal count of cells in the Range.
|
131
|
+
[meage]
|
132
|
+
Meage cells in the Rage.
|
133
|
+
[unmeage]
|
134
|
+
Unmeage cells in the Range.
|
135
|
+
[meagearea]
|
136
|
+
Returns a Range object that represents the merged range containing the specified cell.
|
137
|
+
[cells(v_pos, h_pos)]
|
138
|
+
Returns a Range object that represents the cells in my range.
|
139
|
+
The upper left cell of my Range is self.cells(1, 1).
|
140
|
+
[each_range {|cell| block}]
|
141
|
+
Calls the given block for each sub Range object in my Range.
|
142
|
+
(ex)
|
143
|
+
target_range = sheet["A1:N1"]
|
144
|
+
target_range.each {|cell|
|
145
|
+
p cell.value
|
146
|
+
}
|
147
|
+
[==(other_range)]
|
148
|
+
Compare other range object to my range object based on position.
|
149
|
+
[include?(other_range)]
|
150
|
+
Check the other range is contained in my range.
|
151
|
+
[insert_here(shift)]
|
152
|
+
Copy my Range in the sheet. "shift" specify the direction to move. Excel::XlShiftToRight or Excel::XlShiftDown.
|
153
|
+
When shift is nil, direction is automatically determined.
|
154
|
+
[delete(shift)]
|
155
|
+
Delete my Range in the sheet. "shift" specify the direction to move after delete my Range.
|
156
|
+
Excel::XlShiftToRight or Excel::XlShiftDown.
|
157
|
+
When shift is nil, direction is automatically determined.
|
158
|
+
[get_cursor]
|
159
|
+
Return RangeCursor ofject of my Range object.
|
160
|
+
|
161
|
+
== Excelgrip::RangeCursor
|
162
|
+
Sub Class of Range class. RangeCursor is movable Range.
|
163
|
+
[h_move(offset)]
|
164
|
+
Move cell to horizontal direction. Merged cells is counted one cell.
|
165
|
+
[v_move(offset)]
|
166
|
+
Move cell to virtical direction. Merged cells is counted one cell.
|
167
|
+
[delete(shift)]
|
168
|
+
Delete my Range in the sheet. "shift" specify the direction to move after delete my Range.
|
169
|
+
|
170
|
+
= Author
|
171
|
+
notesgrip@tech-notes.dyndns.org
|
172
|
+
|
173
|
+
= License
|
174
|
+
MIT
|
data/Rakefile
ADDED
data/excelgrip.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'excelgrip/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "excelgrip"
|
8
|
+
spec.version = Excelgrip::VERSION
|
9
|
+
spec.authors = ["yac4423"]
|
10
|
+
spec.email = ["excelgrip@tech-notes.dyndns.org"]
|
11
|
+
spec.summary = %q{Control Excel from Ruby Script.}
|
12
|
+
spec.description = %q{Control Excel from Ruby Script.}
|
13
|
+
spec.homepage = "https://github.com/yac4423/excelgrip"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Excelgrip
|
2
|
+
class GripWrapper
|
3
|
+
def initialize(raw_object)
|
4
|
+
if raw_object.methods.include?("raw")
|
5
|
+
@raw_object = raw_object.raw
|
6
|
+
else
|
7
|
+
@raw_object = raw_object
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def raw
|
12
|
+
@raw_object
|
13
|
+
end
|
14
|
+
|
15
|
+
def inspect()
|
16
|
+
"<#{self.class}>"
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
OLE_METHODS = [:Type, :Initialize]
|
22
|
+
def method_missing(m_id, *params)
|
23
|
+
unless OLE_METHODS.include?(m_id)
|
24
|
+
missing_method_name = m_id.to_s.downcase
|
25
|
+
methods.each {|method|
|
26
|
+
if method.to_s.downcase == missing_method_name
|
27
|
+
return send(method, *params)
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
# Undefined Method is throwed to raw_object
|
32
|
+
begin
|
33
|
+
@raw_object.send(m_id, *params)
|
34
|
+
rescue
|
35
|
+
raise $!,$!.message, caller
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def toRaw(target_obj)
|
40
|
+
target_obj.respond_to?("raw") ? target_obj.raw : target_obj
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class FileSystemObject
|
45
|
+
include Singleton
|
46
|
+
def initialize
|
47
|
+
@body = WIN32OLE.new('Scripting.FileSystemObject')
|
48
|
+
end
|
49
|
+
|
50
|
+
def fullpath(filename)
|
51
|
+
@body.getAbsolutePathName(filename)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/excelgrip.rb
ADDED
@@ -0,0 +1,463 @@
|
|
1
|
+
#!ruby -Ks
|
2
|
+
|
3
|
+
# ExcelGrip Ver.0.00 2014/9/21
|
4
|
+
# Copyright (C) 2014 Yac <excelgrip@tech-notes.dyndns.org>
|
5
|
+
# This software is released under the MIT License, see LICENSE.txt.
|
6
|
+
require 'win32ole'
|
7
|
+
require 'singleton'
|
8
|
+
require 'excelgrip/version'
|
9
|
+
require 'excelgrip/GripWrapper'
|
10
|
+
|
11
|
+
module Excelgrip
|
12
|
+
# ====================================================
|
13
|
+
# ============= Excel Application Class ==============
|
14
|
+
# ====================================================
|
15
|
+
class Excel < GripWrapper
|
16
|
+
def initialize(visible=true)
|
17
|
+
@raw_object = WIN32OLE.new('EXCEL.Application')
|
18
|
+
WIN32OLE.const_load(@raw_object, Excel)
|
19
|
+
@raw_object.visible = visible
|
20
|
+
@raw_object.displayAlerts = false
|
21
|
+
end
|
22
|
+
|
23
|
+
def copy_book(template_filename = nil)
|
24
|
+
if (template_filename == nil) or (template_filename == "") then
|
25
|
+
workbook = @raw_object.Workbooks.add
|
26
|
+
else
|
27
|
+
fullpath = FileSystemObject.instance.fullpath(template_filename)
|
28
|
+
workbook = @raw_object.workbooks.add(fullpath)
|
29
|
+
end
|
30
|
+
Workbook.new(workbook)
|
31
|
+
end
|
32
|
+
|
33
|
+
def open_book(filename)
|
34
|
+
fullpath = FileSystemObject.instance.fullpath(filename)
|
35
|
+
workbook = @raw_object.workbooks.open({'filename'=> fullpath})
|
36
|
+
Workbook.new(workbook)
|
37
|
+
end
|
38
|
+
|
39
|
+
def workbooks(index=nil)
|
40
|
+
if index
|
41
|
+
Workbook.new(@raw_object.Workbooks(index))
|
42
|
+
else
|
43
|
+
Workbooks.new(@raw_object.Workbooks)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def quit
|
48
|
+
@raw_object.Quit
|
49
|
+
@raw_object = nil
|
50
|
+
undef_my_method()
|
51
|
+
end
|
52
|
+
end
|
53
|
+
# ========================================================
|
54
|
+
# ============ Workbook Collection Class =================
|
55
|
+
# ========================================================
|
56
|
+
class Workbooks < GripWrapper
|
57
|
+
def item(index=nil)
|
58
|
+
Workbook.new(@raw_object.item(index))
|
59
|
+
end
|
60
|
+
|
61
|
+
def [](index)
|
62
|
+
Workbook.new(@raw_object.item(index))
|
63
|
+
end
|
64
|
+
|
65
|
+
def size
|
66
|
+
@raw_object.count
|
67
|
+
end
|
68
|
+
|
69
|
+
def each
|
70
|
+
@raw_object.each {|raw_workbook|
|
71
|
+
yield(Workbook.new(raw_workbook))
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
# ========================================================
|
78
|
+
# ============ Worksheet Collection Class ================
|
79
|
+
# ========================================================
|
80
|
+
class Worksheets < GripWrapper
|
81
|
+
def [](index)
|
82
|
+
raw_worksheet = @raw_object.item(index)
|
83
|
+
raw_worksheet.Activate
|
84
|
+
Worksheet.new(raw_worksheet)
|
85
|
+
end
|
86
|
+
|
87
|
+
alias item []
|
88
|
+
|
89
|
+
def size
|
90
|
+
@raw_object.count
|
91
|
+
end
|
92
|
+
|
93
|
+
def each
|
94
|
+
@raw_object.each {|raw_worksheet|
|
95
|
+
raw_worksheet.Activate
|
96
|
+
yield(Worksheet.new(raw_worksheet))
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# =============================================
|
102
|
+
# ============ Workbook Class =================
|
103
|
+
# =============================================
|
104
|
+
class Workbook < GripWrapper
|
105
|
+
def [](sheet_name)
|
106
|
+
raw_worksheet = @raw_object.Worksheets(sheet_name)
|
107
|
+
raw_worksheet.Activate
|
108
|
+
Worksheet.new(raw_worksheet)
|
109
|
+
end
|
110
|
+
|
111
|
+
alias open_sheet []
|
112
|
+
|
113
|
+
def worksheets(index=nil)
|
114
|
+
if index
|
115
|
+
self[index]
|
116
|
+
else
|
117
|
+
Worksheets.new(@raw_object.worksheets)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
alias sheets worksheets
|
122
|
+
|
123
|
+
def add_sheet(source_sheet)
|
124
|
+
last_sheet_no = @raw_object.worksheets.count
|
125
|
+
source_sheet.copy({'after' => @raw_object.worksheets(last_sheet_no)})
|
126
|
+
raw_worksheet = @raw_object.sheets(last_sheet_no + 1)
|
127
|
+
Worksheet.new(raw_worksheet)
|
128
|
+
end
|
129
|
+
|
130
|
+
def save(filename)
|
131
|
+
fullpath = FileSystemObject.instance.fullpath(filename)
|
132
|
+
@raw_object.saveAs({'filename'=>fullpath})
|
133
|
+
end
|
134
|
+
|
135
|
+
def each_sheet
|
136
|
+
@raw_object.worksheets.each {|raw_worksheet|
|
137
|
+
raw_worksheet.Activate
|
138
|
+
yield Worksheet.new(raw_worksheet)
|
139
|
+
}
|
140
|
+
end
|
141
|
+
|
142
|
+
def parent
|
143
|
+
@raw_object.parent
|
144
|
+
end
|
145
|
+
|
146
|
+
def close
|
147
|
+
@raw_object.Close
|
148
|
+
@raw_object = nil
|
149
|
+
undef_my_method()
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# =============================================
|
154
|
+
# ============== Worksheet Class ==============
|
155
|
+
# =============================================
|
156
|
+
class Worksheet < GripWrapper
|
157
|
+
def range(cell_1, cell_2=nil)
|
158
|
+
if cell_1.class == String
|
159
|
+
begin
|
160
|
+
raw_range = @raw_object.range(cell_1)
|
161
|
+
rescue WIN32OLERuntimeError
|
162
|
+
raise $!,$!.message, caller
|
163
|
+
end
|
164
|
+
else
|
165
|
+
raw_range = @raw_object.range(cell_1.raw, cell_2.raw)
|
166
|
+
end
|
167
|
+
Range.new(raw_range)
|
168
|
+
end
|
169
|
+
|
170
|
+
def cells(v_pos, h_pos)
|
171
|
+
begin
|
172
|
+
raw_range = @raw_object.Cells(v_pos, h_pos)
|
173
|
+
rescue WIN32OLERuntimeError
|
174
|
+
raise $!,$!.message, caller
|
175
|
+
end
|
176
|
+
Range.new(raw_range)
|
177
|
+
end
|
178
|
+
|
179
|
+
def [](cell_1)
|
180
|
+
begin
|
181
|
+
range(cell_1)
|
182
|
+
rescue WIN32OLERuntimeError
|
183
|
+
raise $!,$!.message, caller
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def delete
|
188
|
+
@raw_object.delete
|
189
|
+
@raw_object = nil
|
190
|
+
undef_my_method()
|
191
|
+
end
|
192
|
+
|
193
|
+
def name=(sheet_name)
|
194
|
+
@raw_object.name = sheet_name
|
195
|
+
end
|
196
|
+
|
197
|
+
def parent
|
198
|
+
Workbook.new(@raw_object.parent)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
# =========================================
|
203
|
+
# ============== Range Class ==============
|
204
|
+
# =========================================
|
205
|
+
class Range < GripWrapper
|
206
|
+
# Return value of cell.
|
207
|
+
# If range contains multi cells, return Array of value.
|
208
|
+
def value
|
209
|
+
if (h_size() == 1) and (v_size() == 1)
|
210
|
+
return @raw_object.value
|
211
|
+
end
|
212
|
+
|
213
|
+
return_arr = []
|
214
|
+
self.each_range {|inner_range|
|
215
|
+
return_arr.push inner_range.raw.value
|
216
|
+
}
|
217
|
+
if return_arr.size == 1
|
218
|
+
return return_arr[0]
|
219
|
+
else
|
220
|
+
return return_arr
|
221
|
+
end
|
222
|
+
end
|
223
|
+
alias text value
|
224
|
+
|
225
|
+
|
226
|
+
def value=(text)
|
227
|
+
if text.class == Array
|
228
|
+
text_arr = text.dup
|
229
|
+
else
|
230
|
+
text_arr = [text]
|
231
|
+
end
|
232
|
+
|
233
|
+
self.each_range {|inner_range|
|
234
|
+
inner_range.raw.value = text_arr.shift
|
235
|
+
}
|
236
|
+
end
|
237
|
+
alias text= value=
|
238
|
+
|
239
|
+
def name
|
240
|
+
book = parent.parent
|
241
|
+
name_list = book.names
|
242
|
+
name_list.each {|name_obj|
|
243
|
+
# puts "*** name_obj.name = #{name_obj.name}"
|
244
|
+
if Range.new(name_obj.RefersToRange) == self
|
245
|
+
return name_obj.name
|
246
|
+
end
|
247
|
+
}
|
248
|
+
@raw_object.Address("RowAbsolute"=>false, "ColumnAbsolute"=>false, "ReferenceStyle"=>Excel::XlA1)
|
249
|
+
end
|
250
|
+
|
251
|
+
def name=(range_name)
|
252
|
+
sheet = parent
|
253
|
+
book = sheet.parent
|
254
|
+
a1_address = @raw_object.Address("ReferenceStyle"=>Excel::XlA1)
|
255
|
+
# puts "*** Range.name=#{range_name}, address=#{a1_address}"
|
256
|
+
book.names.Add("Name"=>range_name, "RefersToLocal"=>"=#{sheet.name}!#{a1_address}")
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
def v_pos()
|
261
|
+
@raw_object.row
|
262
|
+
end
|
263
|
+
|
264
|
+
def h_pos()
|
265
|
+
@raw_object.column
|
266
|
+
end
|
267
|
+
|
268
|
+
def position()
|
269
|
+
[@raw_object.row, @raw_object.column]
|
270
|
+
end
|
271
|
+
|
272
|
+
def v_size
|
273
|
+
@raw_object.rows.count
|
274
|
+
end
|
275
|
+
|
276
|
+
def h_size
|
277
|
+
@raw_object.columns.count
|
278
|
+
end
|
279
|
+
|
280
|
+
def merge()
|
281
|
+
orig_name = name()
|
282
|
+
@raw_object.Merge()
|
283
|
+
Range.new(@raw_object)
|
284
|
+
end
|
285
|
+
|
286
|
+
def unmerge()
|
287
|
+
top_cell = @raw_object.cells(1,1)
|
288
|
+
@raw_object.UnMerge()
|
289
|
+
Range.new(top_cell)
|
290
|
+
end
|
291
|
+
|
292
|
+
def mergearea()
|
293
|
+
Range.new(@raw_object.MergeArea)
|
294
|
+
end
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
def cells(v_pos, h_pos)
|
299
|
+
Range.new(@raw_object.cells(v_pos, h_pos))
|
300
|
+
end
|
301
|
+
|
302
|
+
def inspect()
|
303
|
+
return self.class unless @raw_object
|
304
|
+
if (v_size > 1) or (h_size > 1)
|
305
|
+
"Range:((V:#{v_pos},H:#{h_pos})-(V:#{v_pos+v_size-1},H:#{h_pos+h_size-1}))"
|
306
|
+
else
|
307
|
+
"Range:(V:#{v_pos},H:#{h_pos})"
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
def each_range()
|
312
|
+
my_h_size = h_size()
|
313
|
+
my_v_size = v_size()
|
314
|
+
|
315
|
+
merge_range_array = []
|
316
|
+
|
317
|
+
my_v_size.times {|v_offset|
|
318
|
+
my_h_size.times {|h_offset|
|
319
|
+
target_cell = Range.new(@raw_object.cells(v_offset+1, h_offset+1))
|
320
|
+
if target_cell.MergeCells
|
321
|
+
if operation_finished?(merge_range_array,target_cell)
|
322
|
+
# skip
|
323
|
+
else
|
324
|
+
merge_range_array.push target_cell.mergeArea
|
325
|
+
yield(target_cell.mergeArea.cells(1,1))
|
326
|
+
end
|
327
|
+
else
|
328
|
+
yield(target_cell)
|
329
|
+
end
|
330
|
+
}
|
331
|
+
}
|
332
|
+
end
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
def ==(other_range)
|
337
|
+
return false unless other_range.methods.include?("position")
|
338
|
+
if (position() == other_range.position()) and
|
339
|
+
(h_size() == other_range.h_size) and (v_size() == other_range.v_size)
|
340
|
+
return true
|
341
|
+
end
|
342
|
+
return false
|
343
|
+
end
|
344
|
+
|
345
|
+
def include?(inner_range)
|
346
|
+
my_v_pos, my_h_pos = position()
|
347
|
+
my_v_size = v_size()
|
348
|
+
my_h_size = h_size()
|
349
|
+
my_h_range = my_h_pos..(my_h_pos+my_h_size-1)
|
350
|
+
my_v_range = my_v_pos..(my_v_pos+my_v_size-1)
|
351
|
+
|
352
|
+
inner_v_pos, inner_h_pos = inner_range.position()
|
353
|
+
inner_v_size = inner_range.v_size()
|
354
|
+
inner_h_size = inner_range.h_size()
|
355
|
+
if ( my_h_range.include?(inner_h_pos) and my_h_range.include?(inner_h_pos+inner_h_size-1) and
|
356
|
+
my_v_range.include?(inner_v_pos) and my_v_range.include?(inner_v_pos+inner_v_size-1))
|
357
|
+
return true
|
358
|
+
else
|
359
|
+
return false
|
360
|
+
end
|
361
|
+
|
362
|
+
|
363
|
+
end
|
364
|
+
|
365
|
+
# shift = Excel::XlShiftToRight or Excel::XlShiftDown
|
366
|
+
def insert_here(shift=nil)
|
367
|
+
@raw_object.copy
|
368
|
+
if shift then
|
369
|
+
@raw_object.insert({'shift'=>shift})
|
370
|
+
else
|
371
|
+
@raw_object.insert # Auto Shift
|
372
|
+
end
|
373
|
+
self
|
374
|
+
end
|
375
|
+
|
376
|
+
# shift = Excel::XlShiftToLeft or Excel::XlShiftUp
|
377
|
+
def delete(shift=nil)
|
378
|
+
if shift then
|
379
|
+
@raw_object.delete({'shift'=>shift})
|
380
|
+
else
|
381
|
+
@raw_object.delete # Auto Shift
|
382
|
+
end
|
383
|
+
@raw_object = nil
|
384
|
+
undef_my_method()
|
385
|
+
end
|
386
|
+
|
387
|
+
def parent
|
388
|
+
Worksheet.new(@raw_object.parent)
|
389
|
+
end
|
390
|
+
|
391
|
+
def get_cursor()
|
392
|
+
RangeCursor.new(self)
|
393
|
+
end
|
394
|
+
alias get_cursol get_cursor # Compativirity old excel_lib. I missed the spell of "cursor".
|
395
|
+
|
396
|
+
private
|
397
|
+
def operation_finished?(merge_range_array,target_cell)
|
398
|
+
merge_range_array.each {|merge_range|
|
399
|
+
if merge_range.include?(target_cell)
|
400
|
+
return true
|
401
|
+
end
|
402
|
+
}
|
403
|
+
return false
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
# ===============================================
|
408
|
+
# ============== RangeCursor Class ==============
|
409
|
+
# ===============================================
|
410
|
+
class RangeCursor < Range
|
411
|
+
def h_move(offset)
|
412
|
+
return(self) if offset == 0
|
413
|
+
sheet = @raw_object.parent
|
414
|
+
sign = (offset.abs / offset) # +1 or -1
|
415
|
+
offset.abs.times do
|
416
|
+
v_pos, h_pos = position()
|
417
|
+
h_pos += (h_size() * sign)
|
418
|
+
h_pos = 1 if h_pos < 1
|
419
|
+
next_top_cell = sheet.cells(v_pos,h_pos)
|
420
|
+
next_bottom_cell = sheet.cells(v_pos + v_size() - 1, h_pos + h_size() -1)
|
421
|
+
next_range = sheet.range(next_top_cell, next_bottom_cell)
|
422
|
+
@raw_object = next_range
|
423
|
+
end
|
424
|
+
|
425
|
+
self.activate
|
426
|
+
self
|
427
|
+
end
|
428
|
+
|
429
|
+
def v_move(offset)
|
430
|
+
return(self) if offset == 0
|
431
|
+
sheet = @raw_object.parent
|
432
|
+
sign = (offset.abs / offset) # +1 or -1
|
433
|
+
offset.abs.times do
|
434
|
+
v_pos, h_pos = position()
|
435
|
+
v_pos += (v_size() * sign)
|
436
|
+
v_pos = 1 if v_pos < 1
|
437
|
+
next_top_cell = sheet.cells(v_pos, h_pos)
|
438
|
+
next_bottom_cell = sheet.cells(v_pos + v_size() - 1, h_pos + h_size() -1)
|
439
|
+
next_range = sheet.range(next_top_cell, next_bottom_cell)
|
440
|
+
@raw_object = next_range
|
441
|
+
end
|
442
|
+
self.activate
|
443
|
+
self
|
444
|
+
end
|
445
|
+
|
446
|
+
# return Excel::Range object
|
447
|
+
def range()
|
448
|
+
@raw_object
|
449
|
+
end
|
450
|
+
alias parent range
|
451
|
+
|
452
|
+
#undef delete
|
453
|
+
def delete(shift=nil)
|
454
|
+
range_name = @raw_object.name
|
455
|
+
parent_sheet = @raw_object.parent
|
456
|
+
@raw_object.delete(shift)
|
457
|
+
@raw_object = parent_sheet.range(range_name)
|
458
|
+
end
|
459
|
+
|
460
|
+
end
|
461
|
+
|
462
|
+
end
|
463
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: excelgrip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yac4423
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Control Excel from Ruby Script.
|
42
|
+
email:
|
43
|
+
- excelgrip@tech-notes.dyndns.org
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.rdoc
|
53
|
+
- Rakefile
|
54
|
+
- excelgrip.gemspec
|
55
|
+
- lib/excelgrip.rb
|
56
|
+
- lib/excelgrip/GripWrapper.rb
|
57
|
+
- lib/excelgrip/version.rb
|
58
|
+
homepage: https://github.com/yac4423/excelgrip
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.0.14
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Control Excel from Ruby Script.
|
82
|
+
test_files: []
|