Skip to main content

Maple 中的现代电子表格

Maple 是一个数值计算平台。它提供了一个强大的基于 C 的扩展系统。

¥Maple is a numeric computing platform. It offers a robust C-based extension system.

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

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

该演示使用 SheetJS 从电子表格中提取数据,以便在 Maple 中进行进一步分析。我们将创建一个 Maple 原生扩展,加载 Duktape JavaScript 引擎并使用 SheetJS 库从电子表格读取数据并转换为 Maple 友好的格式。

¥This demo uses SheetJS to pull data from a spreadsheet for further analysis within Maple. We'll create a Maple native extension that loads the Duktape JavaScript engine and uses the SheetJS library to read data from spreadsheets and converts to a Maple-friendly format.

测试部署

此演示由 SheetJS 用户在以下部署中进行了测试:

¥This demo was tested by SheetJS users in the following deployments:

架构版本日期
darwin-x6420242024-04-25
win10-x6420242024-04-25

Maple 对通过 ExcelTools 包 [^1] 处理电子表格的支持有限。在撰写本文时,它缺乏对 XLSB、NUMBERS 和其他常见电子表格格式的支持。

¥Maple has limited support for processing spreadsheets through the ExcelTools package[^1]. At the time of writing, it lacked support for XLSB, NUMBERS, and other common spreadsheet formats.

SheetJS 库通过将电子表格标准化为 Maple 可以理解的形式来帮助填补这一空白。

¥SheetJS libraries help fill the gap by normalizing spreadsheets to a form that Maple can understand.

集成详情

¥Integration Details

当前的建议涉及一个原生插件,该插件可以读取任意文件并生成 Maple 可以导入的干净的 XLSX 文件。

¥The current recommendation involves a native plugin that reads arbitrary files and generates clean XLSX files that Maple can import.

扩展函数最终将 SheetJS read[^2] 和 write[^3] 方法配对,从旧文件中读取数据并写入新文件:

¥The extension function ultimately pairs the SheetJS read[^2] and write[^3] methods to read data from the old file and write a new file:

var workbook = XLSX.read(original_file_data, { type: "buffer" });
var new_file_data = XLSX.write(workbook, { type: "array", bookType: "xlsx" });

扩展函数将接收文件名并执行以下步骤:

¥The extension function will receive a file name and perform the following steps:

C 扩展

¥C Extensions

Maple 扩展是共享库或 DLL,它们使用特殊的 Maple 方法来解析参数和返回值。它们通常用 C 编程语言编写。

¥Maple extensions are shared libraries or DLLs that use special Maple methods for parsing arguments and returning values. They are typically written in the C programming language.

为了简化流程,新函数将采用一个参数(原始文件名)并返回一个值(新文件名)。

¥To simplify the flow, the new function will take one argument (the original file name) and return one value (the new file name).

官方文档有一个全面的方法列表 [^4]。对于这个演示,使用了以下方法:

¥The official documentation has a comprehensive list[^4] of methods. For this demo, the following methods are used:

  • MapleNumArgsIsMapleString 用于参数验证。如果未指定文件名,演示函数将引发 Maple 异常。

    ¥MapleNumArgs and IsMapleString are used in argument validation. The demo function will raise a Maple exception if no file name is specified.

  • MapleRaiseErrorMapleRaiseError2 以编程方式引发错误。

    ¥MapleRaiseError and MapleRaiseError2 programmatically raise errors.

  • MapleToStringToMapleString 在 Maple 和 C 字符串之间进行转换。

    ¥MapleToString and ToMapleString convert between Maple and C strings.

Duktape JS 引擎

¥Duktape JS Engine

该演示使用 Duktape JavaScript 引擎。SheetJS + Duktape 演示更详细地介绍了引擎集成细节。

¥This demo uses the Duktape JavaScript engine. The SheetJS + Duktape demo covers engine integration details in more detail.

可以通过从文件系统读取源代码将 SheetJS 独立脚本 加载到 Duktape 中。

¥The SheetJS Standalone scripts can be loaded in Duktape by reading the source from the filesystem.

完整演示

¥Complete Demo

该演示在 Windows x64 中进行了测试。路径名称和构建命令在其他平台和操作系统中会有所不同。

¥This demo was tested in Windows x64. The path names and build commands will differ in other platforms and operating systems.

