Class: RubyFromExcel::Area

Inherits:
ExcelRangeCommon show all
Defined in:
lib/formulae/run/excel_range.rb

Instance Attribute Summary collapse

Attributes inherited from ExcelRangeCommon

#sheet

Instance Method Summary collapse

Constructor Details

#initialize(sheet, start_cell, end_cell) ⇒ Area

Returns a new instance of Area.


30
31
32
33
34
# File 'lib/formulae/run/excel_range.rb', line 30

def initialize(sheet, start_cell,end_cell)
  self.sheet = sheet
  self.start_cell = Reference.new(start_cell)
  self.end_cell = Reference.new(end_cell)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


159
160
161
162
163
# File 'lib/formulae/run/excel_range.rb', line 159

def method_missing(method,*args,&block)
  super unless start_cell.to_s == end_cell.to_s
  super unless value_at(start_cell).respond_to?(method)
  value_at(start_cell).send(method,*args,&block)
end

Instance Attribute Details

#end_cellObject

Returns the value of attribute end_cell


28
29
30
# File 'lib/formulae/run/excel_range.rb', line 28

def end_cell
  @end_cell
end

#start_cellObject

Returns the value of attribute start_cell


28
29
30
# File 'lib/formulae/run/excel_range.rb', line 28

def start_cell
  @start_cell
end

Instance Method Details

#array_formula_index(row_index, column_index) ⇒ Object


69
70
71
72
73
74
75
# File 'lib/formulae/run/excel_range.rb', line 69

def array_formula_index(row_index,column_index)
  column_index = 0 if single_column?
  row_index = 0 if single_row?
  reference = start_cell.shift([row_index,column_index])
  return :ref unless include?(reference)
  reference
end

#array_formula_offset(row_index, column_index) ⇒ Object


65
66
67
# File 'lib/formulae/run/excel_range.rb', line 65

def array_formula_offset(row_index,column_index)
  value_at array_formula_index(row_index,column_index).to_ruby
end

#column(index) ⇒ Object

index = 0 for first column


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

def column(index) # index = 0 for first column
  column_number = start_cell.column_number + index
  Area.new(sheet,Reference.ruby_for(column_number,start_cell.row_number),Reference.ruby_for(column_number,end_cell.row_number))
end

#dependenciesObject


94
95
96
97
98
99
100
101
# File 'lib/formulae/run/excel_range.rb', line 94

def dependencies
  @dependencies ||=
  (start_cell.row_number..end_cell.row_number).map do |row|
    (start_cell.column_number..end_cell.column_number).map do |column_number|
      Reference.ruby_for(column_number,row,sheet)
    end
  end.flatten
end

#each(&block) ⇒ Object


77
78
79
80
81
82
83
84
# File 'lib/formulae/run/excel_range.rb', line 77

def each(&block)
  return @array.each(&block) if @array
  start_cell.row_number.upto(end_cell.row_number) do |row|
    start_cell.column_number.upto(end_cell.column_number) do |column_number|
      yield value_at(Reference.ruby_for(column_number,row))
    end
  end    
end

#include?(reference) ⇒ Boolean

Returns:

  • (Boolean)

151
152
153
154
155
156
157
# File 'lib/formulae/run/excel_range.rb', line 151

def include?(reference)
  return false unless reference.column_number >= start_cell.column_number
  return false unless reference.column_number <= end_cell.column_number
  return false unless reference.row_number >= start_cell.row_number
  return false unless reference.row_number <= end_cell.row_number
  true
end

#include_cell?(cell) ⇒ Boolean

Returns:

  • (Boolean)

141
142
143
144
# File 'lib/formulae/run/excel_range.rb', line 141

def include_cell?(cell)
  return false unless cell.worksheet == sheet
  include?(cell.reference)
end

#index(row_number, column_number) ⇒ Object

origin at 1,1


52
53
54
55
56
# File 'lib/formulae/run/excel_range.rb', line 52

def index(row_number,column_number) #origin at 1,1
  reference = index_reference(row_number,column_number)
  return reference if reference.is_a?(Symbol)
  value_at(reference.to_ruby)
end

#index_reference(row_number, column_number) ⇒ Object


58
59
60
61
62
63
# File 'lib/formulae/run/excel_range.rb', line 58

def index_reference(row_number,column_number)
  ref = array_formula_index(row_number-1, column_number-1)
  return ref if ref.is_a?(Symbol)
  ref.worksheet = sheet
  ref
end

#row(index) ⇒ Object

index = 0 for top row


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

def row(index) # index = 0 for top row
  row_number = row_number_for_index(index)
  Area.new(sheet,"#{start_cell.column}#{row_number}","#{end_cell.column}#{row_number}")
end

#row_number_for_index(index) ⇒ Object


146
147
148
149
# File 'lib/formulae/run/excel_range.rb', line 146

def row_number_for_index(index)
  return (end_cell.row_number + 1 + index) if index < 0
  row_number = start_cell.row_number + index
end

#rows(start_index, end_index) ⇒ Object


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

def rows(start_index, end_index)
  start_row_number = row_number_for_index(start_index)
  end_row_number = row_number_for_index(end_index)
  Area.new(sheet,"#{start_cell.column}#{start_row_number}","#{end_cell.column}#{end_row_number}")
end

#single_column?Boolean

Returns:

  • (Boolean)

129
130
131
# File 'lib/formulae/run/excel_range.rb', line 129

def single_column?
  start_cell.column == end_cell.column
end

#single_row?Boolean

Returns:

  • (Boolean)

133
134
135
# File 'lib/formulae/run/excel_range.rb', line 133

def single_row?
  start_cell.row_number == end_cell.row_number
end

#single_row_or_column?Boolean

Returns:

  • (Boolean)

125
126
127
# File 'lib/formulae/run/excel_range.rb', line 125

def single_row_or_column?
  single_row? || single_column?
end

#to_aObject

def to_enum

to_reference_enum.map {|reference| value_at reference}

end


90
91
92
# File 'lib/formulae/run/excel_range.rb', line 90

def to_a
  @array ||= self.map {|v| v }
end

#to_excel_matrixObject


113
114
115
# File 'lib/formulae/run/excel_range.rb', line 113

def to_excel_matrix
  ExcelMatrix.new(to_grid)
end

#to_gridObject


117
118
119
120
121
122
123
# File 'lib/formulae/run/excel_range.rb', line 117

def to_grid
  (start_cell.row_number..end_cell.row_number).map do |row|
    (start_cell.column_number..end_cell.column_number).map do |column_number|
      value_at(Reference.ruby_for(column_number,row))
    end
  end
end

#to_reference_enumObject


103
104
105
106
107
108
109
110
111
# File 'lib/formulae/run/excel_range.rb', line 103

def to_reference_enum
  Enumerator.new do |yielder|
    start_cell.row_number.upto(end_cell.row_number) do |row|
      start_cell.column_number.upto(end_cell.column_number) do |column_number|
        yielder << Reference.ruby_for(column_number,row)
      end
    end
  end    
end

#to_sObject


137
138
139
# File 'lib/formulae/run/excel_range.rb', line 137

def to_s
  "#{sheet}.a('#{start_cell.to_ruby}','#{end_cell.to_ruby}')"
end