docs.sheetjs.com/tests/engines/jurassic.sh
2026-01-22 01:59:11 -05:00

75 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# https://docs.sheetjs.com/docs/demos/engines/jurassic
export DOTNET_CLI_TELEMETRY_OPTOUT=1
cd /tmp
rm -rf SheetJSJurassic
mkdir SheetJSJurassic
cd SheetJSJurassic
dotnet new console
dotnet run
dotnet add package Jurassic --version 3.2.9
curl -LO https://cdn.sheetjs.com/xlsx-latest/package/dist/shim.min.js
curl -LO https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.mini.min.js
curl -LO https://docs.sheetjs.com/pres.xlsx
cat >Program.cs <<EOF
/* sheetjs (C) 2013-present SheetJS -- https://sheetjs.com */
/* vim: set ts=2: */
var engine = new Jurassic.ScriptEngine();
/* Initialize Jurassic */
engine.Evaluate("var global = (function(){ return this; }).call(null);");
/* Load SheetJS Scripts */
engine.ExecuteFile("shim.min.js");
engine.ExecuteFile("xlsx.mini.min.js");
Console.WriteLine("SheetJS version {0}", engine.Evaluate("XLSX.version"));
/* Read and Parse File */
byte[] filedata = File.ReadAllBytes(args[0]);
string b64 = System.Convert.ToBase64String(filedata);
engine.SetGlobalValue("buf", b64);
engine.Evaluate("var wb = XLSX.read(buf, {type:'base64'});");
/* Print CSV of first worksheet*/
engine.Evaluate("var ws = wb.Sheets[wb.SheetNames[0]];");
object csv = engine.Evaluate("XLSX.utils.sheet_to_csv(ws)");
Console.Write(csv);
/* Generate XLSB file and save to SheetJSJurassic.ods */
string ods = engine.Evaluate("XLSX.write(wb, {bookType: 'ods', type: 'base64'})") as string;
File.WriteAllBytes("SheetJSJurassic.ods", System.Convert.FromBase64String(ods));
EOF
dotnet run pres.xlsx; echo $?
npx -y xlsx-cli SheetJSJurassic.ods
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
Darwin) OS="osx" ;;
Linux) OS="linux" ;;
*) echo "unsupported OS: $OS"; exit 1 ;;
esac
case "$ARCH" in
x86_64) ARCH="x64" ;;
arm64|aarch64) ARCH="arm64" ;;
*) echo "unsupported architecture: $ARCH"; exit 1 ;;
esac
RID="${OS}-${ARCH}"
dotnet publish -c Release -r $RID --self-contained true -p:PublishSingleFile=true -p:PublishTrimmed=false
cp bin/Release/net*/$RID/publish/SheetJSJurassic .
rm -f SheetJSJurassic.ods
./SheetJSJurassic pres.xlsx
npx -y xlsx-cli SheetJSJurassic.ods