Skip to main content

Ruby + Bindings

ExecJS is a Ruby abstraction over a number of JS runtimes including V8.

The Standalone scripts can be parsed and evaluated in every supported runtime.

Integration Details

Load SheetJS Scripts

The main library can be loaded and compiled in a new context:

require "execjs"

source ="xlsx.full.min.js").read;
context = ExecJS.compile(source);

To confirm the library is loaded, XLSX.version can be inspected:

puts context.eval("XLSX.version");

Reading and Writing Files

The architecture of ExecJS forces users to combine reading and writing in one function step. Base64 strings should be used for interchange. For example, the following snippet reads data from pres.numbers, generates an XLSB file, and writes to sheetjsw.xlsb:

require "base64"

# read and encode data to Base64
data = Base64.strict_encode64("pres.numbers").read);

# define function and call with the data
xlsb =<<EOF, data);
function(data) {
/* parse data -- the argument is the data from Ruby code */
var wb =, {type: 'base64'});
/* write XLSB data (encoded as base64) */
return XLSX.write(wb, {bookType: "xlsb", type: "base64"});
# at this point, `xlsb` is a Base64-encoded string

# decode and write to file
File.write("sheetjsw.xlsb", Base64.strict_decode64(xlsb), mode: "wb");

The strict_ variants ensure that no newlines are added to the strings.

Complete Example


This demo was tested on 2023 February 14.

0) Install Ruby, gem (RubyGems), and the dependencies:

gem install execjs

1) Download the standalone script and test file:

curl -LO
curl -LO

2) Download ExecSheetJS.rb:

curl -LO

3) Run the demo:

ruby ExecSheetJS.rb pres.numbers

If the program succeeded, the CSV contents will be printed to console and the file sheetjsw.xlsb will be created. That file can be opened with Excel.