     if @loading then existingRow or @_newRow(name) else existingRow
 
   _newRow: (name) ->
-    def = (d for d in @loader.functionDefinitions() when d.name == name)[0]
+    def = (d for d in @sheet.formulas() when d.name == name)[0]
     row = {name: def?.name, formula: def?.expr.text, value: null}
     @data.push row
     row

      console.log 'beforeRemoveRow', index, numberOfRows, self.data
      for row in self.data[index...index + numberOfRows]
        console.log 'Removing row', row
        self.loader.removeFunction row.name

  updateFormula: (name, formula, oldName, nextName) ->
    console.log 'updateFormula', name, formula, oldName, nextName
    if name and formula then @loader.setFunctionAsText name, formula, oldName, nextName
    if name and not formula then @loader.removeFunction name
    if not name and oldName then @loader.removeFunction oldName


  asText: -> @loader.asText()

  loadText: (text) ->
    @loading = true
    @loader.clear()
    @data.length = 0
    @loader.loadDefinitions text
    @_newRow null
    @loading = false


Freesheet = require 'freesheet'
CoreFunctions = require('core-functions')
PageFunctions = require('page-functions')
TimeFunctions = require('time-functions')
