Skip to main content

TxikiJS 独立应用

txiki.js 是一个由 QuickJS 驱动的小型运行时。

¥txiki.js is a small runtime powered by QuickJS.

SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。

¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.

此演示使用 txiki.js 和 SheetJS 创建一个独立的 CLI 工具,用于解析电子表格并生成 CSV 行。

¥This demo uses txiki.js and SheetJS to create a standalone CLI tool for parsing spreadsheets and generating CSV rows.

TxikiJS 支持仍处于实验阶段。

优秀的开源软件会随着用户测试和报告而不断成长。任何问题都应报告给 txiki.js 项目,以便进一步诊断。

¥Great open source software grows with user tests and reports. Any issues should be reported to the txiki.js project for further diagnosis.

集成详情

¥Integration Details

SheetJS 独立脚本 可以在 TxikiJS 中执行和使用。

¥The SheetJS Standalone scripts can be evaluated and consumed in TxikiJS.

平台提供与 NodeJS 不同的文件系统操作 API:

¥The platform provides APIs for filesystem operations that differ from NodeJS:

  • tjs.readFile 从指定文件名读取原始数据,并返回一个解析为 Uint8Array 的 Promise。

    ¥tjs.readFile reads raw data from a specified filename and returns a Promise that resolves to a Uint8Array

  • tjs.args 是一个参数数组。在编译后的程序中,第一个值将是程序名称,第二个值将是第一个参数。

    ¥tjs.args is an array of arguments. In the compiled program, the first value will be the program name and the second value will be the first argument.

SheetJS 文件系统方法(readFilewriteFile)无法识别 txiki.js API。幸运的是,readwrite 可以直接处理 Uint8Array 数据。

¥The SheetJS filesystem methods (readFile and writeFile) do not recognize the txiki.js APIs. Fortunately, read and write directly work with Uint8Array data.

以下示例读取并解析当前目录中的 pres.xlsx

¥The following example reads and parses pres.xlsx in the current directory:

Parse pres.xlsx in TxikiJS
/* read data from pres.xlsx into a Uint8Array */
const data = await tjs.readFile("pres.xlsx");

/* parse data and generate a SheetJS workbook object */
const wb = XLSX.read(data);

脚本要求

¥Script Requirements

编译器不会自动打包脚本。可以打包和编译专门使用 Web API、SheetJS API 方法和 tjs API 方法的脚本。

¥The compiler does not bundle scripts automatically. Scripts that exclusively use web APIs, SheetJS API methods, and tjs API methods can be bundled and compiled.

esbuild 是推荐的打包器。

¥esbuild is the recommended bundler.

例如,以下脚本接受一个命令行参数,使用 tjs.readFile 和 SheetJS read 方法 [^1] 解析指定文件,使用 sheet_to_csv[^2] 从第一​​个工作表生成 CSV 文本,并打印结果:

¥For example, the following script accepts one command line argument, parses the specified file using tjs.readFile and the SheetJS read method[^1], generates CSV text from the first sheet using sheet_to_csv[^2], and prints the result:

sheet2csv.js (before bundling)
const XLSX = require("./xlsx.full.min");

/* tjs.args[1] is the first argument to the script */
const filename = tjs.args[1];

/* read and parse file */
const data = await tjs.readFile(filename);
const wb = XLSX.read(data);

/* 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);

完整示例

¥Complete Example

测试部署

该演示在以下部署中进行了测试:

¥This demo was tested in the following deployments:

架构TxikiJS日期
darwin-x6424.12.02025-04-19
darwin-arm24.12.02025-04-19
win11-x6424.12.02025-04-19
win11-arm24.12.02025-04-19
linux-x6424.12.02025-04-19
linux-arm24.12.02025-04-19

Windows on ARM 上的 TxikiJS 使用 X64 兼容层。它不会生成原生 ARM64 二进制文件!

¥TxikiJS on Windows on ARM uses the X64 compatibility layer. It does not generate a native ARM64 binary!

  1. 创建一个新的项目文件夹并下载 txiki.js[^3]:

    ¥Create a new project folder and download txiki.js[^3]:

mkdir sheetjs-txiki
cd sheetjs-txiki
curl -LO https://github.com/saghul/txiki.js/releases/download/v24.12.0/txiki-macos.zip
unzip txiki-macos.zip
mv txiki-macos/tjs .
chmod +x tjs
  1. 下载测试文件 https://xlsx.nodejs.cn/pres.numbers

    ¥Download the test file https://xlsx.nodejs.cn/pres.numbers:

curl -LO https://xlsx.nodejs.cn/pres.numbers

在 PowerShell 中,命令可能会因参数错误而失败:

¥In PowerShell, the command may fail with a parameter error:

Invoke-WebRequest : A parameter cannot be found that matches parameter name 'LO'.

curl.exe 必须直接调用:

¥curl.exe must be invoked directly:

curl.exe -LO https://xlsx.nodejs.cn/pres.numbers
  1. sheet2csv.js 代码块的内容sheet2csv.js 保存在项目文件夹中。

    ¥Save the contents of the sheet2csv.js code block to sheet2csv.js in the project folder.

curl -LO https://xlsx.nodejs.cn/txikijs/sheet2csv.js

在 PowerShell 中,命令可能会因参数错误而失败:

¥In PowerShell, the command may fail with a parameter error:

Invoke-WebRequest : A parameter cannot be found that matches parameter name 'LO'.

curl.exe 必须直接调用:

¥curl.exe must be invoked directly:

curl.exe -LO https://xlsx.nodejs.cn/txikijs/sheet2csv.js
  1. 下载 SheetJS Standalone 脚本并移至项目目录:

    ¥Download the SheetJS Standalone script and move to the project directory:

curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js

在 PowerShell 中,命令可能会因参数错误而失败:

¥In PowerShell, the command may fail with a parameter error:

Invoke-WebRequest : A parameter cannot be found that matches parameter name 'LO'.

curl.exe 必须直接调用:

¥curl.exe must be invoked directly:

curl.exe -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
  1. 打包脚本:

    ¥Bundle the script:

npx -y esbuild sheet2csv.js --bundle --outfile=bundle.js --platform=neutral
  1. 编译并运行 sheet2csv

    ¥Compile and run sheet2csv:

./tjs compile bundle.js sheet2csv
./sheet2csv pres.numbers

程序应显示与脚本相同的 CSV 内容:

¥The program should display the same CSV contents as the script:

Expected Output
Name,Index
Bill Clinton,42
GeorgeW Bush,43
Barack Obama,44
Donald Trump,45
Joseph Biden,46

[^1]: 见 read 于 "读取文件"

¥See read in "Reading Files"

[^2]: 见 sheet_to_csv 于 "CSV 和文本"

¥See sheet_to_csv in "CSV and Text"

[^3]: 上次测试此演示时,已下载并解压 24.12.0 版本的 ZIP 压缩包

¥When this demo was last tested, the ZIP archive for version 24.12.0 was downloaded and extracted.