Class: RubyFromExcel::DependencyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/optimiser/dependency_builder.rb

Direct Known Subclasses

SharedFormulaDependencyBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formula_cell = nil) ⇒ DependencyBuilder

Returns a new instance of DependencyBuilder.


5
6
7
8
# File 'lib/optimiser/dependency_builder.rb', line 5

def initialize(formula_cell = nil)
  self.formula_cell = formula_cell
  self.worksheet = formula_cell.worksheet
end

Instance Attribute Details

#dependenciesObject

Returns the value of attribute dependencies


3
4
5
# File 'lib/optimiser/dependency_builder.rb', line 3

def dependencies
  @dependencies
end

#formula_cellObject

Returns the value of attribute formula_cell


3
4
5
# File 'lib/optimiser/dependency_builder.rb', line 3

def formula_cell
  @formula_cell
end

#worksheetObject

Returns the value of attribute worksheet


3
4
5
# File 'lib/optimiser/dependency_builder.rb', line 3

def worksheet
  @worksheet
end

Instance Method Details

#area(start_area, end_area) ⇒ Object


56
57
58
# File 'lib/optimiser/dependency_builder.rb', line 56

def area(start_area,end_area)
  self.dependencies.concat Area.new(worksheet,Reference.new(start_area).to_ruby,Reference.new(end_area).to_ruby).dependencies
end

#cell(reference) ⇒ Object


51
52
53
54
# File 'lib/optimiser/dependency_builder.rb', line 51

def cell(reference)
  self.dependencies << Reference.new(reference,worksheet).to_ruby(true)
  Reference.new(reference).to_ruby
end

#dependencies_for(full_reference) ⇒ Object


32
33
34
35
36
37
38
39
40
41
# File 'lib/optimiser/dependency_builder.rb', line 32

def dependencies_for(full_reference)
  return [] unless full_reference  =~ /^(sheet\d+)\.(.*)$/
  sheet_name, reference = $1, $2
  using_worksheet(sheet_name) do
    case reference
    when /^a\('(.*?)','(.*?)'\)$/; area($1,$2)
    else; self.dependencies << full_reference
    end
  end    
end

#formula(*expressions) ⇒ Object


10
11
12
13
14
15
16
# File 'lib/optimiser/dependency_builder.rb', line 10

def formula(*expressions)
  self.dependencies = []
  expressions.each do |e| 
    e.visit(self)
  end
  self.dependencies.uniq.sort
end

#function(name, *args) ⇒ Object


60
61
62
63
64
65
66
67
68
69
# File 'lib/optimiser/dependency_builder.rb', line 60

def function(name,*args)
  if name == "INDIRECT"
    args.first.visit(self)
    reference_for_indirect = FormulaBuilder.new(formula_cell).indirect_function(args.first)
    # puts "INDIRECT REFERENCE: #{[args.first.inspect, reference_for_indirect]}" # if reference_for_indirect.to_s.start_with?(":")
    d = dependencies_for(reference_for_indirect)
  else
    args.each { |a| a.visit(self) }
  end
end

#local_table_reference(structured_reference) ⇒ Object


47
48
49
# File 'lib/optimiser/dependency_builder.rb', line 47

def local_table_reference(structured_reference)
  dependencies_for Table.reference_for_local_reference(formula_cell,structured_reference).to_s
end

#named_reference(name) ⇒ Object


28
29
30
# File 'lib/optimiser/dependency_builder.rb', line 28

def named_reference(name)
  dependencies_for reference_for_name(name)
end

#reference_for_name(name) ⇒ Object


71
72
73
74
75
# File 'lib/optimiser/dependency_builder.rb', line 71

def reference_for_name(name)
  worksheet.named_references[name.downcase] || 
  workbook.named_references[name.downcase] || 
  (raise Exception.new("#{name} in #{formula_cell} not found"))
end

#sheet_reference(sheet_name, reference) ⇒ Object Also known as: quoted_sheet_reference


18
19
20
21
22
23
24
# File 'lib/optimiser/dependency_builder.rb', line 18

def sheet_reference(sheet_name,reference)
  sheet_name = $1 if sheet_name.to_s =~ /^(\d+)\.0+$/
  puts "Warning, #{formula_cell} refers to an external workbook in '#{sheet_name}'" if sheet_name =~ /^\[\d+\]/
  using_worksheet(SheetNames.instance[sheet_name]) do
    reference.visit(self)
  end
end

#table_reference(table_name, structured_reference) ⇒ Object


43
44
45
# File 'lib/optimiser/dependency_builder.rb', line 43

def table_reference(table_name,structured_reference)
  dependencies_for Table.reference_for(table_name,structured_reference,formula_cell && formula_cell.reference).to_s
end

#using_worksheet(sheet_name) ⇒ Object


81
82
83
84
85
86
# File 'lib/optimiser/dependency_builder.rb', line 81

def using_worksheet(sheet_name)
  original_worksheet = self.worksheet
  self.worksheet = workbook.worksheets[sheet_name] || (raise Exception.new("#{sheet_name} in #{formula_cell} not found"))
  yield
  self.worksheet = original_worksheet
end

#workbookObject


77
78
79
# File 'lib/optimiser/dependency_builder.rb', line 77

def workbook
  formula_cell.worksheet.workbook
end