--- sidebar_position: 9 title: Utility Functions pagination_prev: api/write-options --- import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; The `sheet_to_*` functions accept a worksheet and an optional options object. The `*_to_sheet` functions accept a data object and an optional options object. The `sheet_add_*` functions accept worksheet, data, and optional options. The examples are based on the following worksheet:
| S | h | e | e | t | J | S |
| 1 | 2 | 5 | 6 | 7 | ||
| 2 | 3 | 6 | 7 | 8 | ||
| 3 | 4 | 7 | 8 | 9 | ||
| 4 | 5 | 6 | 7 | 8 | 9 | 0 |
Objects
{"\n[\n { C: 2, D: 3 },\n { D: 1, C: 4 } // different key order\n]\n"}
Worksheet when same `header` array is passed to `sheet_add_json`
New contents of `header`
{JSON.stringify(header)}
Worksheet when no `header` property is passed to `sheet_add_json`
)
}
```
:::
## HTML Table Input
[**This has been moved to a separate page**](/docs/api/utilities/html#html-table-input)
### Value Override
[**This has been moved to a separate page**](/docs/api/utilities/html#value-override)
## Delimiter-Separated Output
```js
var csv = XLSX.utils.sheet_to_csv(ws, opts);
```
As an alternative to the `writeFile` CSV type, `XLSX.utils.sheet_to_csv` also
produces CSV output. The function takes an options argument:
| Option Name | Default | Description |
| :----------- | :------: | :------------------------------------------------- |
|`FS` | `","` | "Field Separator" delimiter between fields |
|`RS` | `"\n"` | "Record Separator" delimiter between rows |
|`dateNF` | FMT 14 | Use specified date format in string output |
|`strip` | false | Remove trailing field separators in each record ** |
|`blankrows` | true | Include blank lines in the CSV output |
|`skipHidden` | false | Skips hidden rows/columns in the CSV output |
|`forceQuotes` | false | Force quotes around fields |
- `strip` will remove trailing commas from each line under default `FS/RS`
- `blankrows` must be set to `false` to skip blank lines.
- Fields containing the record or field separator will automatically be wrapped
in double quotes; `forceQuotes` forces all cells to be wrapped in quotes.
- `XLSX.write` with `csv` type will always prepend the UTF-8 byte-order mark for
Excel compatibility. `sheet_to_csv` returns a JS string and omits the mark.
Using `XLSX.write` with type `string` will also skip the mark.
Starting from the example worksheet:
```jsx live
function SheetJSCSVTest() {
var ws = XLSX.utils.aoa_to_sheet([
["S", "h", "e", "e", "t", "J", "S"],
[ 1, 2, , , 5, 6, 7],
[ 2, 3, , , 6, 7, 8],
[ 3, 4, , , 7, 8, 9],
[ 4, 5, 6, 7, 8, 9, 0]
]);
return (
Worksheet (as HTML)
XLSX.utils.sheet_to_csv(ws)
{XLSX.utils.sheet_to_csv(ws)}
XLSX.utils.sheet_to_csv(ws, {'{'} FS: "\t" {'}'})
{XLSX.utils.sheet_to_csv(ws, { FS: "\t" })}
XLSX.utils.sheet_to_csv(ws, {'{'} FS: ":", RS: "|" {'}'})
{XLSX.utils.sheet_to_csv(ws, { FS: ":", RS: "|" })}
);
}
```
**UTF-16 Text Output**
```js
var txt = XLSX.utils.sheet_to_txt(ws, opts);
```
The `txt` output type uses the tab character as the field separator. If the
`codepage` library is available (included in full distribution but not core),
the output will be encoded in `CP1200` and the UTF-16 BOM will be added.
`XLSX.utils.sheet_to_txt` takes the same arguments as `sheet_to_csv`.
## HTML Output
```js
var html = XLSX.utils.sheet_to_html(ws, opts);
```
As an alternative to the `writeFile` HTML type, `XLSX.utils.sheet_to_html` also
produces HTML output. The function takes an options argument:
| Option Name | Default | Description |
| :---------- | :------: | :-------------------------------------------------- |
|`id` | | Specify the `id` attribute for the `TABLE` element |
|`editable` | false | If true, set `contenteditable="true"` for every TD |
|`header` | | Override header (default `html body`) |
|`footer` | | Override footer (default `/body /html`) |
Starting from the example worksheet:
```jsx live
function SheetJSHTML() {
var ws = XLSX.utils.aoa_to_sheet([
["S", "h", "e", "e", "t", "J", "S"],
[ 1, 2, , , 5, 6, 7],
[ 2, 3, , , 6, 7, 8],
[ 3, 4, , , 7, 8, 9],
[ 4, 5, 6, 7, 8, 9, 0]
]);
return (
XLSX.utils.sheet_to_html(ws)
);
}
```
## Array Output
```js
var arr = XLSX.utils.sheet_to_json(ws, opts);
var aoa = XLSX.utils.sheet_to_json(ws, {header: 1, ...other_opts});
```
:::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 |
| :---------- | :------: | :-------------------------------------------------- |
|`raw` | `true` | Use raw values (true) or formatted strings (false) |
|`range` | ** | Override Range (see table below) |
|`header` | | Control output format (see table below) |
|`dateNF` | FMT 14 | Use specified date format in string output |
|`defval` | | Use specified value in place of null or undefined |
|`blankrows` | ** | Include blank lines in the output ** |
- `raw` only affects cells which have a format code (`.z`) field or a formatted
text (`.w`) field.
- If `header` is specified, the first row is considered a data row; if `header`
is not specified, the first row is the header row and not considered data.
- When `header` is not specified, the conversion will automatically disambiguate
header entries by affixing `_` and a count starting at `1`. For example, if
three columns have header `foo` the output fields are `foo`, `foo_1`, `foo_2`
- `null` values are returned when `raw` is true but are skipped when false.
- If `defval` is not specified, null and undefined values are skipped normally.
If specified, all null and undefined points will be filled with `defval`
- When `header` is `1`, the default is to generate blank rows. `blankrows` must
be set to `false` to skip blank rows.
- When `header` is not `1`, the default is to skip blank rows. `blankrows` must
be true to generate blank rows
`range` is expected to be one of:
| `range` | Description |
| :--------------- | :-------------------------------------------------------- |
| (number) | Use worksheet range but set starting row to the value |
| (string) | Use specified range (A1-Style bounded range string) |
| (default) | Use worksheet range (`ws['!ref']`) |
`header` is expected to be one of:
| `header` | Description |
| :--------------- | :-------------------------------------------------------- |
| `1` | Generate an array of arrays |
| `"A"` | Row object keys are literal column labels |
| array of strings | Use specified strings as keys in row objects |
| (default) | Read and disambiguate first row as keys |
- If header is not `1`, the row object will contain the non-enumerable property
`__rowNum__` that represents the row of the sheet corresponding to the entry.
- If header is an array, the keys will not be disambiguated. This can lead to
unexpected results if the array values are not unique!
For the example worksheet:
```jsx live
function SheetJSToJSON() {
/* original data */
var ws = XLSX.utils.aoa_to_sheet([
["S", "h", "e", "e", "t", "J", "S"],
[ 1, 2, , , 5, 6, 7],
[ 2, 3, , , 6, 7, 8],
[ 3, 4, , , 7, 8, 9],
[ 4, 5, 6, 7, 8, 9, 0]
]);
/* display JS objects with some whitespace */
const aoo = o => o.map(r => " " + JSON.stringify(r).replace(/,"/g, ', "').replace(/:/g, ": ").replace(/"([A-Za-z_]\w*)":/g, '$1:')).join("\n");
const aoa = o => o.map(r => " " + JSON.stringify(r).replace(/,/g, ', ').replace(/null/g, "")).join("\n");
return (
Worksheet (as HTML)
XLSX.utils.sheet_to_json(ws, {'{'} header: 1 {'}'}) [array of arrays]
[
{aoa(XLSX.utils.sheet_to_json(ws, { header: 1 }))}
]
XLSX.utils.sheet_to_json(ws) [objects with header disambiguation]
[
{aoo(XLSX.utils.sheet_to_json(ws))}
]
XLSX.utils.sheet_to_json(ws, {'{'} header: "A" {'}'}) [column names as keys]
[
{aoo(XLSX.utils.sheet_to_json(ws, { header: "A" }))}
]
XLSX.utils.sheet_to_json(ws, {'{'} header: ["A","E","I","O","U","6","9"] {'}'})
[
{aoo(XLSX.utils.sheet_to_json(ws, { header: ["A","E","I","O","U","6","9"] }))}
]
);
}
```
## Formulae Output
[**This has been moved to a separate page**](/docs/api/utilities/formulae)