Skip to main content

Ruby + Bindings

ExecJS 是对许多 JS 运行时(包括 V8)的 Ruby 抽象。

¥ExecJS is a Ruby abstraction over a number of JS runtimes including V8.

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

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

此演示使用 ExecJS 和 SheetJS 从工作表中提取数据并打印 CSV 行。我们将探讨如何在 ExecJS 上下文中加载 SheetJS 以及如何在 Ruby 中处理数据。

¥This demo uses ExecJS and SheetJS to pull data from sheets and print CSV rows. We'll explore how to load SheetJS in ExecJS contexts and process data in Ruby.

"完整示例" 部分包含一个完整的 Ruby 脚本,用于从文件读取数据。

¥The "Complete Example" section includes a complete Ruby script for reading data from files.

集成详情

¥Integration Details

SheetJS 独立脚本 可以在每个受支持的运行时进行解析和评估。

¥The SheetJS Standalone scripts can be parsed and evaluated in every supported runtime.

初始化 ExecJS

¥Initialize ExecJS

require 命令执行所需的初始化步骤:

¥The require command performs the required initialization steps:

require "execjs"

加载 SheetJS 脚本

¥Load SheetJS Scripts

主库可以在新的上下文中加载和编译:

¥The main library can be loaded and compiled in a new context:

require "execjs"

source = File.open("xlsx.full.min.js", "rb").read;
source.force_encoding("UTF-8");
context = ExecJS.compile(source);

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

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

puts context.eval("XLSX.version");

读写文件

¥Reading and Writing Files

ExecJS 的架构迫使用户将读和写结合在一个函数步骤中。应使用 Base64 字符串进行交换。例如,以下代码片段从 pres.numbers 读取数据,生成 XLSB 文件,然后写入 sheetjsw.xlsb

¥The architecture of ExecJS forces users to combine reading and writing in one function step. Base64 strings should be used for interchange. For example, the following snippet reads data from pres.numbers, generates an XLSB file, and writes to sheetjsw.xlsb:

require "base64"

# read and encode data to Base64
data = Base64.strict_encode64(File.open("pres.numbers", "rb").read);

# define function and call with the data
xlsb = context.call(<<EOF, data);
function(data) {
/* parse data -- the argument is the data from Ruby code */
var wb = XLSX.read(data, {type: 'base64'});
/* write XLSB data (encoded as base64) */
return XLSX.write(wb, {bookType: "xlsb", type: "base64"});
}
EOF
# at this point, `xlsb` is a Base64-encoded string

# decode and write to file
File.write("sheetjsw.xlsb", Base64.strict_decode64(xlsb), mode: "wb");

strict_ 变体确保不会将换行符添加到字符串中。

¥The strict_ variants ensure that no newlines are added to the strings.

完整示例

¥Complete Example

测试部署

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

¥This demo was tested in the following deployments:

平台Ruby执行 JS日期
darwin-x642.6.102.9.12024-04-25
darwin-arm2.6.102.9.12024-05-25
win10-x643.2.32.9.12024-03-10
win11-arm3.0.22.9.12024-05-25
linux-x643.0.52.9.12024-03-21
linux-arm3.1.22.9.12024-05-25

上次测试演示时,没有针对 ARM 上的 Windows 的官方 Ruby 版本。win11-arm 测试在 WSL 中运行。win10-x64 测试使用了官方的 Ruby for Windows x64 版本。

¥When the demo was last tested, there was no official Ruby release for Windows on ARM. The win11-arm test was run in WSL. The win10-x64 test used the official Ruby for Windows x64 release.

  1. 安装 Ruby、gem (RubyGems) 和依赖:

    ¥Install Ruby, gem (RubyGems), and the dependencies:

gem install execjs
Installation Notes (click to show)

The command may need to be run as an administrator or root user:

sudo gem install execjs

On Arch Linux-based platforms including the Steam Deck, rubygems must be installed through the package manager:

sudo pacman -Syu rubygems
  1. 创建一个新的项目文件夹:

    ¥Create a new project folder:

mkdir sheetjs-rb
cd sheetjs-rb
  1. 下载 SheetJS Standalone 脚本和测试文件。将这两个文件保存在项目目录中:

    ¥Download the SheetJS Standalone script and the 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.numbers
  1. 下载 ExecSheetJS.rb

    ¥Download ExecSheetJS.rb:

curl -LO https://xlsx.nodejs.cn/execjs/ExecSheetJS.rb
  1. 运行演示:

    ¥Run the demo:

ruby ExecSheetJS.rb pres.numbers

如果程序成功,CSV 内容将打印到控制台并创建文件 sheetjsw.xlsb。该文件可以用 Excel 打开。

¥If the program succeeded, the CSV contents will be printed to console and the file sheetjsw.xlsb will be created. That file can be opened with Excel.

如果 JavaScript 运行时不可用,脚本将抛出错误:

¥If a JavaScript runtime is not available, the script will throw an error:

execjs/runtimes.rb:68:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

ExecJS 2.9.1 支持 Bun 运行时。安装 Bun runtime[^1],重新启动终端,然后重新运行脚本。

¥ExecJS 2.9.1 supports the Bun runtime. Install the Bun runtime[^1], restart the terminal, and re-run the script.

[^1]: curl -fsSL https://bun.sh/install | bash 可以在 macOS、Linux 和 Windows WSL 上运行。

¥curl -fsSL https://bun.sh/install | bash can be run from macOS, Linux, and Windows WSL.