requirejs

This commit is contained in:
SheetJS 2022-07-28 01:36:09 -04:00
parent 260bb7014e
commit 89ab3cc4aa
5 changed files with 93 additions and 7 deletions

@ -431,6 +431,36 @@ click the "Click to Export!" button to generate a file.
</details>
## RequireJS
[Standalone scripts](../../installation/standalone) comply with AND `define`
semantics, enabling use in RequireJS out of the box.
To enable use of the alias `xlsx`, the RequireJS config should set an alias in
the `paths` property:
```js
require.config({
baseUrl: ".",
name: "app",
paths: {
// highlight-next-line
xlsx: "xlsx.full.min"
}
});
// highlight-next-line
require(["xlsx"], function(XLSX) {
/* use XLSX here */
console.log(XLSX.version);
});
```
The [Live demo](pathname:///requirejs/requirejs.html) loads RequireJS from the
CDN, uses it to load the standalone script from the SheetJS CDN, and uses the
`XLSX` variable to create a button click handler that creates a workbook.
The `r.js` optimizer also supports the standalone scripts.
## Snowpack
Snowpack works with no caveats.

@ -74,14 +74,14 @@ const workbook = XLSX.readFile("test.xlsx", { cellFormula: true });
</TabItem>
<TabItem value="bun" label="Bun">
Typically file data will be available as a `Buffer` from a network request / API
Typically file data will be available as a `Uint8Array` from a network request
or stored in the filesystem. `cellFormula: true` should be added to the second
options argument to `read` or `readFile`:
**`XLSX.read`**
```js
/* using read in NodeJS, `cellFormula` is in the second argument */
/* using read in Bun, `cellFormula` is in the second argument */
const ab = await (await fetch("test.xlsx")).arrayBuffer();
const workbook = XLSX.read(ab, { cellFormula: true });
// ------------------------------^^^^^^^^^^^^^^^^^
@ -90,7 +90,7 @@ const workbook = XLSX.read(ab, { cellFormula: true });
**`XLSX.readFile`**
```js
/* using readFile in NodeJS, add `cellFormula` to the second argument */
/* using readFile in Bun, add `cellFormula` to the second argument */
const workbook = XLSX.readFile("test.xlsx", { cellFormula: true });
// -------------------------------------------^^^^^^^^^^^^^^^^^
```
@ -208,9 +208,7 @@ function ExportSimpleFormula(props) {
XLSX.writeFile(wb, "SheetJSFormula1.xlsx");
});
return (<>
<button onClick={xport}><b>Export XLSX!</b></button>
</>);
return (<button onClick={xport}><b>Export XLSX!</b></button>);
}
```

@ -33,7 +33,7 @@ extends this export with support for hyperlink styling.
HTTP / HTTPS links can be used directly:
```js
ws["A2"].l = { Target: "https://docs.sheetjs.com/#hyperlinks" };
ws["A2"].l = { Target: "https://docs.sheetjs.com/docs/csf/features/hyperlinks" };
ws["A3"].l = { Target: "http://localhost:7262/yes_localhost_works" };
```
@ -60,9 +60,13 @@ ws["B3"].l = { Target: "SheetJS.xlsb" }; /* Link to SheetJS.xlsb */
ws["B4"].l = { Target: "../SheetJS.xlsm" }; /* Link to ../SheetJS.xlsm */
```
:::caution
Relative Paths have undefined behavior in the SpreadsheetML 2003 format. Excel
2019 will treat a `..\` parent mark as two levels up.
:::
## Internal Links
Links where the target is a cell or range or defined name in the same workbook

@ -62,6 +62,8 @@ The following table covers some common formats:
| `A/P` | Meridien ("A" or "P") |
| `AM/PM` | Meridien ("AM" or "PM") |
:::note
`m` and `mm` are context-dependent. It is interpreted as "minutes" when the
previous or next date token represents a time (hours or seconds):
@ -71,6 +73,8 @@ yyyy-mm-dd hh:mm:ss
month minutes
```
:::
</details>
### 1904 and 1900 Date Systems

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h1>SheetJS Presidents Demo</h1>
<button id="xport">Click here to export</button>
<script src="http://requirejs.org/docs/release/2.3.3/comments/require.js"></script>
<script>
/* Wire up RequireJS */
require.config({
baseUrl: ".",
name: "app",
paths: {
xlsx: "https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min"
}
});
require(["xlsx"], function(XLSX) {
document.getElementById("xport").addEventListener("click", async() => {
/* fetch JSON data and parse */
const url = "https://sheetjs.com/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 = XLSX.utils.json_to_sheet(rows);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dates");
/* fix headers */
XLSX.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 */
XLSX.writeFileXLSX(workbook, "Presidents.xlsx");
});
});
</script>
</body>
</html>