rubyXL-addressing 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/lib/rubyXL/address.rb +69 -4
- data/lib/rubyXL/addressing.rb +5 -68
- data/lib/rubyXL/addressing/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b27386b029963ccb2f11d6e9324d41fb903f1c7a
|
4
|
+
data.tar.gz: 9f62f5a03c5212eed1d739256b26092a75404ef2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c4469b190ff194cd6e5bc8475e07c1b7939d9eacf496b911a359d80b973a88104be3eb59cbc36ddba39781d78ab9607658a506ac657a5bb1bd5f82b3416e890
|
7
|
+
data.tar.gz: c82f77ee0daa53f1a6dd5c04d57c4dab75d6bb69706fb47f8c83f7cf74cb4af658990a338829e0056bd1b1241d9aabbdf24df85c572aa546783a99cd0d98ebf0
|
data/.rubocop.yml
CHANGED
data/lib/rubyXL/address.rb
CHANGED
@@ -1,7 +1,72 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'rubyXL/objects/reference'
|
4
|
+
|
3
5
|
module RubyXL
|
4
6
|
class Address
|
7
|
+
ROW_REF_FORMAT = /\A[1-9]\d*\z/
|
8
|
+
COLUMN_REF_FORMAT = /\A[A-Z]+\z/
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# @param [Integer] ind
|
12
|
+
# @return [String]
|
13
|
+
def row_ind2ref(ind)
|
14
|
+
validate_index(:row, ind)
|
15
|
+
|
16
|
+
(ind + 1).to_s.freeze
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param [Integer] ind
|
20
|
+
# @return [String]
|
21
|
+
def column_ind2ref(ind)
|
22
|
+
validate_index(:column, ind)
|
23
|
+
|
24
|
+
ref = ''.dup
|
25
|
+
loop do
|
26
|
+
ref.prepend((ind % 26 + 65).chr)
|
27
|
+
ind = ind / 26 - 1
|
28
|
+
break if ind < 0
|
29
|
+
end
|
30
|
+
ref.freeze
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param [String, Symbol] ref
|
34
|
+
# @return [Integer]
|
35
|
+
def row_ref2ind(ref)
|
36
|
+
message = "invalid row #{ref.inspect}"
|
37
|
+
raise ArgumentError, message unless ROW_REF_FORMAT =~ ref
|
38
|
+
|
39
|
+
ref.to_s.to_i - 1
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param [String, Symbol] ref
|
43
|
+
# @return [Integer]
|
44
|
+
def column_ref2ind(ref)
|
45
|
+
message = "invalid column #{ref.inspect}"
|
46
|
+
raise ArgumentError, message unless COLUMN_REF_FORMAT =~ ref
|
47
|
+
|
48
|
+
ref.to_s.each_byte.reduce(0) { |a, e| a * 26 + (e - 64) } - 1
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def normalize(row_or_column, value)
|
54
|
+
case value
|
55
|
+
when String, Symbol
|
56
|
+
value = public_send("#{row_or_column}_ref2ind", value)
|
57
|
+
else
|
58
|
+
validate_index(row_or_column, value)
|
59
|
+
end
|
60
|
+
value
|
61
|
+
end
|
62
|
+
|
63
|
+
def validate_index(row_or_column, index)
|
64
|
+
message = "invalid #{row_or_column} #{index.inspect}"
|
65
|
+
raise TypeError, message unless index.is_a?(Integer)
|
66
|
+
raise ArgumentError, message unless index >= 0
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
5
70
|
# @param [RubyXL::Worksheet] worksheet
|
6
71
|
# @return [RubyXL::Worksheet]
|
7
72
|
attr_writer :worksheet
|
@@ -13,7 +78,7 @@ module RubyXL
|
|
13
78
|
def initialize(worksheet, ref: nil, row: nil, column: nil)
|
14
79
|
@worksheet = worksheet
|
15
80
|
|
16
|
-
row, column = Reference.ref2ind(ref) if ref
|
81
|
+
row, column = RubyXL::Reference.ref2ind(ref) if ref
|
17
82
|
self.row = row
|
18
83
|
self.column = column
|
19
84
|
end
|
@@ -51,18 +116,18 @@ module RubyXL
|
|
51
116
|
# @param [Integer, String, Symbol] row
|
52
117
|
# @return [Integer, String, Symbol]
|
53
118
|
def row=(row)
|
54
|
-
@row =
|
119
|
+
@row = self.class.__send__(:normalize, :row, row)
|
55
120
|
end
|
56
121
|
|
57
122
|
# @param [Integer, String, Symbol] column
|
58
123
|
# @return [Integer, String, Symbol]
|
59
124
|
def column=(column)
|
60
|
-
@column =
|
125
|
+
@column = self.class.__send__(:normalize, :column, column)
|
61
126
|
end
|
62
127
|
|
63
128
|
# @return [String]
|
64
129
|
def ref
|
65
|
-
Reference.ind2ref(@row, @column)
|
130
|
+
RubyXL::Reference.ind2ref(@row, @column)
|
66
131
|
end
|
67
132
|
|
68
133
|
# @return [String]
|
data/lib/rubyXL/addressing.rb
CHANGED
@@ -1,75 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rubyXL/addressing/version'
|
4
|
-
require 'rubyXL'
|
5
4
|
|
6
5
|
module RubyXL
|
7
6
|
autoload :Address, 'rubyXL/address'
|
8
7
|
|
9
8
|
module Addressing
|
10
|
-
ROW_REF_FORMAT = /\A[1-9]\d*\z/
|
11
|
-
COLUMN_REF_FORMAT = /\A[A-Z]+\z/
|
12
|
-
|
13
|
-
class << self
|
14
|
-
# @param [Integer] ind
|
15
|
-
# @return [String]
|
16
|
-
def row_ind2ref(ind)
|
17
|
-
validate_index(:row, ind)
|
18
|
-
|
19
|
-
(ind + 1).to_s.freeze
|
20
|
-
end
|
21
|
-
|
22
|
-
# @param [Integer] ind
|
23
|
-
# @return [String]
|
24
|
-
def column_ind2ref(ind)
|
25
|
-
validate_index(:column, ind)
|
26
|
-
|
27
|
-
ref = ''.dup
|
28
|
-
loop do
|
29
|
-
ref.prepend((ind % 26 + 65).chr)
|
30
|
-
ind = ind / 26 - 1
|
31
|
-
break if ind < 0
|
32
|
-
end
|
33
|
-
ref.freeze
|
34
|
-
end
|
35
|
-
|
36
|
-
# @param [String, Symbol] ref
|
37
|
-
# @return [Integer]
|
38
|
-
def row_ref2ind(ref)
|
39
|
-
message = "invalid row #{ref.inspect}"
|
40
|
-
raise ArgumentError, message unless ROW_REF_FORMAT =~ ref
|
41
|
-
|
42
|
-
ref.to_s.to_i - 1
|
43
|
-
end
|
44
|
-
|
45
|
-
# @param [String, Symbol] ref
|
46
|
-
# @return [Integer]
|
47
|
-
def column_ref2ind(ref)
|
48
|
-
message = "invalid column #{ref.inspect}"
|
49
|
-
raise ArgumentError, message unless COLUMN_REF_FORMAT =~ ref
|
50
|
-
|
51
|
-
ref.to_s.each_byte.reduce(0) { |a, e| a * 26 + (e - 64) } - 1
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
def normalize(row_or_column, value)
|
57
|
-
case value
|
58
|
-
when String, Symbol
|
59
|
-
value = public_send("#{row_or_column}_ref2ind", value)
|
60
|
-
else
|
61
|
-
validate_index(row_or_column, value)
|
62
|
-
end
|
63
|
-
value
|
64
|
-
end
|
65
|
-
|
66
|
-
def validate_index(row_or_column, index)
|
67
|
-
message = "invalid #{row_or_column} #{index.inspect}"
|
68
|
-
raise TypeError, message unless index.is_a?(Integer)
|
69
|
-
raise ArgumentError, message unless index >= 0
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
9
|
# @param [String, Symbol, Integer] ref_or_row is ref `'A1'`
|
74
10
|
# or row index `0`
|
75
11
|
# or row label `'1'`
|
@@ -78,12 +14,13 @@ module RubyXL
|
|
78
14
|
# @return [RubyXL::Address]
|
79
15
|
def addr(ref_or_row, column = nil)
|
80
16
|
if column.nil?
|
81
|
-
Address.new(self, ref: ref_or_row)
|
17
|
+
RubyXL::Address.new(self, ref: ref_or_row)
|
82
18
|
else
|
83
|
-
Address.new(self, row: ref_or_row, column: column)
|
19
|
+
RubyXL::Address.new(self, row: ref_or_row, column: column)
|
84
20
|
end
|
85
21
|
end
|
86
22
|
end
|
87
|
-
|
88
|
-
Worksheet.include(Addressing)
|
89
23
|
end
|
24
|
+
|
25
|
+
require 'rubyXL'
|
26
|
+
RubyXL::Worksheet.include(RubyXL::Addressing)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyXL-addressing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masaki Takeuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyXL
|