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_from_absolute ⇒ Object
readonly
Returns the value of attribute col_from_absolute.
-
#col_to_absolute ⇒ Object
readonly
Returns the value of attribute col_to_absolute.
-
#first_col ⇒ Object
readonly
Returns the value of attribute first_col.
-
#first_row ⇒ Object
readonly
Returns the value of attribute first_row.
-
#last_col ⇒ Object
readonly
Returns the value of attribute last_col.
-
#last_row ⇒ Object
readonly
Returns the value of attribute last_row.
-
#row_from_absolute ⇒ Object
readonly
Returns the value of attribute row_from_absolute.
-
#row_to_absolute ⇒ Object
readonly
Returns the value of attribute row_to_absolute.
-
#sheet_name ⇒ Object
readonly
Returns the value of attribute sheet_name.
Class Method Summary collapse
-
.ind2ref(row = 0, col = 0, row_abs = false, col_abs = false) ⇒ Object
Converts
rowandcolzero-based indices to Excel-style cell reference <0> A...Z, AA...AZ, BA... -
.ref2ind(str) ⇒ Object
Converts Excel-style cell reference to
rowandcolzero-based indices.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #col_range ⇒ Object
- #cover?(other) ⇒ Boolean
-
#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) RubyXL::Reference.new(row_from:, row_to:, col_from:, col_to:).
- #inspect ⇒ Object
- #intersect?(other) ⇒ Boolean
- #row_range ⇒ Object
- #single_cell? ⇒ Boolean
- #to_s ⇒ Object
- #valid? ⇒ Boolean
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) RubyXL::Reference.new(row_from:, row_to:, col_from:, col_to:)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rubyXL/objects/reference.rb', line 16 def initialize(*params) row_from = row_to = col_from = col_to = nil @row_from_absolute = @row_to_absolute = @col_from_absolute = @col_to_absolute = false 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 case params.first when Hash then row_from, row_to, col_from, col_to = params.first.fetch_values(:row_from, :row_to, :col_from, :col_to) when String then str = params.first match_data = str.match(/^('(?<sheet_name1>[^']+)'|(?<sheet_name2>[^']+))!/) if match_data then @sheet_name = match_data['sheet_name1'] || match_data['sheet_name2'] str = str[match_data[0].size..-1] end from, to = str.split(':') row_from, col_from, @row_from_absolute, @col_from_absolute = self.class.ref2ind(from) row_to, col_to, @row_to_absolute, @col_to_absolute = self.class.ref2ind(to) unless to.nil? else raise(ArgumentError, "Invalid value for #{self.class}: #{params[0].inspect}") unless params[0].is_a?(String) end end @first_row = row_from || 0 @last_row = row_to || row_from || ROW_MAX @first_col = col_from || 0 @last_col = col_to || col_from || COL_MAX end |
Instance Attribute Details
#col_from_absolute ⇒ Object (readonly)
Returns the value of attribute col_from_absolute.
9 10 11 |
# File 'lib/rubyXL/objects/reference.rb', line 9 def col_from_absolute @col_from_absolute end |
#col_to_absolute ⇒ Object (readonly)
Returns the value of attribute col_to_absolute.
9 10 11 |
# File 'lib/rubyXL/objects/reference.rb', line 9 def col_to_absolute @col_to_absolute end |
#first_col ⇒ Object (readonly)
Returns the value of attribute first_col.
8 9 10 |
# File 'lib/rubyXL/objects/reference.rb', line 8 def first_col @first_col end |
#first_row ⇒ Object (readonly)
Returns the value of attribute first_row.
8 9 10 |
# File 'lib/rubyXL/objects/reference.rb', line 8 def first_row @first_row end |
#last_col ⇒ Object (readonly)
Returns the value of attribute last_col.
8 9 10 |
# File 'lib/rubyXL/objects/reference.rb', line 8 def last_col @last_col end |
#last_row ⇒ Object (readonly)
Returns the value of attribute last_row.
8 9 10 |
# File 'lib/rubyXL/objects/reference.rb', line 8 def last_row @last_row end |
#row_from_absolute ⇒ Object (readonly)
Returns the value of attribute row_from_absolute.
9 10 11 |
# File 'lib/rubyXL/objects/reference.rb', line 9 def row_from_absolute @row_from_absolute end |
#row_to_absolute ⇒ Object (readonly)
Returns the value of attribute row_to_absolute.
9 10 11 |
# File 'lib/rubyXL/objects/reference.rb', line 9 def row_to_absolute @row_to_absolute end |
#sheet_name ⇒ Object (readonly)
Returns the value of attribute sheet_name.
10 11 12 |
# File 'lib/rubyXL/objects/reference.rb', line 10 def sheet_name @sheet_name end |
Class Method Details
.ind2ref(row = 0, col = 0, row_abs = false, col_abs = false) ⇒ 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>
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rubyXL/objects/reference.rb', line 128 def self.ind2ref(row = 0, col = 0, row_abs = false, col_abs = false) col_ref = '' loop do x = col % 26 col_ref = ('A'.ord + x).chr + col_ref col = (col / 26).floor - 1 break if col < 0 end "#{col_abs ? '$' : ''}#{col_ref}#{row_abs ? '$' : ''}#{row + 1}" end |
.ref2ind(str) ⇒ Object
Converts Excel-style cell reference to row and col zero-based indices.
142 143 144 145 146 147 148 149 |
# File 'lib/rubyXL/objects/reference.rb', line 142 def self.ref2ind(str) matchdata = str.match(/\A(?<cabs>\$?)(?<col>[A-Z]+)(?<rabs>\$?)(?<row>\d+)\Z/) return [ -1, -1 ] unless matchdata [ matchdata['row'].to_i - 1, matchdata['col'].each_byte.inject(0) { |col, chr| (col * 26) + (chr - 64) } - 1, !matchdata['rabs'].empty?, !matchdata['cabs'].empty? ] end |
Instance Method Details
#==(other) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/rubyXL/objects/reference.rb', line 67 def ==(other) return false unless other.is_a?(RubyXL::Reference) return false unless @sheet_name == other.sheet_name (@first_row == other.first_row) && (@last_row == other.last_row) && (@first_col == other.first_col) && (@last_col == other.last_col) end |
#col_range ⇒ Object
54 55 56 57 |
# File 'lib/rubyXL/objects/reference.rb', line 54 def col_range warn "[DEPRECATION] `col_range` is deprecated. Please use `first_col` and `last_col` instead." @col_range ||= Range.new(@first_col, @last_col) end |
#cover?(other) ⇒ Boolean
77 78 79 80 81 82 83 84 85 |
# File 'lib/rubyXL/objects/reference.rb', line 77 def cover?(other) return false unless other.is_a?(RubyXL::Reference) return false if @sheet_name != other.sheet_name (@first_row <= other.first_row) && (@last_row >= other.last_row) && (@first_col <= other.first_col) && (@last_col >= other.last_col) end |
#inspect ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/rubyXL/objects/reference.rb', line 118 def inspect if single_cell? then "#<#{self.class} @sheet_name=#{@sheet_name} @row=#{@first_row} @col=#{@first_col}>" else "#<#{self.class} @sheet_name=#{@sheet_name} rows=#{@first_row}..#{@last_row} cols=#{@first_col}..#{@last_col}>" end end |
#intersect?(other) ⇒ Boolean
87 88 89 90 91 92 93 94 95 |
# File 'lib/rubyXL/objects/reference.rb', line 87 def intersect?(other) return false unless other.is_a?(RubyXL::Reference) return false if @sheet_name != other.sheet_name (((@first_row <= other.first_row) && (@last_row >= other.first_row)) || ((@first_row <= other.last_row) && (@last_row >= other.last_row))) && (((@first_col <= other.first_col) && (@last_col >= other.first_col)) || ((@first_col <= other.last_col) && (@last_col >= other.last_col))) end |
#row_range ⇒ Object
49 50 51 52 |
# File 'lib/rubyXL/objects/reference.rb', line 49 def row_range warn "[DEPRECATION] `row_range` is deprecated. Please use `first_row` and `last_row` instead." @row_range ||= Range.new(@first_row, @last_row) end |
#single_cell? ⇒ Boolean
59 60 61 |
# File 'lib/rubyXL/objects/reference.rb', line 59 def single_cell? (@first_row == @last_row) && (@first_col == @last_col) end |
#to_s ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rubyXL/objects/reference.rb', line 97 def to_s result = +'' if @sheet_name then if @sheet_name.include?(' ') then result << "'" << @sheet_name << "'" else result << @sheet_name end result << '!' end if single_cell? then result << self.class.ind2ref(@first_row, @first_col, @row_from_absolute, @col_from_absolute) else result << self.class.ind2ref(@first_row, @first_col, @row_from_absolute, @col_from_absolute) result << ':' result << self.class.ind2ref(@last_row, @last_col, @row_to_absolute, @col_to_absolute) end end |
#valid? ⇒ Boolean
63 64 65 |
# File 'lib/rubyXL/objects/reference.rb', line 63 def valid? !(@first_row.negative? || @first_col.negative?) end |