sheet_to_json

This commit is contained in:
SheetJS 2022-11-30 20:13:00 -05:00
parent 74b94c7ae4
commit 292e61d647
5 changed files with 52 additions and 9 deletions

View File

@ -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");

View File

@ -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");

View File

@ -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:

View File

@ -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);

View File

@ -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
<Tabs>
<TabItem name="JS" value="JavaScript">
```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:
</TabItem>
<TabItem name="TS" value="TypeScript">
:::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<President>(ws);
```
</TabItem>
</Tabs>
`XLSX.utils.sheet_to_json` generates an array of JS objects. The function takes
an options argument:
| Option Name | Default | Description |
| :---------- | :------: | :-------------------------------------------------- |