Skip to main content

使用 Boa 的 Rusty Sheets

在生产应用中,强烈建议使用性能更高的引擎(如 v8)的绑定

¥In a production application, it is strongly recommended to use a binding for a more performant engine like v8

Boa 是一个用 Rust 编写的 JavaScript 引擎。

¥Boa is a JavaScript engine written in Rust.

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

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

"完整示例" 部分创建一个命令行工具,用于从电子表格读取数据并生成 CSV 行。

¥The "Complete Example" section creates a command-line tool for reading data from spreadsheets and generating CSV rows.

集成详情

¥Integration Details

初始化 Boa

¥Initialize Boa

一个 JS 上下文可以用一行构建:

¥A JS context can be constructed in one line:

/* initialize */
let context = &mut boa_engine::Context::default();

以下辅助函数将字符串计算为 JS 代码:

¥The following helper function evaluates strings as JS code:

/* simple wrapper to evaluate code snippets */
fn eval_code(c: &mut boa_engine::Context, code: &str) -> Result<std::string::String, boa_engine::JsError> {
let src = boa_engine::Source::from_bytes(code);
match c.eval(src) {
Ok(res) => { return Ok(res.to_string(c).unwrap().to_std_string_escaped()); }
Err(e) => { return Err(e); }
};
}

加载 SheetJS 脚本

¥Load SheetJS Scripts

SheetJS 独立脚本 可以在 Boa 上下文中解析和评估。

¥The SheetJS Standalone scripts can be parsed and evaluated in a Boa context.

Boa 提供了一个特殊的辅助函数 boa_engine::Source::from_filepath 来从路径读取源代码,但建议使用 include_str! 宏内联 SheetJS 独立脚本:

¥Boa provides a special helper boa_engine::Source::from_filepath to read source code from a path, but it is recommended to inline the SheetJS standalone script using the include_str! macro:

  /* load library */
match eval_code(context, include_str!("../xlsx.full.min.js")) {
Ok(_res) => {}
Err(e) => { return eprintln!("Uncaught {e}"); }
}

要确认库已加载,可以检查 XLSX.version

¥To confirm the library is loaded, XLSX.version can be inspected:

  /* get version string */
match eval_code(context, "XLSX.version") {
Ok(res) => { println!( "SheetJS library version {}", res); }
Err(e) => { return eprintln!("Uncaught {e}"); }
}

读取文件

¥Reading Files

Boa 原生支持 ArrayBuffer。此代码片段将文件中的数据读取到 Vec<u8> 中,并将数据作为 ArrayBuffer 存储在全局范围内:

¥Boa supports ArrayBuffer natively. This snippet reads data from a file into Vec<u8> and stores the data as an ArrayBuffer in global scope:

  /* read file */
let data: Vec<u8> = std::fs::read("pres.xlsx").unwrap();
let array: boa_engine::object::builtins::JsArrayBuffer = boa_engine::object::builtins::JsArrayBuffer::from_byte_block(file, context).unwrap();
let attrs = boa_engine::property::Attribute::WRITABLE | boa_engine::property::Attribute::ENUMERABLE | boa_engine::property::Attribute::CONFIGURABLE;
let _ = context.register_global_property(boa_engine::js_string!("buf"), array, attrs);

/* parse with SheetJS */
match eval_code(context, "void (globalThis.wb = XLSX.read(buf))") {
Ok(_res) => { }
Err(e) => { return eprintln!("Uncaught {e}"); }
}

wb 将是 JS 环境中的一个变量,可以使用各种 SheetJS API 函数进行检查。

¥wb will be a variable in the JS environment that can be inspected using the various SheetJS API functions.

完整示例

¥Complete Example

测试部署

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

¥This demo was tested in the following deployments:

架构Boa日期
darwin-x640.20.02025-03-31
darwin-arm0.20.02025-02-13
win11-x640.20.02025-04-28
win11-arm0.20.02025-02-23
linux-x640.20.02025-04-21
linux-arm0.20.02025-02-15
  1. 安装 Rust。

    ¥Install Rust.

Boa 0.18.0 需要 Rust 版本 1.67 或更高版本。

¥Boa 0.18.0 requires Rust version 1.67 or later.

Debian 12(Bullseye)附带 Rust 版本 1.63.0

¥Debian 12 (Bullseye) ships with Rust version 1.63.0.

强烈建议从官方发行版安装 Rust。

¥It is strongly recommended to install Rust from the official distribution.

  1. 创建一个新项目:

    ¥Create a new project:

cargo new sheetjs-boa
cd sheetjs-boa
cargo run
  1. 添加 boa_engine 箱子:

    ¥Add the boa_engine crate:

cargo add boa_engine
  1. 下载 SheetJS 独立脚本和测试文件。将这两个文件保存在项目目录中:

    ¥Download the SheetJS Standalone script and test file. Save both files in the project directory:

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

PowerShell curl 与官方 curl 程序不兼容。该命令可能会因参数错误而失败:

¥PowerShell curl is incompatible with the official curl program. 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 used instead:

curl.exe -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
curl.exe -LO https://xlsx.nodejs.cn/pres.xlsx
  1. 下载 main.rs 并替换 src/main.rs

    ¥Download main.rs and replace src/main.rs:

curl -L -o src/main.rs https://xlsx.nodejs.cn/boa/main.rs

PowerShell curl 与官方 curl 程序不兼容。该命令可能会因参数错误而失败:

¥PowerShell curl is incompatible with the official curl program. 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 used instead:

curl.exe -L -o src/main.rs https://xlsx.nodejs.cn/boa/main.rs
  1. 在发布模式下构建并运行应用:

    ¥Build and run the app in release mode:

cargo run --release pres.xlsx

稍等片刻后,内容将以 CSV 形式显示。

¥After a short wait, the contents will be displayed in CSV form.

默认调试版本未优化,可能会引发堆栈溢出错误。强烈建议尽可能使用 --release

¥The default debug build is not optimized and can elicit stack overflow errors. It is strongly encouraged to use --release when possible.