2017-03-20 09:02:25 +00:00
|
|
|
## Working with the Workbook
|
|
|
|
|
|
|
|
The full object format is described later in this README.
|
|
|
|
|
2017-05-24 22:52:35 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Reading a specific cell </b> (click to show)</summary>
|
2017-05-24 22:52:35 +00:00
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
This example extracts the value stored in cell A1 from the first worksheet:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var first_sheet_name = workbook.SheetNames[0];
|
|
|
|
var address_of_cell = 'A1';
|
|
|
|
|
|
|
|
/* Get worksheet */
|
|
|
|
var worksheet = workbook.Sheets[first_sheet_name];
|
|
|
|
|
|
|
|
/* Find desired cell */
|
|
|
|
var desired_cell = worksheet[address_of_cell];
|
|
|
|
|
|
|
|
/* Get the value */
|
|
|
|
var desired_value = (desired_cell ? desired_cell.v : undefined);
|
|
|
|
```
|
|
|
|
|
2017-05-24 22:52:35 +00:00
|
|
|
</details>
|
|
|
|
|
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Adding a new worksheet to a workbook</b> (click to show)</summary>
|
2017-05-24 22:52:35 +00:00
|
|
|
|
|
|
|
This example uses [`XLSX.utils.aoa_to_sheet`](#array-of-arrays-input) to make a
|
2018-03-19 21:42:55 +00:00
|
|
|
sheet and `XLSX.utils.book_append_sheet` to append the sheet to the workbook:
|
2017-05-24 22:52:35 +00:00
|
|
|
|
|
|
|
```js
|
2019-07-29 10:14:10 +00:00
|
|
|
var ws_name = "SheetJS";
|
2017-05-24 22:52:35 +00:00
|
|
|
|
|
|
|
/* make worksheet */
|
|
|
|
var ws_data = [
|
2017-09-24 23:40:09 +00:00
|
|
|
[ "S", "h", "e", "e", "t", "J", "S" ],
|
|
|
|
[ 1 , 2 , 3 , 4 , 5 ]
|
2017-05-24 22:52:35 +00:00
|
|
|
];
|
|
|
|
var ws = XLSX.utils.aoa_to_sheet(ws_data);
|
|
|
|
|
2018-03-19 21:42:55 +00:00
|
|
|
/* Add the worksheet to the workbook */
|
|
|
|
XLSX.utils.book_append_sheet(wb, ws, ws_name);
|
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
2017-05-24 22:52:35 +00:00
|
|
|
|
2018-03-19 21:42:55 +00:00
|
|
|
<details>
|
|
|
|
<summary><b>Creating a new workbook from scratch</b> (click to show)</summary>
|
2017-05-24 22:52:35 +00:00
|
|
|
|
2018-03-19 21:42:55 +00:00
|
|
|
The workbook object contains a `SheetNames` array of names and a `Sheets` object
|
|
|
|
mapping sheet names to sheet objects. The `XLSX.utils.book_new` utility function
|
|
|
|
creates a new workbook object:
|
|
|
|
|
|
|
|
```js
|
|
|
|
/* create a new blank workbook */
|
|
|
|
var wb = XLSX.utils.book_new();
|
2017-05-24 22:52:35 +00:00
|
|
|
```
|
|
|
|
|
2018-03-19 21:42:55 +00:00
|
|
|
The new workbook is blank and contains no worksheets. The write functions will
|
|
|
|
error if the workbook is empty.
|
|
|
|
|
2017-05-24 22:52:35 +00:00
|
|
|
</details>
|
|
|
|
|
|
|
|
|
2017-07-05 22:27:54 +00:00
|
|
|
### Parsing and Writing Examples
|
|
|
|
|
|
|
|
- <http://sheetjs.com/demos/modify.html> read + modify + write files
|
2017-03-20 09:02:25 +00:00
|
|
|
|
2021-08-11 02:53:38 +00:00
|
|
|
- <https://github.com/SheetJS/sheetjs/blob/HEAD/bin/xlsx.njs> node
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
The node version installs a command line tool `xlsx` which can read spreadsheet
|
|
|
|
files and output the contents in various formats. The source is available at
|
|
|
|
`xlsx.njs` in the bin directory.
|
|
|
|
|
|
|
|
Some helper functions in `XLSX.utils` generate different views of the sheets:
|
|
|
|
|
|
|
|
- `XLSX.utils.sheet_to_csv` generates CSV
|
2017-12-01 05:48:10 +00:00
|
|
|
- `XLSX.utils.sheet_to_txt` generates UTF16 Formatted Text
|
2017-05-16 17:45:35 +00:00
|
|
|
- `XLSX.utils.sheet_to_html` generates HTML
|
2017-03-20 09:02:25 +00:00
|
|
|
- `XLSX.utils.sheet_to_json` generates an array of objects
|
|
|
|
- `XLSX.utils.sheet_to_formulae` generates a list of formulae
|
|
|
|
|