From 292e61d6478fa199e3f018144f2a7594d4e0bda1 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Wed, 30 Nov 2022 20:13:00 -0500 Subject: [PATCH] sheet_to_json --- docz/docs/03-demos/07-worker.md | 4 +-- docz/docs/03-demos/08-stream.md | 2 +- docz/docs/03-demos/43-ml.mdx | 2 +- docz/docs/06-solutions/01-input.md | 2 +- docz/docs/08-api/09-utilities.md | 51 +++++++++++++++++++++++++++--- 5 files changed, 52 insertions(+), 9 deletions(-) diff --git a/docz/docs/03-demos/07-worker.md b/docz/docs/03-demos/07-worker.md index bb00379..e0cb600 100644 --- a/docz/docs/03-demos/07-worker.md +++ b/docz/docs/03-demos/07-worker.md @@ -361,8 +361,8 @@ is an XLSX file with 300000 rows (approximately 20 MB) yielding a CSV of 10 MB. ```jsx live function SheetJSFetchCSVStreamFile() { - const [state, setState] = React.useState(""); - const [__html, setHTML] = React.useState(""); + const [state, setState] = React.useState(""); + const [__html, setHTML] = React.useState(""); const [cnt, setCnt] = React.useState(0); const [hz, setHz] = React.useState(0); const [url, setUrl] = React.useState("https://oss.sheetjs.com/test_files/large_strings.xlsx"); diff --git a/docz/docs/03-demos/08-stream.md b/docz/docs/03-demos/08-stream.md index 5efb479..00d5bfe 100644 --- a/docz/docs/03-demos/08-stream.md +++ b/docz/docs/03-demos/08-stream.md @@ -153,7 +153,7 @@ is an XLSX file with 300000 rows (approximately 20 MB) ```jsx live function SheetJSFetchCSVStreamWorker() { const [__html, setHTML] = React.useState(""); - const [state, setState] = React.useState(""); + const [state, setState] = React.useState(""); const [cnt, setCnt] = React.useState(0); const [url, setUrl] = React.useState("https://oss.sheetjs.com/test_files/large_strings.xlsx"); diff --git a/docz/docs/03-demos/43-ml.mdx b/docz/docs/03-demos/43-ml.mdx index fcef16f..92a32c6 100644 --- a/docz/docs/03-demos/43-ml.mdx +++ b/docz/docs/03-demos/43-ml.mdx @@ -189,7 +189,7 @@ TF.js and other libraries tend to operate on individual columns, equivalent to: ```js var sepal_lengths = [5.1, 4.9, ...]; -var sepal_widths = [3.5, 3, ...]; +var sepal_widths = [3.5, 3, ...]; ``` When a `tensor2d` can be exported, it will look different from the spreadsheet: diff --git a/docz/docs/06-solutions/01-input.md b/docz/docs/06-solutions/01-input.md index 75771df..5fdf29b 100644 --- a/docz/docs/06-solutions/01-input.md +++ b/docz/docs/06-solutions/01-input.md @@ -651,7 +651,7 @@ import { readAll } from "https://deno.land/std/streams/conversion.ts"; const file = await Deno.open("test.xlsx", {read: true}); /* \`content\` will be a Uint8Array holding the full contents of the stream */ -const content = await readAll(file); +const content = await readAll(file); /* Since this is a Uint8Array, \`XLSX.read\` "just works" */ const wb = XLSX.read(content); diff --git a/docz/docs/08-api/09-utilities.md b/docz/docs/08-api/09-utilities.md index 771d54e..584991e 100644 --- a/docz/docs/08-api/09-utilities.md +++ b/docz/docs/08-api/09-utilities.md @@ -1,9 +1,10 @@ --- sidebar_position: 9 +title: Utility Functions --- - -# Utility Functions +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; The `sheet_to_*` functions accept a worksheet and an optional options object. @@ -557,14 +558,56 @@ function SheetJSHTML() { ## Array Output + + + ```js var arr = XLSX.utils.sheet_to_json(ws, opts); var aoa = XLSX.utils.sheet_to_json(ws, {header: 1, ...other_opts}); ``` -`XLSX.utils.sheet_to_json` generates different types of JS objects. The function -takes an options argument: + + + +:::caution + +TypeScript types are purely informational. They are not included at run time +and do not influence the behavior of the `sheet_to_json` method. + +**`sheet_to_json` does not perform field validation!** + +::: + +The main type signature treats each row as `any`: + +```ts +const data: any[] = XLSX.utils.sheet_to_json(ws, opts); +``` + +The `any[][]` overload is designed for use with `header: 1` (array of arrays): + +```ts +const aoa: any[][] = XLSX.utils.sheet_to_json(ws, { header: 1, ...other_opts }); +``` + +An interface can be passed as a generic parameter. `sheet_to_json` will still +return an array of plain objects (the types do not affect runtime behavior): + +```ts +interface President { + Name: string; + Index: number; +} + +const data: President[] = XLSX.utils.sheet_to_json(ws); +``` + + + + +`XLSX.utils.sheet_to_json` generates an array of JS objects. The function takes +an options argument: | Option Name | Default | Description | | :---------- | :------: | :-------------------------------------------------- |