def encode_formula_result(value = nil)       #:nodoc:
    is_string = 0                 # Formula evaluates to str.
    # my $num;                    # Current value of formula.
    # my $grbit;                  # Option flags.

    unless value
      grbit  = 0x03
      num    = [0].pack("d")
    else
      # The user specified the result of the formula. We turn off the recalc
      # flag and check the result type.
      grbit  = 0x00

      if value.to_s =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
        # Value is a number.
        num = [value].pack("d")
      else
        bools = {
          'TRUE'    => [1,  1],
          'FALSE'   => [1,  0],
          '#NULL!'  => [2,  0],
          '#DIV/0!' => [2,  7],
          '#VALUE!' => [2, 15],
          '#REF!'   => [2, 23],
          '#NAME?'  => [2, 29],
          '#NUM!'   => [2, 36],
          '#N/A'    => [2, 42]
        }

        if bools[value]
          # Value is a boolean.
          num = [bools[value][0], bools[value][1], 0, 0xFFFF].pack("vvvv")
        else
          # Value is a string.
          num = [0, 0, 0, 0xFFFF].pack("vvvv")
          is_string = 1
        end
      end
    end

    [num, grbit, is_string]
  end
def encode_formula_result(value = nil)       #:nodoc:
    is_string = 0                 # Formula evaluates to str.
    # my $num;                    # Current value of formula.
    # my $grbit;                  # Option flags.

    unless value
      grbit  = 0x03
      num    = [0].pack("d")
    else
      # The user specified the result of the formula. We turn off the recalc
      # flag and check the result type.
      grbit  = 0x00

      if value.to_s =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
        # Value is a number.
        num = [value].pack("d")
      else
        bools = {
          'TRUE'    => [1,  1],
          'FALSE'   => [1,  0],
          '#NULL!'  => [2,  0],
          '#DIV/0!' => [2,  7],
          '#VALUE!' => [2, 15],
          '#REF!'   => [2, 23],
          '#NAME?'  => [2, 29],
          '#NUM!'   => [2, 36],
          '#N/A'    => [2, 42]
        }

        if bools[value]
          # Value is a boolean.
          num = [bools[value][0], bools[value][1], 0, 0xFFFF].pack("vvvv")
        else
          # Value is a string.
          num = [0, 0, 0, 0xFFFF].pack("vvvv")
          is_string = 1
        end
      end
    end

    [num, grbit, is_string]
  end
value     # Result to be encoded.

 Encode the user supplied result for a formula.