Method: Writexlsx::Worksheet#write

Defined in:
lib/write_xlsx/worksheet.rb

#write(*args) ⇒ 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:

write_string
write_number
write_blank
write_formula
write_url
write_row
write_col

The general rule is that if the data looks like a something then a something is written. Here are some examples in both row-column and A1 notation:

                                                # Same as:
worksheet.write(0, 0, 'Hello'                ) # write_string()
worksheet.write(1, 0, 'One'                  ) # write_string()
worksheet.write(2, 0,  2                     ) # write_number()
worksheet.write(3, 0,  3.00001               ) # write_number()
worksheet.write(4, 0,  ""                    ) # write_blank()
worksheet.write(5, 0,  ''                    ) # write_blank()
worksheet.write(6, 0,  nil                   ) # write_blank()
worksheet.write(7, 0                         ) # write_blank()
worksheet.write(8, 0,  'http://www.ruby.com/') # write_url()
worksheet.write('A9',  'ftp://ftp.ruby.org/' ) # write_url()
worksheet.write('A10', 'internal:Sheet1!A1'  ) # write_url()
worksheet.write('A11', 'external:c:\foo.xlsx') # write_url()
worksheet.write('A12', '=A3 + 3*A4'          ) # write_formula()
worksheet.write('A13', '=SIN(PI()/4)'        ) # write_formula()
worksheet.write('A14', [1, 2]                ) # write_row()
worksheet.write('A15', [ [1, 2] ]            ) # write_col()

# Write an array formula. Not available in writeexcel gem.
worksheet.write('A16', '{=SUM(A1:B1*A2:B2)}' ) # write_formula()

The format parameter is optional. It should be a valid Format object.

format = workbook.add_format
format.set_bold
format.set_color('red')
format.set_align('center')

worksheet.write(4, 0, 'Hello', format)    # Formatted string

The write() method will ignore empty strings or nil tokens unless a format is also supplied. As such you needn’t worry about special handling for empty or nil in your data. See also the write_blank() method.

One problem with the write() method is that occasionally data looks like a number but you don’t want it treated as a number. For example, zip codes or ID numbers often start with a leading zero. If you want to write this data with leading zero(s), use write_string.

The write methods return:

0 for success.


1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
# File 'lib/write_xlsx/worksheet.rb', line 1651

def write(*args)
  # Check for a cell reference in A1 notation and substitute row and column
  token = row_col_notation(args)[2] || ''

  # Match an array ref.
  if token.respond_to?(:to_ary)
    write_row(*args)
  elsif token.respond_to?(:coerce)  # Numeric
    write_number(*args)
  elsif token =~ /^\d+$/
    write_number(*args)
  # Match http, https or ftp URL
  elsif token =~ %r|^[fh]tt?ps?://|
    write_url(*args)
  # Match mailto:
  elsif token =~ %r|^mailto:|
    write_url(*args)
  # Match internal or external sheet link
  elsif token =~ %r!^(?:in|ex)ternal:!
    write_url(*args)
  # Match formula
  elsif token =~ /^=/
    write_formula(*args)
  # Match array formula
  elsif token =~ /^\{=.*\}$/
    write_formula(*args)
  # Match blank
  elsif token == ''
    args.delete_at(2)     # remove the empty string from the parameter list
    write_blank(*args)
  else
    write_string(*args)
  end
end