javascript-excel 2.0.0-alpha.2 → 2.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -21
- package/dist/address-DUn7-5OS.js +75 -0
- package/dist/address-DZBHchLv.cjs +122 -0
- package/dist/{auto-width-2oBzjeNG.d.cts → auto-width-DRHsPKNk.d.cts} +1 -1
- package/dist/cells-CPXU722t.cjs +33 -0
- package/dist/cells-CrMF8h_t.js +28 -0
- package/dist/colors-B8Nf_xTx.js +114 -0
- package/dist/colors-oBpjx97l.cjs +119 -0
- package/dist/csv.d.cts +4 -1
- package/dist/csv.d.ts +4 -1
- package/dist/dataset-KeMzHCGl.js +664 -0
- package/dist/dataset-UUZCzsi3.cjs +687 -0
- package/dist/dates-BXcSWf9_.js +64 -0
- package/dist/dates-CA1dCizp.cjs +75 -0
- package/dist/dom.cjs +5 -2
- package/dist/dom.d.cts +5 -2
- package/dist/dom.d.ts +4 -1
- package/dist/dom.js +4 -1
- package/dist/{numfmt-Dv6AjopS.cjs → dos-time-FZ7-1tot.cjs} +0 -199
- package/dist/{numfmt-BBgB7iQA.js → dos-time-dD0z1YWU.js} +2 -177
- package/dist/format.cjs +1264 -0
- package/dist/format.d.cts +59 -0
- package/dist/format.d.ts +59 -0
- package/dist/format.js +1262 -0
- package/dist/import.cjs +10 -0
- package/dist/import.d.cts +10 -0
- package/dist/import.d.ts +10 -0
- package/dist/import.js +1 -0
- package/dist/index.cjs +24 -607
- package/dist/index.d.cts +32 -6
- package/dist/index.d.ts +31 -5
- package/dist/index.js +10 -594
- package/dist/{warnings-C_12PT4t.d.cts → model-0JikLfuM.d.cts} +1 -41
- package/dist/{warnings-DXJKe1lx.d.ts → model-0JikLfuM.d.ts} +1 -41
- package/dist/numfmt-BmEbm78Y.cjs +125 -0
- package/dist/numfmt-DCDrX9P1.js +114 -0
- package/dist/pipeline-BAJtzBdW.js +1085 -0
- package/dist/pipeline-Bnf7EU3X.cjs +1132 -0
- package/dist/pipeline-CctEa86r.d.cts +186 -0
- package/dist/pipeline-I8SLmV9S.d.ts +186 -0
- package/dist/{read-Ym1voUct.cjs → read-CZEWV4rK.cjs} +181 -17
- package/dist/{read-DDN5ZsPe.js → read-LWti17lz.js} +169 -5
- package/dist/read.cjs +1 -1
- package/dist/read.d.cts +78 -4
- package/dist/read.d.ts +78 -4
- package/dist/read.js +1 -1
- package/dist/render.cjs +751 -0
- package/dist/render.d.cts +215 -0
- package/dist/render.d.ts +215 -0
- package/dist/render.js +732 -0
- package/dist/{sheet-names-uT2EH5Ui.js → sheet-names-7p-joVsG.js} +2 -101
- package/dist/{sheet-names-qpXsvMpM.cjs → sheet-names-B8U5RAW0.cjs} +14 -167
- package/dist/warnings-B1v58nCZ.d.cts +41 -0
- package/dist/warnings-B1v58nCZ.d.ts +41 -0
- package/dist/worker.cjs +10 -2
- package/dist/worker.d.cts +10 -4
- package/dist/worker.d.ts +10 -4
- package/dist/worker.js +10 -2
- package/dist/{write-CYpLGuzl.d.ts → write-DM6ieuQY.d.ts} +2 -1
- package/dist/{write-DubvcG-x.js → write-DjgWy1PK.js} +5 -2
- package/dist/{write-C5JgeIar.d.cts → write-DtjfdIFm.d.cts} +2 -1
- package/dist/{write-BOh9jfu1.cjs → write-f9kcYf5K.cjs} +31 -28
- package/package.json +32 -1
- package/dist/read-C6L7Nyy3.d.cts +0 -78
- package/dist/read-Ddkks7qC.d.ts +0 -78
package/dist/format.cjs
ADDED
|
@@ -0,0 +1,1264 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_cells = require("./cells-CPXU722t.cjs");
|
|
3
|
+
const require_dates = require("./dates-CA1dCizp.cjs");
|
|
4
|
+
const require_numfmt = require("./numfmt-BmEbm78Y.cjs");
|
|
5
|
+
// src/internal/format/parse.ts
|
|
6
|
+
const COLOR_NAMES = /* @__PURE__ */ new Set([
|
|
7
|
+
"black",
|
|
8
|
+
"blue",
|
|
9
|
+
"cyan",
|
|
10
|
+
"green",
|
|
11
|
+
"magenta",
|
|
12
|
+
"red",
|
|
13
|
+
"white",
|
|
14
|
+
"yellow"
|
|
15
|
+
]);
|
|
16
|
+
/** Splits a code at top-level semicolons (not inside quotes, brackets or after a backslash). */
|
|
17
|
+
function splitSections(code) {
|
|
18
|
+
const out = [];
|
|
19
|
+
let start = 0;
|
|
20
|
+
for (let i = 0; i < code.length; i++) {
|
|
21
|
+
const ch = code[i];
|
|
22
|
+
if (ch === "\\" || ch === "_" || ch === "*") i++;
|
|
23
|
+
else if (ch === "\"") {
|
|
24
|
+
const end = code.indexOf("\"", i + 1);
|
|
25
|
+
i = end === -1 ? code.length : end;
|
|
26
|
+
} else if (ch === "[") {
|
|
27
|
+
const end = code.indexOf("]", i + 1);
|
|
28
|
+
if (end !== -1) i = end;
|
|
29
|
+
} else if (ch === ";") {
|
|
30
|
+
out.push(code.slice(start, i));
|
|
31
|
+
start = i + 1;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
out.push(code.slice(start));
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
const COND = /^(<=|>=|<>|<|>|=)\s*(-?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)$/;
|
|
38
|
+
function bracket(sec, toks, inner) {
|
|
39
|
+
const lower = inner.toLowerCase();
|
|
40
|
+
if (COLOR_NAMES.has(lower)) {
|
|
41
|
+
sec.color = { name: lower };
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const colorN = /^color\s*(\d{1,2})$/.exec(lower);
|
|
45
|
+
if (colorN) {
|
|
46
|
+
const n = Number(colorN[1]);
|
|
47
|
+
if (n >= 1 && n <= 56) sec.color = { index: n };
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const cond = COND.exec(inner.trim());
|
|
51
|
+
if (cond) {
|
|
52
|
+
sec.cond = {
|
|
53
|
+
op: cond[1],
|
|
54
|
+
v: Number(cond[2])
|
|
55
|
+
};
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (/^(h+|m+|s+)$/.test(lower)) {
|
|
59
|
+
toks.push({
|
|
60
|
+
t: "elapsed",
|
|
61
|
+
k: lower[0],
|
|
62
|
+
n: lower.length
|
|
63
|
+
});
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (inner.startsWith("$")) {
|
|
67
|
+
const dash = inner.indexOf("-", 1);
|
|
68
|
+
const symbol = dash === -1 ? inner.slice(1) : inner.slice(1, dash);
|
|
69
|
+
const tag = dash === -1 ? "" : inner.slice(dash + 1).toLowerCase();
|
|
70
|
+
if (symbol) toks.push({
|
|
71
|
+
t: "lit",
|
|
72
|
+
s: symbol
|
|
73
|
+
});
|
|
74
|
+
if (tag === "x-sysdate") sec.system = "longDate";
|
|
75
|
+
else if (tag === "x-systime") sec.system = "time";
|
|
76
|
+
else if (/^[0-9a-f]{1,8}$/.test(tag)) {
|
|
77
|
+
const id = Number.parseInt(tag, 16) & 65535;
|
|
78
|
+
if (id === 63488) sec.system = "longDate";
|
|
79
|
+
else if (id === 62464) sec.system = "time";
|
|
80
|
+
else sec.lcid = id;
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function tokenize(src) {
|
|
86
|
+
const sec = {
|
|
87
|
+
toks: [],
|
|
88
|
+
kind: "literal"
|
|
89
|
+
};
|
|
90
|
+
const toks = sec.toks;
|
|
91
|
+
const lit = (s) => {
|
|
92
|
+
const last = toks[toks.length - 1];
|
|
93
|
+
if (last?.t === "lit") last.s += s;
|
|
94
|
+
else toks.push({
|
|
95
|
+
t: "lit",
|
|
96
|
+
s
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
for (let i = 0; i < src.length; i++) {
|
|
100
|
+
const ch = src[i];
|
|
101
|
+
const rest = (n) => src.slice(i, i + n).toLowerCase();
|
|
102
|
+
switch (ch) {
|
|
103
|
+
case "\"": {
|
|
104
|
+
const end = src.indexOf("\"", i + 1);
|
|
105
|
+
const stop = end === -1 ? src.length : end;
|
|
106
|
+
if (stop > i + 1) lit(src.slice(i + 1, stop));
|
|
107
|
+
i = stop;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
case "\\":
|
|
111
|
+
if (i + 1 < src.length) lit(src[++i]);
|
|
112
|
+
continue;
|
|
113
|
+
case "_":
|
|
114
|
+
if (i + 1 < src.length) i++;
|
|
115
|
+
toks.push({ t: "space" });
|
|
116
|
+
continue;
|
|
117
|
+
case "*":
|
|
118
|
+
if (i + 1 < src.length) toks.push({
|
|
119
|
+
t: "fill",
|
|
120
|
+
c: src[++i]
|
|
121
|
+
});
|
|
122
|
+
continue;
|
|
123
|
+
case "[": {
|
|
124
|
+
const end = src.indexOf("]", i + 1);
|
|
125
|
+
if (end === -1) {
|
|
126
|
+
lit(src.slice(i));
|
|
127
|
+
i = src.length;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
bracket(sec, toks, src.slice(i + 1, end));
|
|
131
|
+
i = end;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
case "0":
|
|
135
|
+
case "#":
|
|
136
|
+
case "?":
|
|
137
|
+
toks.push({
|
|
138
|
+
t: "digit",
|
|
139
|
+
c: ch
|
|
140
|
+
});
|
|
141
|
+
continue;
|
|
142
|
+
case ".":
|
|
143
|
+
toks.push({ t: "point" });
|
|
144
|
+
continue;
|
|
145
|
+
case ",":
|
|
146
|
+
toks.push({ t: "comma" });
|
|
147
|
+
continue;
|
|
148
|
+
case "%":
|
|
149
|
+
toks.push({ t: "pct" });
|
|
150
|
+
continue;
|
|
151
|
+
case "/":
|
|
152
|
+
toks.push({ t: "slash" });
|
|
153
|
+
continue;
|
|
154
|
+
case "@":
|
|
155
|
+
toks.push({ t: "text" });
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (rest(7) === "general") {
|
|
159
|
+
toks.push({ t: "general" });
|
|
160
|
+
i += 6;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (rest(5) === "am/pm") {
|
|
164
|
+
toks.push({
|
|
165
|
+
t: "ampm",
|
|
166
|
+
s: src.slice(i, i + 5)
|
|
167
|
+
});
|
|
168
|
+
i += 4;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (rest(3) === "a/p") {
|
|
172
|
+
toks.push({
|
|
173
|
+
t: "ampm",
|
|
174
|
+
s: src.slice(i, i + 3)
|
|
175
|
+
});
|
|
176
|
+
i += 2;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if ((ch === "E" || ch === "e") && (src[i + 1] === "+" || src[i + 1] === "-")) {
|
|
180
|
+
toks.push({
|
|
181
|
+
t: "exp",
|
|
182
|
+
s: src.slice(i, i + 2)
|
|
183
|
+
});
|
|
184
|
+
i++;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const k = ch.toLowerCase();
|
|
188
|
+
if (k === "y" || k === "m" || k === "d" || k === "h" || k === "s" || k === "e" || k === "b" || k === "g") {
|
|
189
|
+
let n = 1;
|
|
190
|
+
while (i + n < src.length && src[i + n].toLowerCase() === k) n++;
|
|
191
|
+
toks.push({
|
|
192
|
+
t: "date",
|
|
193
|
+
k,
|
|
194
|
+
n
|
|
195
|
+
});
|
|
196
|
+
i += n - 1;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
lit(ch);
|
|
200
|
+
}
|
|
201
|
+
return sec;
|
|
202
|
+
}
|
|
203
|
+
const isDateTok = (t) => t.t === "date" || t.t === "elapsed" || t.t === "ampm" || t.t === "minute";
|
|
204
|
+
/** Classifies a section and resolves date-only tokens (minutes vs months, sub-seconds). */
|
|
205
|
+
function finish(sec) {
|
|
206
|
+
const toks = sec.toks;
|
|
207
|
+
if (toks.some(isDateTok)) {
|
|
208
|
+
sec.kind = "date";
|
|
209
|
+
const dateIdx = toks.map((t, i) => isDateTok(t) ? i : -1).filter((i) => i >= 0);
|
|
210
|
+
dateIdx.forEach((idx, j) => {
|
|
211
|
+
const t = toks[idx];
|
|
212
|
+
if (t.t !== "date" || t.k !== "m" || t.n > 2) return;
|
|
213
|
+
const prev = j > 0 ? toks[dateIdx[j - 1]] : void 0;
|
|
214
|
+
const next = j + 1 < dateIdx.length ? toks[dateIdx[j + 1]] : void 0;
|
|
215
|
+
const afterHour = prev?.t === "date" && prev.k === "h" || prev?.t === "elapsed" && prev.k === "h";
|
|
216
|
+
const beforeSecond = next?.t === "date" && next.k === "s" || next?.t === "elapsed" && next.k === "s";
|
|
217
|
+
if (afterHour || beforeSecond) toks[idx] = {
|
|
218
|
+
t: "minute",
|
|
219
|
+
n: t.n
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
const out = [];
|
|
223
|
+
let seenSeconds = false;
|
|
224
|
+
for (let i = 0; i < toks.length; i++) {
|
|
225
|
+
const t = toks[i];
|
|
226
|
+
if (t.t === "date" && t.k === "s" || t.t === "elapsed" && t.k === "s") seenSeconds = true;
|
|
227
|
+
if (t.t === "point" && seenSeconds && toks[i + 1]?.t === "digit" && toks[i + 1].c === "0") {
|
|
228
|
+
let n = 0;
|
|
229
|
+
while (toks[i + 1 + n]?.t === "digit" && toks[i + 1 + n].c === "0") n++;
|
|
230
|
+
out.push({
|
|
231
|
+
t: "subsec",
|
|
232
|
+
n: Math.min(n, 3)
|
|
233
|
+
});
|
|
234
|
+
i += n;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
out.push(literalInDate(t));
|
|
238
|
+
}
|
|
239
|
+
sec.toks = mergeLits(out);
|
|
240
|
+
return sec;
|
|
241
|
+
}
|
|
242
|
+
if (toks.some((t) => t.t === "text")) sec.kind = "text";
|
|
243
|
+
else if (toks.some((t) => t.t === "digit")) sec.kind = "number";
|
|
244
|
+
else if (toks.some((t) => t.t === "general")) sec.kind = "general";
|
|
245
|
+
else sec.kind = "literal";
|
|
246
|
+
return sec;
|
|
247
|
+
}
|
|
248
|
+
function literalInDate(t) {
|
|
249
|
+
switch (t.t) {
|
|
250
|
+
case "digit": return {
|
|
251
|
+
t: "lit",
|
|
252
|
+
s: t.c
|
|
253
|
+
};
|
|
254
|
+
case "point": return {
|
|
255
|
+
t: "lit",
|
|
256
|
+
s: "."
|
|
257
|
+
};
|
|
258
|
+
case "comma": return {
|
|
259
|
+
t: "lit",
|
|
260
|
+
s: ","
|
|
261
|
+
};
|
|
262
|
+
case "pct": return {
|
|
263
|
+
t: "lit",
|
|
264
|
+
s: "%"
|
|
265
|
+
};
|
|
266
|
+
case "slash": return {
|
|
267
|
+
t: "lit",
|
|
268
|
+
s: "/"
|
|
269
|
+
};
|
|
270
|
+
case "exp": return {
|
|
271
|
+
t: "lit",
|
|
272
|
+
s: t.s
|
|
273
|
+
};
|
|
274
|
+
default: return t;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function mergeLits(toks) {
|
|
278
|
+
const out = [];
|
|
279
|
+
for (const t of toks) {
|
|
280
|
+
const last = out[out.length - 1];
|
|
281
|
+
if (t.t === "lit" && last?.t === "lit") last.s += t.s;
|
|
282
|
+
else out.push(t);
|
|
283
|
+
}
|
|
284
|
+
return out;
|
|
285
|
+
}
|
|
286
|
+
/** Compiles a format code. At most four sections are used, as in Excel. */
|
|
287
|
+
function compile(code) {
|
|
288
|
+
return { sections: splitSections(code).slice(0, 4).map((s) => finish(tokenize(s))) };
|
|
289
|
+
}
|
|
290
|
+
// src/internal/format/locale.ts
|
|
291
|
+
const cache = /* @__PURE__ */ new Map();
|
|
292
|
+
function supported(tag) {
|
|
293
|
+
try {
|
|
294
|
+
return Intl.getCanonicalLocales(tag)[0] ?? "en-US";
|
|
295
|
+
} catch {
|
|
296
|
+
return "en-US";
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/** Locale data for a requested tag, cached by the tag as given (canonicalising is not free). */
|
|
300
|
+
function localeData(requested) {
|
|
301
|
+
const key = requested ?? "en-US";
|
|
302
|
+
let data = cache.get(key);
|
|
303
|
+
if (!data) {
|
|
304
|
+
const tag = supported(key);
|
|
305
|
+
data = (tag !== key ? cache.get(tag) : void 0) ?? build(tag);
|
|
306
|
+
if (cache.size > 50) cache.clear();
|
|
307
|
+
cache.set(key, data);
|
|
308
|
+
cache.set(tag, data);
|
|
309
|
+
}
|
|
310
|
+
return data;
|
|
311
|
+
}
|
|
312
|
+
function build(tag) {
|
|
313
|
+
const parts = new Intl.NumberFormat(tag, {
|
|
314
|
+
useGrouping: true,
|
|
315
|
+
minimumFractionDigits: 1
|
|
316
|
+
}).formatToParts(1234567.5);
|
|
317
|
+
const decimal = parts.find((p) => p.type === "decimal")?.value ?? ".";
|
|
318
|
+
const group = parts.find((p) => p.type === "group")?.value ?? ",";
|
|
319
|
+
const names = (opts, n, at) => {
|
|
320
|
+
const f = new Intl.DateTimeFormat(tag, {
|
|
321
|
+
...opts,
|
|
322
|
+
timeZone: "UTC"
|
|
323
|
+
});
|
|
324
|
+
return Array.from({ length: n }, (_, i) => f.format(at(i)));
|
|
325
|
+
};
|
|
326
|
+
const month = (i) => Date.UTC(2021, i, 15);
|
|
327
|
+
const day = (i) => Date.UTC(2021, 0, 3 + i);
|
|
328
|
+
const shortDate = [];
|
|
329
|
+
for (const p of new Intl.DateTimeFormat(tag, {
|
|
330
|
+
year: "numeric",
|
|
331
|
+
month: "numeric",
|
|
332
|
+
day: "numeric",
|
|
333
|
+
timeZone: "UTC"
|
|
334
|
+
}).formatToParts(Date.UTC(2021, 10, 25))) if (p.type === "year") shortDate.push("yyyy");
|
|
335
|
+
else if (p.type === "month") shortDate.push("m");
|
|
336
|
+
else if (p.type === "day") shortDate.push("d");
|
|
337
|
+
else if (p.type === "literal") shortDate.push(`"${p.value.replace(/"/g, "")}"`);
|
|
338
|
+
const short = new Intl.DateTimeFormat(tag, {
|
|
339
|
+
dateStyle: "short",
|
|
340
|
+
timeZone: "UTC"
|
|
341
|
+
}).formatToParts(Date.UTC(2021, 0, 5));
|
|
342
|
+
if (short.find((p) => p.type === "day")?.value === "05") {
|
|
343
|
+
for (let i = 0; i < shortDate.length; i++) if (shortDate[i] === "d") shortDate[i] = "dd";
|
|
344
|
+
}
|
|
345
|
+
if (short.find((p) => p.type === "month")?.value === "01") {
|
|
346
|
+
for (let i = 0; i < shortDate.length; i++) if (shortDate[i] === "m") shortDate[i] = "mm";
|
|
347
|
+
}
|
|
348
|
+
return {
|
|
349
|
+
tag,
|
|
350
|
+
decimal,
|
|
351
|
+
group,
|
|
352
|
+
monthsShort: names({ month: "short" }, 12, month),
|
|
353
|
+
monthsLong: names({ month: "long" }, 12, month),
|
|
354
|
+
daysShort: names({ weekday: "short" }, 7, day),
|
|
355
|
+
daysLong: names({ weekday: "long" }, 7, day),
|
|
356
|
+
shortDate
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Windows locale ids (LCIDs) seen in [$-xxxx] tags, mapped to BCP 47 tags.
|
|
361
|
+
* Only the common ones; an unknown id falls back to the caller's locale.
|
|
362
|
+
* Source: [MS-LCID] `[to verify for each entry used in real files]`.
|
|
363
|
+
*/
|
|
364
|
+
const LCIDS = /* @__PURE__ */ new Map([
|
|
365
|
+
[1025, "ar-SA"],
|
|
366
|
+
[1028, "zh-TW"],
|
|
367
|
+
[1029, "cs-CZ"],
|
|
368
|
+
[1030, "da-DK"],
|
|
369
|
+
[1031, "de-DE"],
|
|
370
|
+
[1032, "el-GR"],
|
|
371
|
+
[1033, "en-US"],
|
|
372
|
+
[1034, "es-ES"],
|
|
373
|
+
[1035, "fi-FI"],
|
|
374
|
+
[1036, "fr-FR"],
|
|
375
|
+
[1037, "he-IL"],
|
|
376
|
+
[1038, "hu-HU"],
|
|
377
|
+
[1040, "it-IT"],
|
|
378
|
+
[1041, "ja-JP"],
|
|
379
|
+
[1042, "ko-KR"],
|
|
380
|
+
[1043, "nl-NL"],
|
|
381
|
+
[1044, "nb-NO"],
|
|
382
|
+
[1045, "pl-PL"],
|
|
383
|
+
[1046, "pt-BR"],
|
|
384
|
+
[1049, "ru-RU"],
|
|
385
|
+
[1053, "sv-SE"],
|
|
386
|
+
[1054, "th-TH"],
|
|
387
|
+
[1055, "tr-TR"],
|
|
388
|
+
[1057, "id-ID"],
|
|
389
|
+
[1058, "uk-UA"],
|
|
390
|
+
[1066, "vi-VN"],
|
|
391
|
+
[1081, "hi-IN"],
|
|
392
|
+
[2052, "zh-CN"],
|
|
393
|
+
[2055, "de-CH"],
|
|
394
|
+
[2057, "en-GB"],
|
|
395
|
+
[2058, "es-MX"],
|
|
396
|
+
[2070, "pt-PT"],
|
|
397
|
+
[3079, "de-AT"],
|
|
398
|
+
[3081, "en-AU"],
|
|
399
|
+
[3082, "es-ES"],
|
|
400
|
+
[3084, "fr-CA"],
|
|
401
|
+
[4105, "en-CA"],
|
|
402
|
+
[4108, "fr-CH"],
|
|
403
|
+
[5129, "en-NZ"],
|
|
404
|
+
[6153, "en-IE"],
|
|
405
|
+
[16393, "en-IN"]
|
|
406
|
+
]);
|
|
407
|
+
function lcidLocale(id) {
|
|
408
|
+
return LCIDS.get(id);
|
|
409
|
+
}
|
|
410
|
+
/** The eight colour names of format codes, as ARGB. */
|
|
411
|
+
const NAMED_COLORS = {
|
|
412
|
+
black: "FF000000",
|
|
413
|
+
blue: "FF0000FF",
|
|
414
|
+
cyan: "FF00FFFF",
|
|
415
|
+
green: "FF00FF00",
|
|
416
|
+
magenta: "FFFF00FF",
|
|
417
|
+
red: "FFFF0000",
|
|
418
|
+
white: "FFFFFFFF",
|
|
419
|
+
yellow: "FFFFFF00"
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* The default indexed palette, entries 8–63 (ECMA-376 §18.8.27), as RGB.
|
|
423
|
+
* [ColorN] is entry N + 7. `[to verify: conformance.md, 12-colours]`
|
|
424
|
+
*/
|
|
425
|
+
const PALETTE = "000000 FFFFFF FF0000 00FF00 0000FF FFFF00 FF00FF 00FFFF 800000 008000 000080 808000 800080 008080 C0C0C0 808080 9999FF 993366 FFFFCC CCFFFF 660066 FF8080 0066CC CCCCFF 000080 FF00FF FFFF00 00FFFF 800080 800000 008080 0000FF 00CCFF CCFFFF CCFFCC FFFF99 99CCFF FF99CC CC99FF FFCC99 3366FF 33CCCC 99CC00 FFCC00 FF9900 FF6600 666699 969696 003366 339966 003300 333300 993300 993366 333399 333333".split(" ");
|
|
426
|
+
/** ARGB of [ColorN], from the workbook's palette when given (64 entries, ARGB), else the default. */
|
|
427
|
+
function paletteColor(n, indexedColors) {
|
|
428
|
+
const custom = indexedColors?.[n + 7];
|
|
429
|
+
if (custom && /^[0-9a-f]{8}$/i.test(custom)) return custom.toUpperCase();
|
|
430
|
+
return `FF${PALETTE[n - 1] ?? "000000"}`;
|
|
431
|
+
}
|
|
432
|
+
// src/internal/format/date.ts
|
|
433
|
+
/**
|
|
434
|
+
* Date and time rendering from Excel serials (docs/api.md §5.7). Pure
|
|
435
|
+
* arithmetic on the serial: no Date in the local zone is ever built, so the
|
|
436
|
+
* output does not depend on the machine's time zone.
|
|
437
|
+
*/
|
|
438
|
+
const DAY = 86400;
|
|
439
|
+
/** Largest serial Excel shows as a date: 9999-12-31 in the 1900 system. */
|
|
440
|
+
const MAX_1900 = 2958465;
|
|
441
|
+
const MAX_1904 = 2957003;
|
|
442
|
+
function parts(serial, date1904, digits) {
|
|
443
|
+
const unit = 10 ** digits;
|
|
444
|
+
const units = Math.round(serial * DAY * unit);
|
|
445
|
+
const totalS = Math.floor(units / unit);
|
|
446
|
+
const sub = units - totalS * unit;
|
|
447
|
+
const days = Math.floor(totalS / DAY);
|
|
448
|
+
const secs = totalS - days * DAY;
|
|
449
|
+
let y;
|
|
450
|
+
let mo;
|
|
451
|
+
let d;
|
|
452
|
+
if (!date1904 && days === 60) [y, mo, d] = [
|
|
453
|
+
1900,
|
|
454
|
+
2,
|
|
455
|
+
29
|
|
456
|
+
];
|
|
457
|
+
else if (!date1904 && days === 0) [y, mo, d] = [
|
|
458
|
+
1900,
|
|
459
|
+
1,
|
|
460
|
+
0
|
|
461
|
+
];
|
|
462
|
+
else {
|
|
463
|
+
const t = new Date((date1904 ? Date.UTC(1904, 0, 1) : days < 60 ? Date.UTC(1899, 11, 31) : Date.UTC(1899, 11, 30)) + days * DAY * 1e3);
|
|
464
|
+
[y, mo, d] = [
|
|
465
|
+
t.getUTCFullYear(),
|
|
466
|
+
t.getUTCMonth() + 1,
|
|
467
|
+
t.getUTCDate()
|
|
468
|
+
];
|
|
469
|
+
}
|
|
470
|
+
const wd = date1904 ? ((days + 5) % 7 + 7) % 7 : ((days + 6) % 7 + 7) % 7;
|
|
471
|
+
return {
|
|
472
|
+
y,
|
|
473
|
+
mo,
|
|
474
|
+
d,
|
|
475
|
+
wd,
|
|
476
|
+
H: Math.floor(secs / 3600),
|
|
477
|
+
M: Math.floor(secs % 3600 / 60),
|
|
478
|
+
S: secs % 60,
|
|
479
|
+
sub,
|
|
480
|
+
totalH: Math.floor(totalS / 3600),
|
|
481
|
+
totalM: Math.floor(totalS / 60),
|
|
482
|
+
totalS
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
const two = (n) => String(n).padStart(2, "0");
|
|
486
|
+
/** Formats a serial with a date section. Returns null when Excel would show a cell full of '#'. */
|
|
487
|
+
function formatDate(sec, serial, date1904, loc) {
|
|
488
|
+
const hasElapsed = sec.toks.some((t) => t.t === "elapsed");
|
|
489
|
+
if (!Number.isFinite(serial) || serial < 0 && !hasElapsed || serial > (date1904 ? MAX_1904 : MAX_1900) + 1) return null;
|
|
490
|
+
if (sec.system) return { text: systemFormat(sec.system, serial, date1904, loc) };
|
|
491
|
+
const names = sec.lcid !== void 0 ? localeData(lcidLocale(sec.lcid) ?? loc.tag) : loc;
|
|
492
|
+
const digits = sec.toks.reduce((m, t) => t.t === "subsec" ? Math.max(m, t.n) : m, 0);
|
|
493
|
+
const neg = serial < 0;
|
|
494
|
+
const p = parts(Math.abs(serial), date1904, digits);
|
|
495
|
+
const ampm = sec.toks.some((t) => t.t === "ampm");
|
|
496
|
+
const elapsedKind = sec.toks.find((t) => t.t === "elapsed");
|
|
497
|
+
let text = neg ? "-" : "";
|
|
498
|
+
let fill;
|
|
499
|
+
for (const t of sec.toks) switch (t.t) {
|
|
500
|
+
case "lit":
|
|
501
|
+
text += t.s;
|
|
502
|
+
break;
|
|
503
|
+
case "space":
|
|
504
|
+
text += " ";
|
|
505
|
+
break;
|
|
506
|
+
case "fill":
|
|
507
|
+
if (!fill) fill = {
|
|
508
|
+
char: t.c,
|
|
509
|
+
at: text.length
|
|
510
|
+
};
|
|
511
|
+
break;
|
|
512
|
+
case "elapsed": {
|
|
513
|
+
const v = t.k === "h" ? p.totalH : t.k === "m" ? elapsedKind?.k === "h" ? p.M : p.totalM : elapsedKind?.k === "s" ? p.totalS : p.S;
|
|
514
|
+
text += String(v).padStart(t.n, "0");
|
|
515
|
+
break;
|
|
516
|
+
}
|
|
517
|
+
case "minute": {
|
|
518
|
+
const m = elapsedKind?.k === "m" ? p.totalM : p.M;
|
|
519
|
+
text += t.n >= 2 ? two(m) : String(m);
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
case "subsec":
|
|
523
|
+
text += loc.decimal + String(p.sub).padStart(digits, "0").slice(0, t.n);
|
|
524
|
+
break;
|
|
525
|
+
case "ampm": {
|
|
526
|
+
const pm = p.H >= 12;
|
|
527
|
+
const lower = t.s[0] === t.s[0].toLowerCase();
|
|
528
|
+
const word = t.s.length === 3 ? pm ? "P" : "A" : pm ? "PM" : "AM";
|
|
529
|
+
text += lower ? word.toLowerCase() : word;
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
case "date": text += dateToken(t.k, t.n, p, ampm, names);
|
|
533
|
+
}
|
|
534
|
+
return fill ? {
|
|
535
|
+
text,
|
|
536
|
+
fill
|
|
537
|
+
} : { text };
|
|
538
|
+
}
|
|
539
|
+
function dateToken(k, n, p, ampm, loc) {
|
|
540
|
+
switch (k) {
|
|
541
|
+
case "y":
|
|
542
|
+
case "e": return k === "y" && n <= 2 ? two(p.y % 100) : String(p.y);
|
|
543
|
+
case "b": {
|
|
544
|
+
const by = p.y + 543;
|
|
545
|
+
return n <= 2 ? two(by % 100) : String(by);
|
|
546
|
+
}
|
|
547
|
+
case "g": return "";
|
|
548
|
+
case "m":
|
|
549
|
+
if (n === 1) return String(p.mo);
|
|
550
|
+
if (n === 2) return two(p.mo);
|
|
551
|
+
if (n === 3) return loc.monthsShort[p.mo - 1] ?? "";
|
|
552
|
+
if (n === 5) return (loc.monthsLong[p.mo - 1] ?? "").slice(0, 1);
|
|
553
|
+
return loc.monthsLong[p.mo - 1] ?? "";
|
|
554
|
+
case "d":
|
|
555
|
+
if (n === 1) return String(p.d);
|
|
556
|
+
if (n === 2) return two(p.d);
|
|
557
|
+
if (n === 3) return loc.daysShort[p.wd] ?? "";
|
|
558
|
+
return loc.daysLong[p.wd] ?? "";
|
|
559
|
+
case "h": {
|
|
560
|
+
const h = ampm ? p.H % 12 || 12 : p.H;
|
|
561
|
+
return n >= 2 ? two(h) : String(h);
|
|
562
|
+
}
|
|
563
|
+
case "s": return n >= 2 ? two(p.S) : String(p.S);
|
|
564
|
+
default: return "";
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
const systemCache = /* @__PURE__ */ new Map();
|
|
568
|
+
/** Excel's system long date ([$-F800]) and time ([$-F400]), from Intl `[to verify against Windows settings]`. */
|
|
569
|
+
function systemFormat(kind, serial, date1904, loc) {
|
|
570
|
+
const p = parts(serial, date1904, 0);
|
|
571
|
+
const t = Date.UTC(p.y, p.mo - 1, Math.max(1, p.d), p.H, p.M, p.S);
|
|
572
|
+
const opts = kind === "longDate" ? {
|
|
573
|
+
dateStyle: "full",
|
|
574
|
+
timeZone: "UTC"
|
|
575
|
+
} : {
|
|
576
|
+
timeStyle: "medium",
|
|
577
|
+
timeZone: "UTC"
|
|
578
|
+
};
|
|
579
|
+
return new Intl.DateTimeFormat(loc.tag, opts).format(t);
|
|
580
|
+
}
|
|
581
|
+
/** The locale's numeric short date with a four-digit year, as a compiled section (built-in id 14). */
|
|
582
|
+
function shortDateSection(loc, withTime) {
|
|
583
|
+
const code = loc.shortDate.join("") + (withTime ? " h:mm" : "");
|
|
584
|
+
let sec = systemCache.get(code);
|
|
585
|
+
if (!sec) {
|
|
586
|
+
sec = compile(code).sections[0];
|
|
587
|
+
systemCache.set(code, sec);
|
|
588
|
+
}
|
|
589
|
+
return sec;
|
|
590
|
+
}
|
|
591
|
+
// src/internal/format/number.ts
|
|
592
|
+
function decimalOf(v) {
|
|
593
|
+
if (v === 0) return {
|
|
594
|
+
digits: "",
|
|
595
|
+
point: 0
|
|
596
|
+
};
|
|
597
|
+
const [mant, e] = Math.abs(v).toPrecision(15).split("e");
|
|
598
|
+
const [ip, fp = ""] = mant.split(".");
|
|
599
|
+
let digits = ip + fp;
|
|
600
|
+
let point = ip.length + (e ? Number(e) : 0);
|
|
601
|
+
let i = 0;
|
|
602
|
+
while (i < digits.length - 1 && digits[i] === "0") {
|
|
603
|
+
i++;
|
|
604
|
+
point--;
|
|
605
|
+
}
|
|
606
|
+
digits = digits.slice(i).replace(/0+$/, "");
|
|
607
|
+
return digits === "" ? {
|
|
608
|
+
digits: "",
|
|
609
|
+
point: 0
|
|
610
|
+
} : {
|
|
611
|
+
digits,
|
|
612
|
+
point
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
const shift = (d, k) => d.digits === "" ? d : {
|
|
616
|
+
digits: d.digits,
|
|
617
|
+
point: d.point + k
|
|
618
|
+
};
|
|
619
|
+
/** Rounds half away from zero to `decimals` places: integer digits ("" for zero) and exactly `decimals` fraction digits. */
|
|
620
|
+
function roundDec(d, decimals) {
|
|
621
|
+
const keep = d.point + decimals;
|
|
622
|
+
if (d.digits === "" || keep < 0) return {
|
|
623
|
+
int: "",
|
|
624
|
+
frac: "0".repeat(decimals)
|
|
625
|
+
};
|
|
626
|
+
let kept = d.digits.slice(0, keep).padEnd(keep, "0");
|
|
627
|
+
const next = d.digits[keep];
|
|
628
|
+
if (next !== void 0 && next >= "5") kept = increment(kept);
|
|
629
|
+
const full = kept.padStart(decimals, "0");
|
|
630
|
+
return {
|
|
631
|
+
int: full.slice(0, full.length - decimals).replace(/^0+/, ""),
|
|
632
|
+
frac: decimals > 0 ? full.slice(full.length - decimals) : ""
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
function increment(s) {
|
|
636
|
+
const a = s.split("");
|
|
637
|
+
let i = a.length - 1;
|
|
638
|
+
while (i >= 0) if (a[i] === "9") {
|
|
639
|
+
a[i] = "0";
|
|
640
|
+
i--;
|
|
641
|
+
} else {
|
|
642
|
+
a[i] = String(Number(a[i]) + 1);
|
|
643
|
+
return a.join("");
|
|
644
|
+
}
|
|
645
|
+
return `1${a.join("")}`;
|
|
646
|
+
}
|
|
647
|
+
/** Collects the output, tracking a `*x` fill position. */
|
|
648
|
+
var Out = class {
|
|
649
|
+
text = "";
|
|
650
|
+
fill;
|
|
651
|
+
push(s) {
|
|
652
|
+
this.text += s;
|
|
653
|
+
}
|
|
654
|
+
tok(t, text) {
|
|
655
|
+
switch (t.t) {
|
|
656
|
+
case "lit":
|
|
657
|
+
this.text += t.s;
|
|
658
|
+
return true;
|
|
659
|
+
case "space":
|
|
660
|
+
this.text += " ";
|
|
661
|
+
return true;
|
|
662
|
+
case "fill":
|
|
663
|
+
if (!this.fill) this.fill = {
|
|
664
|
+
char: t.c,
|
|
665
|
+
at: this.text.length
|
|
666
|
+
};
|
|
667
|
+
return true;
|
|
668
|
+
case "text":
|
|
669
|
+
this.text += text ?? "";
|
|
670
|
+
return true;
|
|
671
|
+
default: return false;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
result() {
|
|
675
|
+
return this.fill ? {
|
|
676
|
+
text: this.text,
|
|
677
|
+
fill: this.fill
|
|
678
|
+
} : { text: this.text };
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
/** Formats a non-negative value with a number section (the caller adds any minus sign). */
|
|
682
|
+
function formatNumber(sec, v, loc, generalWidth) {
|
|
683
|
+
const toks = sec.toks;
|
|
684
|
+
const expAt = toks.findIndex((t) => t.t === "exp");
|
|
685
|
+
if (expAt !== -1) return scientific(toks, expAt, v, loc);
|
|
686
|
+
const slashAt = fractionSlash(toks);
|
|
687
|
+
if (slashAt !== -1) return fraction(toks, slashAt, v, loc);
|
|
688
|
+
return plain(toks, v, loc, generalWidth);
|
|
689
|
+
}
|
|
690
|
+
/** Index of a slash with digit placeholders before it and placeholders or digits after it, else -1. */
|
|
691
|
+
function fractionSlash(toks) {
|
|
692
|
+
for (let i = 0; i < toks.length; i++) {
|
|
693
|
+
if (toks[i].t !== "slash") continue;
|
|
694
|
+
const before = toks[i - 1];
|
|
695
|
+
const after = toks[i + 1];
|
|
696
|
+
if (before?.t === "digit" && (after?.t === "digit" || after?.t === "lit" && /^[1-9]/.test(after.s))) return i;
|
|
697
|
+
}
|
|
698
|
+
return -1;
|
|
699
|
+
}
|
|
700
|
+
function layout(toks, from, to) {
|
|
701
|
+
const intToks = [];
|
|
702
|
+
const fracToks = [];
|
|
703
|
+
let pointAt = -1;
|
|
704
|
+
let scale = 0;
|
|
705
|
+
for (let i = from; i < to; i++) {
|
|
706
|
+
const t = toks[i];
|
|
707
|
+
if (t.t === "point" && pointAt === -1) pointAt = i;
|
|
708
|
+
else if (t.t === "digit") (pointAt === -1 ? intToks : fracToks).push(i);
|
|
709
|
+
else if (t.t === "pct") scale += 2;
|
|
710
|
+
}
|
|
711
|
+
const commaRole = /* @__PURE__ */ new Map();
|
|
712
|
+
let grouping = false;
|
|
713
|
+
const lastInt = intToks[intToks.length - 1] ?? -1;
|
|
714
|
+
const firstInt = intToks[0] ?? -1;
|
|
715
|
+
const lastDigit = fracToks[fracToks.length - 1] ?? lastInt;
|
|
716
|
+
for (let i = from; i < to; i++) {
|
|
717
|
+
if (toks[i].t !== "comma") continue;
|
|
718
|
+
if (i > firstInt && i < lastInt && firstInt !== -1) {
|
|
719
|
+
commaRole.set(i, "group");
|
|
720
|
+
grouping = true;
|
|
721
|
+
} else if (lastDigit !== -1 && i > lastDigit) {
|
|
722
|
+
let j = lastDigit + 1;
|
|
723
|
+
while (j < i && toks[j].t === "comma") j++;
|
|
724
|
+
if (j === i) {
|
|
725
|
+
commaRole.set(i, "scale");
|
|
726
|
+
scale -= 3;
|
|
727
|
+
} else commaRole.set(i, "lit");
|
|
728
|
+
} else commaRole.set(i, "lit");
|
|
729
|
+
}
|
|
730
|
+
return {
|
|
731
|
+
intToks,
|
|
732
|
+
fracToks,
|
|
733
|
+
pointAt,
|
|
734
|
+
grouping,
|
|
735
|
+
scale,
|
|
736
|
+
commaRole
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
/** Places integer digits into placeholders, right to left; extra digits go to the leftmost one. */
|
|
740
|
+
function placeInt(toks, idx, digits) {
|
|
741
|
+
const out = /* @__PURE__ */ new Map();
|
|
742
|
+
let d = digits.length;
|
|
743
|
+
for (let k = idx.length - 1; k >= 0; k--) {
|
|
744
|
+
const c = toks[idx[k]].c;
|
|
745
|
+
let s = d > 0 ? digits[--d] : c === "0" ? "0" : c === "?" ? " " : "";
|
|
746
|
+
if (k === 0 && d > 0) s = digits.slice(0, d) + s;
|
|
747
|
+
out.set(idx[k], s);
|
|
748
|
+
}
|
|
749
|
+
return out;
|
|
750
|
+
}
|
|
751
|
+
function group(s, sep) {
|
|
752
|
+
let out = "";
|
|
753
|
+
let count = 0;
|
|
754
|
+
for (let i = s.length - 1; i >= 0; i--) {
|
|
755
|
+
const ch = s[i];
|
|
756
|
+
if (ch >= "0" && ch <= "9") {
|
|
757
|
+
if (count > 0 && count % 3 === 0) out = sep + out;
|
|
758
|
+
count++;
|
|
759
|
+
}
|
|
760
|
+
out = ch + out;
|
|
761
|
+
}
|
|
762
|
+
return out;
|
|
763
|
+
}
|
|
764
|
+
function placeFrac(toks, idx, frac) {
|
|
765
|
+
const out = /* @__PURE__ */ new Map();
|
|
766
|
+
let trim = true;
|
|
767
|
+
for (let k = idx.length - 1; k >= 0; k--) {
|
|
768
|
+
const c = toks[idx[k]].c;
|
|
769
|
+
const digit = frac[k] ?? "0";
|
|
770
|
+
if (trim && digit === "0" && c !== "0") out.set(idx[k], c === "?" ? " " : "");
|
|
771
|
+
else {
|
|
772
|
+
trim = false;
|
|
773
|
+
out.set(idx[k], digit);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return out;
|
|
777
|
+
}
|
|
778
|
+
function plain(toks, v, loc, generalWidth) {
|
|
779
|
+
const lay = layout(toks, 0, toks.length);
|
|
780
|
+
const r = roundDec(shift(decimalOf(v), lay.scale), lay.fracToks.length);
|
|
781
|
+
const ints = placeInt(toks, lay.intToks, r.int);
|
|
782
|
+
const fracs = placeFrac(toks, lay.fracToks, r.frac);
|
|
783
|
+
const out = new Out();
|
|
784
|
+
let intText = "";
|
|
785
|
+
const flushInt = () => {
|
|
786
|
+
if (intText) out.push(lay.grouping ? group(intText, loc.group) : intText);
|
|
787
|
+
intText = "";
|
|
788
|
+
};
|
|
789
|
+
const firstInt = lay.intToks[0] ?? -1;
|
|
790
|
+
const lastInt = lay.intToks[lay.intToks.length - 1] ?? -1;
|
|
791
|
+
for (let i = 0; i < toks.length; i++) {
|
|
792
|
+
const t = toks[i];
|
|
793
|
+
const inInt = i >= firstInt && i <= lastInt && firstInt !== -1;
|
|
794
|
+
if (t.t === "digit") {
|
|
795
|
+
if (ints.has(i)) intText += ints.get(i);
|
|
796
|
+
else out.push(fracs.get(i) ?? "");
|
|
797
|
+
if (i === lastInt) flushInt();
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
if (inInt) {
|
|
801
|
+
if (t.t === "comma") continue;
|
|
802
|
+
if (t.t === "lit" || t.t === "space") {
|
|
803
|
+
intText += t.t === "lit" ? t.s : " ";
|
|
804
|
+
continue;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
switch (t.t) {
|
|
808
|
+
case "point":
|
|
809
|
+
if (i === lay.pointAt) out.push(loc.decimal);
|
|
810
|
+
else out.push(".");
|
|
811
|
+
break;
|
|
812
|
+
case "comma":
|
|
813
|
+
if (lay.commaRole.get(i) === "lit") out.push(",");
|
|
814
|
+
break;
|
|
815
|
+
case "pct":
|
|
816
|
+
out.push("%");
|
|
817
|
+
break;
|
|
818
|
+
case "slash":
|
|
819
|
+
out.push("/");
|
|
820
|
+
break;
|
|
821
|
+
case "exp":
|
|
822
|
+
out.push(t.s);
|
|
823
|
+
break;
|
|
824
|
+
case "general":
|
|
825
|
+
out.push(general(v, loc, generalWidth));
|
|
826
|
+
break;
|
|
827
|
+
default: out.tok(t);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
flushInt();
|
|
831
|
+
return out.result();
|
|
832
|
+
}
|
|
833
|
+
function scientific(toks, expAt, v, loc) {
|
|
834
|
+
const lay = layout(toks, 0, expAt);
|
|
835
|
+
const intCount = Math.max(1, lay.intToks.length);
|
|
836
|
+
const engineering = lay.intToks.some((i) => toks[i].c === "#") && lay.intToks.length > 1;
|
|
837
|
+
const d = decimalOf(v);
|
|
838
|
+
let exp = 0;
|
|
839
|
+
let r = {
|
|
840
|
+
int: "",
|
|
841
|
+
frac: "0".repeat(lay.fracToks.length)
|
|
842
|
+
};
|
|
843
|
+
if (d.digits !== "") {
|
|
844
|
+
const e10 = d.point - 1;
|
|
845
|
+
exp = engineering ? Math.floor(e10 / intCount) * intCount : e10 - (intCount - 1);
|
|
846
|
+
r = roundDec(shift(d, -exp), lay.fracToks.length);
|
|
847
|
+
if (r.int.length > intCount) {
|
|
848
|
+
exp += engineering ? intCount : 1;
|
|
849
|
+
r = roundDec(shift(d, -exp), lay.fracToks.length);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
const ints = placeInt(toks, lay.intToks, r.int);
|
|
853
|
+
const fracs = placeFrac(toks, lay.fracToks, r.frac);
|
|
854
|
+
const out = new Out();
|
|
855
|
+
for (let i = 0; i < expAt; i++) {
|
|
856
|
+
const t = toks[i];
|
|
857
|
+
if (t.t === "digit") out.push(ints.get(i) ?? fracs.get(i) ?? "");
|
|
858
|
+
else if (t.t === "point") out.push(i === lay.pointAt ? loc.decimal : ".");
|
|
859
|
+
else if (t.t === "pct") out.push("%");
|
|
860
|
+
else if (t.t === "comma") continue;
|
|
861
|
+
else out.tok(t);
|
|
862
|
+
}
|
|
863
|
+
const expTok = toks[expAt];
|
|
864
|
+
out.push(expTok.s[0]);
|
|
865
|
+
if (exp < 0) out.push("-");
|
|
866
|
+
else if (expTok.s[1] === "+") out.push("+");
|
|
867
|
+
const expDigits = String(Math.abs(exp));
|
|
868
|
+
const after = toks.slice(expAt + 1);
|
|
869
|
+
const minDigits = after.filter((t) => t.t === "digit" && t.c === "0").length;
|
|
870
|
+
let placed = false;
|
|
871
|
+
for (const t of after) {
|
|
872
|
+
if (t.t === "digit") {
|
|
873
|
+
if (!placed) {
|
|
874
|
+
out.push(expDigits.padStart(minDigits, "0"));
|
|
875
|
+
placed = true;
|
|
876
|
+
}
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
out.tok(t);
|
|
880
|
+
}
|
|
881
|
+
if (!placed) out.push(expDigits);
|
|
882
|
+
return out.result();
|
|
883
|
+
}
|
|
884
|
+
function fraction(toks, slashAt, v, loc) {
|
|
885
|
+
let numStart = slashAt - 1;
|
|
886
|
+
while (numStart > 0 && toks[numStart - 1].t === "digit") numStart--;
|
|
887
|
+
const numIdx = range(numStart, slashAt);
|
|
888
|
+
const intIdx = [];
|
|
889
|
+
for (let i = 0; i < numStart; i++) if (toks[i].t === "digit") intIdx.push(i);
|
|
890
|
+
const after = toks[slashAt + 1];
|
|
891
|
+
let denEnd = slashAt + 1;
|
|
892
|
+
let fixedDen = 0;
|
|
893
|
+
let denIdx = [];
|
|
894
|
+
if (after.t === "lit") {
|
|
895
|
+
const m = /^[1-9]\d*/.exec(after.s);
|
|
896
|
+
fixedDen = Number(m[0]);
|
|
897
|
+
denEnd = slashAt + 2;
|
|
898
|
+
} else {
|
|
899
|
+
while (toks[denEnd]?.t === "digit") denEnd++;
|
|
900
|
+
denIdx = range(slashAt + 1, denEnd);
|
|
901
|
+
}
|
|
902
|
+
const hasInt = intIdx.length > 0;
|
|
903
|
+
let whole = hasInt ? Math.floor(v) : 0;
|
|
904
|
+
const part = hasInt ? v - whole : v;
|
|
905
|
+
let num;
|
|
906
|
+
let den;
|
|
907
|
+
if (fixedDen) {
|
|
908
|
+
den = fixedDen;
|
|
909
|
+
num = Math.round(part * den);
|
|
910
|
+
} else [num, den] = approximate(part, 10 ** denIdx.length - 1);
|
|
911
|
+
if (hasInt && num === den) {
|
|
912
|
+
whole += 1;
|
|
913
|
+
num = 0;
|
|
914
|
+
}
|
|
915
|
+
const out = new Out();
|
|
916
|
+
const ints = placeInt(toks, intIdx, whole === 0 && !(hasInt && num === 0) ? "" : String(whole));
|
|
917
|
+
const blankFraction = hasInt && num === 0;
|
|
918
|
+
const numText = pad(String(num), numIdx.map((i) => toks[i].c), true);
|
|
919
|
+
const denText = fixedDen ? String(fixedDen) : pad(String(den), denIdx.map((i) => toks[i].c), false);
|
|
920
|
+
for (let i = 0; i < toks.length; i++) {
|
|
921
|
+
const t = toks[i];
|
|
922
|
+
if (i < numStart) {
|
|
923
|
+
if (t.t === "digit") out.push(ints.get(i) ?? "");
|
|
924
|
+
else if (t.t === "point") out.push(loc.decimal);
|
|
925
|
+
else if (t.t === "comma") continue;
|
|
926
|
+
else out.tok(t);
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
if (i === numStart) {
|
|
930
|
+
out.push(blankFraction ? " ".repeat(numText.length) : numText);
|
|
931
|
+
continue;
|
|
932
|
+
}
|
|
933
|
+
if (i > numStart && i < slashAt) continue;
|
|
934
|
+
if (i === slashAt) {
|
|
935
|
+
out.push(blankFraction ? " " : "/");
|
|
936
|
+
continue;
|
|
937
|
+
}
|
|
938
|
+
if (i === slashAt + 1) {
|
|
939
|
+
out.push(blankFraction ? " ".repeat(denText.length) : denText);
|
|
940
|
+
if (fixedDen && t.t === "lit") {
|
|
941
|
+
const rest = t.s.slice(String(fixedDen).length);
|
|
942
|
+
if (rest) out.push(rest);
|
|
943
|
+
}
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
if (i < denEnd) continue;
|
|
947
|
+
out.tok(t);
|
|
948
|
+
}
|
|
949
|
+
return out.result();
|
|
950
|
+
}
|
|
951
|
+
function range(a, b) {
|
|
952
|
+
return Array.from({ length: Math.max(0, b - a) }, (_, i) => a + i);
|
|
953
|
+
}
|
|
954
|
+
/** Pads per placeholders: '0' zero, '?' space, '#' nothing. Numerators pad on the left, denominators on the right. */
|
|
955
|
+
function pad(s, cs, left) {
|
|
956
|
+
if (s.length >= cs.length) return s;
|
|
957
|
+
const fill = cs.slice(0, cs.length - s.length).map((c) => c === "0" ? "0" : c === "?" ? " " : "");
|
|
958
|
+
if (left) return fill.join("") + s;
|
|
959
|
+
return s + fill.map((f) => f === "" ? "" : " ").join("");
|
|
960
|
+
}
|
|
961
|
+
/** Best fraction num/den ≈ x with den ≤ maxDen (continued fractions, then the closest semiconvergent). */
|
|
962
|
+
function approximate(x, maxDen) {
|
|
963
|
+
if (maxDen < 1) maxDen = 1;
|
|
964
|
+
let [p0, q0, p1, q1] = [
|
|
965
|
+
0,
|
|
966
|
+
1,
|
|
967
|
+
1,
|
|
968
|
+
0
|
|
969
|
+
];
|
|
970
|
+
let y = x;
|
|
971
|
+
for (let iter = 0; iter < 64; iter++) {
|
|
972
|
+
const a = Math.floor(y);
|
|
973
|
+
const q2 = q0 + a * q1;
|
|
974
|
+
if (q2 > maxDen) {
|
|
975
|
+
const k = Math.floor((maxDen - q0) / q1);
|
|
976
|
+
const [pk, qk] = [p0 + k * p1, q0 + k * q1];
|
|
977
|
+
return Math.abs(x - pk / qk) < Math.abs(x - p1 / q1) ? [pk, qk] : [p1, q1];
|
|
978
|
+
}
|
|
979
|
+
[p0, q0, p1, q1] = [
|
|
980
|
+
p1,
|
|
981
|
+
q1,
|
|
982
|
+
p0 + a * p1,
|
|
983
|
+
q2
|
|
984
|
+
];
|
|
985
|
+
const frac = y - a;
|
|
986
|
+
if (frac < 1e-12 || Math.abs(x - p1 / q1) < 1e-15) break;
|
|
987
|
+
y = 1 / frac;
|
|
988
|
+
}
|
|
989
|
+
return [p1, q1];
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Excel's General format for a number: as many digits as fit in `width`
|
|
993
|
+
* characters (11 by default, the standard column), switching to scientific
|
|
994
|
+
* notation for numbers of 1E+11 and above, and below 1E-04 `[to verify]`.
|
|
995
|
+
*/
|
|
996
|
+
function general(v, loc, width) {
|
|
997
|
+
if (!Number.isFinite(v)) return "#NUM!";
|
|
998
|
+
if (v === 0) return "0";
|
|
999
|
+
const neg = v < 0;
|
|
1000
|
+
const d = decimalOf(v);
|
|
1001
|
+
const e10 = d.point - 1;
|
|
1002
|
+
const room = width - (neg ? 1 : 0);
|
|
1003
|
+
const sign = neg ? "-" : "";
|
|
1004
|
+
if (e10 >= -4 && e10 < room) {
|
|
1005
|
+
const intLen = Math.max(1, d.point);
|
|
1006
|
+
const r = roundDec(d, Math.max(0, room - intLen - 1));
|
|
1007
|
+
if ((r.int || "0").length <= room) {
|
|
1008
|
+
const frac = r.frac.replace(/0+$/, "");
|
|
1009
|
+
return sign + (r.int || "0") + (frac ? loc.decimal + frac : "");
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
let exp = e10;
|
|
1013
|
+
let expText = (exp < 0 ? "-" : "+") + String(Math.abs(exp)).padStart(2, "0");
|
|
1014
|
+
let decimals = Math.max(0, room - 2 - 1 - expText.length);
|
|
1015
|
+
let r = roundDec(shift(d, -exp), decimals);
|
|
1016
|
+
if (r.int.length > 1) {
|
|
1017
|
+
exp += 1;
|
|
1018
|
+
expText = (exp < 0 ? "-" : "+") + String(Math.abs(exp)).padStart(2, "0");
|
|
1019
|
+
decimals = Math.max(0, room - 2 - 1 - expText.length);
|
|
1020
|
+
r = roundDec(shift(d, -exp), decimals);
|
|
1021
|
+
}
|
|
1022
|
+
const frac = r.frac.replace(/0+$/, "");
|
|
1023
|
+
return `${sign}${r.int || "0"}${frac ? loc.decimal + frac : ""}E${expText}`;
|
|
1024
|
+
}
|
|
1025
|
+
// src/format.ts
|
|
1026
|
+
const BUILTIN_EXTRA = {
|
|
1027
|
+
5: "\"$\"#,##0_);\\(\"$\"#,##0\\)",
|
|
1028
|
+
6: "\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)",
|
|
1029
|
+
7: "\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)",
|
|
1030
|
+
8: "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)",
|
|
1031
|
+
41: "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)",
|
|
1032
|
+
42: "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)",
|
|
1033
|
+
43: "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)",
|
|
1034
|
+
44: "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)"
|
|
1035
|
+
};
|
|
1036
|
+
/** Ids that show the locale's short date: 14, and the East Asian ids 27–31, 36 and 50–58 `[to verify per locale]`. */
|
|
1037
|
+
const SHORT_DATE_IDS = /* @__PURE__ */ new Set([
|
|
1038
|
+
14,
|
|
1039
|
+
27,
|
|
1040
|
+
28,
|
|
1041
|
+
29,
|
|
1042
|
+
30,
|
|
1043
|
+
31,
|
|
1044
|
+
36,
|
|
1045
|
+
50,
|
|
1046
|
+
51,
|
|
1047
|
+
52,
|
|
1048
|
+
53,
|
|
1049
|
+
54,
|
|
1050
|
+
55,
|
|
1051
|
+
56,
|
|
1052
|
+
57,
|
|
1053
|
+
58
|
|
1054
|
+
]);
|
|
1055
|
+
/** Ids 32–35 are times in the East Asian locales that define them; shown as h:mm:ss, as LibreOffice does in en-US `[to verify]`. */
|
|
1056
|
+
const TIME_IDS = /* @__PURE__ */ new Set([
|
|
1057
|
+
32,
|
|
1058
|
+
33,
|
|
1059
|
+
34,
|
|
1060
|
+
35
|
|
1061
|
+
]);
|
|
1062
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
1063
|
+
const GENERAL = compile("General");
|
|
1064
|
+
function compileCached(code) {
|
|
1065
|
+
let c = compiled.get(code);
|
|
1066
|
+
if (!c) {
|
|
1067
|
+
c = compile(code);
|
|
1068
|
+
if (compiled.size >= 1e3) compiled.clear();
|
|
1069
|
+
compiled.set(code, c);
|
|
1070
|
+
}
|
|
1071
|
+
return c;
|
|
1072
|
+
}
|
|
1073
|
+
/** The compiled format for a NumFmt, or null for the locale short date (with or without time). */
|
|
1074
|
+
function resolve(numFmt, loc) {
|
|
1075
|
+
if (numFmt === void 0 || numFmt === 0 || numFmt === "General") return GENERAL;
|
|
1076
|
+
if (typeof numFmt === "number") {
|
|
1077
|
+
if (SHORT_DATE_IDS.has(numFmt)) return { sections: [shortDateSection(loc, false)] };
|
|
1078
|
+
if (numFmt === 22) return { sections: [shortDateSection(loc, true)] };
|
|
1079
|
+
if (TIME_IDS.has(numFmt)) return compileCached("h:mm:ss");
|
|
1080
|
+
const code = require_numfmt.BUILTIN_NUM_FMTS[numFmt] ?? BUILTIN_EXTRA[numFmt];
|
|
1081
|
+
return code === void 0 ? GENERAL : compileCached(code);
|
|
1082
|
+
}
|
|
1083
|
+
return compileCached(numFmt);
|
|
1084
|
+
}
|
|
1085
|
+
const holds = (cond, x) => {
|
|
1086
|
+
switch (cond.op) {
|
|
1087
|
+
case "<": return x < cond.v;
|
|
1088
|
+
case "<=": return x <= cond.v;
|
|
1089
|
+
case ">": return x > cond.v;
|
|
1090
|
+
case ">=": return x >= cond.v;
|
|
1091
|
+
case "=": return x === cond.v;
|
|
1092
|
+
default: return x !== cond.v;
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
/**
|
|
1096
|
+
* Picks the section for a number and whether its minus sign is shown.
|
|
1097
|
+
* Without conditions: one section for all; two: non-negative, negative (shown without its sign);
|
|
1098
|
+
* three: positive, negative, zero. With a condition on the first or second section, the first
|
|
1099
|
+
* section whose condition holds wins, and the next section without a condition takes the rest.
|
|
1100
|
+
* A conditional section that only admits negative numbers ([<0], [<=-1]) drops the sign, like the
|
|
1101
|
+
* implicit negative section; other conditional sections keep it `[to verify against Excel]`.
|
|
1102
|
+
*/
|
|
1103
|
+
function pick(sections, v) {
|
|
1104
|
+
const n = Math.min(sections.length, 3);
|
|
1105
|
+
const [a, b, c] = sections;
|
|
1106
|
+
const signed = (s) => !(s?.cond && (s.cond.op === "<" ? s.cond.v <= 0 : s.cond.op === "<=" ? s.cond.v < 0 : false));
|
|
1107
|
+
if (a?.cond || n >= 2 && b?.cond) {
|
|
1108
|
+
if (!a?.cond || holds(a.cond, v)) return {
|
|
1109
|
+
sec: a,
|
|
1110
|
+
signed: signed(a)
|
|
1111
|
+
};
|
|
1112
|
+
if (n < 2) return {
|
|
1113
|
+
sec: void 0,
|
|
1114
|
+
signed: true
|
|
1115
|
+
};
|
|
1116
|
+
if (!b.cond || holds(b.cond, v)) return {
|
|
1117
|
+
sec: b,
|
|
1118
|
+
signed: signed(b)
|
|
1119
|
+
};
|
|
1120
|
+
return {
|
|
1121
|
+
sec: n >= 3 ? c : void 0,
|
|
1122
|
+
signed: true
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
1125
|
+
if (n >= 3 && v === 0) return {
|
|
1126
|
+
sec: c,
|
|
1127
|
+
signed: false
|
|
1128
|
+
};
|
|
1129
|
+
if (n >= 2 && v < 0) return {
|
|
1130
|
+
sec: b,
|
|
1131
|
+
signed: false
|
|
1132
|
+
};
|
|
1133
|
+
return {
|
|
1134
|
+
sec: a,
|
|
1135
|
+
signed: true
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
function colorOf(sec, opts) {
|
|
1139
|
+
const c = sec?.color;
|
|
1140
|
+
if (!c) return void 0;
|
|
1141
|
+
return "name" in c ? NAMED_COLORS[c.name] : paletteColor(c.index, opts?.indexedColors);
|
|
1142
|
+
}
|
|
1143
|
+
function done(text, align, sec, opts, fill) {
|
|
1144
|
+
const out = {
|
|
1145
|
+
text,
|
|
1146
|
+
align
|
|
1147
|
+
};
|
|
1148
|
+
const color = colorOf(sec, opts);
|
|
1149
|
+
if (color) out.color = color;
|
|
1150
|
+
if (fill) out.fill = fill;
|
|
1151
|
+
return out;
|
|
1152
|
+
}
|
|
1153
|
+
function formatNumberValue(v, fmt, loc, opts) {
|
|
1154
|
+
const width = opts?.generalWidth ?? 11;
|
|
1155
|
+
if (!Number.isFinite(v)) return done("#NUM!", "center");
|
|
1156
|
+
const { sec, signed } = pick(fmt.sections, v);
|
|
1157
|
+
if (!sec) return done(general(v, loc, width), "right");
|
|
1158
|
+
const minus = signed && v < 0 ? "-" : "";
|
|
1159
|
+
const abs = Math.abs(v);
|
|
1160
|
+
switch (sec.kind) {
|
|
1161
|
+
case "date": {
|
|
1162
|
+
const r = formatDate(sec, signed ? v : abs, opts?.date1904 === true, loc);
|
|
1163
|
+
if (r === null) return done("", "right", void 0, void 0, {
|
|
1164
|
+
char: "#",
|
|
1165
|
+
at: 0
|
|
1166
|
+
});
|
|
1167
|
+
return done(r.text, "right", sec, opts, r.fill);
|
|
1168
|
+
}
|
|
1169
|
+
case "general":
|
|
1170
|
+
case "number": {
|
|
1171
|
+
const r = formatNumber(sec, abs, loc, width);
|
|
1172
|
+
const showMinus = minus && /[1-9]/.test(r.text);
|
|
1173
|
+
return done(showMinus ? `-${r.text}` : r.text, "right", sec, opts, r.fill ? {
|
|
1174
|
+
char: r.fill.char,
|
|
1175
|
+
at: r.fill.at + (showMinus ? 1 : 0)
|
|
1176
|
+
} : void 0);
|
|
1177
|
+
}
|
|
1178
|
+
case "text": return done(general(v, loc, width), "right", sec, opts);
|
|
1179
|
+
default: {
|
|
1180
|
+
const r = formatNumber(sec, abs, loc, width);
|
|
1181
|
+
return done(minus + r.text, "right", sec, opts, r.fill ? {
|
|
1182
|
+
char: r.fill.char,
|
|
1183
|
+
at: r.fill.at + minus.length
|
|
1184
|
+
} : void 0);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
function formatText(s, fmt, opts, loc) {
|
|
1189
|
+
const sections = fmt.sections;
|
|
1190
|
+
const sec = sections.length === 4 ? sections[3] : sections.find((x) => x.kind === "text");
|
|
1191
|
+
if (!sec) return done(s, "left");
|
|
1192
|
+
let text = "";
|
|
1193
|
+
let fill;
|
|
1194
|
+
for (const t of sec.toks) if (t.t === "text") text += s;
|
|
1195
|
+
else if (t.t === "lit") text += t.s;
|
|
1196
|
+
else if (t.t === "space") text += " ";
|
|
1197
|
+
else if (t.t === "fill") fill ??= {
|
|
1198
|
+
char: t.c,
|
|
1199
|
+
at: text.length
|
|
1200
|
+
};
|
|
1201
|
+
else if (t.t === "point") text += loc.decimal;
|
|
1202
|
+
else if (t.t === "digit") text += t.c;
|
|
1203
|
+
return done(text, "left", sec, opts, fill);
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Formats a value with a number format, as Excel shows it in a cell.
|
|
1207
|
+
*
|
|
1208
|
+
* `numFmt` is a built-in id or a format code (§3.5.5); undefined is General.
|
|
1209
|
+
* Numbers are serials for date formats. A Date is converted with
|
|
1210
|
+
* `dateToSerial` (`dateZone`, `date1904`). Text uses the format's text
|
|
1211
|
+
* section; booleans show TRUE/FALSE and errors as written, whatever the format.
|
|
1212
|
+
*/
|
|
1213
|
+
function formatValue(value, numFmt, options) {
|
|
1214
|
+
const loc = localeData(options?.locale);
|
|
1215
|
+
if (value === null || value === void 0) return {
|
|
1216
|
+
text: "",
|
|
1217
|
+
align: "left"
|
|
1218
|
+
};
|
|
1219
|
+
if (typeof value === "boolean") return {
|
|
1220
|
+
text: value ? "TRUE" : "FALSE",
|
|
1221
|
+
align: "center"
|
|
1222
|
+
};
|
|
1223
|
+
if (value instanceof Date) {
|
|
1224
|
+
let serial;
|
|
1225
|
+
try {
|
|
1226
|
+
serial = require_dates.dateToSerial(value, {
|
|
1227
|
+
date1904: options?.date1904,
|
|
1228
|
+
dateZone: options?.dateZone
|
|
1229
|
+
});
|
|
1230
|
+
} catch {
|
|
1231
|
+
return {
|
|
1232
|
+
text: "",
|
|
1233
|
+
align: "right",
|
|
1234
|
+
fill: {
|
|
1235
|
+
char: "#",
|
|
1236
|
+
at: 0
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
return formatNumberValue(serial, resolve(numFmt, loc), loc, options);
|
|
1241
|
+
}
|
|
1242
|
+
if (typeof value === "number") return formatNumberValue(value === 0 ? 0 : value, resolve(numFmt, loc), loc, options);
|
|
1243
|
+
if (typeof value === "string") return formatText(value, resolve(numFmt, loc), options, loc);
|
|
1244
|
+
if ("error" in value) return {
|
|
1245
|
+
text: value.error,
|
|
1246
|
+
align: "center"
|
|
1247
|
+
};
|
|
1248
|
+
return formatText(require_cells.cellText(value), resolve(numFmt, loc), options, loc);
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Formats a cell (§3.4) with its own number format (`cell.style.numFmt`).
|
|
1252
|
+
* A formula cell shows its cached value; without one the text is empty.
|
|
1253
|
+
*/
|
|
1254
|
+
function formatCell(cell, options) {
|
|
1255
|
+
if (cell === null || cell === void 0) return {
|
|
1256
|
+
text: "",
|
|
1257
|
+
align: "left"
|
|
1258
|
+
};
|
|
1259
|
+
if (typeof cell !== "object" || cell instanceof Date || "error" in cell || "richText" in cell) return formatValue(cell, void 0, options);
|
|
1260
|
+
const c = cell;
|
|
1261
|
+
return formatValue(c.value, c.style?.numFmt, options);
|
|
1262
|
+
}
|
|
1263
|
+
exports.formatCell = formatCell;
|
|
1264
|
+
exports.formatValue = formatValue;
|