Class: RubyXL::Address
- Inherits:
-
Object
- Object
- RubyXL::Address
- Defined in:
- lib/rubyXL/address.rb
Constant Summary collapse
- ROW_REF_FORMAT =
/\A[1-9]\d*\z/
- COLUMN_REF_FORMAT =
/\A[A-Z]+\z/
Instance Attribute Summary collapse
Class Method Summary collapse
- .column_ind2ref(ind) ⇒ String
- .column_ref2ind(ref) ⇒ Integer
- .row_ind2ref(ind) ⇒ String
- .row_ref2ind(ref) ⇒ Integer
Instance Method Summary collapse
- #cell ⇒ RubyXL::Cell?
- #column(column = nil) ⇒ Integer, RubyXL::Address
- #column=(column) ⇒ Integer, ...
- #down(amount = 1) ⇒ RubyXL::Address
- #down!(amount = 1) ⇒ self
-
#initialize(worksheet, ref: nil, row: nil, column: nil) ⇒ Address
constructor
A new instance of Address.
- #inspect ⇒ String
- #left(amount = 1) ⇒ RubyXL::Address
- #left!(amount = 1) ⇒ self
- #ref ⇒ String
- #right(amount = 1) ⇒ RubyXL::Address
- #right!(amount = 1) ⇒ self
- #row(row = nil) ⇒ Integer, RubyXL::Address
- #row=(row) ⇒ Integer, ...
- #up(amount = 1) ⇒ RubyXL::Address
- #up!(amount = 1) ⇒ self
- #value ⇒ Object
- #value=(value) ⇒ Object
Constructor Details
#initialize(worksheet, ref: nil, row: nil, column: nil) ⇒ Address
Returns a new instance of Address.
83 84 85 86 87 88 89 |
# File 'lib/rubyXL/address.rb', line 83 def initialize(worksheet, ref: nil, row: nil, column: nil) @worksheet = worksheet row, column = RubyXL::Reference.ref2ind(ref) if ref self.row = row self.column = column end |
Instance Attribute Details
#worksheet(worksheet = nil) ⇒ RubyXL::Workbook, RubyXL::Address
93 94 95 96 97 98 99 |
# File 'lib/rubyXL/address.rb', line 93 def worksheet(worksheet = nil) if worksheet.nil? @worksheet else self.class.new(worksheet, row: @row, column: @column) end end |
Class Method Details
.column_ind2ref(ind) ⇒ String
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rubyXL/address.rb', line 26 def column_ind2ref(ind) validate_index(:column, ind) ref = ''.dup loop do ref.prepend((ind % 26 + 65).chr) ind = ind / 26 - 1 break if ind < 0 end ref.freeze end |
.column_ref2ind(ref) ⇒ Integer
49 50 51 52 53 54 |
# File 'lib/rubyXL/address.rb', line 49 def column_ref2ind(ref) = "invalid column #{ref.inspect}" raise ArgumentError, unless COLUMN_REF_FORMAT =~ ref ref.to_s.each_byte.reduce(0) { |a, e| a * 26 + (e - 64) } - 1 end |
.row_ind2ref(ind) ⇒ String
18 19 20 21 22 |
# File 'lib/rubyXL/address.rb', line 18 def row_ind2ref(ind) validate_index(:row, ind) (ind + 1).to_s.freeze end |
.row_ref2ind(ref) ⇒ Integer
40 41 42 43 44 45 |
# File 'lib/rubyXL/address.rb', line 40 def row_ref2ind(ref) = "invalid row #{ref.inspect}" raise ArgumentError, unless ROW_REF_FORMAT =~ ref ref.to_s.to_i - 1 end |
Instance Method Details
#cell ⇒ RubyXL::Cell?
196 197 198 |
# File 'lib/rubyXL/address.rb', line 196 def cell (row = @worksheet[@row]) && row[@column] end |
#column(column = nil) ⇒ Integer, RubyXL::Address
113 114 115 116 117 118 119 |
# File 'lib/rubyXL/address.rb', line 113 def column(column = nil) if column.nil? @column else self.class.new(@worksheet, row: @row, column: column) end end |
#column=(column) ⇒ Integer, ...
129 130 131 |
# File 'lib/rubyXL/address.rb', line 129 def column=(column) @column = self.class.__send__(:normalize, :column, column) end |
#down(amount = 1) ⇒ RubyXL::Address
151 152 153 |
# File 'lib/rubyXL/address.rb', line 151 def down(amount = 1) row(@row + amount) end |
#down!(amount = 1) ⇒ self
176 177 178 179 |
# File 'lib/rubyXL/address.rb', line 176 def down!(amount = 1) self.row += amount self end |
#inspect ⇒ String
139 140 141 |
# File 'lib/rubyXL/address.rb', line 139 def inspect format('#<%s %s!%s>', self.class.name, @worksheet.sheet_name, ref) end |
#left(amount = 1) ⇒ RubyXL::Address
157 158 159 |
# File 'lib/rubyXL/address.rb', line 157 def left(amount = 1) column(@column - amount) end |
#left!(amount = 1) ⇒ self
183 184 185 186 |
# File 'lib/rubyXL/address.rb', line 183 def left!(amount = 1) self.column -= amount self end |
#ref ⇒ String
134 135 136 |
# File 'lib/rubyXL/address.rb', line 134 def ref RubyXL::Reference.ind2ref(@row, @column) end |
#right(amount = 1) ⇒ RubyXL::Address
163 164 165 |
# File 'lib/rubyXL/address.rb', line 163 def right(amount = 1) column(@column + amount) end |
#right!(amount = 1) ⇒ self
190 191 192 193 |
# File 'lib/rubyXL/address.rb', line 190 def right!(amount = 1) self.column += amount self end |
#row(row = nil) ⇒ Integer, RubyXL::Address
103 104 105 106 107 108 109 |
# File 'lib/rubyXL/address.rb', line 103 def row(row = nil) if row.nil? @row else self.class.new(@worksheet, row: row, column: @column) end end |
#row=(row) ⇒ Integer, ...
123 124 125 |
# File 'lib/rubyXL/address.rb', line 123 def row=(row) @row = self.class.__send__(:normalize, :row, row) end |
#up(amount = 1) ⇒ RubyXL::Address
145 146 147 |
# File 'lib/rubyXL/address.rb', line 145 def up(amount = 1) row(@row - amount) end |
#up!(amount = 1) ⇒ self
169 170 171 172 |
# File 'lib/rubyXL/address.rb', line 169 def up!(amount = 1) self.row -= amount self end |
#value ⇒ Object
201 202 203 |
# File 'lib/rubyXL/address.rb', line 201 def value cell && cell.value end |
#value=(value) ⇒ Object
207 208 209 210 |
# File 'lib/rubyXL/address.rb', line 207 def value=(value) @worksheet.add_cell(@row, @column) unless cell cell.change_contents(value) end |