docs.sheetjs.com/docz/docs/03-demos/42-engines/22-perl.md

3.4 KiB

title pagination_prev pagination_next
Perl + JE demos/bigdata/index solutions/input

import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';

:::danger pass

In a production application, it is strongly recommended to use a binding for a C engine like JavaScript::Duktape

:::

JE is a pure-Perl JavaScript engine.

The ExtendScript build can be parsed and evaluated in a JE context.

Integration Details

The engine deviates from ES3. Modifying prototypes can fix some behavior:

/* String#charCodeAt is missing */
var string = "";
for(var i = 0; i < 256; ++i) string += String.fromCharCode(i);
String.prototype.charCodeAt = function(n) {
  var result = string.indexOf(this.charAt(n));
  if(result == -1) throw this.charAt(n);
  return result;
};

/* workaround for String split bug */
Number.prototype.charCodeAt = function(n) { return this + 48; };

/* String#match bug with empty results */
String.prototype.old_match = String.prototype.match;
String.prototype.match = function(p) {
  var result = this.old_match(p);
  return (Array.isArray(result) && result.length == 0) ? null : result;
};

When loading the ExtendScript build, the BOM must be removed:

## Load SheetJS source
my $src = read_file('xlsx.extendscript.js', { binmode => ':raw' });
$src =~ s/^\xEF\xBB\xBF//; ## remove UTF8 BOM
my $XLSX = $je->eval($src);

Reading Files

Data should be passed as Base64 strings:

use File::Slurp;
use MIME::Base64 qw( encode_base64 );

## Set up conversion method
$je->eval(<<'EOF');
function sheetjsparse(data) { try {
  return XLSX.read(String(data), {type: "base64", WTF:1});
} catch(e) { return String(e); } }
EOF

## Read file
my $raw_data = encode_base64(read_file($ARGV[0], { binmode => ':raw' }), "");

## Call method with data
$return_val = $je->method(sheetjsparse => $raw_data);

Writing Files

Due to bugs in data interchange, it is strongly recommended to use a simple format like .fods:

use File::Slurp;

## Set up conversion method
$je->eval(<<'EOF');
function sheetjswrite(wb) { try {
  return XLSX.write(wb, { WTF:1, bookType: "fods", type: "string" });
} catch(e) { return String(e); } }
EOF

## Generate file
my $fods = $je->method(sheetjswrite => $workbook);

## Write to filesystem
write_file("SheetJE.fods", $fods);

Complete Example

:::note Tested Deployments

This demo was tested in the following deployments:

Architecture Version Date
darwin-x64 0.066 2024-02-13
darwin-arm 0.066 2024-05-25
linux-x64 0.066 2024-02-13
linux-arm 0.066 2024-05-25

:::

  1. Install JE and File::Slurp through CPAN:
cpan install JE File::Slurp
  1. Download the ExtendScript build:

{\ curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.extendscript.js}

  1. Download the demo SheetJE.pl:
curl -LO https://docs.sheetjs.com/perl/SheetJE.pl
  1. Download a test file and run:
curl -LO https://docs.sheetjs.com/cd.xls
perl SheetJE.pl cd.xls

After a short wait, the contents will be displayed in CSV form. The script will also generate the spreadsheet SheetJE.fods which can be opened in LibreOffice.