Class: RubyXL::Reference
- Inherits:
-
Object
- Object
- RubyXL::Reference
- Defined in:
- lib/rubyXL/objects/reference.rb
Constant Summary collapse
- ROW_MAX =
1024*1024
- COL_MAX =
16393
Instance Attribute Summary collapse
-
#col_range ⇒ Object
readonly
Returns the value of attribute col_range.
-
#row_range ⇒ Object
readonly
Returns the value of attribute row_range.
Class Method Summary collapse
-
.ind2ref(row = 0, col = 0) ⇒ Object
Converts
row
andcol
zero-based indices to Excel-style cell reference (0) A…Z, AA…AZ, BA… -
.ref2ind(str) ⇒ Object
Converts Excel-style cell reference to
row
andcol
zero-based indices.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #cover?(other) ⇒ Boolean
- #first_col ⇒ Object
- #first_row ⇒ Object
-
#initialize(*params) ⇒ Reference
constructor
RubyXL::Reference.new(row, col) RubyXL::Reference.new(row_from, row_to, col_from, col_to) RubyXL::Reference.new(reference_string).
- #inspect ⇒ Object
- #last_col ⇒ Object
- #last_row ⇒ Object
- #single_cell? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(*params) ⇒ Reference
RubyXL::Reference.new(row, col) RubyXL::Reference.new(row_from, row_to, col_from, col_to) RubyXL::Reference.new(reference_string)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rubyXL/objects/reference.rb', line 11 def initialize(*params) row_from = row_to = col_from = col_to = nil case params.size when 4 then row_from, row_to, col_from, col_to = params when 2 then row_from, col_from = params when 1 then raise ArgumentError.new("invalid value for #{self.class}: #{params[0].inspect}") unless params[0].is_a?(String) from, to = params[0].split(':') row_from, col_from = self.class.ref2ind(from) row_to, col_to = self.class.ref2ind(to) unless to.nil? end @row_range = Range.new(row_from || 0, row_to || row_from || ROW_MAX) @col_range = Range.new(col_from || 0, col_to || col_from || COL_MAX) end |
Instance Attribute Details
#col_range ⇒ Object (readonly)
Returns the value of attribute col_range.
6 7 8 |
# File 'lib/rubyXL/objects/reference.rb', line 6 def col_range @col_range end |
#row_range ⇒ Object (readonly)
Returns the value of attribute row_range.
6 7 8 |
# File 'lib/rubyXL/objects/reference.rb', line 6 def row_range @row_range end |
Class Method Details
.ind2ref(row = 0, col = 0) ⇒ Object
Converts row
and col
zero-based indices to Excel-style cell reference (0) A…Z, AA…AZ, BA… …ZZ, AAA… …AZZ, BAA… …XFD (16383)
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rubyXL/objects/reference.rb', line 75 def self.ind2ref(row = 0, col = 0) str = '' loop do x = col % 26 str = ('A'.ord + x).chr + str col = (col / 26).floor - 1 break if col < 0 end str += (row + 1).to_s end |
.ref2ind(str) ⇒ Object
Converts Excel-style cell reference to row
and col
zero-based indices.
89 90 91 92 93 94 95 |
# File 'lib/rubyXL/objects/reference.rb', line 89 def self.ref2ind(str) return [ -1, -1 ] unless str =~ /\A([A-Z]+)(\d+)\Z/ col = 0 $1.each_byte { |chr| col = col * 26 + (chr - 64) } [ $2.to_i - 1, col - 1 ] end |
Instance Method Details
#==(other) ⇒ Object
48 49 50 |
# File 'lib/rubyXL/objects/reference.rb', line 48 def ==(other) !other.nil? && (@row_range == other.row_range) && (@col_range == other.col_range) end |
#cover?(other) ⇒ Boolean
52 53 54 55 |
# File 'lib/rubyXL/objects/reference.rb', line 52 def cover?(other) !other.nil? && (@row_range.cover?(other.row_range.begin) && @row_range.cover?(other.row_range.end) && @col_range.cover?(other.col_range.begin) && @col_range.cover?(other.col_range.end)) end |
#first_col ⇒ Object
40 41 42 |
# File 'lib/rubyXL/objects/reference.rb', line 40 def first_col @col_range.begin end |
#first_row ⇒ Object
32 33 34 |
# File 'lib/rubyXL/objects/reference.rb', line 32 def first_row @row_range.begin end |
#inspect ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/rubyXL/objects/reference.rb', line 65 def inspect if single_cell? then "#<#{self.class} @row=#{@row_range.begin} @col=#{@col_range.begin}>" else "#<#{self.class} @row_range=#{@row_range} @col_range=#{@col_range}>" end end |
#last_col ⇒ Object
44 45 46 |
# File 'lib/rubyXL/objects/reference.rb', line 44 def last_col @col_range.end end |
#last_row ⇒ Object
36 37 38 |
# File 'lib/rubyXL/objects/reference.rb', line 36 def last_row @row_range.end end |
#single_cell? ⇒ Boolean
28 29 30 |
# File 'lib/rubyXL/objects/reference.rb', line 28 def single_cell? (@row_range.begin == @row_range.end) && (@col_range.begin == @col_range.end) end |
#to_s ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rubyXL/objects/reference.rb', line 57 def to_s if single_cell? then self.class.ind2ref(@row_range.begin, @col_range.begin) else self.class.ind2ref(@row_range.begin, @col_range.begin) + ':' + self.class.ind2ref(@row_range.end, @col_range.end) end end |