sheetjs-clone/demos/deno/README.md

89 lines
2.7 KiB
Markdown
Raw Normal View History

2022-02-12 06:31:47 +00:00
# Deno
Deno is a runtime capable of running JS code including this library. There are
a few different builds and recommended use cases as covered in this demo.
2022-03-22 22:19:52 +00:00
Due to ongoing stability and sync issues with the Deno registry, scripts should
2022-04-12 11:59:15 +00:00
use [the CDN build](https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs):
2022-03-22 22:19:52 +00:00
```js
2022-04-12 11:59:15 +00:00
// @deno-types="https://cdn.sheetjs.com/xlsx-latest/package/types/index.d.ts"
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs';
2022-03-22 22:19:52 +00:00
/* load the codepage support library for extended support with older formats */
2022-04-12 11:59:15 +00:00
import * as cptable from 'https://cdn.sheetjs.com/xlsx-latest/package/dist/cpexcel.full.mjs';
2022-03-22 22:19:52 +00:00
XLSX.set_cptable(cptable);
```
2022-02-12 06:31:47 +00:00
## Reading and Writing Files
In general, the command-line flag `--allow-read` must be passed to enable file
reading. The flag `--allow-write` must be passed to enable file writing.
_Reading a File_
```ts
2022-03-22 22:19:52 +00:00
const workbook = XLSX.readFile("test.xlsx");
2022-02-12 06:31:47 +00:00
```
_Writing a File_
Older versions of the library did not properly detect features from Deno, so the
`buffer` export would return an array of bytes. Since `Deno.writeFileSync` does
not handle byte arrays, user code must generate a `Uint8Array` first:
```ts
2022-03-22 22:19:52 +00:00
XLSX.writeFile(workbook, "test.xlsb");
2022-02-12 06:31:47 +00:00
```
## Demos
2022-03-07 07:36:36 +00:00
**Complete Examples**
2022-02-13 09:35:34 +00:00
`sheet2csv.ts` is a complete command-line tool for generating CSV text from
workbooks. Building the application is incredibly straightforward:
```bash
$ deno compile -r --allow-read sheet2csv.ts # build the sheet2csv binary
$ ./sheet2csv test.xlsx # print the first worksheet as CSV
$ ./sheet2csv test.xlsx s5s # print worksheet "s5s" as CSV
```
2022-03-07 07:36:36 +00:00
The [`server` demo](../server) includes a sample Deno server for parsing uploads
and generating HTML TABLE previews.
2022-02-13 09:35:34 +00:00
**Module Import Scenarios**
2022-02-12 06:31:47 +00:00
All demos attempt to read a file and write a new file. [`doit.ts`](./doit.ts)
accepts the `XLSX` module as an argument.
- `x` imports the ESM build without the codepage library:
```ts
2022-04-12 11:59:15 +00:00
// @deno-types="https://cdn.sheetjs.com/xlsx-latest/package/types/index.d.ts"
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-latest/package/xlsx.mjs';
2022-02-12 06:31:47 +00:00
```
- `mjs` imports the ESM build and the associated codepage library:
```ts
import * as XLSX from '../../xlsx.mjs';
/* recommended for reading XLS files */
import * as cptable from '../../dist/cptable.full.mjs';
XLSX.set_cptable(cptable);
```
- `node` uses the node compatibility layer:
```ts
import { createRequire } from 'https://deno.land/std/node/module.ts';
const require = createRequire(import.meta.url);
const XLSX = require('../../');
```
[![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-xlsx?pixel)](https://github.com/SheetJS/js-xlsx)