def define_name(name, formula)
      sheet_index = nil
      sheetname   = ''

      # Local defined names are formatted like "Sheet1!name".
      if name =~ /^(.*)!(.*)$/
        sheetname   = $1
        name        = $2
        sheet_index = @worksheets.index_by_name(sheetname)
      else
        sheet_index = -1   # Use -1 to indicate global names.
      end

      # Raise if the sheet index wasn't found.
      if !sheet_index
       raise "Unknown sheet name #{sheetname} in defined_name()\n"
      end

      # Raise if the name contains invalid chars as defined by Excel help.
      # Refer to the following to see Excel's syntax rules for defined names:
      # http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names
      #
      if name =~ /\A[-0-9 !"#\$%&'\(\)\*\+,\.:;<=>\?@\[\]\^`\{\}~]/ || name =~ /.+[- !"#\$%&'\(\)\*\+,\\:;<=>\?@\[\]\^`\{\}~]/
        raise "Invalid characters in name '#{name}' used in defined_name()\n"
      end

      # Raise if the name looks like a cell name.
      if name =~ %r(^[a-zA-Z][a-zA-Z]?[a-dA-D]?[0-9]+$)
        raise "Invalid name '#{name}' looks like a cell name in defined_name()\n"
      end

      # Raise if the name looks like a R1C1
      if name =~ /\A[rcRC]\Z/ || name =~ /\A[rcRC]\d+[rcRC]\d+\Z/
        raise "Invalid name '#{name}' like a RC cell ref in defined_name()\n"
      end

      @defined_names.push([ name, sheet_index, formula.sub(/^=/, '') ])
    end
def define_name(name, formula)
      sheet_index = nil
      sheetname   = ''

      # Local defined names are formatted like "Sheet1!name".
      if name =~ /^(.*)!(.*)$/
        sheetname   = $1
        name        = $2
        sheet_index = @worksheets.index_by_name(sheetname)
      else
        sheet_index = -1   # Use -1 to indicate global names.
      end

      # Raise if the sheet index wasn't found.
      if !sheet_index
       raise "Unknown sheet name #{sheetname} in defined_name()\n"
      end

      # Raise if the name contains invalid chars as defined by Excel help.
      # Refer to the following to see Excel's syntax rules for defined names:
      # http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names
      #
      if name =~ /\A[-0-9 !"#\$%&'\(\)\*\+,\.:;<=>\?@\[\]\^`\{\}~]/ || name =~ /.+[- !"#\$%&'\(\)\*\+,\\:;<=>\?@\[\]\^`\{\}~]/
        raise "Invalid characters in name '#{name}' used in defined_name()\n"
      end

      # Raise if the name looks like a cell name.
      if name =~ %r(^[a-zA-Z][a-zA-Z]?[a-dA-D]?[0-9]+$)
        raise "Invalid name '#{name}' looks like a cell name in defined_name()\n"
      end

      # Raise if the name looks like a R1C1
      if name =~ /\A[rcRC]\Z/ || name =~ /\A[rcRC]\d+[rcRC]\d+\Z/
        raise "Invalid name '#{name}' like a RC cell ref in defined_name()\n"
      end

      @defined_names.push([ name, sheet_index, formula.sub(/^=/, '') ])
    end
Create a defined name in Excel. We handle global/workbook level names and
 local/worksheet names.

 This method is used to defined a name that can be used to represent
 a value, a single cell or a range of cells in a workbook.

 For example to set a global/workbook name:

   # Global/workbook names.
   workbook.define_name('Exchange_rate', '=0.96')
   workbook.define_name('Sales',         '=Sheet1!$G$1:$H$10')

 It is also possible to define a local/worksheet name by prefixing the name
 with the sheet name using the syntax +sheetname!definedname+:

   # Local/worksheet name.
   workbook.define_name('Sheet2!Sales',  '=Sheet2!$G$1:$G$10')

 If the sheet name contains spaces or special characters
 you must enclose it in single quotes like in Excel:

   workbook.define_name("'New Data'!Sales",  '=Sheet2!$G$1:$G$10')

 See the defined_name.rb program in the examples dir of the distro.

 Refer to the following to see Excel's syntax rules for defined names:
 <http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names>