Method: Writeexcel::Worksheet#write_utf16be_string

Defined in:
lib/writeexcel/worksheet.rb

#write_utf16be_string(*args) ⇒ Object

:call-seq:

write_utf16be_string(row, col, string, format)

Write a Unicode string to the specified row and column (zero indexed). $format 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


2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
# File 'lib/writeexcel/worksheet.rb', line 2403

def write_utf16be_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

  record      = 0x00FD                        # Record identifier
  length      = 0x000A                        # Bytes to follow

  row, col, str = args
  xf          = xf_record_index(row, col, args[3]) # The cell format
  encoding    = 0x1
  str_error   = 0

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

  # Limit the utf16 string to the max number of chars (not bytes).
  if str.bytesize > 32767* 2
    str       = str[0..32767*2]
    str_error = -3
  end

  num_bytes = str.bytesize
  num_chars = (num_bytes / 2).to_i

  # Check for a valid 2-byte char string.
  raise "Uneven number of bytes in Unicode string" unless num_bytes % 2 == 0

  # Change from UTF16 big-endian to little endian
  str = utf16be_to_16le(str)

  # Add the encoding and length header to the string.
  str_header  = [num_chars, encoding].pack("vC")
  str         = str_header + str

  str_unique = update_workbook_str_table(str)

  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