docs.sheetjs.com/docz/docs/03-demos/06-desktop/09-cli.md
2023-05-24 21:36:15 -04:00

6.7 KiB

title pagination_prev pagination_next sidebar_custom_props
Command-Line Tools demos/mobile/index demos/data/index
cli
true

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

With the availability of JS engines and the success of server-side platforms, it is feasible to build command-line tools for various workflows.

This demo covers a number of strategies for building standalone processors. The goal is to generate CSV output from an arbitrary spreadsheet file.

NodeJS

There are a few popular tools for compiling NodeJS scripts to CLI programs.

The demo script presents a friendly command line interface including flags:

$ ./xlsx-cli -h
Usage: xlsx-cli [options] <file> [sheetname]

Options:
  -V, --version            output the version number
  -f, --file <file>        use specified workbook
  -s, --sheet <sheet>      print specified sheet (default first sheet)
...
Tested Deployments (click to show)

This demo was tested in the following deployments:

pkg

Architecture Version Node Target Date
darwin-x64 5.8.1 18.5.0 2023-05-08
win32-x64 5.8.1 18.5.0 2023-05-08
linux-x64 5.8.1 18.5.0 2023-05-08

nexe

Architecture Version Node Target Date
darwin-x64 4.0.0-rc.2 14.15.3 2023-05-08
win32-x64 4.0.0-rc.2 14.15.3 2023-05-08
linux-x64 4.0.0-rc.2 14.15.3 2023-05-08

boxednode

Architecture Version Node Target Date
darwin-x64 2.0.1 20.1.0 2023-05-08
linux-x64 2.0.1 20.1.0 2023-05-08
  1. Download the test file https://sheetjs.com/pres.numbers:
curl -LO https://sheetjs.com/pres.numbers
  1. Download xlsx-cli.js
curl -LO https://docs.sheetjs.com/cli/xlsx-cli.js
  1. Install the dependencies:
{`\ npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`} {`\ pnpm install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`} {`\ yarn add https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz exit-on-epipe commander@2`}
  1. Follow tooling steps:

Run nexe and manually specify NodeJS version 14.15.3

npx nexe -t 14.15.3 xlsx-cli.js

This generates xlsx-cli or xlsx-cli.exe depending on platform.

Run pkg:

npx pkg xlsx-cli.js

This generates xlsx-cli-linux, xlsx-cli-macos, and xlsx-cli-win.exe .

Run boxednode:

npx boxednode@2.0.1 -s xlsx-cli.js -t xlsx-cli
  1. Run the generated program, passing pres.numbers as the argument. For example, on an Intel Mac, nexe generates xlsx-cli so the command is:
./xlsx-cli pres.numbers

V8

The V8 demo covers standalone programs that embed the V8 engine. This demo uses the Rust integration to generate a command line tool.

Tested Deployments (click to show)

This demo was last tested in the following deployments:

Architecture V8 Version Date
darwin-x64 11.4.183.2 2023-05-22
linux-x64 11.4.183.2 2023-05-23
win32-x64 11.4.183.2 2023-05-23
  1. Make a new folder for the project:
mkdir sheetjs2csv
cd sheetjs2csv
  1. Download the following scripts:
curl -LO https://docs.sheetjs.com/cli/Cargo.toml
curl -LO https://docs.sheetjs.com/cli/snapshot.rs
curl -LO https://docs.sheetjs.com/cli/sheet2csv.rs
  1. Download the standalone build:

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

  1. Build the V8 snapshot:
cargo build --bin snapshot
cargo run --bin snapshot
  1. Build sheet2csv (sheet2csv.exe in Windows):
cargo build --release --bin sheet2csv
  1. Download the test file https://sheetjs.com/pres.numbers:
curl -LO https://sheetjs.com/pres.numbers
  1. Test the application:
mv target/release/sheet2csv .
./sheet2csv pres.numbers
mv target/release/sheet2csv.exe .
./sheet2csv pres.numbers

Deno

deno compile generates a standalone executable that includes the entire JS runtime as well as user JS code.

When compiling, the --allow-read option must be specified to allow the script to read files from the filesystem with Deno.readFileSync.

https://docs.sheetjs.com/cli/sheet2csv.ts can be compiled and run from Deno.

Tested Deployments (click to show)

This demo was last tested in the following deployments:

Architecture Version Date
darwin-x64 1.33.2 2023-05-08
win32-x64 1.33.2 2023-05-08
linux-x64 1.33.2 2023-05-08
  1. Download the test file https://sheetjs.com/pres.numbers:
curl -LO https://sheetjs.com/pres.numbers
  1. Test the script with deno run:
deno run -r --allow-read https://docs.sheetjs.com/cli/sheet2csv.ts pres.numbers

If this worked, the program will print a CSV of the first worksheet.

  1. Compile and run sheet2csv:
deno compile -r --allow-read https://docs.sheetjs.com/cli/sheet2csv.ts
./sheet2csv pres.numbers

Dedicated Engines

The following demos for JS engines produce standalone programs: