使用 Browserify 打包工作表
Browserify 是一个模块打包器。
¥Browserify is a module bundler.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
该演示使用 Browserify 和 SheetJS 导出数据。我们将探讨如何使用 Browserify 将 SheetJS 添加到网站以及如何将数据导出到电子表格。
¥This demo uses Browserify and SheetJS to export data. We'll explore how to add SheetJS to a site using Browserify and how to export data to spreadsheets.
该演示重点介绍与 Browserify 打包程序的集成细节。
¥This demo focuses on integration details with the Browserify 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:
Browserify | 日期 |
---|---|
17.0.0 | 2024-04-13 |
16.5.2 | 2024-04-13 |
15.2.0 | 2024-04-13 |
14.5.0 | 2024-04-13 |
13.3.0 | 2024-04-13 |
12.0.2 | 2024-04-13 |
11.2.0 | 2024-04-13 |
10.2.6 | 2024-04-13 |
9.0.8 | 2024-04-13 |
8.1.3 | 2024-04-13 |
7.1.0 | 2024-04-13 |
6.3.4 | 2024-04-13 |
5.13.1 | 2024-04-13 |
4.2.3 | 2024-04-13 |
3.46.1 | 2024-04-13 |
集成详情
¥Integration Details
"构架" 章节 涵盖了 Yarn 和其他包管理器的安装。
¥The "Frameworks" section covers installation with Yarn and other package managers.
在 Browserify 项目中安装 SheetJS 模块后,require
表达式可以加载库的相关部分。
¥After installing the SheetJS module in a Browserify project, require
expressions can load relevant parts of the library.
var XLSX = require("xlsx");
// ... use XLSX ...
Browserify 还可以处理 Web Worker 脚本中的 require
表达式。
¥Browserify can also process require
expressions in Web Worker scripts.
完整示例
¥Complete Example
-
初始化一个新项目:
¥Initialize a new project:
mkdir sheetjs-browserify
cd sheetjs-browserify
npm init -y
-
使用包管理器安装 tarball:
¥Install the tarball using a package manager:
- npm
- pnpm
- Yarn
npm i --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
pnpm install --save https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
yarn add https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
-
将以下内容保存到
index.js
:¥Save the following to
index.js
:
const { utils, version, writeFileXLSX } = require('xlsx');
document.getElementById("xport").addEventListener("click", function() {
/* fetch JSON data and parse */
var url = "https://xlsx.nodejs.cn/executive.json";
fetch(url).then(function(res) { return res.json(); }).then(function(raw_data) {
/* filter for the Presidents */
var prez = raw_data.filter(function(row) { return row.terms.some(function(term) { return term.type === "prez"; }); });
/* sort by first presidential term */
prez.forEach(function(row) {
row.start = row.terms.find(function(term) {
return term.type === "prez";
}).start
});
prez.sort(function(l,r) { return l.start.localeCompare(r.start); });
/* flatten objects */
var rows = prez.map(function(row) { return {
name: row.name.first + " " + row.name.last,
birthday: row.bio.birthday
}; });
/* generate worksheet and workbook */
var worksheet = utils.json_to_sheet(rows);
var 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 */
var max_width = rows.reduce(function(w, r) { return 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");
});
});
-
打包脚本:
¥Bundle the scripts:
npx browserify index.js > index.min.js
旧 browserify
版本必须使用本地版本。对于版本 3.46.1
:
¥Legacy browserify
versions must use a local version. For version 3.46.1
:
npm install --save browserify@3.46.1
./node_modules/.bin/browserify index.js > index.min.js
-
创建一个加载脚本的小型 HTML 页面。保存到
index.html
:¥Create a small HTML page that loads the script. Save to
index.html
:
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script src="./index.min.js"></script>
</body>
</html>
-
启动本地 HTTP 服务器:
¥Start a local HTTP server:
npx http-server .
-
在 Web 浏览器中加载显示的 URL(通常为
http://localhost:8080/
)。¥Load the displayed URL (typically
http://localhost:8080/
) in a web browser.
点击 "点击此处导出" 生成文件。
¥Click on "Click here to export" to generate a file.