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/render.js
ADDED
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
import { a as decodeRange, n as MAX_ROWS, r as decodeCell, t as MAX_COLS } from "./address-DUn7-5OS.js";
|
|
2
|
+
import { t as cellText } from "./cells-CrMF8h_t.js";
|
|
3
|
+
import { t as resolveColor } from "./colors-B8Nf_xTx.js";
|
|
4
|
+
import { t as dateToSerial } from "./dates-BXcSWf9_.js";
|
|
5
|
+
// src/render.ts
|
|
6
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
const BUCKET = 64;
|
|
8
|
+
const CELL_CACHE = 2e4;
|
|
9
|
+
const key = (r, c) => r * MAX_COLS + c;
|
|
10
|
+
function state(s) {
|
|
11
|
+
const st = states.get(s);
|
|
12
|
+
if (!st) throw new TypeError("Not a RenderSheet: create one with createRenderSheet().");
|
|
13
|
+
return st;
|
|
14
|
+
}
|
|
15
|
+
const asCell = (c) => {
|
|
16
|
+
if (c === void 0 || c === null) return void 0;
|
|
17
|
+
if (typeof c !== "object" || c instanceof Date || "error" in c || "richText" in c) return { value: c };
|
|
18
|
+
return c;
|
|
19
|
+
};
|
|
20
|
+
function charsToPx(chars, mdw) {
|
|
21
|
+
if (!(chars > 0)) return 0;
|
|
22
|
+
const fileWidth = Math.trunc((chars * mdw + 5) / mdw * 256) / 256;
|
|
23
|
+
return Math.trunc((256 * fileWidth + Math.trunc(128 / mdw)) / 256 * mdw);
|
|
24
|
+
}
|
|
25
|
+
/** Creates the render model of one sheet. Reader output (`Workbook<Cell>`) and built workbooks with bare values both work. */
|
|
26
|
+
function createRenderSheet(workbook, sheet, options = {}) {
|
|
27
|
+
const sh = workbook.sheets[sheet];
|
|
28
|
+
if (!sh) throw new RangeError(`Sheet index ${sheet} does not exist.`);
|
|
29
|
+
const mdw = options.maxDigitWidth ?? 7;
|
|
30
|
+
let usedRows = 0;
|
|
31
|
+
let usedCols = 0;
|
|
32
|
+
let dim;
|
|
33
|
+
try {
|
|
34
|
+
dim = sh.dimension ? decodeRange(sh.dimension) : void 0;
|
|
35
|
+
} catch {
|
|
36
|
+
dim = void 0;
|
|
37
|
+
}
|
|
38
|
+
if (dim) {
|
|
39
|
+
usedRows = dim.end.row + 1;
|
|
40
|
+
usedCols = dim.end.col + 1;
|
|
41
|
+
} else {
|
|
42
|
+
usedRows = sh.rows.length;
|
|
43
|
+
for (const row of sh.rows) if (row && row.length > usedCols) usedCols = row.length;
|
|
44
|
+
}
|
|
45
|
+
const merges = [];
|
|
46
|
+
for (const ref of sh.merges ?? []) try {
|
|
47
|
+
const m = decodeRange(ref);
|
|
48
|
+
merges.push({
|
|
49
|
+
r0: m.start.row,
|
|
50
|
+
c0: m.start.col,
|
|
51
|
+
r1: m.end.row,
|
|
52
|
+
c1: m.end.col
|
|
53
|
+
});
|
|
54
|
+
usedRows = Math.max(usedRows, m.end.row + 1);
|
|
55
|
+
usedCols = Math.max(usedCols, m.end.col + 1);
|
|
56
|
+
} catch {}
|
|
57
|
+
const freeze = sh.view?.freeze;
|
|
58
|
+
const rows = Math.min(MAX_ROWS, Math.max(usedRows, freeze?.rows ?? 0) + Math.max(0, options.margin?.rows ?? 50));
|
|
59
|
+
const cols = Math.min(MAX_COLS, Math.max(usedCols, freeze?.cols ?? 0) + Math.max(0, options.margin?.cols ?? 10));
|
|
60
|
+
const defaultRowPx = (sh.defaultRowHeight ?? 15) * 96 / 72;
|
|
61
|
+
const rowTops = new Float64Array(rows + 1);
|
|
62
|
+
for (let r = 0; r < rows; r++) {
|
|
63
|
+
const p = sh.rowProps?.[r];
|
|
64
|
+
const h = p?.hidden ? 0 : p?.height !== void 0 ? p.height * 96 / 72 : defaultRowPx;
|
|
65
|
+
rowTops[r + 1] = rowTops[r] + Math.max(0, h);
|
|
66
|
+
}
|
|
67
|
+
const defaultColPx = charsToPx(sh.defaultColWidth ?? 8.43, mdw);
|
|
68
|
+
const colLefts = new Float64Array(cols + 1);
|
|
69
|
+
for (let c = 0; c < cols; c++) {
|
|
70
|
+
const p = sh.colProps?.[c];
|
|
71
|
+
let w = defaultColPx;
|
|
72
|
+
if (p?.hidden) w = 0;
|
|
73
|
+
else if (typeof p?.width === "number") w = charsToPx(p.width, mdw);
|
|
74
|
+
else if (p?.width && typeof p.width === "object") w = "wpx" in p.width ? Math.max(0, p.width.wpx) : charsToPx(p.width.wch, mdw);
|
|
75
|
+
colLefts[c + 1] = colLefts[c] + w;
|
|
76
|
+
}
|
|
77
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
78
|
+
merges.forEach((m, i) => {
|
|
79
|
+
for (let b = Math.floor(m.r0 / BUCKET); b <= Math.floor(m.r1 / BUCKET); b++) {
|
|
80
|
+
const list = buckets.get(b);
|
|
81
|
+
if (list) list.push(i);
|
|
82
|
+
else buckets.set(b, [i]);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
const handle = Object.freeze({
|
|
86
|
+
sheet,
|
|
87
|
+
name: sh.name
|
|
88
|
+
});
|
|
89
|
+
const st = {
|
|
90
|
+
wb: workbook,
|
|
91
|
+
sh,
|
|
92
|
+
rows,
|
|
93
|
+
cols,
|
|
94
|
+
usedRows,
|
|
95
|
+
usedCols,
|
|
96
|
+
rowTops,
|
|
97
|
+
colLefts,
|
|
98
|
+
merges,
|
|
99
|
+
buckets,
|
|
100
|
+
format: options.format,
|
|
101
|
+
locale: options.locale ?? "en-US",
|
|
102
|
+
schemes: new Set((options.hyperlinkSchemes ?? [
|
|
103
|
+
"http",
|
|
104
|
+
"https",
|
|
105
|
+
"mailto"
|
|
106
|
+
]).map((x) => x.toLowerCase())),
|
|
107
|
+
defaultFont: workbook.defaultFont ?? {},
|
|
108
|
+
styles: /* @__PURE__ */ new WeakMap(),
|
|
109
|
+
plain: void 0,
|
|
110
|
+
cells: /* @__PURE__ */ new Map()
|
|
111
|
+
};
|
|
112
|
+
st.plain = buildStyle(st, void 0);
|
|
113
|
+
states.set(handle, st);
|
|
114
|
+
return handle;
|
|
115
|
+
}
|
|
116
|
+
function sheetExtent(s) {
|
|
117
|
+
const st = state(s);
|
|
118
|
+
return {
|
|
119
|
+
rows: st.rows,
|
|
120
|
+
cols: st.cols
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function sheetSettings(s) {
|
|
124
|
+
const st = state(s);
|
|
125
|
+
const v = st.sh.view;
|
|
126
|
+
const cell = (ref) => {
|
|
127
|
+
try {
|
|
128
|
+
return ref ? decodeCell(ref) : void 0;
|
|
129
|
+
} catch {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
let selection = null;
|
|
134
|
+
try {
|
|
135
|
+
selection = v?.selection ? decodeRange(v.selection) : null;
|
|
136
|
+
} catch {
|
|
137
|
+
selection = null;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
freeze: {
|
|
141
|
+
rows: v?.freeze?.rows ?? 0,
|
|
142
|
+
cols: v?.freeze?.cols ?? 0
|
|
143
|
+
},
|
|
144
|
+
rightToLeft: v?.rightToLeft === true,
|
|
145
|
+
showGridLines: v?.showGridLines !== false,
|
|
146
|
+
showHeaders: v?.showRowColHeaders !== false,
|
|
147
|
+
zoom: v?.zoom ?? 100,
|
|
148
|
+
activeCell: cell(v?.activeCell) ?? {
|
|
149
|
+
row: 0,
|
|
150
|
+
col: 0
|
|
151
|
+
},
|
|
152
|
+
selection,
|
|
153
|
+
tabColor: st.sh.tabColor ? hex(st, st.sh.tabColor) : null
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const clampTo = (i, n) => Math.max(0, Math.min(n, Math.trunc(i)));
|
|
157
|
+
function rowTop(s, row) {
|
|
158
|
+
const st = state(s);
|
|
159
|
+
return st.rowTops[clampTo(row, st.rows)];
|
|
160
|
+
}
|
|
161
|
+
function rowHeight(s, row) {
|
|
162
|
+
const st = state(s);
|
|
163
|
+
const r = clampTo(row, st.rows - 1);
|
|
164
|
+
return st.rowTops[r + 1] - st.rowTops[r];
|
|
165
|
+
}
|
|
166
|
+
function colLeft(s, col) {
|
|
167
|
+
const st = state(s);
|
|
168
|
+
return st.colLefts[clampTo(col, st.cols)];
|
|
169
|
+
}
|
|
170
|
+
function colWidth(s, col) {
|
|
171
|
+
const st = state(s);
|
|
172
|
+
const c = clampTo(col, st.cols - 1);
|
|
173
|
+
return st.colLefts[c + 1] - st.colLefts[c];
|
|
174
|
+
}
|
|
175
|
+
function isRowHidden(s, row) {
|
|
176
|
+
return rowHeight(s, row) === 0;
|
|
177
|
+
}
|
|
178
|
+
function isColHidden(s, col) {
|
|
179
|
+
return colWidth(s, col) === 0;
|
|
180
|
+
}
|
|
181
|
+
/** The last index i with offsets[i] <= pos whose size is > 0; the first visible one when pos is before 0. */
|
|
182
|
+
function at(offsets, n, pos) {
|
|
183
|
+
let lo = 0;
|
|
184
|
+
let hi = n;
|
|
185
|
+
while (lo < hi) {
|
|
186
|
+
const mid = lo + hi + 1 >> 1;
|
|
187
|
+
if (offsets[mid] <= pos) lo = mid;
|
|
188
|
+
else hi = mid - 1;
|
|
189
|
+
}
|
|
190
|
+
let i = Math.min(lo, n - 1);
|
|
191
|
+
while (i > 0 && offsets[i + 1] - offsets[i] === 0) i--;
|
|
192
|
+
while (i < n - 1 && offsets[i + 1] - offsets[i] === 0) i++;
|
|
193
|
+
return i;
|
|
194
|
+
}
|
|
195
|
+
function rowAt(s, y) {
|
|
196
|
+
const st = state(s);
|
|
197
|
+
return at(st.rowTops, st.rows, y);
|
|
198
|
+
}
|
|
199
|
+
function colAt(s, x) {
|
|
200
|
+
const st = state(s);
|
|
201
|
+
return at(st.colLefts, st.cols, x);
|
|
202
|
+
}
|
|
203
|
+
function mergeIndex(st, r, c) {
|
|
204
|
+
const list = st.buckets.get(Math.floor(r / BUCKET));
|
|
205
|
+
if (!list) return void 0;
|
|
206
|
+
for (const i of list) {
|
|
207
|
+
const m = st.merges[i];
|
|
208
|
+
if (r >= m.r0 && r <= m.r1 && c >= m.c0 && c <= m.c1) return m;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function mergeAt(s, row, col) {
|
|
212
|
+
const m = mergeIndex(state(s), row, col);
|
|
213
|
+
return m ? {
|
|
214
|
+
anchor: {
|
|
215
|
+
row: m.r0,
|
|
216
|
+
col: m.c0
|
|
217
|
+
},
|
|
218
|
+
range: {
|
|
219
|
+
start: {
|
|
220
|
+
row: m.r0,
|
|
221
|
+
col: m.c0
|
|
222
|
+
},
|
|
223
|
+
end: {
|
|
224
|
+
row: m.r1,
|
|
225
|
+
col: m.c1
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} : null;
|
|
229
|
+
}
|
|
230
|
+
const hex = (st, c) => `#${resolveColor(c, {
|
|
231
|
+
theme: st.wb.theme,
|
|
232
|
+
indexedColors: st.wb.indexedColors
|
|
233
|
+
}).slice(2)}`;
|
|
234
|
+
const isAuto = (c) => typeof c === "object" && c !== null && "auto" in c;
|
|
235
|
+
function buildFont(st, f) {
|
|
236
|
+
const font = {
|
|
237
|
+
...st.defaultFont,
|
|
238
|
+
...f
|
|
239
|
+
};
|
|
240
|
+
const u = font.underline;
|
|
241
|
+
return {
|
|
242
|
+
family: font.name ?? "Calibri",
|
|
243
|
+
sizePt: font.size ?? 11,
|
|
244
|
+
color: font.color && !isAuto(font.color) ? hex(st, font.color) : "#000000",
|
|
245
|
+
bold: font.bold === true,
|
|
246
|
+
italic: font.italic === true,
|
|
247
|
+
underline: u === true ? "single" : u === false || u === void 0 ? "none" : u,
|
|
248
|
+
strike: font.strike === true,
|
|
249
|
+
vertAlign: font.vertAlign ?? "baseline"
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
function side(st, b) {
|
|
253
|
+
if (!b || b.style === "none") return void 0;
|
|
254
|
+
return {
|
|
255
|
+
style: b.style,
|
|
256
|
+
color: b.color && !isAuto(b.color) ? hex(st, b.color) : "#000000"
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function buildStyle(st, style) {
|
|
260
|
+
let background = { kind: "none" };
|
|
261
|
+
const fill = style?.fill;
|
|
262
|
+
if (typeof fill === "string") background = {
|
|
263
|
+
kind: "solid",
|
|
264
|
+
color: hex(st, fill)
|
|
265
|
+
};
|
|
266
|
+
else if (fill && fill.type === "gradient") background = {
|
|
267
|
+
kind: "gradient",
|
|
268
|
+
gradient: fill.gradient,
|
|
269
|
+
degree: fill.degree ?? 0,
|
|
270
|
+
stops: fill.stops.map((x) => ({
|
|
271
|
+
position: x.position,
|
|
272
|
+
color: hex(st, x.color)
|
|
273
|
+
}))
|
|
274
|
+
};
|
|
275
|
+
else if (fill && "pattern" in fill && fill.pattern !== "none") {
|
|
276
|
+
const fg = fill.fgColor && !isAuto(fill.fgColor) ? hex(st, fill.fgColor) : "#000000";
|
|
277
|
+
background = fill.pattern === "solid" ? {
|
|
278
|
+
kind: "solid",
|
|
279
|
+
color: fg
|
|
280
|
+
} : {
|
|
281
|
+
kind: "pattern",
|
|
282
|
+
pattern: fill.pattern,
|
|
283
|
+
fg,
|
|
284
|
+
bg: fill.bgColor && !isAuto(fill.bgColor) ? hex(st, fill.bgColor) : "#FFFFFF"
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
const b = style?.border;
|
|
288
|
+
const diagonal = side(st, b?.diagonal);
|
|
289
|
+
const a = style?.alignment;
|
|
290
|
+
const out = {
|
|
291
|
+
font: buildFont(st, style?.font),
|
|
292
|
+
background,
|
|
293
|
+
border: {
|
|
294
|
+
top: side(st, b?.top),
|
|
295
|
+
right: side(st, b?.right),
|
|
296
|
+
bottom: side(st, b?.bottom),
|
|
297
|
+
left: side(st, b?.left),
|
|
298
|
+
diagonalUp: b?.diagonalUp ? diagonal : void 0,
|
|
299
|
+
diagonalDown: b?.diagonalDown ? diagonal : void 0
|
|
300
|
+
},
|
|
301
|
+
align: {
|
|
302
|
+
horizontal: a?.horizontal ?? "general",
|
|
303
|
+
vertical: a?.vertical ?? "bottom",
|
|
304
|
+
wrap: a?.wrapText === true,
|
|
305
|
+
shrink: a?.shrinkToFit === true,
|
|
306
|
+
indent: a?.indent ?? 0,
|
|
307
|
+
rotation: a?.rotation ?? 0,
|
|
308
|
+
direction: a?.readingOrder === "ltr" ? "ltr" : a?.readingOrder === "rtl" ? "rtl" : "auto"
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
return Object.freeze(out);
|
|
312
|
+
}
|
|
313
|
+
function styleOf(st, style) {
|
|
314
|
+
if (!style) return st.plain;
|
|
315
|
+
let r = st.styles.get(style);
|
|
316
|
+
if (!r) {
|
|
317
|
+
r = buildStyle(st, style);
|
|
318
|
+
st.styles.set(style, r);
|
|
319
|
+
}
|
|
320
|
+
return r;
|
|
321
|
+
}
|
|
322
|
+
/** The model style that applies at (r, c): the merge anchor's for covered cells, else the cell's, the row's or the column's. */
|
|
323
|
+
function modelStyle(st, r, c) {
|
|
324
|
+
const m = mergeIndex(st, r, c);
|
|
325
|
+
const [rr, cc] = m ? [m.r0, m.c0] : [r, c];
|
|
326
|
+
return asCell(st.sh.rows[rr]?.[cc])?.style ?? st.sh.rowProps?.[rr]?.style ?? st.sh.colProps?.[cc]?.style;
|
|
327
|
+
}
|
|
328
|
+
const WEIGHT = {
|
|
329
|
+
hair: 1,
|
|
330
|
+
dotted: 2,
|
|
331
|
+
dashDotDot: 3,
|
|
332
|
+
dashDot: 4,
|
|
333
|
+
dashed: 5,
|
|
334
|
+
thin: 6,
|
|
335
|
+
mediumDashDotDot: 7,
|
|
336
|
+
slantDashDot: 8,
|
|
337
|
+
mediumDashDot: 9,
|
|
338
|
+
mediumDashed: 10,
|
|
339
|
+
medium: 11,
|
|
340
|
+
double: 12,
|
|
341
|
+
thick: 13
|
|
342
|
+
};
|
|
343
|
+
/** The edge between `before` (upper/left cell) and `after` (lower/right cell); the later cell wins a tie. */
|
|
344
|
+
function winner(before, after) {
|
|
345
|
+
if (!before) return after;
|
|
346
|
+
if (!after) return before;
|
|
347
|
+
return (WEIGHT[before.style] ?? 0) > (WEIGHT[after.style] ?? 0) ? before : after;
|
|
348
|
+
}
|
|
349
|
+
function prevVisible(offsets, i) {
|
|
350
|
+
for (let j = i - 1; j >= 0; j--) if (offsets[j + 1] - offsets[j] > 0) return j;
|
|
351
|
+
return -1;
|
|
352
|
+
}
|
|
353
|
+
function nextVisible(offsets, n, i) {
|
|
354
|
+
for (let j = i + 1; j < n; j++) if (offsets[j + 1] - offsets[j] > 0) return j;
|
|
355
|
+
return -1;
|
|
356
|
+
}
|
|
357
|
+
function edgesAt(st, r, c, own) {
|
|
358
|
+
const edges = {};
|
|
359
|
+
const up = prevVisible(st.rowTops, r);
|
|
360
|
+
const left = prevVisible(st.colLefts, c);
|
|
361
|
+
const top = up >= 0 ? winner(styleOf(st, modelStyle(st, up, c)).border.bottom, own.border.top) : own.border.top;
|
|
362
|
+
const lft = left >= 0 ? winner(styleOf(st, modelStyle(st, r, left)).border.right, own.border.left) : own.border.left;
|
|
363
|
+
if (top) edges.top = top;
|
|
364
|
+
if (lft) edges.left = lft;
|
|
365
|
+
if (nextVisible(st.colLefts, st.cols, c) < 0 && own.border.right) edges.right = own.border.right;
|
|
366
|
+
if (nextVisible(st.rowTops, st.rows, r) < 0 && own.border.bottom) edges.bottom = own.border.bottom;
|
|
367
|
+
return edges;
|
|
368
|
+
}
|
|
369
|
+
const PREFIXES = [
|
|
370
|
+
"_xlfn._xlws.",
|
|
371
|
+
"_xlfn.",
|
|
372
|
+
"_xlpm.",
|
|
373
|
+
"_xlws."
|
|
374
|
+
];
|
|
375
|
+
/** '=' + formula with Excel's storage prefixes removed outside string literals. */
|
|
376
|
+
function displayFormula(f) {
|
|
377
|
+
let out = "=";
|
|
378
|
+
let inString = false;
|
|
379
|
+
for (let i = 0; i < f.length; i++) {
|
|
380
|
+
const ch = f[i];
|
|
381
|
+
if (ch === "\"") inString = !inString;
|
|
382
|
+
if (!inString && ch === "_") {
|
|
383
|
+
const p = PREFIXES.find((x) => f.slice(i, i + x.length).toLowerCase() === x);
|
|
384
|
+
if (p) {
|
|
385
|
+
i += p.length - 1;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
out += ch;
|
|
390
|
+
}
|
|
391
|
+
return out;
|
|
392
|
+
}
|
|
393
|
+
function typeOf(cell) {
|
|
394
|
+
if (cell.type) return cell.type;
|
|
395
|
+
const v = cell.value;
|
|
396
|
+
if (v === null || v === void 0) return "empty";
|
|
397
|
+
if (typeof v === "number") return "number";
|
|
398
|
+
if (typeof v === "string") return "string";
|
|
399
|
+
if (typeof v === "boolean") return "boolean";
|
|
400
|
+
if (v instanceof Date) return "date";
|
|
401
|
+
return "error" in v ? "error" : "richText";
|
|
402
|
+
}
|
|
403
|
+
const alignFor = (t) => t === "number" || t === "date" ? "right" : t === "boolean" || t === "error" ? "center" : "left";
|
|
404
|
+
const argbToHex = (s) => s && /^[0-9a-f]{8}$/i.test(s) ? `#${s.slice(2).toUpperCase()}` : void 0;
|
|
405
|
+
function sheetRef(st, text) {
|
|
406
|
+
const bang = text.lastIndexOf("!");
|
|
407
|
+
const ref = (bang === -1 ? text : text.slice(bang + 1)).replace(/\$/g, "");
|
|
408
|
+
if (bang === -1) return {
|
|
409
|
+
sheet: st.wb.sheets.indexOf(st.sh),
|
|
410
|
+
ref
|
|
411
|
+
};
|
|
412
|
+
let name = text.slice(0, bang);
|
|
413
|
+
if (name.startsWith("'") && name.endsWith("'")) name = name.slice(1, -1).replace(/''/g, "'");
|
|
414
|
+
const i = st.wb.sheets.findIndex((x) => x.name.toLowerCase() === name.toLowerCase());
|
|
415
|
+
return {
|
|
416
|
+
sheet: i === -1 ? null : i,
|
|
417
|
+
ref
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
function hyperlink(st, h) {
|
|
421
|
+
if (h === void 0) return void 0;
|
|
422
|
+
const tooltip = typeof h === "object" ? h.tooltip : void 0;
|
|
423
|
+
const tip = tooltip === void 0 ? {} : { tooltip };
|
|
424
|
+
if (typeof h === "object" && "location" in h) {
|
|
425
|
+
const loc = h.location.replace(/^#/, "");
|
|
426
|
+
const named = st.wb.definedNames?.find((d) => d.name.toLowerCase() === loc.toLowerCase());
|
|
427
|
+
const { sheet, ref } = sheetRef(st, named ? named.ref : loc);
|
|
428
|
+
let valid = true;
|
|
429
|
+
try {
|
|
430
|
+
decodeRange(ref);
|
|
431
|
+
} catch {
|
|
432
|
+
valid = false;
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
kind: "internal",
|
|
436
|
+
sheet: valid ? sheet : null,
|
|
437
|
+
ref,
|
|
438
|
+
...tip
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
const href = (typeof h === "string" ? h : h.target).trim();
|
|
442
|
+
const scheme = /[\u0000-\u001f\u007f]/.test(href) ? void 0 : /^([a-z][a-z0-9+.-]*):/i.exec(href)?.[1];
|
|
443
|
+
if (!scheme) return {
|
|
444
|
+
kind: "external",
|
|
445
|
+
href,
|
|
446
|
+
allowed: false,
|
|
447
|
+
reason: "malformed",
|
|
448
|
+
...tip
|
|
449
|
+
};
|
|
450
|
+
if (!st.schemes.has(scheme.toLowerCase())) return {
|
|
451
|
+
kind: "external",
|
|
452
|
+
href,
|
|
453
|
+
allowed: false,
|
|
454
|
+
reason: "scheme",
|
|
455
|
+
...tip
|
|
456
|
+
};
|
|
457
|
+
return {
|
|
458
|
+
kind: "external",
|
|
459
|
+
href,
|
|
460
|
+
allowed: true,
|
|
461
|
+
...tip
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function computeCell(st, r, c) {
|
|
465
|
+
const m = mergeIndex(st, r, c);
|
|
466
|
+
if (m && (m.r0 !== r || m.c0 !== c)) return null;
|
|
467
|
+
const raw = st.sh.rows[r]?.[c];
|
|
468
|
+
const cell = asCell(raw);
|
|
469
|
+
const ownStyle = cell?.style ?? st.sh.rowProps?.[r]?.style ?? st.sh.colProps?.[c]?.style;
|
|
470
|
+
const style = styleOf(st, ownStyle);
|
|
471
|
+
const edges = edgesAt(st, r, c, style);
|
|
472
|
+
const hasEdges = edges.top !== void 0 || edges.left !== void 0 || edges.right !== void 0 || edges.bottom !== void 0;
|
|
473
|
+
if (!cell && !ownStyle && !m && !hasEdges) return null;
|
|
474
|
+
const base = cell ?? {};
|
|
475
|
+
const type = typeOf(base);
|
|
476
|
+
const out = {
|
|
477
|
+
text: "",
|
|
478
|
+
value: base.value ?? null,
|
|
479
|
+
type,
|
|
480
|
+
style,
|
|
481
|
+
edges,
|
|
482
|
+
align: alignFor(type)
|
|
483
|
+
};
|
|
484
|
+
if (m) out.merge = {
|
|
485
|
+
rows: m.r1 - m.r0 + 1,
|
|
486
|
+
cols: m.c1 - m.c0 + 1
|
|
487
|
+
};
|
|
488
|
+
if (base.formula !== void 0) {
|
|
489
|
+
out.formula = displayFormula(base.formula);
|
|
490
|
+
if (base.arrayRef) out.arrayFormula = true;
|
|
491
|
+
}
|
|
492
|
+
const noValue = base.value === null || base.value === void 0;
|
|
493
|
+
if (base.formula !== void 0 && noValue) out.noCachedValue = true;
|
|
494
|
+
else if (!noValue) {
|
|
495
|
+
if (st.format) {
|
|
496
|
+
const f = st.format(base, {
|
|
497
|
+
locale: st.locale,
|
|
498
|
+
date1904: st.wb.date1904,
|
|
499
|
+
indexedColors: st.wb.indexedColors
|
|
500
|
+
});
|
|
501
|
+
out.text = f.text;
|
|
502
|
+
if (f.align) out.align = f.align;
|
|
503
|
+
const color = argbToHex(f.color);
|
|
504
|
+
if (color) out.color = color;
|
|
505
|
+
if (f.fill) out.fill = f.fill;
|
|
506
|
+
} else out.text = cellText(base);
|
|
507
|
+
const v = base.value;
|
|
508
|
+
if (v && typeof v === "object" && "richText" in v) out.runs = v.richText.map((run) => ({
|
|
509
|
+
text: run.text,
|
|
510
|
+
font: run.font ? buildFont(st, {
|
|
511
|
+
...ownStyle?.font,
|
|
512
|
+
...run.font
|
|
513
|
+
}) : style.font
|
|
514
|
+
}));
|
|
515
|
+
}
|
|
516
|
+
const link = hyperlink(st, base.hyperlink);
|
|
517
|
+
if (link) out.hyperlink = link;
|
|
518
|
+
return out;
|
|
519
|
+
}
|
|
520
|
+
/** What to draw at (row, col), or null when there is nothing (§5.8.2). Covered merge cells are null. */
|
|
521
|
+
function renderCell(s, row, col) {
|
|
522
|
+
const st = state(s);
|
|
523
|
+
if (!(row >= 0 && row < st.rows && col >= 0 && col < st.cols)) return null;
|
|
524
|
+
const k = key(row, col);
|
|
525
|
+
if (st.cells.has(k)) return st.cells.get(k);
|
|
526
|
+
const out = computeCell(st, row, col);
|
|
527
|
+
if (st.cells.size >= CELL_CACHE) st.cells.clear();
|
|
528
|
+
st.cells.set(k, out);
|
|
529
|
+
return out;
|
|
530
|
+
}
|
|
531
|
+
function anchorOf(st, r, c) {
|
|
532
|
+
const m = mergeIndex(st, r, c);
|
|
533
|
+
return m ? {
|
|
534
|
+
row: m.r0,
|
|
535
|
+
col: m.c0
|
|
536
|
+
} : {
|
|
537
|
+
row: r,
|
|
538
|
+
col: c
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
function isEmpty(st, r, c) {
|
|
542
|
+
const m = mergeIndex(st, r, c);
|
|
543
|
+
const cell = asCell(m ? st.sh.rows[m.r0]?.[m.c0] : st.sh.rows[r]?.[c]);
|
|
544
|
+
return !cell || (cell.value === null || cell.value === void 0) && cell.formula === void 0;
|
|
545
|
+
}
|
|
546
|
+
/** The next visible index from `i` in the direction, skipping the merge `i` sits in; -1 at the edge. */
|
|
547
|
+
function nextIndex(st, from, dir) {
|
|
548
|
+
const m = mergeIndex(st, from.row, from.col);
|
|
549
|
+
const vertical = dir === "up" || dir === "down";
|
|
550
|
+
const offsets = vertical ? st.rowTops : st.colLefts;
|
|
551
|
+
const n = vertical ? st.rows : st.cols;
|
|
552
|
+
if (dir === "down") return nextVisible(offsets, n, m ? m.r1 : from.row);
|
|
553
|
+
if (dir === "up") return prevVisible(offsets, m ? m.r0 : from.row);
|
|
554
|
+
if (dir === "right") return nextVisible(offsets, n, m ? m.c1 : from.col);
|
|
555
|
+
return prevVisible(offsets, m ? m.c0 : from.col);
|
|
556
|
+
}
|
|
557
|
+
const moveTo = (from, dir, i) => dir === "up" || dir === "down" ? {
|
|
558
|
+
row: i,
|
|
559
|
+
col: from.col
|
|
560
|
+
} : {
|
|
561
|
+
row: from.row,
|
|
562
|
+
col: i
|
|
563
|
+
};
|
|
564
|
+
/** Arrow keys: the next visible cell, merge-aware; stays put at the edge. */
|
|
565
|
+
function step(s, from, dir) {
|
|
566
|
+
const st = state(s);
|
|
567
|
+
const start = {
|
|
568
|
+
row: clampTo(from.row, st.rows - 1),
|
|
569
|
+
col: clampTo(from.col, st.cols - 1)
|
|
570
|
+
};
|
|
571
|
+
const i = nextIndex(st, start, dir);
|
|
572
|
+
if (i < 0) return anchorOf(st, start.row, start.col);
|
|
573
|
+
const to = moveTo(start, dir, i);
|
|
574
|
+
return anchorOf(st, to.row, to.col);
|
|
575
|
+
}
|
|
576
|
+
/** Ctrl+arrow: Excel's data-region edge (§5.8.3). */
|
|
577
|
+
function edge(s, from, dir) {
|
|
578
|
+
const st = state(s);
|
|
579
|
+
let cur = {
|
|
580
|
+
row: clampTo(from.row, st.rows - 1),
|
|
581
|
+
col: clampTo(from.col, st.cols - 1)
|
|
582
|
+
};
|
|
583
|
+
let i = nextIndex(st, cur, dir);
|
|
584
|
+
if (i < 0) return anchorOf(st, cur.row, cur.col);
|
|
585
|
+
let next = moveTo(cur, dir, i);
|
|
586
|
+
if (!isEmpty(st, cur.row, cur.col) && !isEmpty(st, next.row, next.col)) {
|
|
587
|
+
while (true) {
|
|
588
|
+
cur = next;
|
|
589
|
+
i = nextIndex(st, cur, dir);
|
|
590
|
+
if (i < 0) break;
|
|
591
|
+
next = moveTo(cur, dir, i);
|
|
592
|
+
if (isEmpty(st, next.row, next.col)) break;
|
|
593
|
+
}
|
|
594
|
+
return anchorOf(st, cur.row, cur.col);
|
|
595
|
+
}
|
|
596
|
+
while (isEmpty(st, next.row, next.col)) {
|
|
597
|
+
i = nextIndex(st, next, dir);
|
|
598
|
+
if (i < 0) break;
|
|
599
|
+
next = moveTo(next, dir, i);
|
|
600
|
+
}
|
|
601
|
+
return anchorOf(st, next.row, next.col);
|
|
602
|
+
}
|
|
603
|
+
/** Ctrl+End: the last row and column of the used range. */
|
|
604
|
+
function lastUsedCell(s) {
|
|
605
|
+
const st = state(s);
|
|
606
|
+
return {
|
|
607
|
+
row: Math.max(0, st.usedRows - 1),
|
|
608
|
+
col: Math.max(0, st.usedCols - 1)
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function clampRange(st, range) {
|
|
612
|
+
const r0 = clampTo(Math.min(range.start.row, range.end.row), st.rows - 1);
|
|
613
|
+
const r1 = clampTo(Math.max(range.start.row, range.end.row), st.rows - 1);
|
|
614
|
+
const c0 = clampTo(Math.min(range.start.col, range.end.col), st.cols - 1);
|
|
615
|
+
const c1 = clampTo(Math.max(range.start.col, range.end.col), st.cols - 1);
|
|
616
|
+
return {
|
|
617
|
+
start: {
|
|
618
|
+
row: r0,
|
|
619
|
+
col: c0
|
|
620
|
+
},
|
|
621
|
+
end: {
|
|
622
|
+
row: r1,
|
|
623
|
+
col: c1
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
const tsvField = (t) => /[\t\r\n"]/.test(t) ? `"${t.replace(/"/g, "\"\"")}"` : t;
|
|
628
|
+
/** text/plain for the clipboard: TAB between cells, CRLF between rows (§5.8.4). */
|
|
629
|
+
function rangeAsTsv(s, range, options) {
|
|
630
|
+
const { start, end } = clampRange(state(s), range);
|
|
631
|
+
const raw = options?.values === "raw";
|
|
632
|
+
const lines = [];
|
|
633
|
+
for (let r = start.row; r <= end.row; r++) {
|
|
634
|
+
const fields = [];
|
|
635
|
+
for (let c = start.col; c <= end.col; c++) {
|
|
636
|
+
const cell = renderCell(s, r, c);
|
|
637
|
+
const text = !cell ? "" : raw ? cell.value === null ? "" : cellText({ value: cell.value }) : cell.text;
|
|
638
|
+
fields.push(tsvField(text));
|
|
639
|
+
}
|
|
640
|
+
lines.push(fields.join(" "));
|
|
641
|
+
}
|
|
642
|
+
return lines.join("\r\n");
|
|
643
|
+
}
|
|
644
|
+
const esc = (t) => t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
645
|
+
const cssFamily = (f) => `"${f.replace(/["\\]/g, "")}"`;
|
|
646
|
+
const borderCss = (b) => `${b.style === "thick" ? 3 : b.style === "medium" || b.style.startsWith("medium") ? 2 : 1}px ${b.style === "double" ? "double" : b.style === "dotted" || b.style === "hair" ? "dotted" : /dash/i.test(b.style) ? "dashed" : "solid"} ${b.color}`;
|
|
647
|
+
/** text/html for the clipboard: an escaped <table> with inline styles (§5.8.4). */
|
|
648
|
+
function rangeAsHtml(s, range) {
|
|
649
|
+
const st = state(s);
|
|
650
|
+
const { start, end } = clampRange(st, range);
|
|
651
|
+
let html = "<table>";
|
|
652
|
+
for (let r = start.row; r <= end.row; r++) {
|
|
653
|
+
html += "<tr>";
|
|
654
|
+
for (let c = start.col; c <= end.col; c++) {
|
|
655
|
+
const m = mergeIndex(st, r, c);
|
|
656
|
+
const covered = m && (m.r0 !== r || m.c0 !== c);
|
|
657
|
+
if (covered && m.r0 >= start.row && m.c0 >= start.col) continue;
|
|
658
|
+
const cell = covered ? null : renderCell(s, r, c);
|
|
659
|
+
if (!cell) {
|
|
660
|
+
html += "<td></td>";
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
const f = cell.style.font;
|
|
664
|
+
const css = [
|
|
665
|
+
`font-family:${cssFamily(f.family)}`,
|
|
666
|
+
`font-size:${f.sizePt}pt`,
|
|
667
|
+
`color:${cell.color ?? f.color}`
|
|
668
|
+
];
|
|
669
|
+
if (f.bold) css.push("font-weight:bold");
|
|
670
|
+
if (f.italic) css.push("font-style:italic");
|
|
671
|
+
if (f.underline !== "none" || f.strike) css.push(`text-decoration:${[f.underline !== "none" ? "underline" : "", f.strike ? "line-through" : ""].filter(Boolean).join(" ")}`);
|
|
672
|
+
if (cell.style.background.kind === "solid") css.push(`background-color:${cell.style.background.color}`);
|
|
673
|
+
const h = cell.style.align.horizontal;
|
|
674
|
+
css.push(`text-align:${h === "general" ? cell.align : h === "centerContinuous" ? "center" : h === "distributed" ? "justify" : h === "fill" ? "left" : h}`);
|
|
675
|
+
css.push(`white-space:${cell.style.align.wrap ? "pre-wrap" : "pre"}`);
|
|
676
|
+
for (const [sideName, b] of Object.entries(cell.style.border)) if (b && !sideName.startsWith("diagonal")) css.push(`border-${sideName}:${borderCss(b)}`);
|
|
677
|
+
let span = "";
|
|
678
|
+
if (m) {
|
|
679
|
+
const rows = Math.min(m.r1, end.row) - r + 1;
|
|
680
|
+
const cols = Math.min(m.c1, end.col) - c + 1;
|
|
681
|
+
if (rows > 1) span += ` rowspan="${rows}"`;
|
|
682
|
+
if (cols > 1) span += ` colspan="${cols}"`;
|
|
683
|
+
}
|
|
684
|
+
html += `<td${span} style="${esc(css.join(";"))}">${esc(cell.text)}</td>`;
|
|
685
|
+
}
|
|
686
|
+
html += "</tr>";
|
|
687
|
+
}
|
|
688
|
+
return `${html}</table>`;
|
|
689
|
+
}
|
|
690
|
+
/** A status-bar summary of the cached values in a range (§5.8.4). */
|
|
691
|
+
function rangeStats(s, range, limit = 1e6) {
|
|
692
|
+
const st = state(s);
|
|
693
|
+
const { start, end } = clampRange(st, range);
|
|
694
|
+
let count = 0;
|
|
695
|
+
let numericCount = 0;
|
|
696
|
+
let sum = 0;
|
|
697
|
+
let scanned = 0;
|
|
698
|
+
let truncated = false;
|
|
699
|
+
outer: for (let r = start.row; r <= end.row; r++) {
|
|
700
|
+
const row = st.sh.rows[r];
|
|
701
|
+
for (let c = start.col; c <= end.col; c++) {
|
|
702
|
+
if (++scanned > limit) {
|
|
703
|
+
truncated = true;
|
|
704
|
+
break outer;
|
|
705
|
+
}
|
|
706
|
+
const v = asCell(row?.[c])?.value;
|
|
707
|
+
if (v === null || v === void 0) continue;
|
|
708
|
+
const m = mergeIndex(st, r, c);
|
|
709
|
+
if (m && (m.r0 !== r || m.c0 !== c)) continue;
|
|
710
|
+
count++;
|
|
711
|
+
let n;
|
|
712
|
+
if (typeof v === "number") n = v;
|
|
713
|
+
else if (v instanceof Date) try {
|
|
714
|
+
n = dateToSerial(v, { date1904: st.wb.date1904 });
|
|
715
|
+
} catch {
|
|
716
|
+
n = void 0;
|
|
717
|
+
}
|
|
718
|
+
if (n !== void 0 && Number.isFinite(n)) {
|
|
719
|
+
numericCount++;
|
|
720
|
+
sum += n;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return {
|
|
725
|
+
count,
|
|
726
|
+
numericCount,
|
|
727
|
+
sum,
|
|
728
|
+
average: numericCount ? sum / numericCount : null,
|
|
729
|
+
truncated
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
export { colAt, colLeft, colWidth, createRenderSheet, edge, isColHidden, isRowHidden, lastUsedCell, mergeAt, rangeAsHtml, rangeAsTsv, rangeStats, renderCell, rowAt, rowHeight, rowTop, sheetExtent, sheetSettings, step };
|