docs.sheetjs.com/docz/docs/02-getting-started/01-installation/01-standalone.mdx

173 lines
5.6 KiB
Plaintext
Raw Normal View History

2022-05-16 03:26:04 +00:00
---
2022-08-24 23:48:22 +00:00
pagination_prev: getting-started/index
pagination_next: getting-started/example
2022-05-16 03:26:04 +00:00
sidebar_position: 1
sidebar_custom_props:
summary: Classic pages with simple <script> tags
---
import current from '/version.js';
2023-05-07 13:58:36 +00:00
import CodeBlock from '@theme/CodeBlock';
2022-05-16 03:26:04 +00:00
# Standalone Browser Scripts
Each standalone release script is available at <https://cdn.sheetjs.com/>.
2023-05-07 13:58:36 +00:00
<p>The current version is {current} and can be referenced as follows:</p>
2022-05-16 03:26:04 +00:00
2023-05-07 13:58:36 +00:00
<CodeBlock language="html">{`\
2022-05-16 03:26:04 +00:00
<!-- use version ${current} -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js"></script>`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-05-16 03:26:04 +00:00
2023-06-25 09:36:58 +00:00
:::tip pass
2022-05-16 03:26:04 +00:00
2023-04-27 09:12:19 +00:00
[Watch the repo](https://git.sheetjs.com/SheetJS/sheetjs) or subscribe to the
[RSS feed](https://git.sheetjs.com/sheetjs/sheetjs/tags.rss) to be notified when
new versions are released!
:::
2022-05-16 03:26:04 +00:00
2022-08-08 06:59:57 +00:00
:::warning
2022-07-25 23:18:00 +00:00
2022-08-25 08:22:28 +00:00
A number of services host older versions of the SheetJS libraries. Due to
syncing issues, they are generally out of date.
2022-07-25 23:18:00 +00:00
They are known CDN bugs.
<https://cdn.sheetjs.com/> is the authoritative source for SheetJS modules.
:::
2022-05-16 03:26:04 +00:00
2022-06-05 22:43:44 +00:00
## Browser Scripts
2022-05-16 03:26:04 +00:00
2022-06-05 22:43:44 +00:00
`xlsx.full.min.js` is the complete standalone script. It includes support for
reading and writing many spreadsheet formats.
2022-05-16 03:26:04 +00:00
2023-05-07 13:58:36 +00:00
<CodeBlock language="html">{`\
2022-06-05 22:43:44 +00:00
<!-- use xlsx.full.min.js from version ${current} -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js"></script>`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-05-16 03:26:04 +00:00
A slimmer build is generated at `dist/xlsx.mini.min.js`. Compared to full build:
- codepage library skipped (no support for XLS encodings)
- no support for XLSB / XLS / Lotus 1-2-3 / SpreadsheetML 2003 / Numbers
- node stream utils removed
2022-06-05 22:43:44 +00:00
<details><summary><b>How to integrate the mini build</b> (click to show)</summary>
Replace references to `xlsx.full.min.js` with `xlsx.mini.min.js`. Starting from
scratch, a single script tag should be added at the top of the HTML page:
2022-05-16 03:26:04 +00:00
2023-05-07 13:58:36 +00:00
<CodeBlock language="html">{`\
2022-05-16 03:26:04 +00:00
<!-- use xlsx.mini.min.js from version ${current} -->
<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.mini.min.js"></script>`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-05-16 03:26:04 +00:00
</details>
2022-06-05 22:43:44 +00:00
### Internet Explorer and Older Browsers
2022-05-16 03:26:04 +00:00
For broad compatibility with JavaScript engines, the library is written using
2022-06-05 22:43:44 +00:00
ECMAScript 3 language dialect. A "shim" script provides implementations of
functions for older browsers and environments.
2023-05-07 13:58:36 +00:00
Due to SSL compatibility issues, older versions of IE will not be able to
2022-06-05 22:43:44 +00:00
use the CDN scripts directly. They should be downloaded and saved to a public
directory in the site:
2023-05-07 13:58:36 +00:00
2022-06-05 22:43:44 +00:00
<ul>
<li>Standalone: <a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.mini.min.js`}>https://cdn.sheetjs.com/xlsx-{current}/package/dist/xlsx.mini.min.js</a></li>
<li>Shim: <a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/shim.min.js`}>https://cdn.sheetjs.com/xlsx-{current}/package/dist/shim.min.js</a></li>
</ul>
2022-05-16 03:26:04 +00:00
2023-05-07 13:58:36 +00:00
Add a `script` reference to the shim before the standalone script:
2022-05-16 03:26:04 +00:00
```html
<!-- add the shim first -->
<script type="text/javascript" src="shim.min.js"></script>
<!-- after the shim is referenced, add the library -->
<script type="text/javascript" src="xlsx.full.min.js"></script>
```
2022-06-05 22:43:44 +00:00
### Web Workers
2022-05-16 03:26:04 +00:00
2022-06-05 22:43:44 +00:00
The standalone scripts can be loaded using `importScripts` at the top of the
worker scripts:
2023-05-07 13:58:36 +00:00
<CodeBlock language="js">{`\
2022-06-05 22:43:44 +00:00
importScripts("https://cdn.sheetjs.com/xlsx-${current}/package/dist/shim.min.js");
importScripts("https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js");`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-05-16 03:26:04 +00:00
2022-10-21 00:10:10 +00:00
## ECMAScript Module Imports
2022-06-05 22:43:44 +00:00
:::caution
This section refers to imports using `script type="module"`. For imports in
2022-08-25 08:22:28 +00:00
modern projects using Webpack or React or Angular or VueJS, the installation is
2022-10-30 05:45:37 +00:00
described [in the next section](/docs/getting-started/installation/frameworks).
2022-06-05 22:43:44 +00:00
:::
2022-05-16 03:26:04 +00:00
The ECMAScript Module build is saved to `xlsx.mjs` and can be directly added to
a page with a `script` tag using `type="module"`:
2023-05-07 13:58:36 +00:00
<CodeBlock language="html">{`\
2022-05-16 03:26:04 +00:00
<script type="module">
import { read, writeFileXLSX } from "https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs";
</script>`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-05-16 03:26:04 +00:00
2022-08-26 19:21:53 +00:00
If Encoding support is required, `cpexcel.full.mjs` must be manually imported:
2022-05-16 03:26:04 +00:00
2023-05-07 13:58:36 +00:00
<CodeBlock language="html">{`\
2022-05-16 03:26:04 +00:00
<script type="module">
/* load the codepage support library for extended support with older formats */
import { set_cptable } from "https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs";
import * as cptable from 'https://cdn.sheetjs.com/xlsx-${current}/package/dist/cpexcel.full.mjs';
set_cptable(cptable);
</script>`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-05-16 03:26:04 +00:00
2022-06-05 22:43:44 +00:00
Dynamic imports with `import()` can be used in data export scenarios. This
example will download the library only when the export button is pressed:
2023-05-07 13:58:36 +00:00
<CodeBlock language="html">{`\
2022-06-05 22:43:44 +00:00
<button id="xport">Export</button>
<script type="module">
xport.addEventListener("click", async() => {
2023-05-07 13:58:36 +00:00
\n\
2022-06-05 22:43:44 +00:00
/* dynamically import the library in the event listener */
// highlight-next-line
const XLSX = await import("https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs");
2023-05-07 13:58:36 +00:00
\n\
2022-06-05 22:43:44 +00:00
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet([["a","b","c"],[1,2,3]]);
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, "SheetJSESMTest.xlsx");
});
</script>`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-06-05 22:43:44 +00:00
2023-04-29 11:21:37 +00:00
Web Worker support is noted in [the demo](/docs/demos/bigdata/worker#installation)
2022-06-05 22:43:44 +00:00
## Bower
:::caution
Bower is deprecated and the maintainers recommend using other tools.
:::
2023-03-28 04:57:47 +00:00
The Bower package manager plays nice with the CDN tarballs:
2022-06-05 22:43:44 +00:00
2023-05-07 13:58:36 +00:00
<CodeBlock language="bash">{`\
2022-08-07 07:48:40 +00:00
npx bower install https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz`}
2023-05-07 13:58:36 +00:00
</CodeBlock>
2022-06-05 22:43:44 +00:00
Bower will place the standalone scripts in `bower_components/js-xlsx/dist/`