sheetjs_sheetjs/demos/deno
2022-03-22 15:38:02 -07:00
..
.gitignore deno and node ESM tests 2022-02-13 04:49:30 -05:00
doit.ts esm support and deno demo 2022-02-12 01:31:47 -05:00
Makefile remove broken CDNs [ci skip] 2022-03-22 15:38:02 -07:00
mjs.ts esm support and deno demo 2022-02-12 01:31:47 -05:00
node.ts esm support and deno demo 2022-02-12 01:31:47 -05:00
README.md remove broken CDNs [ci skip] 2022-03-22 15:38:02 -07:00
sheet2csv.ts remove broken CDNs [ci skip] 2022-03-22 15:38:02 -07:00
stream.ts "side-effect free" 2022-03-19 21:54:41 -04:00
x.ts remove broken CDNs [ci skip] 2022-03-22 15:38:02 -07: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.

Due to ongoing stability and sync issues with the Deno registry, scripts should use the unpkg CDN build:

// @deno-types="https://unpkg.com/xlsx/types/index.d.ts"
import * as XLSX from 'https://unpkg.com/xlsx/xlsx.mjs';

/* load the codepage support library for extended support with older formats  */
import * as cptable from 'https://unpkg.com/xlsx/dist/cpexcel.full.mjs';
XLSX.set_cptable(cptable);

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

const workbook = XLSX.readFile("test.xlsx");

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:

XLSX.writeFile(workbook, "test.xlsb");

Demos

Complete Examples

sheet2csv.ts is a complete command-line tool for generating CSV text from workbooks. Building the application is incredibly straightforward:

$ 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

The server demo includes a sample Deno server for parsing uploads and generating HTML TABLE previews.

Module Import Scenarios

All demos attempt to read a file and write a new file. doit.ts accepts the XLSX module as an argument.

  • x imports the ESM build without the codepage library:
// @deno-types="https://unpkg.com/xlsx/types/index.d.ts"
import * as XLSX from 'https://unpkg.com/xlsx/xlsx.mjs';
  • mjs imports the ESM build and the associated codepage library:
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:
import { createRequire } from 'https://deno.land/std/node/module.ts';
const require = createRequire(import.meta.url);
const XLSX = require('../../');

Analytics