Deno SEA
Deno 是一个 JavaScript 运行时,支持将脚本编译成独立的可执行文件。
¥Deno is a JavaScript runtime with support for compiling scripts into self-contained executables.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
该演示使用 Deno 编译器和 SheetJS 创建一个独立的 CLI 工具,用于解析电子表格和生成 CSV 行。
¥This demo uses the Deno compiler and SheetJS to create a standalone CLI tool for parsing spreadsheets and generating CSV rows.
强烈建议在命令行工具中使用 SheetJS 库的系统上安装 Deno。仅当认为需要独立的二进制文件时才应考虑此解决方法。
¥It is strongly recommended to install Deno on systems using SheetJS libraries in command-line tools. This workaround should only be considered if a standalone binary is considered desirable.
优秀的开源软件会随着用户测试和报告而不断成长。任何问题都应报告给 Deno 项目以进行进一步诊断。
¥Great open source software grows with user tests and reports. Any issues should be reported to the Deno project for further diagnosis.
集成详情
¥Integration Details
SheetJS Deno 模块 可以从 Deno 脚本导入。
¥The SheetJS Deno module can be imported from Deno scripts.
deno compile
生成一个独立的可执行文件,其中包括整个 JS 运行时以及用户 JS 代码。
¥deno compile
generates a standalone executable that includes the entire JS
runtime as well as user JS code.
脚本要求
¥Script Requirements
专门使用 SheetJS 库和 Deno 内置模块的脚本可以使用 deno compile
进行打包。应直接导入 ESM 脚本:
¥Scripts that exclusively use SheetJS libraries and Deno built-in modules can be
bundled using deno compile
. The ESM script should be imported directly:
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs';
例如,以下脚本接受一个命令行参数,使用 SheetJS readFile
方法 [^1] 解析指定文件,使用 sheet_to_csv
[^2] 从第一个工作表生成 CSV 文本,然后打印到终端:
¥For example, the following script accepts one command line argument, parses the
specified file using the SheetJS readFile
method[^1], generates CSV text from
the first worksheet using sheet_to_csv
[^2], and prints to terminal:
// @deno-types="https://cdn.sheetjs.com/xlsx-0.20.3/package/types/index.d.ts"
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.20.3/package/xlsx.mjs';
import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/cpexcel.full.mjs';
XLSX.set_cptable(cptable);
/* Deno.args[0] is the first argument to the script */
const filename = Deno.args[0];
/* read file */
const wb = XLSX.readFile(filename);
/* generate CSV of first sheet */
const ws = wb.Sheets[wb.SheetNames[0]];
const csv = XLSX.utils.sheet_to_csv(ws);
/* print to terminal */
console.log(csv);
Deno 权限
¥Deno Permissions
适用于普通 Deno 脚本的相同权限也适用于编译器:
¥The same permissions that apply to normal Deno scripts apply to the compiler:
-
必须指定
--allow-read
选项,以允许程序使用 SheetJSreadFile
[^3] 方法从文件系统读取文件。¥The
--allow-read
option must be specified to allow the program to read files from the filesystem with the SheetJSreadFile
[^3] method. -
必须指定
--allow-write
选项,以允许程序使用 SheetJSwriteFile
[^4] 方法将文件写入文件系统。¥The
--allow-write
option must be specified to allow the program to write files to the filesystem with the SheetJSwriteFile
[^4] method. -
必须指定
--allow-net
选项才能允许程序下载和上传电子表格。¥The
--allow-net
option must be specified to allow the program to download and upload spreadsheets.
更多 flags 可以在官方权限列表中找到 [^5]
¥More flags can be found in the official permissions list[^5]
完整示例
¥Complete Example
该演示最后在以下部署中进行了测试:
¥This demo was last tested in the following deployments:
架构 | Deno | 日期 |
---|---|---|
darwin-x64 | 1.43.6 | 2024-05-28 |
darwin-arm | 1.43.6 | 2024-05-23 |
win10-x64 | 1.41.3 | 2024-03-24 |
win11-x64 | 1.43.6 | 2024-05-25 |
win11-arm | 1.43.6 | 2024-05-25 |
linux-x64 | 1.41.3 | 2024-03-18 |
linux-arm | 1.43.6 | 2024-05-25 |
-
安装 Deno.[^6]
¥Install Deno.[^6]
-
下载测试文件 https://xlsx.nodejs.cn/pres.numbers:
¥Download the test file https://xlsx.nodejs.cn/pres.numbers:
curl -o pres.numbers https://xlsx.nodejs.cn/pres.numbers
-
使用
deno run
测试脚本:¥Test the script with
deno run
:
deno run -r --allow-read https://xlsx.nodejs.cn/cli/sheet2csv.ts pres.numbers
该脚本应显示第一张工作表中的 CSV 内容:
¥The script should display CSV contents from the first sheet:
Name,Index
Bill Clinton,42
GeorgeW Bush,43
Barack Obama,44
Donald Trump,45
Joseph Biden,46
-
编译并运行
sheet2csv
:¥Compile and run
sheet2csv
:
deno compile -r --allow-read https://xlsx.nodejs.cn/cli/sheet2csv.ts
./sheet2csv pres.numbers
该程序应显示与脚本相同的 CSV 内容(来自步骤 2)
¥The program should display the same CSV contents as the script (from step 2)
[^1]: 见 readFile
于 "读取文件"
¥See readFile
in "Reading Files"
[^2]: 见 sheet_to_csv
于 "CSV 和文本"
¥See sheet_to_csv
in "CSV and Text"
[^3]: 见 readFile
于 "读取文件"
¥See readFile
in "Reading Files"
[^4]: 见 writeFile
于 "写入文件"
¥See writeFile
in "Writing Files"
[^5]: 请参阅 Deno 官方文档中的 "权限列表"
¥See "Permissions list" in the official Deno documentation
[^6]: 官方说明 覆盖了大部分平台。Deno 不提供官方 Linux ARM64 版本,但提供 有非官方的社区建设。
¥The official instructions cover most platforms. Deno does not provide official Linux ARM64 builds, but there are unofficial community builds.