Class: RubyFromExcel::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/formulae/run/reference.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference_as_text, worksheet = nil) ⇒ Reference

Returns a new instance of Reference.


28
29
30
31
32
33
34
35
# File 'lib/formulae/run/reference.rb', line 28

def initialize(reference_as_text, worksheet = nil)
  @worksheet = worksheet
  reference_as_text.downcase!
  @row = reference_as_text[/\d+/]
  @column = reference_as_text[/[a-z]+/]
  @absolute_row = (reference_as_text =~ /\$\d/)
  @absolute_column = (reference_as_text =~ /\$[a-z]/)
end

Instance Attribute Details

#absolute_columnObject

Returns the value of attribute absolute_column


4
5
6
# File 'lib/formulae/run/reference.rb', line 4

def absolute_column
  @absolute_column
end

#absolute_rowObject

Returns the value of attribute absolute_row


4
5
6
# File 'lib/formulae/run/reference.rb', line 4

def absolute_row
  @absolute_row
end

#columnObject

Returns the value of attribute column


3
4
5
# File 'lib/formulae/run/reference.rb', line 3

def column
  @column
end

#rowObject

Returns the value of attribute row


3
4
5
# File 'lib/formulae/run/reference.rb', line 3

def row
  @row
end

#worksheetObject

Returns the value of attribute worksheet


3
4
5
# File 'lib/formulae/run/reference.rb', line 3

def worksheet
  @worksheet
end

Class Method Details

.calculate_integer_to_column(integer) ⇒ Object

Raises:

  • (Exception)

22
23
24
25
26
# File 'lib/formulae/run/reference.rb', line 22

def self.calculate_integer_to_column(integer)
  raise Exception.new("Column numbering starts at 1, received #{integer}") unless integer >= 1
  text = (integer-1).to_i.to_s(26)
  (text[0...-1].tr('1-9a-z','abcdefghijklmnopqrstuvwxyz')+text[-1,1].tr('0-9a-z','abcdefghijklmnopqrstuvwxyz')).gsub('a0','z').gsub(/([b-z])0/) { $1.tr('b-z','a-y')+"z" }
end

.column_to_integer(string) ⇒ Object


10
11
12
13
14
15
# File 'lib/formulae/run/reference.rb', line 10

def self.column_to_integer(string)
  @column_to_integer_cache ||= {}
  return @column_to_integer_cache[string] ||= string.downcase.each_byte.to_a.reverse.each.with_index.inject(0) do |memo,byte_with_index|
    memo = memo + ((byte_with_index.first - 96) * (26**byte_with_index.last))
  end
end

.integer_to_column(integer) ⇒ Object


17
18
19
20
# File 'lib/formulae/run/reference.rb', line 17

def self.integer_to_column(integer)
  @integer_to_column_cache ||= {}
  return @integer_to_column_cache[integer] ||= self.calculate_integer_to_column(integer)
end

.ruby_for(column_number, row_number, worksheet = nil) ⇒ Object


6
7
8
# File 'lib/formulae/run/reference.rb', line 6

def self.ruby_for(column_number,row_number,worksheet = nil)
  worksheet ? "#{worksheet}.#{integer_to_column(column_number)}#{row_number.to_i}" : "#{integer_to_column(column_number)}#{row_number.to_i}"
end

Instance Method Details

#-(other_reference) ⇒ Object


62
63
64
# File 'lib/formulae/run/reference.rb', line 62

def -(other_reference)
  [row_number - other_reference.row_number, column_number - other_reference.column_number]
end

#column_numberObject


58
59
60
# File 'lib/formulae/run/reference.rb', line 58

def column_number
  Reference.column_to_integer(column)
end

#row_numberObject


54
55
56
# File 'lib/formulae/run/reference.rb', line 54

def row_number
  row.to_i
end

#shift(offset_array) ⇒ Object


37
38
39
# File 'lib/formulae/run/reference.rb', line 37

def shift(offset_array)
  self.dup.shift!(offset_array)
end

#shift!(offset_array) ⇒ Object


41
42
43
44
45
46
# File 'lib/formulae/run/reference.rb', line 41

def shift!(offset_array)
  return self unless offset_array && offset_array.size == 2
  shift_row_by! offset_array.first
  shift_column_by! offset_array.last
  self
end

#to_ruby(include_worksheet = false) ⇒ Object Also known as: to_s


48
49
50
# File 'lib/formulae/run/reference.rb', line 48

def to_ruby(include_worksheet = false)
   include_worksheet ? "#{worksheet}.#{column}#{row && row.to_i}".downcase  : "#{column}#{row && row.to_i}".downcase
end