Method: Writexlsx::Worksheet#write

Defined in:
lib/write_xlsx/worksheet.rb

#write(row, col, token = nil, format = nil, value1 = nil, value2 = nil) ⇒ Object

:call-seq:

write(row, column [ , token [ , format ] ])

Excel makes a distinction between data types such as strings, numbers, blanks, formulas and hyperlinks. To simplify the process of writing data the #write() method acts as a general alias for several more specific methods:



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/write_xlsx/worksheet.rb', line 1012

def write(row, col, token = nil, format = nil, value1 = nil, value2 = nil)
  # Check for a cell reference in A1 notation and substitute row and column
  if (row_col_array = row_col_notation(row))
    _row, _col = row_col_array
    _token     = col
    _format    = token
    _value1    = format
    _value2    = value1
  else
    _row = row
    _col = col
    _token = token
    _format = format
    _value1 = value1
    _value2 = value2
  end
  _token ||= ''
  _token = _token.to_s if token.instance_of?(Time) || token.instance_of?(Date)

  if _format.respond_to?(:force_text_format?) && _format.force_text_format?
    write_string(_row, _col, _token, _format) # Force text format
  # Match an array ref.
  elsif _token.respond_to?(:to_ary)
    write_row(_row, _col, _token, _format, _value1, _value2)
  elsif _token.respond_to?(:coerce)  # Numeric
    write_number(_row, _col, _token, _format)
  elsif _token.respond_to?(:=~)  # String
    # Match integer with leading zero(s)
    if @leading_zeros && _token =~ /^0\d*$/
      write_string(_row, _col, _token, _format)
    elsif _token =~ /\A([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\Z/
      write_number(_row, _col, _token, _format)
    # Match formula
    elsif _token =~ /^=/
      write_formula(_row, _col, _token, _format, _value1)
    # Match array formula
    elsif _token =~ /^\{=.*\}$/
      write_formula(_row, _col, _token, _format, _value1)
    # Match blank
    elsif _token == ''
      #        row_col_args.delete_at(2)     # remove the empty string from the parameter list
      write_blank(_row, _col, _format)
    elsif @workbook.strings_to_urls
      # Match http, https or ftp URL
      if _token =~ %r{\A[fh]tt?ps?://}
        write_url(_row, _col, _token, _format, _value1, _value2)
      # Match mailto:
      elsif _token =~ /\Amailto:/
        write_url(_row, _col, _token, _format, _value1, _value2)
      # Match internal or external sheet link
      elsif _token =~ /\A(?:in|ex)ternal:/
        write_url(_row, _col, _token, _format, _value1, _value2)
      else
        write_string(_row, _col, _token, _format)
      end
    else
      write_string(_row, _col, _token, _format)
    end
  else
    write_string(_row, _col, _token, _format)
  end
end