sheetjs-clone/demos/deno
2022-02-12 01:31:47 -05:00
..
doit.ts esm support and deno demo 2022-02-12 01:31:47 -05:00
jspm.ts esm support and deno demo 2022-02-12 01:31:47 -05:00
Makefile esm support and deno demo 2022-02-12 01:31:47 -05: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 esm support and deno demo 2022-02-12 01:31:47 -05:00
x.ts esm support and deno demo 2022-02-12 01:31:47 -05: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.

For user code, the sheetjs module can be used.

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.

Starting in version 0.18.1, this library will check for the Deno global and use Deno.readFileSync and Deno.writeFileSync behind the scenes.

For older versions, the API functions must be called from user code.

Reading a File

const filedata = Deno.readFileSync("test.xlsx");
const workbook = XLSX.read(filedata, {type: "buffer"});
/* DO SOMETHING WITH workbook HERE */

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:

const buf = XLSX.write(workbook, {type: "buffer", bookType: "xlsb"});
const u8: Uint8Array = new Uint8Array(buf);
Deno.writeFileSync("test.xlsb", u8);

Demos

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:
import * as XLSX from 'https://deno.land/x/sheetjs/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);
  • jspm imports the browser standalone script using JSPM:
import * as XLSX from 'https://jspm.dev/npm:xlsx!cjs';
  • 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