Search code examples
rubyexcelrubygemsosx-elcapitan

Syntax Error When Writing a Multi-Dimensional Array to Excel File


I am attempting to write a multi-dimensional array to an Excel file (extension .xls) using the WriteExcel Ruby gem on El Capitan.

My attempt:

# -*- coding:utf-8 -*-
require 'writeexcel'

# Create a new Excel workbook
workbook = WriteExcel.new('ruby.xls')

# Add a worksheet
worksheet = workbook.add_worksheet

eec =  [
  ['maggie', 'milly', 'molly', 'may'  ],
  [13,       14,      15,      16     ],
  ['shell',  'star',  'crab',  'stone']
  ]

worksheet.write_col('A1', \@eec)

workbook.close

The error I get:

iMac:scrapScripts guy$ ruby script.rb
script.rb:16: syntax error, unexpected $undefined, expecting ')'
worksheet.write_col('A1', \@eec)
                           ^

Using the gem documentation found here.

Can anyone explain why this syntax error is occurring, and how I can fix it?


Solution

  • Remove garbage symbols from the call to write_col:

    - worksheet.write_col('A1', \@eec)
    + worksheet.write_col('A1', eec)