2017-03-20 09:02:25 +00:00
|
|
|
## Parsing Options
|
|
|
|
|
|
|
|
The exported `read` and `readFile` functions accept an options argument:
|
|
|
|
|
|
|
|
| Option Name | Default | Description |
|
|
|
|
| :---------- | ------: | :--------------------------------------------------- |
|
2017-09-24 23:40:09 +00:00
|
|
|
|`type` | | Input data encoding (see Input Type below) |
|
|
|
|
|`raw` | false | If true, plain text parsing will not parse values ** |
|
2017-12-09 07:17:25 +00:00
|
|
|
|`codepage` | | If specified, use code page when appropriate ** |
|
2017-09-24 23:40:09 +00:00
|
|
|
|`cellFormula`| true | Save formulae to the .f field |
|
|
|
|
|`cellHTML` | true | Parse rich text and save HTML to the `.h` field |
|
|
|
|
|`cellNF` | false | Save number format string to the `.z` field |
|
|
|
|
|`cellStyles` | false | Save style/theme info to the `.s` field |
|
|
|
|
|`cellText` | true | Generated formatted text to the `.w` field |
|
|
|
|
|`cellDates` | false | Store dates as type `d` (default is `n`) |
|
|
|
|
|`dateNF` | | If specified, use the string for date code 14 ** |
|
|
|
|
|`sheetStubs` | false | Create cell objects of type `z` for stub cells |
|
|
|
|
|`sheetRows` | 0 | If >0, read the first `sheetRows` rows ** |
|
|
|
|
|`bookDeps` | false | If true, parse calculation chains |
|
|
|
|
|`bookFiles` | false | If true, add raw files to book object ** |
|
|
|
|
|`bookProps` | false | If true, only parse enough to get book metadata ** |
|
|
|
|
|`bookSheets` | false | If true, only parse enough to get the sheet names |
|
|
|
|
|`bookVBA` | false | If true, copy VBA blob to `vbaraw` field ** |
|
|
|
|
|`password` | "" | If defined and file is encrypted, use password ** |
|
|
|
|
|`WTF` | false | If true, throw errors on unexpected file features ** |
|
2020-01-28 01:20:38 +00:00
|
|
|
|`sheets` | | If specified, only parse specified sheets ** |
|
2020-06-05 05:42:28 +00:00
|
|
|
|`PRN` | false | If true, allow parsing of PRN files ** |
|
2020-08-12 21:58:02 +00:00
|
|
|
|`xlfn` | false | If true, preserve `_xlfn.` prefixes in formulae ** |
|
2021-11-09 00:31:56 +00:00
|
|
|
|`FS` | | DSV Field Separator override |
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
- Even if `cellNF` is false, formatted text will be generated and saved to `.w`
|
|
|
|
- In some cases, sheets may be parsed even if `bookSheets` is false.
|
2017-09-24 23:40:09 +00:00
|
|
|
- Excel aggressively tries to interpret values from CSV and other plain text.
|
2017-07-26 08:35:28 +00:00
|
|
|
This leads to surprising behavior! The `raw` option suppresses value parsing.
|
2017-03-20 09:02:25 +00:00
|
|
|
- `bookSheets` and `bookProps` combine to give both sets of information
|
2017-09-24 23:40:09 +00:00
|
|
|
- `Deps` will be an empty object if `bookDeps` is false
|
2017-03-20 09:02:25 +00:00
|
|
|
- `bookFiles` behavior depends on file type:
|
|
|
|
* `keys` array (paths in the ZIP) for ZIP-based formats
|
|
|
|
* `files` hash (mapping paths to objects representing the files) for ZIP
|
|
|
|
* `cfb` object for formats using CFB containers
|
|
|
|
- `sheetRows-1` rows will be generated when looking at the JSON object output
|
|
|
|
(since the header row is counted as a row when parsing the data)
|
2020-01-28 01:20:38 +00:00
|
|
|
- By default all worksheets are parsed. `sheets` restricts based on input type:
|
|
|
|
* number: zero-based index of worksheet to parse (`0` is first worksheet)
|
|
|
|
* string: name of worksheet to parse (case insensitive)
|
|
|
|
* array of numbers and strings to select multiple worksheets.
|
2017-09-24 23:40:09 +00:00
|
|
|
- `bookVBA` merely exposes the raw VBA CFB object. It does not parse the data.
|
2017-10-02 08:15:36 +00:00
|
|
|
XLSM and XLSB store the VBA CFB object in `xl/vbaProject.bin`. BIFF8 XLS mixes
|
|
|
|
the VBA entries alongside the core Workbook entry, so the library generates a
|
|
|
|
new XLSB-compatible blob from the XLS CFB container.
|
2017-12-09 07:17:25 +00:00
|
|
|
- `codepage` is applied to BIFF2 - BIFF5 files without `CodePage` records and to
|
|
|
|
CSV files without BOM in `type:"binary"`. BIFF8 XLS always defaults to 1200.
|
2020-06-05 05:42:28 +00:00
|
|
|
- `PRN` affects parsing of text files without a common delimiter character.
|
2017-03-20 09:02:25 +00:00
|
|
|
- Currently only XOR encryption is supported. Unsupported error will be thrown
|
|
|
|
for files employing other encryption methods.
|
2020-08-12 21:58:02 +00:00
|
|
|
- Newer Excel functions are serialized with the `_xlfn.` prefix, hidden from the
|
|
|
|
user. SheetJS will strip `_xlfn.` normally. The `xlfn` option preserves them.
|
2017-03-20 09:02:25 +00:00
|
|
|
- WTF is mainly for development. By default, the parser will suppress read
|
|
|
|
errors on single worksheets, allowing you to read from the worksheets that do
|
2020-06-20 01:24:29 +00:00
|
|
|
parse properly. Setting `WTF:true` forces those errors to be thrown.
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
### Input Type
|
|
|
|
|
|
|
|
Strings can be interpreted in multiple ways. The `type` parameter for `read`
|
|
|
|
tells the library how to parse the data argument:
|
|
|
|
|
|
|
|
| `type` | expected input |
|
|
|
|
|------------|-----------------------------------------------------------------|
|
2017-09-24 23:40:09 +00:00
|
|
|
| `"base64"` | string: Base64 encoding of the file |
|
|
|
|
| `"binary"` | string: binary string (byte `n` is `data.charCodeAt(n)`) |
|
2017-09-30 06:18:11 +00:00
|
|
|
| `"string"` | string: JS string (characters interpreted as UTF8) |
|
2017-03-20 09:02:25 +00:00
|
|
|
| `"buffer"` | nodejs Buffer |
|
2017-09-24 23:40:09 +00:00
|
|
|
| `"array"` | array: array of 8-bit unsigned int (byte `n` is `data[n]`) |
|
|
|
|
| `"file"` | string: path of file that will be read (nodejs only) |
|
2017-03-20 09:02:25 +00:00
|
|
|
|
|
|
|
### Guessing File Type
|
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Implementation Details</b> (click to show)</summary>
|
2017-04-30 20:37:53 +00:00
|
|
|
|
2017-03-20 09:02:25 +00:00
|
|
|
Excel and other spreadsheet tools read the first few bytes and apply other
|
|
|
|
heuristics to determine a file type. This enables file type punning: renaming
|
|
|
|
files with the `.xls` extension will tell your computer to use Excel to open the
|
|
|
|
file but Excel will know how to handle it. This library applies similar logic:
|
|
|
|
|
|
|
|
| Byte 0 | Raw File Type | Spreadsheet Types |
|
|
|
|
|:-------|:--------------|:----------------------------------------------------|
|
2021-12-29 09:16:02 +00:00
|
|
|
| `0xD0` | CFB Container | BIFF 5/8 or protected XLSX/XLSB or WQ3/QPW or XLR |
|
2017-03-20 09:02:25 +00:00
|
|
|
| `0x09` | BIFF Stream | BIFF 2/3/4/5 |
|
2017-09-24 23:40:09 +00:00
|
|
|
| `0x3C` | XML/HTML | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
|
|
|
|
| `0x50` | ZIP Archive | XLSB or XLSX/M or ODS or UOS2 or plain text |
|
|
|
|
| `0x49` | Plain Text | SYLK or plain text |
|
|
|
|
| `0x54` | Plain Text | DIF or plain text |
|
|
|
|
| `0xEF` | UTF8 Encoded | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
|
|
|
|
| `0xFF` | UTF16 Encoded | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
|
|
|
|
| `0x00` | Record Stream | Lotus WK\* or Quattro Pro or plain text |
|
|
|
|
| `0x7B` | Plain text | RTF or plain text |
|
|
|
|
| `0x0A` | Plain text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
|
|
|
|
| `0x0D` | Plain text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
|
|
|
|
| `0x20` | Plain text | SpreadsheetML / Flat ODS / UOS1 / HTML / plain text |
|
2017-03-28 04:41:01 +00:00
|
|
|
|
|
|
|
DBF files are detected based on the first byte as well as the third and fourth
|
|
|
|
bytes (corresponding to month and day of the file date)
|
2017-03-20 09:02:25 +00:00
|
|
|
|
2021-12-29 09:16:02 +00:00
|
|
|
Works for Windows files are detected based on the BOF record with type `0xFF`
|
|
|
|
|
2017-09-24 23:40:09 +00:00
|
|
|
Plain text format guessing follows the priority order:
|
2017-04-03 00:16:03 +00:00
|
|
|
|
|
|
|
| Format | Test |
|
|
|
|
|:-------|:--------------------------------------------------------------------|
|
2017-07-26 08:35:28 +00:00
|
|
|
| XML | `<?xml` appears in the first 1024 characters |
|
|
|
|
| HTML | starts with `<` and HTML tags appear in the first 1024 characters * |
|
2021-09-30 08:46:35 +00:00
|
|
|
| XML | starts with `<` and the first tag is valid |
|
2017-07-26 08:35:28 +00:00
|
|
|
| RTF | starts with `{\rt` |
|
2017-04-03 06:02:02 +00:00
|
|
|
| DSV | starts with `/sep=.$/`, separator is the specified character |
|
2021-09-30 08:46:35 +00:00
|
|
|
| DSV | more unquoted `|` chars than `;` `\t` `,` in the first 1024 |
|
|
|
|
| DSV | more unquoted `;` chars than `\t` or `,` in the first 1024 |
|
|
|
|
| TSV | more unquoted `\t` chars than `,` chars in the first 1024 |
|
2017-10-17 00:14:32 +00:00
|
|
|
| CSV | one of the first 1024 characters is a comma `","` |
|
2017-12-04 04:41:41 +00:00
|
|
|
| ETH | starts with `socialcalc:version:` |
|
2021-09-30 08:46:35 +00:00
|
|
|
| PRN | `PRN` option is set to true |
|
|
|
|
| CSV | (fallback) |
|
2017-06-08 06:19:11 +00:00
|
|
|
|
2017-07-26 08:35:28 +00:00
|
|
|
- HTML tags include: `html`, `table`, `head`, `meta`, `script`, `style`, `div`
|
|
|
|
|
2017-04-30 20:37:53 +00:00
|
|
|
</details>
|
2017-04-03 00:16:03 +00:00
|
|
|
|
2017-05-16 17:45:35 +00:00
|
|
|
<details>
|
2017-09-24 23:40:09 +00:00
|
|
|
<summary><b>Why are random text files valid?</b> (click to show)</summary>
|
2017-05-16 17:45:35 +00:00
|
|
|
|
|
|
|
Excel is extremely aggressive in reading files. Adding an XLS extension to any
|
|
|
|
display text file (where the only characters are ANSI display chars) tricks
|
|
|
|
Excel into thinking that the file is potentially a CSV or TSV file, even if it
|
|
|
|
is only one column! This library attempts to replicate that behavior.
|
|
|
|
|
|
|
|
The best approach is to validate the desired worksheet and ensure it has the
|
|
|
|
expected number of rows or columns. Extracting the range is extremely simple:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var range = XLSX.utils.decode_range(worksheet['!ref']);
|
2018-03-06 00:34:04 +00:00
|
|
|
var ncols = range.e.c - range.s.c + 1, nrows = range.e.r - range.s.r + 1;
|
2017-05-16 17:45:35 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|