class ReplaceTableReferenceAst

Attributes

function_needs_area[RW]
referring_cell[RW]
tables[RW]
worksheet[RW]

Public Class Methods

new(tables, worksheet = nil, referring_cell = nil) click to toggle source
# File src/simplify/replace_table_references.rb, line 5
def initialize(tables, worksheet = nil, referring_cell = nil)
  @tables, @worksheet, @referring_cell = tables, worksheet, referring_cell
  @function_needs_area = false
end

Public Instance Methods

check_function_needs_area(ast) click to toggle source
# File src/simplify/replace_table_references.rb, line 25
def check_function_needs_area(ast)
  if ast[1] == :MATCH
    @function_needs_area = false
    map(ast[2])
    @function_needs_area = true
    map(ast[3])
    @function_needs_area = false
    ast[4..-1].each { |a| map(a) }
  else
    @function_needs_area = false
    ast.each { |a| map(a) }
  end
  ast
end
local_table_reference(ast) click to toggle source

Of the format [:local_table_reference, table_reference]

# File src/simplify/replace_table_references.rb, line 49
def local_table_reference(ast)
  table_reference = ast[1]
  table = tables.values.find do |table|
    table.includes?(worksheet,referring_cell)
  end
  return ast.replace([:error, :"#REF!"]) unless table
  ast.replace(table.reference_for(table.name,table_reference,worksheet,referring_cell, @function_needs_area))
end
map(ast) click to toggle source
# File src/simplify/replace_table_references.rb, line 10
def map(ast)
  return ast unless ast.is_a?(Array)
  case ast[0]
  when :function;
    check_function_needs_area(ast)
  when :table_reference;
    table_reference(ast)
  when :local_table_reference;
    local_table_reference(ast)
  else
    ast.each { |a| map(a) }
  end
  ast
end
table_reference(ast) click to toggle source

Of the format [:table_reference, table_name, table_reference]

# File src/simplify/replace_table_references.rb, line 41
def table_reference(ast)
  table_name = ast[1]
  table_reference = ast[2]
  return ast.replace([:error, :"#REF!"]) unless tables.has_key?(table_name.downcase)
  ast.replace(tables[table_name.downcase].reference_for(table_name,table_reference,worksheet,referring_cell, @function_needs_area))
end