5.0 KiB
title | sidebar_label | pagination_prev | pagination_next | sidebar_position |
---|---|---|---|---|
Bundling Sheets with SWC | SWC spack | demos/index | demos/grid/index | 21 |
import current from '/version.js'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import CodeBlock from '@theme/CodeBlock';
SWC1 is a JS toolchain. SWC provides spack
(formally called "swcpack") for
bundling scripts.
SheetJS is a JavaScript library for reading and writing data from spreadsheets.
This demo uses spack
and SheetJS to export data. We'll explore how to bundle
SheetJS in a site using spack
and how to export data to spreadsheets.
:::note
This demo was last tested on 2023 October 20 against SWC 1.2.246
:::
Integration Details
The "Frameworks" section covers installation with Yarn and other package managers.
After installing the SheetJS module in a SWC spack
project, import
statements can load relevant parts of the library.
Projects that import data will use methods such as read
2 to parse workbooks
and sheet_to_json
3 to generate usable data from files. As sheet_to_json
is part of the utils
object, the required import is:
import { read, utils } from 'xlsx';
Projects that export data will use methods such as json_to_sheet
4 to
generate worksheets and writeFile
5 to export files. As json_to_sheet
is
part of the utils
object, the required import is:
import { utils, writeFile } from 'xlsx';
:::warning pass
When this demo was tested against the latest version, spack
crashed:
thread '<unnamed>' panicked at 'cannot access a scoped thread local variable without calling `set` first',
Until the bug is fixed, it is strongly recommended to use @swc/core@1.2.246
.
:::
Complete Example
- Initialize a new project:
mkdir sheetjs-spack
cd sheetjs-spack
npm init -y
- Install the dependencies using a package manager:
:::note pass
The regenerator-runtime
dependency is used for transpiling fetch
and is not
required if the interface code does not use fetch
or Promises.
:::
- Save the following to
index.js
:
import { utils, version, writeFileXLSX } from 'xlsx';
document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
const url = "https://sheetjs.com/data/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"));
/* 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");
});
- Create an
spack.config.js
config file:
module.exports = ({
entry: {
'web': __dirname + '/index.js',
},
output: {
path: __dirname + '/lib'
},
module: {},
});
- Build for production:
npx spack
This command will create the script lib/web.js
- Create a small HTML page that loads the generated script:
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script src="lib/web.js"></script>
</body>
</html>
- Start a local HTTP server, then go to
http://localhost:8080/
npx http-server
Click on "Click here to export" to generate a file.
-
See "Bundling Configuration" in the SWC documentation for more details. ↩︎