Skip to main content

使用 RollupJS 打包工作表

RollupJS 是一个模块打包器。

¥RollupJS is a module bundler.

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

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

本演示使用 RollupJS 和 SheetJS 导出数据。我们将探讨如何使用 RollupJS 将 SheetJS 打包到站点中,以及如何将数据导出到电子表格。

¥This demo uses RollupJS and SheetJS to export data. We'll explore how to bundle SheetJS in a site using RollupJS and how to export data to spreadsheets.

该演示重点介绍与 RollupJS 打包器的集成细节。

¥This demo focuses on integration details with the RollupJS bundler.

这些演示遵循 "导出教程",其中更详细地介绍了 SheetJS 库的用法。

¥The demos follow the "Export Tutorial", which covers SheetJS library usage in more detail.

测试部署

本 demo 在以下环境下进行了测试:

¥This demo was tested in the following environments:

版本日期
4.13.02024-03-25
3.29.42024-03-25
2.79.12024-03-25
1.32.12024-03-25

集成详情

¥Integration Details

"构架" 章节 涵盖了 Yarn 和其他包管理器的安装。

¥The "Frameworks" section covers installation with Yarn and other package managers.

在 RollupJS 项目中安装 SheetJS 模块后,import 语句可以加载库的相关部分:

¥After installing the SheetJS module in a RollupJS project, import statements can load relevant parts of the library:

import { read, utils, writeFileXLSX } from 'xlsx';

所需插件

¥Required Plugin

RollupJS 可以使用 @rollup/plugin-node-resolve 插件支持 NodeJS 模块。应将标志 --plugin @rollup/plugin-node-resolve 传递给 RollupJS CLI 工具

¥RollupJS can support NodeJS modules using the @rollup/plugin-node-resolve plugin. The flag --plugin @rollup/plugin-node-resolve should be passed to the RollupJS CLI tool

npx rollup index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife

对于 RollupJS 主要版本 1,插件是 rollup-plugin-node-resolve

¥For RollupJS major version 1, the plugin is rollup-plugin-node-resolve:

npx rollup@1.x index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife

完整示例

¥Complete Example

  1. 初始化一个新项目:

    ¥Initialize a new project:

mkdir sheetjs-rollup
cd sheetjs-rollup
npm init -y
  1. 使用包管理器安装 tarball:

    ¥Install the tarball using a package manager:

yarn add https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz rollup@4.x @rollup/plugin-node-resolve
  1. 将以下内容保存到 index.js

    ¥Save the following to index.js:

index.js
import { utils, version, writeFileXLSX } from 'xlsx';

document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
const url = "https://xlsx.nodejs.cn/executive.json";
const raw_data = await (await fetch(url)).json();

/* filter for the Presidents */
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));

/* sort by first presidential term */
prez.forEach(row => row.start = row.terms.find(term => term.type === "prez").start);
prez.sort((l,r) => l.start.localeCompare(r.start));

/* flatten objects */
const rows = prez.map(row => ({
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}));

/* generate worksheet and workbook */
const worksheet = utils.json_to_sheet(rows);
const workbook = utils.book_new();
utils.book_append_sheet(workbook, worksheet, "Dates");

/* fix headers */
utils.sheet_add_aoa(worksheet, [["Name", "Birthday"]], { origin: "A1" });

/* calculate column width */
const max_width = rows.reduce((w, r) => Math.max(w, r.name.length), 10);
worksheet["!cols"] = [ { wch: max_width } ];

/* create an XLSX file and try to save to Presidents.xlsx */
writeFileXLSX(workbook, "Presidents.xlsx");
});
  1. 打包脚本:

    ¥Bundle the script:

npx rollup index.js --plugin @rollup/plugin-node-resolve --file bundle.js --format iife

此步骤将创建 bundle.js

¥This step will create bundle.js

  1. 创建一个加载脚本的小型 HTML 页面。保存到 index.html

    ¥Create a small HTML page that loads the script. Save to index.html:

index.html
<!DOCTYPE html>
<html lang="en">


<head></head>


<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script type="module" src="./bundle.js"></script>
</body>
</html>
  1. 启动本地 HTTP 服务器:

    ¥Start a local HTTP server:

npx http-server .

在 Web 浏览器中访问显示的 URL(通常为 http://localhost:8080/)。点击 "点击此处导出" 生成文件。

¥Access the displayed URL (typically http://localhost:8080/) in a web browser. Click on "Click here to export" to generate a file.