docs.sheetjs.com/docz/docs/02-getting-started/01-installation/06-deno.md
2022-11-13 15:45:13 -05:00

4.3 KiB

pagination_prev pagination_next sidebar_position sidebar_custom_props
getting-started/index getting-started/example 6
summary
Import ECMAScript Modules and TypeScript definitions

import current from '/version.js'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Deno

Module scripts and type definitions are available at https://cdn.sheetjs.com/.

Using the URL imports, deno run will automatically download scripts and types:

{`\
// @deno-types="https://cdn.sheetjs.com/xlsx-${current}/package/types/index.d.ts"
import * as XLSX from 'https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs';`}

The @deno-types comment instructs Deno to use the type definitions.

:::caution Deno support is considered experimental.

Great open source software grows with user tests and reports. Any issues should be reported to the Deno project for further diagnosis.

:::

Encoding support

If Encoding support is required, cpexcel.full.mjs must be manually imported:

{`\
/* load the codepage support library for extended support with older formats  */
import * as cptable from 'https://cdn.sheetjs.com/xlsx-${current}/package/dist/cpexcel.full.mjs';
XLSX.set_cptable(cptable);`}

Node Compatibility Mode

:::caution

Node Compatibility Mode is useful for migrating existing NodeJS scripts to Deno. It is considered unstable by the platform developers.

For greenfield Deno projects, HTTP imports are strongly recommended.

:::

The "NodeJS" installation section includes instructions for installing the NodeJS package.

After installing, the std/node require can be used directly:

/* load the `require` machinery */
import { createRequire } from "https://deno.land/std/node/module.ts";
const require = createRequire(import.meta.url);

/* require the library */
const XLSX = require("xlsx");

Upgrade Notes

Upgrading to the latest version involves changing the import URLs. The import and the types URLs should be updated at the same time:

{`\
-// @deno-types="https://cdn.sheetjs.com/xlsx-0.18.3/package/types/index.d.ts"
+// @deno-types="https://cdn.sheetjs.com/xlsx-${current}/package/types/index.d.ts"

-import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.18.3/package/xlsx.mjs';
+import * as XLSX from 'https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs';

-import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.18.3/package/dist/cpexcel.full.mjs';
+import * as cptable from 'https://cdn.sheetjs.com/xlsx-${current}/package/dist/cpexcel.full.mjs';
`}

Deno Registry

:::warning

The official Deno registry is out of date. This is a registry bug.

https://cdn.sheetjs.com/ is the authoritative source for SheetJS scripts.

:::

Applications using the Deno registry can migrate by changing the URLs. After migrating, it is easy to update by changing the version number.

The SheetJS CDN version has no v. For example, v0.18.3 maps to 0.18.3:

-// @deno-types="https://deno.land/x/sheetjs@v0.18.3/types/index.d.ts"
+// @deno-types="https://cdn.sheetjs.com/xlsx-0.18.3/package/types/index.d.ts"

-import * as XLSX from 'https://deno.land/x/sheetjs@v0.18.3/xlsx.mjs';
+import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.18.3/package/xlsx.mjs';

-import * as cptable from 'https://deno.land/x/sheetjs@v0.18.3/dist/cpexcel.full.mjs';
+import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.18.3/package/dist/cpexcel.full.mjs';

Version-less imports (https://deno.land/x/sheetjs/xlsx.mjs) map to 0.18.3:

-// @deno-types="https://deno.land/x/sheetjs/types/index.d.ts"
+// @deno-types="https://cdn.sheetjs.com/xlsx-0.18.3/package/types/index.d.ts"

-import * as XLSX from 'https://deno.land/x/sheetjs/xlsx.mjs';
+import * as XLSX from 'https://cdn.sheetjs.com/xlsx-0.18.3/package/xlsx.mjs';

-import * as cptable from 'https://deno.land/x/sheetjs/dist/cpexcel.full.mjs';
+import * as cptable from 'https://cdn.sheetjs.com/xlsx-0.18.3/package/dist/cpexcel.full.mjs';