def write_string(*args)
    # Check for a cell reference in A1 notation and substitute row and column
    args = row_col_notation(args)

    return -1 if (args.size < 3)                # Check the number of args

    row, col, str, format = args
    str = str.to_s
    xf  = xf_record_index(row, col, format)    # The cell format
    encoding    = 0x0
    str_error   = 0

    ruby_19 {str = convert_to_ascii_if_ascii(str) }

    # Handle utf8 strings
    if is_utf8?(str)
      str_utf16le = utf8_to_16le(str)
      return write_utf16le_string(row, col, str_utf16le, args[3])
    end

    # Check that row and col are valid and store max and min values
    return -2 unless check_dimensions(row, col) == 0

    # Limit the string to the max number of chars.
    if str.bytesize > 32767
      str       = str[0, 32767]
      str_error = -3
    end

    # Prepend the string with the type.
    str_header  = [str.length, encoding].pack('vC')
    str         = str_header + str

    str_unique = update_workbook_str_table(str)

    record      = 0x00FD                        # Record identifier
    length      = 0x000A                        # Bytes to follow
    header = [record, length].pack('vv')
    data   = [row, col, xf, str_unique].pack('vvvV')

    # Store the data or write immediately depending on the compatibility mode.
    store_with_compatibility(row, col, header + data)

    str_error
  end
def write_string(*args)
    # Check for a cell reference in A1 notation and substitute row and column
    args = row_col_notation(args)

    return -1 if (args.size < 3)                # Check the number of args

    row, col, str, format = args
    str = str.to_s
    xf  = xf_record_index(row, col, format)    # The cell format
    encoding    = 0x0
    str_error   = 0

    ruby_19 {str = convert_to_ascii_if_ascii(str) }

    # Handle utf8 strings
    if is_utf8?(str)
      str_utf16le = utf8_to_16le(str)
      return write_utf16le_string(row, col, str_utf16le, args[3])
    end

    # Check that row and col are valid and store max and min values
    return -2 unless check_dimensions(row, col) == 0

    # Limit the string to the max number of chars.
    if str.bytesize > 32767
      str       = str[0, 32767]
      str_error = -3
    end

    # Prepend the string with the type.
    str_header  = [str.length, encoding].pack('vC')
    str         = str_header + str

    str_unique = update_workbook_str_table(str)

    record      = 0x00FD                        # Record identifier
    length      = 0x000A                        # Bytes to follow
    header = [record, length].pack('vv')
    data   = [row, col, xf, str_unique].pack('vvvV')

    # Store the data or write immediately depending on the compatibility mode.
    store_with_compatibility(row, col, header + data)

    str_error
  end
:call-seq:
   write_string(row, col,    token[, format])
   write_string(A1_notation, token[, format])

 Write a string to the specified row and column (zero indexed).

 The format parameter is optional.

 Returns  0 : normal termination
         -1 : insufficient number of arguments
         -2 : row or column out of range
         -3 : long string truncated to 255 chars


    worksheet.write_string(0, 0, 'Your text here')
    worksheet.write_string('A2', 'or here')

 The maximum string size is 32767 characters. However the maximum string
 segment that Excel can display in a cell is 1000. All 32767 characters can
 be displayed in the formula bar.

 The write() method will also handle strings in UTF-8 format.
 You can also write Unicode in UTF16 format via the
 write_utf16be_string() method.

 In general it is sufficient to use the write() method. However, you may
 sometimes wish to use the write_string() method to write data that looks
 like a number but that you don't want treated as a number. For example,
 zip codes or phone numbers:

    # Write as a plain string
    worksheet.write_string('A1', '01209')

 However, if the user edits this string Excel may convert it back to a
 number. To get around this you can use the Excel text format @:

    # Format as a string. Doesn't change to a number when edited
    format1 = workbook.add_format(:num_format => '@')
    worksheet.write_string('A2', '01209', format1)