JavaScript 引擎
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
JavaScript 代码无法在大多数现代计算机上直接执行。软件组件 ("JavaScript 引擎") 执行代码。嵌入 JS 引擎后,程序可以利用 SheetJS 库来处理电子表格和数据。
¥JavaScript code cannot be directly executed on most modern computers. A software component ("JavaScript engine") executes code. After embedding a JS engine, programs can leverage SheetJS libraries to process spreadsheets and data.
本节中的演示展示了许多 JS 引擎和语言绑定。在每种情况下,我们都将构建一个示例应用,该应用嵌入 JS 引擎、加载 SheetJS 库脚本以及读取和写入电子表格文件。
¥The demos in this section showcase a number of JS engines and language bindings. In each case, we will build a sample application that embeds a JS engine, loads SheetJS library scripts, and reads and writes spreadsheet files.
一般注意事项
¥General Caveats
有许多具有不同设计目标的 JS 引擎。有些是专为低功耗或 低内存环境而设计的。其他目标是与特定编程语言或环境的互操作性。通常它们支持 ES3 并且能够运行 SheetJS 代码。
¥There are many JS engines with different design goals. Some are designed for low-power or low-memory environments. Others aim for interoperability with specific programming languages or environments. Typically they support ES3 and are capable of running SheetJS code.
轻量级 JS 引擎通常缺少通用浏览器和 NodeJS API。
¥Common browser and NodeJS APIs are often missing from light-weight JS engines.
全局的
¥Global
有些发动机不提供 globalThis
或 global
或 window
。global
变量可以在应在 JS 引擎中运行的一行中公开:
¥Some engines do not provide globalThis
or global
or window
. A global
variable can be exposed in one line that should be run in the JS engine:
var global = (function(){ return this; }).call(null);
控制台
¥Console
某些引擎不提供 console
对象,但提供其他方式打印到标准输出。例如,Hermes[^1] 提供 print()
。应使用引擎打印函数创建 console
对象:
¥Some engines do not provide a console
object but offer other ways to print to
standard output. For example, Hermes[^1] provides print()
. A console
object
should be created using the engine print function:
var console = { log: function(x) { print(x); } };
二进制数据
¥Binary Data
某些引擎不提供交换二进制数据的简单方法。例如,某些库传递以 null 结尾的数组,这会截断 XLSX、XLS 和其他导出。应避免接受无长度指针的 API。
¥Some engines do not provide easy ways to exchange binary data. For example, some libraries pass null-terminated arrays, which would truncate XLSX, XLS, and other exports. APIs that accept pointers without length should be avoided.
Base64 字符串是安全的,因为它们不使用空字符,但仅应在没有安全方法传递 ArrayBuffer
或 Uint8Array
对象时使用。SheetJS read
[^2] 和 write
[^3] 方法直接支持 Base64 字符串。
¥Base64 strings are safe, as they do not use null characters, but should only be
used when there is no safe way to pass ArrayBuffer
or Uint8Array
objects.
The SheetJS read
[^2] and write
[^3] methods directly support Base64 strings.
字节约定
¥Byte Conventions
Java 本身没有无符号字节的概念。byte[]
中的值限制在 -128 .. 127
范围内。它们需要在 JS 引擎中修复。
¥Java has no native concept of unsigned bytes. Values in a byte[]
are limited
to the range -128 .. 127
. They need to be fixed within the JS engine.
某些引擎支持类型化数组。Uint8Array
构造函数将修复值:
¥Some engines support typed arrays. The Uint8Array
constructor will fix values:
var signed_data = [-48, -49, 17, -32, /* ... */]; // 0xD0 0xCF 0x11 0xE0 ...
var fixed_data = new Uint8Array(signed_data);
当不支持 Uint8Array
时,可以通过按位运算固定值:
¥When Uint8Array
is not supported, values can be fixed with bitwise operations:
var signed_data = [-48, -49, 17, -32, /* ... */]; // 0xD0 0xCF 0x11 0xE0 ...
var fixed_data = new Array(signed_data.length);
for(var i = 0; i < signed_data.length; ++i) fixed_data[i] = signed_data[i] & 0xFF;