rrxcell 0.1.0 → 0.2.0
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 +4 -4
- data/.gitignore +3 -0
- data/README.md +12 -9
- data/example/book.xls +0 -0
- data/example/book.xlsx +0 -0
- data/lib/rrxcell/addressing.rb +73 -0
- data/lib/rrxcell/book.rb +35 -2
- data/lib/rrxcell/cell.rb +13 -1
- data/lib/rrxcell/excel/book.rb +3 -3
- data/lib/rrxcell/excel/cell.rb +24 -1
- data/lib/rrxcell/excel/row.rb +3 -3
- data/lib/rrxcell/excel/sheet.rb +3 -3
- data/lib/rrxcell/excelx/book.rb +1 -1
- data/lib/rrxcell/excelx/cell.rb +28 -1
- data/lib/rrxcell/excelx/row.rb +1 -1
- data/lib/rrxcell/excelx/sheet.rb +3 -3
- data/lib/rrxcell/row.rb +4 -0
- data/lib/rrxcell/sheet.rb +20 -3
- data/lib/rrxcell/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35618ff57d747cacd603dd2d4771f08c71a2bba4
|
4
|
+
data.tar.gz: e7bd1240b834f139594b606046729ed5feb8c430
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 060bbc3ee2ba1bf40907683b7a82fcf545c81c77c3fa70ca6e69ccb6b3bffde2bbd8d1317e3ed37336f5af41dd7337c810bc1ffa8e1459432d7b05c2632945e7
|
7
|
+
data.tar.gz: cfc5c9021e196c8ecd46d8fcb24280200aa4bd3e6632a3454546c8b01fae35c144fea127433a7032757ac351f9e506544efdf7624ffbae5555df092dfa713592
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,8 @@ require "rrxcell"
|
|
25
25
|
|
26
26
|
#--------------------#
|
27
27
|
# Excelx file with '.xlsx'
|
28
|
-
|
28
|
+
# Excel file with '.xls'
|
29
|
+
path = File.expand_path("PATH/TO/FILE.xlsx") # or "PATH/TO/FILE.xls"
|
29
30
|
book = Rrxcell.load(path)
|
30
31
|
|
31
32
|
puts book.sheet(0).row(0).column(0).value
|
@@ -34,16 +35,18 @@ puts book.sheet(0).row(0).column(0).value
|
|
34
35
|
puts book[0][1][2].value
|
35
36
|
# => "Sheet1!C2"
|
36
37
|
|
37
|
-
|
38
|
-
#
|
39
|
-
path = File.expand_path("PATH/TO/FILE.xls")
|
40
|
-
book = Rrxcell.load(path)
|
38
|
+
puts book.address("Sheet1!B2").value
|
39
|
+
# => "Sheet1!B2"
|
41
40
|
|
42
|
-
puts book.sheet(0).
|
43
|
-
# => "Sheet1!
|
41
|
+
puts book.sheet(0).address("B2").value
|
42
|
+
# => "Sheet1!B2"
|
43
|
+
|
44
|
+
puts book.sheet_names
|
45
|
+
# => ["Sheet1"]
|
46
|
+
|
47
|
+
puts book.sheet("Sheet1").address("AB3").value
|
48
|
+
# => "Sheet1!AB3"
|
44
49
|
|
45
|
-
puts book[0][1][2].value
|
46
|
-
# => "Sheet1!C2"
|
47
50
|
|
48
51
|
```
|
49
52
|
|
data/example/book.xls
CHANGED
Binary file
|
data/example/book.xlsx
CHANGED
Binary file
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Rrxcell::Addressing
|
2
|
+
ALPHABET_CHARCTORS = 26
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def cell_address_to_column_position(cell_address)
|
6
|
+
v = 0
|
7
|
+
factor = 1
|
8
|
+
|
9
|
+
column_string(cell_address).reverse.each_char do |char|
|
10
|
+
_v = char.downcase.ord - "a".ord + 1
|
11
|
+
v += ((_v * factor) - 1)
|
12
|
+
factor += ALPHABET_CHARCTORS
|
13
|
+
end
|
14
|
+
|
15
|
+
v
|
16
|
+
end
|
17
|
+
|
18
|
+
def cell_address_to_row_column_position(cell_address)
|
19
|
+
[
|
20
|
+
cell_address_to_row_position(cell_address),
|
21
|
+
cell_address_to_column_position(cell_address)
|
22
|
+
]
|
23
|
+
end
|
24
|
+
|
25
|
+
def cell_address_to_row_position(cell_address)
|
26
|
+
row_string(cell_address).to_i - 1
|
27
|
+
end
|
28
|
+
|
29
|
+
def cell_address_to_sheet_position(cell_address, book = nil)
|
30
|
+
validate_book!(book)
|
31
|
+
book.sheet_names.index(sheet_string(cell_address))
|
32
|
+
end
|
33
|
+
|
34
|
+
def cell_address_to_sheet_row_column_position(cell_address, book = nil)
|
35
|
+
validate_book!(book)
|
36
|
+
[
|
37
|
+
cell_address_to_sheet_position(cell_address, book),
|
38
|
+
cell_address_to_row_column_position(cell_address)
|
39
|
+
].flatten
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def column_string(cell_address)
|
45
|
+
validate_cell_address!(cell_address)
|
46
|
+
cell_address.scan(/\!*([[:alpha:]]+)/).flatten.last
|
47
|
+
end
|
48
|
+
|
49
|
+
def row_string(cell_address)
|
50
|
+
validate_cell_address!(cell_address)
|
51
|
+
cell_address.match(/[0-9]+\z/).to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
def sheet_string(cell_address)
|
55
|
+
validate_cell_address!(cell_address)
|
56
|
+
cell_address.scan(/\A(.+)\!/).flatten.first
|
57
|
+
end
|
58
|
+
|
59
|
+
def validate_cell_address!(cell_address)
|
60
|
+
return true if cell_address.match(/\A[[:alpha:]]+[0-9]+\z/)
|
61
|
+
|
62
|
+
unless cell_address.match(/\A.+\![[:alpha:]]+[0-9]+\z/)
|
63
|
+
raise "invalid address"
|
64
|
+
end
|
65
|
+
true
|
66
|
+
end
|
67
|
+
|
68
|
+
def validate_book!(book)
|
69
|
+
raise "invalid Rrxcell::Book" unless book.is_a?(Rrxcell::Book)
|
70
|
+
true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/rrxcell/book.rb
CHANGED
@@ -6,16 +6,49 @@ class Rrxcell::Book
|
|
6
6
|
@path = path
|
7
7
|
end
|
8
8
|
|
9
|
-
def [](
|
10
|
-
|
9
|
+
def [](position_or_sheet_name)
|
10
|
+
case position_or_sheet_name
|
11
|
+
when Numeric
|
12
|
+
sheet(position_or_sheet_name)
|
13
|
+
when String
|
14
|
+
sheet(sheet_position_by_name!(position_or_sheet_name))
|
15
|
+
else
|
16
|
+
raise "invalid sheet position"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def address(cell_address)
|
21
|
+
sheet_position, row_position, column_position =
|
22
|
+
Rrxcell::Addressing.cell_address_to_sheet_row_column_position(cell_address, self)
|
23
|
+
|
24
|
+
sheet(sheet_position).row(row_position).column(column_position)
|
11
25
|
end
|
12
26
|
|
13
27
|
def sheet(position)
|
14
28
|
raise ShouldBeImplementedError
|
15
29
|
end
|
16
30
|
|
31
|
+
def sheets
|
32
|
+
sheet_names.map { |s| sheet(sheet_names.index(s)) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def sheet_names
|
36
|
+
object.sheets
|
37
|
+
end
|
38
|
+
|
17
39
|
protected
|
18
40
|
|
19
41
|
attr_reader :book
|
20
42
|
|
43
|
+
private
|
44
|
+
|
45
|
+
def cell_address_to_sheet_row_column_position(cell_address)
|
46
|
+
validate_cell_address!(cell_address)
|
47
|
+
end
|
48
|
+
|
49
|
+
def sheet_position_by_name!(name)
|
50
|
+
pos = sheet_names.index(name)
|
51
|
+
raise if pos.nil?
|
52
|
+
pos
|
53
|
+
end
|
21
54
|
end
|
data/lib/rrxcell/cell.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Rrxcell::Cell
|
2
|
-
attr_reader :row
|
2
|
+
attr_reader :row, :position
|
3
3
|
|
4
4
|
def initialize(row, position)
|
5
5
|
super()
|
@@ -7,6 +7,18 @@ class Rrxcell::Cell
|
|
7
7
|
@position = position
|
8
8
|
end
|
9
9
|
|
10
|
+
def book
|
11
|
+
row.book
|
12
|
+
end
|
13
|
+
|
14
|
+
def object
|
15
|
+
raise ShouldBeImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
def sheet
|
19
|
+
row.sheet
|
20
|
+
end
|
21
|
+
|
10
22
|
def value
|
11
23
|
raise ShouldBeImplementedError
|
12
24
|
end
|
data/lib/rrxcell/excel/book.rb
CHANGED
@@ -3,10 +3,10 @@ require "rrxcell/book"
|
|
3
3
|
|
4
4
|
class Rrxcell::Excel::Book < Rrxcell::Book
|
5
5
|
def object
|
6
|
-
@object ||= Roo::Excel.new(
|
6
|
+
@object ||= Roo::Excel.new(path)
|
7
7
|
end
|
8
8
|
|
9
|
-
def sheet(
|
10
|
-
Rrxcell::Excel::Sheet.new(self,
|
9
|
+
def sheet(sheet_position)
|
10
|
+
Rrxcell::Excel::Sheet.new(self, sheet_position)
|
11
11
|
end
|
12
12
|
end
|
data/lib/rrxcell/excel/cell.rb
CHANGED
@@ -2,7 +2,30 @@ require "rrxcell/excel"
|
|
2
2
|
require "rrxcell/cell"
|
3
3
|
|
4
4
|
class Rrxcell::Excel::Cell < Rrxcell::Cell
|
5
|
+
def type
|
6
|
+
book.object.celltype(row.position + 1 , position + 1, sheet.position)
|
7
|
+
end
|
8
|
+
|
5
9
|
def value
|
6
|
-
|
10
|
+
case type
|
11
|
+
when :time
|
12
|
+
value_as_time
|
13
|
+
else
|
14
|
+
raw_value
|
15
|
+
end
|
7
16
|
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def raw_value
|
21
|
+
book.object.cell(row.position + 1, position + 1, sheet.position)
|
22
|
+
end
|
23
|
+
|
24
|
+
def value_as_time
|
25
|
+
h = (raw_value / 3600).to_i
|
26
|
+
m = (((raw_value / 60).to_i) % 60).to_i
|
27
|
+
s = (raw_value % 60).to_i
|
28
|
+
today = Date.today
|
29
|
+
Time.new(today.year, today.month, today.day, h, m, s)
|
30
|
+
end
|
8
31
|
end
|
data/lib/rrxcell/excel/row.rb
CHANGED
@@ -2,11 +2,11 @@ require "rrxcell/excel"
|
|
2
2
|
require "rrxcell/row"
|
3
3
|
|
4
4
|
class Rrxcell::Excel::Row < Rrxcell::Row
|
5
|
-
def column(
|
6
|
-
Rrxcell::Excel::Cell.new(self,
|
5
|
+
def column(column_position)
|
6
|
+
Rrxcell::Excel::Cell.new(self, column_position)
|
7
7
|
end
|
8
8
|
|
9
9
|
def object
|
10
|
-
|
10
|
+
sheet.object.row(position)
|
11
11
|
end
|
12
12
|
end
|
data/lib/rrxcell/excel/sheet.rb
CHANGED
@@ -3,10 +3,10 @@ require "rrxcell/sheet"
|
|
3
3
|
|
4
4
|
class Rrxcell::Excel::Sheet < Rrxcell::Sheet
|
5
5
|
def object
|
6
|
-
@object ||=
|
6
|
+
@object ||= book.object.worksheets[position]
|
7
7
|
end
|
8
8
|
|
9
|
-
def row(
|
10
|
-
Rrxcell::Excel::Row.new(self,
|
9
|
+
def row(row_position)
|
10
|
+
Rrxcell::Excel::Row.new(self, row_position)
|
11
11
|
end
|
12
12
|
end
|
data/lib/rrxcell/excelx/book.rb
CHANGED
data/lib/rrxcell/excelx/cell.rb
CHANGED
@@ -2,7 +2,34 @@ require "rrxcell/excelx"
|
|
2
2
|
require "rrxcell/cell"
|
3
3
|
|
4
4
|
class Rrxcell::Excelx::Cell < Rrxcell::Cell
|
5
|
+
def type
|
6
|
+
sheet.object.celltype(row.position + 1 , position + 1)
|
7
|
+
end
|
8
|
+
|
9
|
+
def object
|
10
|
+
sheet.object.cell(row.position + 1, position + 1)
|
11
|
+
end
|
12
|
+
|
5
13
|
def value
|
6
|
-
|
14
|
+
case type
|
15
|
+
when :time
|
16
|
+
value_as_time
|
17
|
+
else
|
18
|
+
raw_value
|
19
|
+
end
|
7
20
|
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def raw_value
|
25
|
+
row.object.at(position)
|
26
|
+
end
|
27
|
+
|
28
|
+
def value_as_time
|
29
|
+
h = (raw_value / 3600).to_i
|
30
|
+
m = (((raw_value / 60).to_i) % 60).to_i
|
31
|
+
s = (raw_value % 60).to_i
|
32
|
+
today = Date.today
|
33
|
+
Time.new(today.year, today.month, today.day, h, m, s)
|
34
|
+
end
|
8
35
|
end
|
data/lib/rrxcell/excelx/row.rb
CHANGED
data/lib/rrxcell/excelx/sheet.rb
CHANGED
@@ -3,10 +3,10 @@ require "rrxcell/sheet"
|
|
3
3
|
|
4
4
|
class Rrxcell::Excelx::Sheet < Rrxcell::Sheet
|
5
5
|
def object
|
6
|
-
|
6
|
+
book.object.sheet(position)
|
7
7
|
end
|
8
8
|
|
9
|
-
def row(
|
10
|
-
Rrxcell::Excelx::Row.new(self,
|
9
|
+
def row(row_position)
|
10
|
+
Rrxcell::Excelx::Row.new(self, row_position)
|
11
11
|
end
|
12
12
|
end
|
data/lib/rrxcell/row.rb
CHANGED
data/lib/rrxcell/sheet.rb
CHANGED
@@ -7,11 +7,28 @@ class Rrxcell::Sheet
|
|
7
7
|
@position = position
|
8
8
|
end
|
9
9
|
|
10
|
-
def [](
|
11
|
-
row(
|
10
|
+
def [](row_position)
|
11
|
+
row(row_position)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
14
|
+
def address(cell_address)
|
15
|
+
row_position, column_position = Rrxcell::Addressing.cell_address_to_row_column_position(cell_address)
|
16
|
+
|
17
|
+
row(row_position).column(column_position)
|
18
|
+
end
|
19
|
+
|
20
|
+
def name
|
21
|
+
book.sheet_names[position]
|
22
|
+
end
|
23
|
+
|
24
|
+
def object
|
15
25
|
raise ShouldBeImplementedError
|
16
26
|
end
|
27
|
+
|
28
|
+
def row(row_position)
|
29
|
+
raise ShouldBeImplementedError
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
17
34
|
end
|
data/lib/rrxcell/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rrxcell
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sekizo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- example/book.xls
|
143
143
|
- example/book.xlsx
|
144
144
|
- lib/rrxcell.rb
|
145
|
+
- lib/rrxcell/addressing.rb
|
145
146
|
- lib/rrxcell/book.rb
|
146
147
|
- lib/rrxcell/cell.rb
|
147
148
|
- lib/rrxcell/errors/should_be_implemented_error.rb
|