Cell value inference
inferCellType, escape helpers, error code constants.
Escape
src/utils/escape.tsescapeCellString function
src/utils/escape.ts:47Escape a string for safe storage in an OOXML cell. An underscore that opens
an _xHHHH_ sequence becomes _x005F_; illegal codepoints are replaced
with their _xHHHH_ representation. unescapeCellString recovers the
input for any string.
function escapeCellString(s: string): stringParameters
| Name | Type | Description |
|---|---|---|
s | string |
Returns
string
unescapeCellString function
src/utils/escape.ts:62Inverse of escapeCellString. Looking from left to right
we replace any _xHHHH_ sequence with the corresponding code unit;
the protected _x005F_ becomes a literal underscore which the
subsequent replacements skip safely (replace's regex is non-overlapping).
function unescapeCellString(s: string): stringParameters
| Name | Type | Description |
|---|---|---|
s | string |
Returns
string
Inference
src/utils/inference.tsinferCellType function
src/utils/inference.ts:67Infer the cell t attribute for a runtime value.
- boolean → 'b'
- number → 'n' (incl. integer numerics; date inference is left to
the caller because Excel decides on type via the cell's number
format, not the raw value)
- Date → 'd'
- string starting with = → 'f' (formula), except a lone '=', which is 's'
- string in ERROR_CODES → 'e'
- any other string → 's'
- null / undefined → 'n' (empty)
Throws nothing: 'n' is the no-information fallback. It reports the spelling
and nothing more, so 'f' is not a promise that the formula constructors will
accept the text: '==A1' classifies as 'f' and setFormula rejects it.
function inferCellType(value: unknown): CellDataTypeParameters
| Name | Type | Description |
|---|---|---|
value | unknown |
Returns
CellDataType
ERROR_CODES const
src/utils/inference.ts:28Excel error tokens this library knows by name. On a write, a string outside
this set is text, so the set is what inferCellType and makeErrorValue
accept as an error.
It is not a closed description of the format: Excel has added nine tokens
since 2018 and can add more, so a t="e" cell read out of a file keeps
whatever token it carries, listed here or not, as long as the token is
shaped like one (isExcelErrorToken in ./cell-error.js).
Adding a token here changes what a write does with the matching string:
inferCellType and bindValue turn it into an error value instead of
text, and makeErrorValue starts accepting it.
const ERROR_CODES: ReadonlySet<string>Css
src/utils/css.tscssRecordToInlineStyle function
src/utils/css.ts:25Serialize a CSS-property record to an inline-style declaration string
(prop1: val1; prop2: val2). Properties are alphabetised so the
output is deterministic across runs.
- Empty record returns ''.
- Empty-string values are skipped (treat as "unset").
- Values containing ; are dropped — they would terminate the
declaration early and risk attribute-injection in style="…"
contexts. Callers should pre-escape user data; this is a
defensive last line.
The returned string is suitable for direct interpolation into an
HTML style="…" attribute *after* the usual attribute-value HTML
escaping (no & / " injection here — this only guards against
stray semicolons).
function cssRecordToInlineStyle(record: Record<string, string> | undefined): stringParameters
| Name | Type | Description |
|---|---|---|
record | Record<string, string> | undefined |
Returns
string