sheetjs/docbits/63_numfmt.md
SheetJS b9bc0a1627 version bump 0.9.13: string formatting
- dateNF parse option controls interpretation of code 14
- SSF updated to 0.9.1
- SYLK write formulae
- DIF support Excel-style data storage
- ODS/FODS automatic styles for date formatting

Issues:
- Fixes #181 h/t @CharlesNo
- Fixes #200 h/t @JohnJeong123
- Fixes #208 h/t @jerryhe88
- Fixes #262 h/t @JohnJeong123
- Fixes #269 h/t @calebeaires
- Fixes #326 h/t @railty
- Fixes #392 h/t @FourLeafClover
- Fixes #449 h/t @dougschiller
- Fixes #560 h/t @dpackage
2017-04-30 12:27:03 -04:00

2.9 KiB

Number Formats

The cell.w formatted text for each cell is produced from cell.v and cell.z format. If the format is not specified, the Excel General format is used. The format can either be specified as a string or as an index into the format table. Parsers are expected to populate workbook.SSF with the number format table. Writers are expected to serialize the table.

Custom tools should ensure that the local table has each used format string somewhere in the table. Excel convention mandates that the custom formats start at index 164. The following example creates a custom format from scratch:

var tbl = {};
XLSX.SSF.init_table(tbl); // <-- load builtin formats
tbl[164] = "\"T\"\ #0.00";
var wb = {
	SSF: tbl,
	SheetNames: ["Sheet1"],
	Sheets: {
		Sheet1: {
			"!ref":"A1:C1",
			A1: { t:"n", v:10000 },             // <-- General format
			B1: { t:"n", v:10000, z: tbl[4] },  // <-- Builtin format
			C1: { t:"n", v:10000, z: tbl[164] } // <-- Custom format
		}
	}
}

The rules are slightly different from how Excel displays custom number formats. In particular, literal characters must be wrapped in double quotes or preceded by a backslash. For more info, see the Excel documentation article Create or delete a custom number format or ECMA-376 18.8.31 (Number Formats)

The default formats are listed in ECMA-376 18.8.30:

ID Format
0 General
1 0
2 0.00
3 #,##0
4 #,##0.00
9 0%
10 0.00%
11 0.00E+00
12 # ?/?
13 # ??/??
14 m/d/yy (see below)
15 d-mmm-yy
16 d-mmm
17 mmm-yy
18 h:mm AM/PM
19 h:mm:ss AM/PM
20 h:mm
21 h:mm:ss
22 m/d/yy h:mm
37 #,##0 ;(#,##0)
38 #,##0 ;[Red](#,##0)
39 #,##0.00;(#,##0.00)
40 #,##0.00;[Red](#,##0.00)
45 mm:ss
46 [h]:mm:ss
47 mmss.0
48 ##0.0E+0
49 @

Format 14 (m/d/yy) is localized by Excel: even though the file specifies that number format, it will be drawn differently based on system settings. It makes sense when the producer and consumer of files are in the same locale, but that is not always the case over the Internet. To get around this ambiguity, parse functions accept the dateNF option to override the interpretation of that specific format string.