sheetjs-maple.c 扩展导出 SheetToXLSX Maple 方法。它接受一个文件名参数,解析指定的文件,将数据导出到 sheetjsw.xlsx 并返回字符串 "sheetjsw.xlsx"

¥The sheetjs-maple.c extension exports the SheetToXLSX Maple method. It takes a file name argument, parses the specified file, exports data to sheetjsw.xlsx and returns the string "sheetjsw.xlsx".

这可以与 ExcelTools 中的 Import 链接:

¥This can be chained with Import from ExcelTools:

with(ExcelTools);
Import(SheetToXLSX("pres.numbers"))
  1. 安装 "Linux 的 Windows 子系统" (WSL)[^5] 和 Visual Studio[^6]。

    ¥Install "Windows Subsystem for Linux" (WSL)[^5] and Visual Studio[^6].

  2. 打开一个新的 "x64 原生工具命令提示符" 窗口并创建一个项目文件夹 c:\sheetjs-maple

    ¥Open a new "x64 Native Tools Command Prompt" window and create a project folder c:\sheetjs-maple:

cd c:\
mkdir sheetjs-maple
cd sheetjs-maple
  1. 将标头和 lib 文件从 Maple 文件夹复制到项目文件夹。例如,在 Windows x64 上使用 Maple 2024:

    ¥Copy the headers and lib files from the Maple folder to the project folder. For example, using Maple 2024 on Windows x64:

copy "C:\Program Files\Maple 2024\extern\include\"*.h .
copy "c:\Program Files\Maple 2024\bin.x86_64_WINDOWS"\*.lib .
  1. 运行 bash 进入 WSL

    ¥Run bash to enter WSL

  2. 在 WSL 中,安装 Duktape:

    ¥Within WSL, install Duktape:

curl -LO https://duktape.org/duktape-2.7.0.tar.xz
tar -xJf duktape-2.7.0.tar.xz
mv duktape-2.7.0/src/*.{c,h} .
  1. 仍在 WSL 中,下载 SheetJS 脚本和测试文件。

    ¥Still within WSL, download SheetJS scripts and the test file.

curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/shim.min.js
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. 仍在 WSL 中,下载扩展 C 代码

    ¥Still within WSL, download the extension C code

curl -LO https://xlsx.nodejs.cn/maple/sheetjs-maple.c
  1. 通过运行 exit 退出 WSL。窗口将返回到命令提示符。

    ¥Exit WSL by running exit. The window will return to the command prompt.

  2. 构建扩展 DLL:

    ¥Build the extension DLL:

cl -Gz sheetjs-maple.c duktape.c /EHsc -link -dll -out:sheetjs-maple.dll maplec.lib
  1. 关闭并重新打开 Maple,然后创建新的 Maple 工作表或文档

    ¥Close and re-open Maple, then create a new Maple Worksheet or Document

  2. 在 Maple 中运行以下命令来更改工作目录:

    ¥Run the following command in Maple to change the working directory:

currentdir("c:\\sheetjs-maple");
  1. 从扩展加载 SheetToXLSX 方法:

    ¥Load the SheetToXLSX method from the extension:

with(ExternalCalling):
dll:=ExternalLibraryName("sheetjs-maple"):
SheetToXLSX:=DefineExternal("SheetToXLSX",dll):
  1. 读取 pres.numbers 测试文件:

    ¥Read the pres.numbers test file:

with(ExcelTools);
Import(SheetToXLSX("pres.numbers"))

结果将显示 pres.numbers 的数据

¥The result will show the data from pres.numbers

Maple Screenshot

[^1]: 请参阅 Maple 文档中的 "ExcelTools"

¥See "ExcelTools" in the Maple documentation.

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

¥See read in "Reading Files"

[^3]: 见 write 于 "写入文件"

¥See write in "Writing Files"

[^4]: 请参阅 Maple 文档中的 "C OpenMaple 和 ExternalCalling 应用接口(API)"

¥See "C OpenMaple and ExternalCalling Application Program Interface (API)" in the Maple documentation.

[^5]: 在 PowerShell 终端窗口中,运行 wsl --install Ubuntu

¥In a PowerShell terminal window, run wsl --install Ubuntu

[^6]: 请参阅 Visual Studio 网站 下载链接。在 Visual Studio 安装程序中,安装 "使用 C++ 进行桌面开发" 工作流。

¥See the Visual Studio website for download links. In the Visual Studio Installer, install the "Desktop development with C++" workflow.