2017-03-20 09:02:25 +00:00
|
|
|
## Utility Functions
|
|
|
|
|
|
|
|
The `sheet_to_*` functions accept a worksheet and an optional options object.
|
|
|
|
|
2017-03-25 01:36:40 +00:00
|
|
|
The `*_to_sheet` functions accept a data object and an optional options object.
|
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
The examples are based on the following worksheet:
|
|
|
|
|
|
|
|
```
|
|
|
|
XXX| A | B | C | D | E | F | G |
|
|
|
|
---+---+---+---+---+---+---+---+
|
|
|
|
1 | S | h | e | e | t | J | S |
|
|
|
|
2 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
|
|
|
3 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|
|
|
|
```
|
|
|
|
|
2017-03-25 01:36:40 +00:00
|
|
|
### Array of Arrays Input
|
|
|
|
|
|
|
|
`XLSX.utils.aoa_to_sheet` takes an array of arrays of JS values and returns a
|
|
|
|
worksheet resembling the input data. Numbers, Booleans and Strings are stored
|
|
|
|
as the corresponding styles. Dates are stored as date or numbers. Array holes
|
|
|
|
and explicit `undefined` values are skipped. `null` values may be stubbed. All
|
|
|
|
other values are stored as strings. The function takes an options argument:
|
|
|
|
|
2021-09-30 07:28:03 +00:00
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :-----: | :--------------------------------------------------- |
|
|
|
|
|`dateNF` | FMT 14 | Use specified date format in string output |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
|
|
|
|`sheetStubs` | false | Create cell objects of type `z` for `null` values |
|
|
|
|
|`nullError` | false | If true, emit `#NULL!` error cells for `null` values |
|
2017-03-25 01:36:40 +00:00
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
2017-03-25 01:36:40 +00:00
|
|
|
To generate the example sheet:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ws = XLSX.utils.aoa_to_sheet([
|
2017-09-24 23:40:09 +00:00
|
|
|
"SheetJS".split(""),
|
|
|
|
[1,2,3,4,5,6,7],
|
|
|
|
[2,3,4,5,6,7,8]
|
2017-03-25 01:36:40 +00:00
|
|
|
]);
|
|
|
|
```
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-03-25 01:36:40 +00:00
|
|
|
|
2018-01-09 07:36:02 +00:00
|
|
|
`XLSX.utils.sheet_add_aoa` takes an array of arrays of JS values and updates an
|
|
|
|
existing worksheet object. It follows the same process as `aoa_to_sheet` and
|
|
|
|
accepts an options argument:
|
|
|
|
|
2021-09-30 07:28:03 +00:00
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :-----: | :--------------------------------------------------- |
|
|
|
|
|`dateNF` | FMT 14 | Use specified date format in string output |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
|
|
|
|`sheetStubs` | false | Create cell objects of type `z` for `null` values |
|
|
|
|
|`nullError` | false | If true, emit `#NULL!` error cells for `null` values |
|
|
|
|
|`origin` | | Use specified cell as starting point (see below) |
|
2018-01-09 07:36:02 +00:00
|
|
|
|
|
|
|
`origin` is expected to be one of:
|
|
|
|
|
|
|
|
| `origin` | Description |
|
|
|
|
| :--------------- | :-------------------------------------------------------- |
|
|
|
|
| (cell object) | Use specified cell (cell object) |
|
|
|
|
| (string) | Use specified cell (A1-style cell) |
|
|
|
|
| (number >= 0) | Start from the first column at specified row (0-indexed) |
|
|
|
|
| -1 | Append to bottom of worksheet starting on first column |
|
|
|
|
| (default) | Start from cell A1 |
|
|
|
|
|
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
|
|
|
|
|
|
|
Consider the worksheet:
|
|
|
|
|
|
|
|
```
|
|
|
|
XXX| A | B | C | D | E | F | G |
|
|
|
|
---+---+---+---+---+---+---+---+
|
|
|
|
1 | S | h | e | e | t | J | S |
|
|
|
|
2 | 1 | 2 | | | 5 | 6 | 7 |
|
|
|
|
3 | 2 | 3 | | | 6 | 7 | 8 |
|
|
|
|
4 | 3 | 4 | | | 7 | 8 | 9 |
|
|
|
|
5 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
|
|
|
|
```
|
|
|
|
|
|
|
|
This worksheet can be built up in the order `A1:G1, A2:B4, E2:G4, A5:G5`:
|
|
|
|
|
|
|
|
```js
|
|
|
|
/* Initial row */
|
|
|
|
var ws = XLSX.utils.aoa_to_sheet([ "SheetJS".split("") ]);
|
|
|
|
|
|
|
|
/* Write data starting at A2 */
|
|
|
|
XLSX.utils.sheet_add_aoa(ws, [[1,2], [2,3], [3,4]], {origin: "A2"});
|
|
|
|
|
|
|
|
/* Write data starting at E2 */
|
|
|
|
XLSX.utils.sheet_add_aoa(ws, [[5,6,7], [6,7,8], [7,8,9]], {origin:{r:1, c:4}});
|
|
|
|
|
|
|
|
/* Append row */
|
|
|
|
XLSX.utils.sheet_add_aoa(ws, [[4,5,6,7,8,9,0]], {origin: -1});
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
2017-05-11 18:23:21 +00:00
|
|
|
### Array of Objects Input
|
|
|
|
|
|
|
|
`XLSX.utils.json_to_sheet` takes an array of objects and returns a worksheet
|
2017-08-08 20:31:17 +00:00
|
|
|
with automatically-generated "headers" based on the keys of the objects. The
|
|
|
|
default column order is determined by the first appearance of the field using
|
2021-09-11 08:33:48 +00:00
|
|
|
`Object.keys`. The function accepts an options argument:
|
2017-08-08 20:31:17 +00:00
|
|
|
|
2021-09-11 08:18:49 +00:00
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :-----: | :--------------------------------------------------- |
|
|
|
|
|`header` | | Use specified field order (default `Object.keys`) ** |
|
|
|
|
|`dateNF` | FMT 14 | Use specified date format in string output |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
|
|
|
|`skipHeader` | false | If true, do not include header row in output |
|
2021-09-30 07:28:03 +00:00
|
|
|
|`nullError` | false | If true, emit `#NULL!` error cells for `null` values |
|
2021-09-11 08:18:49 +00:00
|
|
|
|
2021-09-11 08:33:48 +00:00
|
|
|
- All fields from each row will be written. If `header` is an array and it does
|
|
|
|
not contain a particular field, the key will be appended to the array.
|
|
|
|
- Cell types are deduced from the type of each value. For example, a `Date`
|
|
|
|
object will generate a Date cell, while a string will generate a Text cell.
|
2021-09-30 07:28:03 +00:00
|
|
|
- Null values will be skipped by default. If `nullError` is true, an error cell
|
|
|
|
corresponding to `#NULL!` will be written to the worksheet.
|
2017-05-11 18:23:21 +00:00
|
|
|
|
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-05-11 18:23:21 +00:00
|
|
|
|
2019-11-15 01:46:49 +00:00
|
|
|
The original sheet cannot be reproduced using plain objects since JS object keys
|
2018-01-09 07:36:02 +00:00
|
|
|
must be unique. After replacing the second `e` and `S` with `e_1` and `S_1`:
|
2017-05-11 18:23:21 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
var ws = XLSX.utils.json_to_sheet([
|
2018-01-09 07:36:02 +00:00
|
|
|
{ S:1, h:2, e:3, e_1:4, t:5, J:6, S_1:7 },
|
|
|
|
{ S:2, h:3, e:4, e_1:5, t:6, J:7, S_1:8 }
|
2017-08-08 20:31:17 +00:00
|
|
|
], {header:["S","h","e","e_1","t","J","S_1"]});
|
2017-05-11 18:23:21 +00:00
|
|
|
```
|
2018-01-09 07:36:02 +00:00
|
|
|
|
|
|
|
Alternatively, the header row can be skipped:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ws = XLSX.utils.json_to_sheet([
|
|
|
|
{ A:"S", B:"h", C:"e", D:"e", E:"t", F:"J", G:"S" },
|
|
|
|
{ A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7 },
|
|
|
|
{ A: 2, B: 3, C: 4, D: 5, E: 6, F: 7, G: 8 }
|
|
|
|
], {header:["A","B","C","D","E","F","G"], skipHeader:true});
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
`XLSX.utils.sheet_add_json` takes an array of objects and updates an existing
|
|
|
|
worksheet object. It follows the same process as `json_to_sheet` and accepts
|
|
|
|
an options argument:
|
|
|
|
|
2021-09-30 07:28:03 +00:00
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :-----: | :--------------------------------------------------- |
|
|
|
|
|`header` | | Use specified column order (default `Object.keys`) |
|
|
|
|
|`dateNF` | FMT 14 | Use specified date format in string output |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
|
|
|
|`skipHeader` | false | If true, do not include header row in output |
|
|
|
|
|`nullError` | false | If true, emit `#NULL!` error cells for `null` values |
|
|
|
|
|`origin` | | Use specified cell as starting point (see below) |
|
2018-01-09 07:36:02 +00:00
|
|
|
|
|
|
|
`origin` is expected to be one of:
|
|
|
|
|
|
|
|
| `origin` | Description |
|
|
|
|
| :--------------- | :-------------------------------------------------------- |
|
|
|
|
| (cell object) | Use specified cell (cell object) |
|
|
|
|
| (string) | Use specified cell (A1-style cell) |
|
|
|
|
| (number >= 0) | Start from the first column at specified row (0-indexed) |
|
|
|
|
| -1 | Append to bottom of worksheet starting on first column |
|
|
|
|
| (default) | Start from cell A1 |
|
|
|
|
|
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
|
|
|
|
|
|
|
Consider the worksheet:
|
|
|
|
|
|
|
|
```
|
|
|
|
XXX| A | B | C | D | E | F | G |
|
|
|
|
---+---+---+---+---+---+---+---+
|
|
|
|
1 | S | h | e | e | t | J | S |
|
|
|
|
2 | 1 | 2 | | | 5 | 6 | 7 |
|
|
|
|
3 | 2 | 3 | | | 6 | 7 | 8 |
|
|
|
|
4 | 3 | 4 | | | 7 | 8 | 9 |
|
|
|
|
5 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
|
|
|
|
```
|
|
|
|
|
|
|
|
This worksheet can be built up in the order `A1:G1, A2:B4, E2:G4, A5:G5`:
|
|
|
|
|
|
|
|
```js
|
|
|
|
/* Initial row */
|
|
|
|
var ws = XLSX.utils.json_to_sheet([
|
|
|
|
{ A: "S", B: "h", C: "e", D: "e", E: "t", F: "J", G: "S" }
|
|
|
|
], {header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true});
|
|
|
|
|
|
|
|
/* Write data starting at A2 */
|
|
|
|
XLSX.utils.sheet_add_json(ws, [
|
|
|
|
{ A: 1, B: 2 }, { A: 2, B: 3 }, { A: 3, B: 4 }
|
|
|
|
], {skipHeader: true, origin: "A2"});
|
|
|
|
|
|
|
|
/* Write data starting at E2 */
|
|
|
|
XLSX.utils.sheet_add_json(ws, [
|
|
|
|
{ A: 5, B: 6, C: 7 }, { A: 6, B: 7, C: 8 }, { A: 7, B: 8, C: 9 }
|
|
|
|
], {skipHeader: true, origin: { r: 1, c: 4 }, header: [ "A", "B", "C" ]});
|
|
|
|
|
|
|
|
/* Append row */
|
|
|
|
XLSX.utils.sheet_add_json(ws, [
|
|
|
|
{ A: 4, B: 5, C: 6, D: 7, E: 8, F: 9, G: 0 }
|
|
|
|
], {header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true, origin: -1});
|
|
|
|
```
|
|
|
|
|
2017-05-11 18:23:21 +00:00
|
|
|
</details>
|
|
|
|
|
2017-03-29 19:14:15 +00:00
|
|
|
### HTML Table Input
|
|
|
|
|
|
|
|
`XLSX.utils.table_to_sheet` takes a table DOM element and returns a worksheet
|
|
|
|
resembling the input table. Numbers are parsed. All other data will be stored
|
|
|
|
as strings.
|
|
|
|
|
|
|
|
`XLSX.utils.table_to_book` produces a minimal workbook based on the worksheet.
|
|
|
|
|
2017-08-09 22:38:23 +00:00
|
|
|
Both functions accept options arguments:
|
|
|
|
|
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :------: | :-------------------------------------------------- |
|
2018-04-06 06:39:48 +00:00
|
|
|
|`raw` | | If true, every cell will hold raw strings |
|
2017-09-24 23:40:09 +00:00
|
|
|
|`dateNF` | FMT 14 | Use specified date format in string output |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
2018-04-06 06:39:48 +00:00
|
|
|
|`sheetRows` | 0 | If >0, read the first `sheetRows` rows of the table |
|
2018-05-27 07:27:47 +00:00
|
|
|
|`display` | false | If true, hidden rows and cells will not be parsed |
|
2017-08-09 22:38:23 +00:00
|
|
|
|
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
2017-03-29 19:14:15 +00:00
|
|
|
To generate the example sheet, start with the HTML table:
|
|
|
|
|
|
|
|
```html
|
|
|
|
<table id="sheetjs">
|
|
|
|
<tr><td>S</td><td>h</td><td>e</td><td>e</td><td>t</td><td>J</td><td>S</td></tr>
|
|
|
|
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td></tr>
|
|
|
|
<tr><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>
|
|
|
|
</table>
|
|
|
|
```
|
|
|
|
|
|
|
|
To process the table:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var tbl = document.getElementById('sheetjs');
|
|
|
|
var wb = XLSX.utils.table_to_book(tbl);
|
|
|
|
```
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-03-29 19:14:15 +00:00
|
|
|
|
|
|
|
Note: `XLSX.read` can handle HTML represented as strings.
|
|
|
|
|
2020-07-31 03:38:44 +00:00
|
|
|
|
|
|
|
`XLSX.utils.sheet_add_dom` takes a table DOM element and updates an existing
|
|
|
|
worksheet object. It follows the same process as `table_to_sheet` and accepts
|
|
|
|
an options argument:
|
|
|
|
|
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :------: | :-------------------------------------------------- |
|
|
|
|
|`raw` | | If true, every cell will hold raw strings |
|
|
|
|
|`dateNF` | FMT 14 | Use specified date format in string output |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
|
|
|
|`sheetRows` | 0 | If >0, read the first `sheetRows` rows of the table |
|
|
|
|
|`display` | false | If true, hidden rows and cells will not be parsed |
|
|
|
|
|
|
|
|
`origin` is expected to be one of:
|
|
|
|
|
|
|
|
| `origin` | Description |
|
|
|
|
| :--------------- | :-------------------------------------------------------- |
|
|
|
|
| (cell object) | Use specified cell (cell object) |
|
|
|
|
| (string) | Use specified cell (A1-style cell) |
|
|
|
|
| (number >= 0) | Start from the first column at specified row (0-indexed) |
|
|
|
|
| -1 | Append to bottom of worksheet starting on first column |
|
|
|
|
| (default) | Start from cell A1 |
|
|
|
|
|
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
|
|
|
|
|
|
|
A small helper function can create gap rows between tables:
|
|
|
|
|
|
|
|
```js
|
|
|
|
function create_gap_rows(ws, nrows) {
|
|
|
|
var ref = XLSX.utils.decode_range(ws["!ref"]); // get original range
|
|
|
|
ref.e.r += nrows; // add to ending row
|
|
|
|
ws["!ref"] = XLSX.utils.encode_range(ref); // reassign row
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first table */
|
|
|
|
var ws = XLSX.utils.table_to_sheet(document.getElementById('table1'));
|
|
|
|
create_gap_rows(ws, 1); // one row gap after first table
|
|
|
|
|
|
|
|
/* second table */
|
|
|
|
XLSX.utils.sheet_add_dom(ws, document.getElementById('table2'), {origin: -1});
|
|
|
|
create_gap_rows(ws, 3); // three rows gap after second table
|
|
|
|
|
|
|
|
/* third table */
|
|
|
|
XLSX.utils.sheet_add_dom(ws, document.getElementById('table3'), {origin: -1});
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
### Formulae Output
|
|
|
|
|
|
|
|
`XLSX.utils.sheet_to_formulae` generates an array of commands that represent
|
|
|
|
how a person would enter data into an application. Each entry is of the form
|
|
|
|
`A1-cell-address=formula-or-value`. String literals are prefixed with a `'` in
|
2017-04-30 20:37:53 +00:00
|
|
|
accordance with Excel.
|
|
|
|
|
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
|
|
|
For the example sheet:
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
> var o = XLSX.utils.sheet_to_formulae(ws);
|
2018-02-28 10:41:49 +00:00
|
|
|
> [o[0], o[5], o[10], o[15], o[20]];
|
2017-03-20 09:02:25 +00:00
|
|
|
[ 'A1=\'S', 'F1=\'J', 'D2=4', 'B3=3', 'G3=8' ]
|
|
|
|
```
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-03-20 09:02:25 +00:00
|
|
|
|
2017-04-03 00:16:03 +00:00
|
|
|
### Delimiter-Separated Output
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
As an alternative to the `writeFile` CSV type, `XLSX.utils.sheet_to_csv` also
|
|
|
|
produces CSV output. The function takes an options argument:
|
|
|
|
|
2020-06-29 08:07:23 +00:00
|
|
|
| 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 |
|
2017-03-22 07:50:11 +00:00
|
|
|
|
|
|
|
- `strip` will remove trailing commas from each line under default `FS/RS`
|
2017-09-24 23:40:09 +00:00
|
|
|
- `blankrows` must be set to `false` to skip blank lines.
|
2020-06-29 08:07:23 +00:00
|
|
|
- Fields containing the record or field separator will automatically be wrapped
|
|
|
|
in double quotes; `forceQuotes` forces all cells to be wrapped in quotes.
|
2022-03-20 01:54:41 +00:00
|
|
|
- `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.
|
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
For the example sheet:
|
|
|
|
|
|
|
|
```js
|
|
|
|
> console.log(XLSX.utils.sheet_to_csv(ws));
|
|
|
|
S,h,e,e,t,J,S
|
|
|
|
1,2,3,4,5,6,7
|
|
|
|
2,3,4,5,6,7,8
|
|
|
|
> console.log(XLSX.utils.sheet_to_csv(ws, {FS:"\t"}));
|
|
|
|
S h e e t J S
|
|
|
|
1 2 3 4 5 6 7
|
|
|
|
2 3 4 5 6 7 8
|
2017-04-13 17:05:42 +00:00
|
|
|
> console.log(XLSX.utils.sheet_to_csv(ws,{FS:":",RS:"|"}));
|
2017-03-20 09:02:25 +00:00
|
|
|
S:h:e:e:t:J:S|1:2:3:4:5:6:7|2:3:4:5:6:7:8|
|
|
|
|
```
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-03-20 09:02:25 +00:00
|
|
|
|
2017-04-03 00:16:03 +00:00
|
|
|
#### UTF-16 Unicode Text
|
|
|
|
|
|
|
|
The `txt` output type uses the tab character as the field separator. If the
|
2017-09-24 23:40:09 +00:00
|
|
|
`codepage` library is available (included in full distribution but not core),
|
|
|
|
the output will be encoded in `CP1200` and the BOM will be prepended.
|
2017-04-03 00:16:03 +00:00
|
|
|
|
2017-12-01 05:48:10 +00:00
|
|
|
`XLSX.utils.sheet_to_txt` takes the same arguments as `sheet_to_csv`.
|
|
|
|
|
2017-05-16 17:45:35 +00:00
|
|
|
### HTML Output
|
|
|
|
|
|
|
|
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 |
|
|
|
|
| :---------- | :------: | :-------------------------------------------------- |
|
2018-02-03 20:46:32 +00:00
|
|
|
|`id` | | Specify the `id` attribute for the `TABLE` element |
|
2017-09-24 23:40:09 +00:00
|
|
|
|`editable` | false | If true, set `contenteditable="true"` for every TD |
|
|
|
|
|`header` | | Override header (default `html body`) |
|
|
|
|
|`footer` | | Override footer (default `/body /html`) |
|
2017-05-16 17:45:35 +00:00
|
|
|
|
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-05-16 17:45:35 +00:00
|
|
|
|
|
|
|
For the example sheet:
|
|
|
|
|
|
|
|
```js
|
|
|
|
> console.log(XLSX.utils.sheet_to_html(ws));
|
|
|
|
// ...
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
### JSON
|
|
|
|
|
2017-05-09 18:07:57 +00:00
|
|
|
`XLSX.utils.sheet_to_json` generates different types of JS objects. The function
|
|
|
|
takes an options argument:
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | :------: | :-------------------------------------------------- |
|
2018-09-06 07:55:28 +00:00
|
|
|
|`raw` | `true` | Use raw values (true) or formatted strings (false) |
|
2017-09-24 23:40:09 +00:00
|
|
|
|`range` | from WS | 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 ** |
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
- `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`
|
2017-03-23 05:54:27 +00:00
|
|
|
- `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`
|
2017-03-25 01:36:40 +00:00
|
|
|
- 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
|
2017-09-24 23:40:09 +00:00
|
|
|
be true to generate blank rows
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
`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 |
|
|
|
|
| :--------------- | :-------------------------------------------------------- |
|
2017-04-13 17:05:42 +00:00
|
|
|
| `1` | Generate an array of arrays ("2D Array") |
|
2017-03-20 09:02:25 +00:00
|
|
|
| `"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 |
|
|
|
|
|
2022-03-09 01:44:10 +00:00
|
|
|
- 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!
|
|
|
|
|
2017-03-25 01:36:40 +00:00
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Examples</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
For the example sheet:
|
|
|
|
|
|
|
|
```js
|
2017-09-24 23:40:09 +00:00
|
|
|
> XLSX.utils.sheet_to_json(ws);
|
2017-03-20 09:02:25 +00:00
|
|
|
[ { S: 1, h: 2, e: 3, e_1: 4, t: 5, J: 6, S_1: 7 },
|
|
|
|
{ S: 2, h: 3, e: 4, e_1: 5, t: 6, J: 7, S_1: 8 } ]
|
|
|
|
|
2017-09-24 23:40:09 +00:00
|
|
|
> XLSX.utils.sheet_to_json(ws, {header:"A"});
|
2017-03-20 09:02:25 +00:00
|
|
|
[ { A: 'S', B: 'h', C: 'e', D: 'e', E: 't', F: 'J', G: 'S' },
|
2017-04-13 17:05:42 +00:00
|
|
|
{ A: '1', B: '2', C: '3', D: '4', E: '5', F: '6', G: '7' },
|
|
|
|
{ A: '2', B: '3', C: '4', D: '5', E: '6', F: '7', G: '8' } ]
|
2017-09-24 23:40:09 +00:00
|
|
|
|
|
|
|
> XLSX.utils.sheet_to_json(ws, {header:["A","E","I","O","U","6","9"]});
|
2017-03-20 09:02:25 +00:00
|
|
|
[ { '6': 'J', '9': 'S', A: 'S', E: 'h', I: 'e', O: 'e', U: 't' },
|
2017-04-13 17:05:42 +00:00
|
|
|
{ '6': '6', '9': '7', A: '1', E: '2', I: '3', O: '4', U: '5' },
|
|
|
|
{ '6': '7', '9': '8', A: '2', E: '3', I: '4', O: '5', U: '6' } ]
|
2017-09-24 23:40:09 +00:00
|
|
|
|
|
|
|
> XLSX.utils.sheet_to_json(ws, {header:1});
|
|
|
|
[ [ 'S', 'h', 'e', 'e', 't', 'J', 'S' ],
|
|
|
|
[ '1', '2', '3', '4', '5', '6', '7' ],
|
|
|
|
[ '2', '3', '4', '5', '6', '7', '8' ] ]
|
2017-03-20 09:02:25 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Example showing the effect of `raw`:
|
|
|
|
|
|
|
|
```js
|
2017-04-13 17:05:42 +00:00
|
|
|
> ws['A2'].w = "3"; // set A2 formatted string value
|
2017-09-24 23:40:09 +00:00
|
|
|
|
2018-09-06 07:55:28 +00:00
|
|
|
> XLSX.utils.sheet_to_json(ws, {header:1, raw:false});
|
2017-03-20 09:02:25 +00:00
|
|
|
[ [ 'S', 'h', 'e', 'e', 't', 'J', 'S' ],
|
2017-04-13 17:05:42 +00:00
|
|
|
[ '3', '2', '3', '4', '5', '6', '7' ], // <-- A2 uses the formatted string
|
|
|
|
[ '2', '3', '4', '5', '6', '7', '8' ] ]
|
2017-09-24 23:40:09 +00:00
|
|
|
|
2018-09-06 07:55:28 +00:00
|
|
|
> XLSX.utils.sheet_to_json(ws, {header:1});
|
2017-03-20 09:02:25 +00:00
|
|
|
[ [ 'S', 'h', 'e', 'e', 't', 'J', 'S' ],
|
|
|
|
[ 1, 2, 3, 4, 5, 6, 7 ], // <-- A2 uses the raw value
|
|
|
|
[ 2, 3, 4, 5, 6, 7, 8 ] ]
|
|
|
|
```
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-03-20 09:02:25 +00:00
|
|
|
|