docs.sheetjs.com/docz/docs/03-demos/09-cloud/19-deno.md
2023-07-23 17:01:30 -04:00

3.1 KiB

title pagination_prev pagination_next
Deno Deploy demos/local/index demos/extensions/index

import current from '/version.js'; import CodeBlock from '@theme/CodeBlock';

Deno Deploy offers "Serverless Functions" powered by Deno.

The Deno installation instructions apply to Deno Deploy scripts.

:::warning

Deno Deploy does not offer any sort of temporary file access in functions.

This breaks web frameworks that use the filesystem in body parsing.

:::

:::caution

When the demo was last tested, Deno Deploy required a GitHub account.

:::

Supported Frameworks

When the demo was last tested, the drash server framework used an in-memory approach for parsing POST request bodies.

Parsing Data

When files are submitted via HTTP POST, the bodyParam method can fetch data. The content property of the returned object can be parsed with XLSX.read.

The following example assumes the file is submitted at field name file:

{\ // @deno-types="https://cdn.sheetjs.com/xlsx-${current}/package/types/index.d.ts" import { read, utils } from 'https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs'; import * as Drash from "https://deno.land/x/drash@v2.5.4/mod.ts"; \n\ class SheetJSResource extends Drash.Resource { public paths = ["/"]; \n\ public POST(request: Drash.Request, response: Drash.Response) { // highlight-start /* get data from body */ const file = request.bodyParam<Drash.Types.BodyFile>("file"); /* parse */ var wb = read(file.content, {type: "buffer", dense: true}); // highlight-end /* generate HTML from first worksheet */ return response.html(utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]])); } }}

Demo

:::note

This demo was last tested on 2023 June 05. The service https://s2c.sheetjs.com was implemented using this exact sequence.

:::

  1. Register and Sign in.

  2. Click "New Project" to create a new Project. In the next screen, look for the "Hello World" sample and click the corresponding "Fork" button.

  3. Download s2c.ts. Open with a text editor and copy the contents into the playground editor (left pane).

  4. Click "Save and Deploy".

Testing

  1. Download the test file https://sheetjs.com/pres.xlsx

  2. In the browser window, click "Choose File" and select the downloaded file. Click "Submit" and the page will show the contents in a HTML TABLE.

  3. Click the "Fullscreen" icon in the top-right corner of the page window.

  4. Open a terminal window and download https://sheetjs.com/pres.numbers:

curl -LO https://sheetjs.com/pres.numbers
  1. Copy the first curl line from the page and run in the terminal. For example, if the deployment is clean-badger-69, the command would be
curl -X POST -F"file=@pres.numbers" https://clean-badger-69.deno.dev/

The output will be an HTML table

  1. Copy the second curl line from the page and run in the terminal. For example, if the deployment is clean-badger-69, the command would be
curl -X POST -F"file=@pres.numbers" -F"type=csv" https://clean-badger-69.deno.dev/

The output will be CSV.