#!/usr/bin/env ruby
# ExecSheetJS.rb (c) SheetJS LLC -- https://sheetjs.com
require "execjs"
require "base64"

source = File.open("xlsx.full.min.js", "rb").read();
source.force_encoding("UTF-8");
context = ExecJS.compile(source);
puts context.eval("XLSX.version");

data = Base64.strict_encode64(File.open(ARGV[0], "rb").read);
result = context.call(<<EOF, data);
function(data) {
  var wb = XLSX.read(data, {type: 'base64'});
  var ws = wb.Sheets[wb.SheetNames[0]];
  /* to avoid re-parsing, CSV and XLSB are written in the same call */
  return [
    XLSX.utils.sheet_to_csv(ws),
    XLSX.write(wb, {bookType: "xlsb", type: "base64"})
  ];
}
EOF
puts result[0];
xlsb = Base64.strict_decode64(result[1]);
File.write("sheetjsw.xlsb", xlsb, mode: "wb");