@20baht/spreadsheet 0.1.0 → 0.2.0
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 +9 -5
- package/dist/book.d.ts +10 -2
- package/dist/book.js +2 -1
- package/dist/compute.d.ts +8 -3
- package/dist/compute.js +11 -0
- package/dist/custom-elements.json +153 -1
- package/dist/dates.d.ts +22 -0
- package/dist/dates.js +53 -0
- package/dist/elements/baht-spreadsheet.d.ts +33 -0
- package/dist/elements/baht-spreadsheet.js +196 -6
- package/dist/formula.d.ts +1 -1
- package/dist/formula.js +82 -3
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/parse.js +13 -1
- package/dist/refs.d.ts +43 -0
- package/dist/refs.js +120 -0
- package/dist/session.d.ts +26 -1
- package/dist/session.js +190 -10
- package/dist/spreadsheet.css +57 -2
- package/dist/workbook.d.ts +3 -1
- package/dist/workbook.js +2 -0
- package/package.json +1 -1
|
@@ -23,13 +23,23 @@ export const SPREADSHEET_LABELS = {
|
|
|
23
23
|
alignLeft: 'Align left',
|
|
24
24
|
alignCenter: 'Align centre',
|
|
25
25
|
alignRight: 'Align right',
|
|
26
|
+
borderAll: 'All borders',
|
|
27
|
+
borderOuter: 'Outside borders',
|
|
28
|
+
borderNone: 'No border',
|
|
26
29
|
merge: 'Merge cells',
|
|
27
30
|
unmerge: 'Unmerge cells',
|
|
31
|
+
insertRow: 'Insert row above',
|
|
32
|
+
insertColumn: 'Insert column left',
|
|
33
|
+
removeRow: 'Delete row',
|
|
34
|
+
removeColumn: 'Delete column',
|
|
35
|
+
fillHandle: 'Drag to fill',
|
|
28
36
|
format: 'Number format',
|
|
29
37
|
formatGeneral: 'General',
|
|
30
38
|
formatNumber: '1,234.50',
|
|
31
39
|
formatInteger: '1,235',
|
|
32
40
|
formatPercent: '12.5%',
|
|
41
|
+
formatDate: '2026-12-31',
|
|
42
|
+
formatDateTime: '2026-12-31 14:30',
|
|
33
43
|
undo: 'Undo',
|
|
34
44
|
redo: 'Redo',
|
|
35
45
|
cell: 'Selected cells',
|
|
@@ -55,9 +65,13 @@ const WRITES = new Set([
|
|
|
55
65
|
'clear',
|
|
56
66
|
'paste',
|
|
57
67
|
'style',
|
|
68
|
+
'border',
|
|
58
69
|
'merge',
|
|
59
70
|
'unmerge',
|
|
60
71
|
'width',
|
|
72
|
+
'spread',
|
|
73
|
+
'insert',
|
|
74
|
+
'remove',
|
|
61
75
|
'add-sheet',
|
|
62
76
|
'rename-sheet',
|
|
63
77
|
'remove-sheet',
|
|
@@ -69,6 +83,11 @@ const ROWS_SHOWN = 40;
|
|
|
69
83
|
const OVERSCAN = 8;
|
|
70
84
|
const textOf = (event) => event.target.value;
|
|
71
85
|
const cellOf = (sheet, address) => Object.hasOwn(sheet.cells, address) ? sheet.cells[address] : undefined;
|
|
86
|
+
const holds = (range, at) => range !== undefined &&
|
|
87
|
+
at.row >= range.top &&
|
|
88
|
+
at.row <= range.bottom &&
|
|
89
|
+
at.column >= range.left &&
|
|
90
|
+
at.column <= range.right;
|
|
72
91
|
function inkOn(fill) {
|
|
73
92
|
const [red = 0, green = 0, blue = 0] = [1, 3, 5].map((at) => parseInt(fill.slice(at, at + 2), 16));
|
|
74
93
|
return red * 0.299 + green * 0.587 + blue * 0.114 > 150 ? '#1a2233' : '#ffffff';
|
|
@@ -78,6 +97,41 @@ const ALIGN_PATHS = {
|
|
|
78
97
|
center: 'M2 3h12M4.5 6.5h7M2 10h12M4.5 13.5h7',
|
|
79
98
|
right: 'M2 3h12M7 6.5h7M2 10h12M7 13.5h7',
|
|
80
99
|
};
|
|
100
|
+
const BORDERS = [
|
|
101
|
+
{ sides: 'all', label: 'borderAll', path: 'M2 2h12v12H2zM8 2v12M2 8h12' },
|
|
102
|
+
{ sides: 'outer', label: 'borderOuter', path: 'M2 2h12v12H2z' },
|
|
103
|
+
{
|
|
104
|
+
sides: 'none',
|
|
105
|
+
label: 'borderNone',
|
|
106
|
+
path: 'M2 2h1.5M6 2h1.5M10 2h1.5M14 2v1.5M14 6v1.5M14 10v1.5M14 14h-1.5M10 14H8.5M6 14H4.5M2 14v-1.5M2 10V8.5M2 6V4.5',
|
|
107
|
+
},
|
|
108
|
+
];
|
|
109
|
+
const STRUCTURE = [
|
|
110
|
+
{
|
|
111
|
+
name: 'insert-row',
|
|
112
|
+
label: 'insertRow',
|
|
113
|
+
msg: { type: 'insert', axis: 'row' },
|
|
114
|
+
path: 'M2 3h12M2 13h12M8 5.5v5M5.5 8h5',
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'insert-column',
|
|
118
|
+
label: 'insertColumn',
|
|
119
|
+
msg: { type: 'insert', axis: 'column' },
|
|
120
|
+
path: 'M3 2v12M13 2v12M8 5.5v5M5.5 8h5',
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'remove-row',
|
|
124
|
+
label: 'removeRow',
|
|
125
|
+
msg: { type: 'remove', axis: 'row' },
|
|
126
|
+
path: 'M2 3h12M2 13h12M5.5 8h5',
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: 'remove-column',
|
|
130
|
+
label: 'removeColumn',
|
|
131
|
+
msg: { type: 'remove', axis: 'column' },
|
|
132
|
+
path: 'M3 2v12M13 2v12M5.5 8h5',
|
|
133
|
+
},
|
|
134
|
+
];
|
|
81
135
|
const icon = (path) => html `<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
|
|
82
136
|
<path d=${path} fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
|
|
83
137
|
</svg>`;
|
|
@@ -94,6 +148,8 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
94
148
|
this.failures = new Set();
|
|
95
149
|
this.renaming = null;
|
|
96
150
|
this.resizing = null;
|
|
151
|
+
this.filling = null;
|
|
152
|
+
this.menu = null;
|
|
97
153
|
this.ownValue = false;
|
|
98
154
|
this.stale = false;
|
|
99
155
|
this.dragging = false;
|
|
@@ -172,7 +228,28 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
172
228
|
this.grid()?.focus();
|
|
173
229
|
event.preventDefault();
|
|
174
230
|
};
|
|
231
|
+
this.fillStart = (event) => {
|
|
232
|
+
event.preventDefault();
|
|
233
|
+
event.stopPropagation();
|
|
234
|
+
const from = selectionOf(this.session);
|
|
235
|
+
this.filling = { from, to: from };
|
|
236
|
+
const done = () => {
|
|
237
|
+
window.removeEventListener('pointerup', done);
|
|
238
|
+
const { filling } = this;
|
|
239
|
+
this.filling = null;
|
|
240
|
+
if (filling)
|
|
241
|
+
this.send({ type: 'spread', ...filling });
|
|
242
|
+
this.grid()?.focus();
|
|
243
|
+
};
|
|
244
|
+
window.addEventListener('pointerup', done);
|
|
245
|
+
};
|
|
175
246
|
this.pointerOver = (event) => {
|
|
247
|
+
if (this.filling) {
|
|
248
|
+
const over = this.placeOf(event);
|
|
249
|
+
if (over)
|
|
250
|
+
this.filling = { ...this.filling, to: this.fillTo(this.filling.from, over) };
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
176
253
|
if (!this.dragging || event.buttons === 0) {
|
|
177
254
|
this.dragging = false;
|
|
178
255
|
return;
|
|
@@ -181,6 +258,25 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
181
258
|
if (cell)
|
|
182
259
|
this.send({ type: 'select', cell, extend: true });
|
|
183
260
|
};
|
|
261
|
+
this.contextMenu = (event) => {
|
|
262
|
+
const cell = this.placeOf(event);
|
|
263
|
+
const sheet = shownSheet(this.session);
|
|
264
|
+
if (!cell || !sheet || sheet.source || this.readonly)
|
|
265
|
+
return;
|
|
266
|
+
event.preventDefault();
|
|
267
|
+
const selection = selectionOf(this.session);
|
|
268
|
+
const inside = holds(selection, cell);
|
|
269
|
+
if (!inside)
|
|
270
|
+
this.send({ type: 'select', cell });
|
|
271
|
+
const box = this.getBoundingClientRect();
|
|
272
|
+
this.menu = { x: event.clientX - box.left, y: event.clientY - box.top };
|
|
273
|
+
window.addEventListener('pointerdown', this.menuAway, true);
|
|
274
|
+
};
|
|
275
|
+
this.menuAway = (event) => {
|
|
276
|
+
const inside = event.target instanceof Element && event.target.closest('.baht-spreadsheet-menu');
|
|
277
|
+
if (!inside)
|
|
278
|
+
this.closeMenu();
|
|
279
|
+
};
|
|
184
280
|
this.doubleClick = (event) => {
|
|
185
281
|
if (this.placeOf(event))
|
|
186
282
|
this.send({ type: 'edit' });
|
|
@@ -302,16 +398,71 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
302
398
|
this.toggle('italic');
|
|
303
399
|
else if (key === 'u')
|
|
304
400
|
this.toggle('underline');
|
|
401
|
+
else if (key === 'd' || key === 'r')
|
|
402
|
+
this.fillFromEdge(key === 'd');
|
|
305
403
|
else
|
|
306
404
|
return false;
|
|
307
405
|
return true;
|
|
308
406
|
}
|
|
407
|
+
fillFromEdge(down) {
|
|
408
|
+
const to = selectionOf(this.session);
|
|
409
|
+
const from = down ? { ...to, bottom: to.top } : { ...to, right: to.left };
|
|
410
|
+
this.send({ type: 'spread', from, to });
|
|
411
|
+
}
|
|
309
412
|
placeOf(event) {
|
|
310
413
|
const cell = event.target.closest('td[data-row]');
|
|
311
414
|
if (!cell)
|
|
312
415
|
return null;
|
|
313
416
|
return { row: Number(cell.dataset.row), column: Number(cell.dataset.column) };
|
|
314
417
|
}
|
|
418
|
+
fillTo(from, cell) {
|
|
419
|
+
const rows = Math.max(from.top - cell.row, cell.row - from.bottom, 0);
|
|
420
|
+
const columns = Math.max(from.left - cell.column, cell.column - from.right, 0);
|
|
421
|
+
return rows >= columns
|
|
422
|
+
? { ...from, top: Math.min(from.top, cell.row), bottom: Math.max(from.bottom, cell.row) }
|
|
423
|
+
: {
|
|
424
|
+
...from,
|
|
425
|
+
left: Math.min(from.left, cell.column),
|
|
426
|
+
right: Math.max(from.right, cell.column),
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
closeMenu() {
|
|
430
|
+
this.menu = null;
|
|
431
|
+
window.removeEventListener('pointerdown', this.menuAway, true);
|
|
432
|
+
}
|
|
433
|
+
disconnectedCallback() {
|
|
434
|
+
super.disconnectedCallback();
|
|
435
|
+
this.closeMenu();
|
|
436
|
+
}
|
|
437
|
+
menuBox() {
|
|
438
|
+
if (!this.menu)
|
|
439
|
+
return nothing;
|
|
440
|
+
const words = this.words;
|
|
441
|
+
return html `<div
|
|
442
|
+
class="baht-spreadsheet-menu"
|
|
443
|
+
role="menu"
|
|
444
|
+
style="left:${this.menu.x}px;top:${this.menu.y}px"
|
|
445
|
+
@keydown=${(event) => {
|
|
446
|
+
if (event.key !== 'Escape')
|
|
447
|
+
return;
|
|
448
|
+
this.closeMenu();
|
|
449
|
+
this.grid()?.focus();
|
|
450
|
+
}}
|
|
451
|
+
>
|
|
452
|
+
${STRUCTURE.map((item) => html `<button
|
|
453
|
+
type="button"
|
|
454
|
+
role="menuitem"
|
|
455
|
+
data-menu=${item.name}
|
|
456
|
+
@click=${() => {
|
|
457
|
+
this.closeMenu();
|
|
458
|
+
this.send(item.msg);
|
|
459
|
+
this.grid()?.focus();
|
|
460
|
+
}}
|
|
461
|
+
>
|
|
462
|
+
${icon(item.path)} ${words[item.label]}
|
|
463
|
+
</button>`)}
|
|
464
|
+
</div>`;
|
|
465
|
+
}
|
|
315
466
|
resizeStart(event, column, width) {
|
|
316
467
|
event.preventDefault();
|
|
317
468
|
event.stopPropagation();
|
|
@@ -344,7 +495,7 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
344
495
|
const sheet = shownSheet(this.session);
|
|
345
496
|
return html `<div class="baht-spreadsheet" ?data-readonly=${this.readonly}>
|
|
346
497
|
${sheet ? [this.tools(sheet), this.bar(sheet), this.notice(sheet), this.table(sheet)] : nothing}
|
|
347
|
-
${this.tabs()}
|
|
498
|
+
${this.tabs()} ${this.menuBox()}
|
|
348
499
|
</div>`;
|
|
349
500
|
}
|
|
350
501
|
tool(label, content, click, options) {
|
|
@@ -407,9 +558,17 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
407
558
|
${align('left', words.alignLeft)} ${align('center', words.alignCenter)}
|
|
408
559
|
${align('right', words.alignRight)}
|
|
409
560
|
<span class="baht-spreadsheet-gap"></span>
|
|
561
|
+
${BORDERS.map((item) => this.tool(words[item.label], icon(item.path), () => {
|
|
562
|
+
this.send({ type: 'border', sides: item.sides });
|
|
563
|
+
}, { name: `border-${item.sides}`, disabled: locked }))}
|
|
410
564
|
${this.tool(isMerged ? words.unmerge : words.merge, icon('M2 3h12v10H2zM5 8h6M5 8l2-2M5 8l2 2M11 8 9 6M11 8l-2 2'), () => {
|
|
411
565
|
this.send({ type: isMerged ? 'unmerge' : 'merge' });
|
|
412
566
|
}, { name: 'merge', pressed: isMerged, disabled: locked || (one && !isMerged) })}
|
|
567
|
+
<span class="baht-spreadsheet-gap"></span>
|
|
568
|
+
${STRUCTURE.map((item) => this.tool(words[item.label], icon(item.path), () => {
|
|
569
|
+
this.send(item.msg);
|
|
570
|
+
}, { name: item.name, disabled: locked }))}
|
|
571
|
+
<span class="baht-spreadsheet-gap"></span>
|
|
413
572
|
<select
|
|
414
573
|
class="baht-spreadsheet-format"
|
|
415
574
|
aria-label=${words.format}
|
|
@@ -433,6 +592,8 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
433
592
|
number: words.formatNumber,
|
|
434
593
|
integer: words.formatInteger,
|
|
435
594
|
percent: words.formatPercent,
|
|
595
|
+
date: words.formatDate,
|
|
596
|
+
datetime: words.formatDateTime,
|
|
436
597
|
};
|
|
437
598
|
return labels[format];
|
|
438
599
|
}
|
|
@@ -515,6 +676,7 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
515
676
|
@pointerdown=${this.pointerDown}
|
|
516
677
|
@pointerover=${this.pointerOver}
|
|
517
678
|
@dblclick=${this.doubleClick}
|
|
679
|
+
@contextmenu=${this.contextMenu}
|
|
518
680
|
>
|
|
519
681
|
<colgroup>
|
|
520
682
|
<col class="baht-spreadsheet-corner" />
|
|
@@ -575,17 +737,37 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
575
737
|
})}
|
|
576
738
|
</tr>`;
|
|
577
739
|
}
|
|
740
|
+
lines(sheet, at, sides = []) {
|
|
741
|
+
const has = (row, column, side) => cellOf(sheet, addressOf({ row, column }))?.style?.border?.includes(side) === true;
|
|
742
|
+
return sides.map((side) => {
|
|
743
|
+
if (side === 'bottom' || side === 'right')
|
|
744
|
+
return `border-${side}-color:var(--baht-spreadsheet-line)`;
|
|
745
|
+
const drawn = side === 'top'
|
|
746
|
+
? at.row > 0 && has(at.row - 1, at.column, 'bottom')
|
|
747
|
+
: at.column > 0 && has(at.row, at.column - 1, 'right');
|
|
748
|
+
return drawn ? '' : `--baht-spreadsheet-${side}:1px`;
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
handle(sheet) {
|
|
752
|
+
if (this.readonly || sheet.source || this.session.editing !== null)
|
|
753
|
+
return nothing;
|
|
754
|
+
return html `<span
|
|
755
|
+
class="baht-spreadsheet-handle"
|
|
756
|
+
title=${this.words.fillHandle}
|
|
757
|
+
@pointerdown=${this.fillStart}
|
|
758
|
+
></span>`;
|
|
759
|
+
}
|
|
578
760
|
cell(sheet, at, selection, rowspan, colspan) {
|
|
579
761
|
const { session } = this;
|
|
580
762
|
const cell = cellOf(sheet, addressOf(at));
|
|
581
763
|
const style = cell?.style ?? {};
|
|
582
764
|
const value = this.values.at(sheet.key, at.row, at.column);
|
|
583
765
|
const active = session.active.row === at.row && session.active.column === at.column;
|
|
584
|
-
const selected =
|
|
585
|
-
at.row <= selection.bottom &&
|
|
586
|
-
at.column >= selection.left &&
|
|
587
|
-
at.column <= selection.right;
|
|
766
|
+
const selected = holds(selection, at);
|
|
588
767
|
const align = style.align ?? (typeof value === 'number' ? 'right' : undefined);
|
|
768
|
+
const filling = holds(this.filling?.to, at);
|
|
769
|
+
const corner = at.row + rowspan - 1 === selection.bottom && at.column + colspan - 1 === selection.right;
|
|
770
|
+
const handle = corner ? this.handle(sheet) : nothing;
|
|
589
771
|
const css = [
|
|
590
772
|
style.bold === true ? 'font-weight:700' : '',
|
|
591
773
|
style.italic === true ? 'font-style:italic' : '',
|
|
@@ -593,6 +775,7 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
593
775
|
`color:${style.color ?? (style.fill === undefined ? '' : inkOn(style.fill))}`,
|
|
594
776
|
style.fill === undefined ? '' : `background-color:${style.fill}`,
|
|
595
777
|
align === undefined ? '' : `text-align:${align}`,
|
|
778
|
+
...this.lines(sheet, at, style.border),
|
|
596
779
|
]
|
|
597
780
|
.filter((rule) => rule !== '' && rule !== 'color:')
|
|
598
781
|
.join(';');
|
|
@@ -605,6 +788,7 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
605
788
|
?data-selected=${selected}
|
|
606
789
|
?data-active=${active}
|
|
607
790
|
?data-error=${isError(value)}
|
|
791
|
+
?data-filling=${filling}
|
|
608
792
|
aria-selected=${String(selected)}
|
|
609
793
|
style=${css || nothing}
|
|
610
794
|
>
|
|
@@ -621,7 +805,7 @@ let BahtSpreadsheet = class BahtSpreadsheet extends BahtElement {
|
|
|
621
805
|
this.send({ type: 'commit' });
|
|
622
806
|
}}
|
|
623
807
|
/>`
|
|
624
|
-
: displayText(value, style)}
|
|
808
|
+
: displayText(value, style)}${handle}
|
|
625
809
|
</td>`;
|
|
626
810
|
}
|
|
627
811
|
tabs() {
|
|
@@ -706,6 +890,12 @@ __decorate([
|
|
|
706
890
|
__decorate([
|
|
707
891
|
state()
|
|
708
892
|
], BahtSpreadsheet.prototype, "resizing", void 0);
|
|
893
|
+
__decorate([
|
|
894
|
+
state()
|
|
895
|
+
], BahtSpreadsheet.prototype, "filling", void 0);
|
|
896
|
+
__decorate([
|
|
897
|
+
state()
|
|
898
|
+
], BahtSpreadsheet.prototype, "menu", void 0);
|
|
709
899
|
BahtSpreadsheet = __decorate([
|
|
710
900
|
customElement('baht-spreadsheet')
|
|
711
901
|
], BahtSpreadsheet);
|
package/dist/formula.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type CellRange } from './book.js';
|
|
2
|
-
export declare const ERRORS: readonly ["#DIV/0!", "#NAME?", "#REF!", "#VALUE!", "#CYCLE!", "#ERROR!"];
|
|
2
|
+
export declare const ERRORS: readonly ["#DIV/0!", "#NAME?", "#REF!", "#VALUE!", "#N/A", "#CYCLE!", "#ERROR!"];
|
|
3
3
|
export type ErrorCode = (typeof ERRORS)[number];
|
|
4
4
|
/** What a formula could not work out. */
|
|
5
5
|
export interface FormulaError {
|
package/dist/formula.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import { columnIndex, MAX_COLUMNS, MAX_ROWS } from './book.js';
|
|
2
|
-
|
|
2
|
+
import { dateSerial, parseDateText, serialOf, serialParts } from './dates.js';
|
|
3
|
+
export const ERRORS = [
|
|
4
|
+
'#DIV/0!',
|
|
5
|
+
'#NAME?',
|
|
6
|
+
'#REF!',
|
|
7
|
+
'#VALUE!',
|
|
8
|
+
'#N/A',
|
|
9
|
+
'#CYCLE!',
|
|
10
|
+
'#ERROR!',
|
|
11
|
+
];
|
|
3
12
|
export const failed = (error) => ({ error });
|
|
4
13
|
export function isError(value) {
|
|
5
14
|
return typeof value === 'object' && value !== null && 'error' in value;
|
|
6
15
|
}
|
|
16
|
+
const LOST = '#REF!';
|
|
7
17
|
const NUMBER = /^\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/;
|
|
8
18
|
const WORD = /^[$\p{L}_][$\p{L}\p{N}_.]*/u;
|
|
9
19
|
const OPS = [
|
|
@@ -51,6 +61,8 @@ function tokenAt(rest) {
|
|
|
51
61
|
read[1],
|
|
52
62
|
];
|
|
53
63
|
}
|
|
64
|
+
if (rest.startsWith(LOST))
|
|
65
|
+
return [{ type: 'lost' }, LOST.length];
|
|
54
66
|
const number = NUMBER.exec(rest);
|
|
55
67
|
if (number)
|
|
56
68
|
return [{ type: 'number', value: Number(number[0]) }, number[0].length];
|
|
@@ -136,6 +148,8 @@ class Reader {
|
|
|
136
148
|
this.#at++;
|
|
137
149
|
if (token.type === 'number' || token.type === 'text')
|
|
138
150
|
return { type: 'value', value: token.value };
|
|
151
|
+
if (token.type === 'lost')
|
|
152
|
+
return { type: 'error', error: LOST };
|
|
139
153
|
if (token.type === 'op') {
|
|
140
154
|
if (token.value !== '(')
|
|
141
155
|
throw new Unreadable();
|
|
@@ -259,8 +273,8 @@ function truthOf(value) {
|
|
|
259
273
|
}
|
|
260
274
|
return value !== null && value !== 0 && value !== false;
|
|
261
275
|
}
|
|
276
|
+
const rank = (value) => typeof value === 'boolean' ? 2 : typeof value === 'string' ? 1 : 0;
|
|
262
277
|
function compare(a, b) {
|
|
263
|
-
const rank = (value) => typeof value === 'boolean' ? 2 : typeof value === 'string' ? 1 : 0;
|
|
264
278
|
const left = a ?? (typeof b === 'string' ? '' : 0);
|
|
265
279
|
const right = b ?? (typeof a === 'string' ? '' : 0);
|
|
266
280
|
if (rank(left) !== rank(right))
|
|
@@ -337,7 +351,8 @@ function criterion(value) {
|
|
|
337
351
|
const match = CRITERION.exec(value);
|
|
338
352
|
const op = match?.[1] ?? '=';
|
|
339
353
|
const text = match?.[2] ?? '';
|
|
340
|
-
const
|
|
354
|
+
const plain = text.trim() === '' ? Number.NaN : Number(text);
|
|
355
|
+
const asNumber = Number.isNaN(plain) ? (parseDateText(text)?.serial ?? Number.NaN) : plain;
|
|
341
356
|
const test = COMPARISONS[op];
|
|
342
357
|
if (!Number.isNaN(asNumber))
|
|
343
358
|
return (cell) => {
|
|
@@ -377,6 +392,52 @@ function round(args) {
|
|
|
377
392
|
const scale = 10 ** Math.trunc(places);
|
|
378
393
|
return (Math.sign(value) * Math.round(Math.abs(value) * scale)) / scale;
|
|
379
394
|
}
|
|
395
|
+
function lookedUp(table, wanted, sorted) {
|
|
396
|
+
let found;
|
|
397
|
+
for (const row of table) {
|
|
398
|
+
const first = row[0] ?? null;
|
|
399
|
+
if (first === null || isError(first) || rank(first) !== rank(wanted))
|
|
400
|
+
continue;
|
|
401
|
+
const order = compare(first, wanted);
|
|
402
|
+
if (order === 0 || (sorted && order < 0))
|
|
403
|
+
found = row;
|
|
404
|
+
if (sorted ? order > 0 : order === 0)
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
return found;
|
|
408
|
+
}
|
|
409
|
+
function lookup(args) {
|
|
410
|
+
const wanted = single(args[0]);
|
|
411
|
+
const table = rowsOf(args[1]);
|
|
412
|
+
const column = numberOf(single(args[2]));
|
|
413
|
+
const sorted = args[3] === undefined ? true : truthOf(single(args[3]));
|
|
414
|
+
if (isError(column))
|
|
415
|
+
return column;
|
|
416
|
+
if (isError(sorted))
|
|
417
|
+
return sorted;
|
|
418
|
+
if (column < 1)
|
|
419
|
+
return failed('#VALUE!');
|
|
420
|
+
if (Math.trunc(column) > (table[0]?.length ?? 0))
|
|
421
|
+
return failed('#REF!');
|
|
422
|
+
const found = lookedUp(table, wanted, sorted);
|
|
423
|
+
return found ? (found[Math.trunc(column) - 1] ?? null) : failed('#N/A');
|
|
424
|
+
}
|
|
425
|
+
const partOf = (part) => (args) => {
|
|
426
|
+
const serial = numberOf(single(args[0]));
|
|
427
|
+
return isError(serial) ? serial : serialParts(serial)[part];
|
|
428
|
+
};
|
|
429
|
+
function dateFrom(args) {
|
|
430
|
+
const parts = [0, 1, 2].map((i) => numberOf(single(args[i])));
|
|
431
|
+
const broken = parts.find((part) => isError(part));
|
|
432
|
+
if (broken)
|
|
433
|
+
return broken;
|
|
434
|
+
const [year = 0, month = 0, day = 0] = parts;
|
|
435
|
+
return serialOf(Math.trunc(year), Math.trunc(month), Math.trunc(day)) ?? failed('#VALUE!');
|
|
436
|
+
}
|
|
437
|
+
const clock = (whole) => () => {
|
|
438
|
+
const serial = dateSerial(new Date()) ?? 0;
|
|
439
|
+
return whole ? Math.floor(serial) : serial;
|
|
440
|
+
};
|
|
380
441
|
const truths = (all) => (args) => {
|
|
381
442
|
let result = all;
|
|
382
443
|
for (const value of flat(args)) {
|
|
@@ -400,6 +461,13 @@ export const FUNCTIONS = {
|
|
|
400
461
|
.flat()
|
|
401
462
|
.filter(criterion(single(args[1]))).length,
|
|
402
463
|
SUMIF: sumIf,
|
|
464
|
+
VLOOKUP: lookup,
|
|
465
|
+
DATE: dateFrom,
|
|
466
|
+
TODAY: clock(true),
|
|
467
|
+
NOW: clock(false),
|
|
468
|
+
YEAR: partOf('year'),
|
|
469
|
+
MONTH: partOf('month'),
|
|
470
|
+
DAY: partOf('day'),
|
|
403
471
|
ROUND: round,
|
|
404
472
|
ABS: (args) => {
|
|
405
473
|
const value = numberOf(single(args[0]));
|
|
@@ -441,9 +509,20 @@ function rangeValues(node, scope) {
|
|
|
441
509
|
function argument(node, scope) {
|
|
442
510
|
return node.type === 'range' ? rangeValues(node, scope) : evaluate(node, scope);
|
|
443
511
|
}
|
|
512
|
+
function rescued(node, scope) {
|
|
513
|
+
const [tried, otherwise] = node.args;
|
|
514
|
+
if (!tried)
|
|
515
|
+
return failed('#ERROR!');
|
|
516
|
+
const value = evaluate(tried, scope);
|
|
517
|
+
if (!isError(value))
|
|
518
|
+
return value;
|
|
519
|
+
return otherwise ? evaluate(otherwise, scope) : '';
|
|
520
|
+
}
|
|
444
521
|
function called(node, scope) {
|
|
445
522
|
if (node.name === 'IF')
|
|
446
523
|
return branch(node, scope);
|
|
524
|
+
if (node.name === 'IFERROR')
|
|
525
|
+
return rescued(node, scope);
|
|
447
526
|
const fn = FUNCTIONS[node.name];
|
|
448
527
|
if (!fn)
|
|
449
528
|
return failed('#NAME?');
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
export { addressOf, ALIGNS, columnIndex, columnName, DEFAULT_COLUMNS, DEFAULT_ROWS, DEFAULT_WIDTH, emptyBook, extentOf, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, mergeAt, mergesOf, NUMBER_FORMATS, parseAddress, parseRange, rangeName, rangeOf, sheetOf, type Book, type BookCell, type BookSheet, type BookSource, type CellAlign, type CellPos, type CellRange, type CellStyle, type CellValue, type NumberFormat, } from './book.js';
|
|
1
|
+
export { addressOf, ALIGNS, BORDER_SIDES, columnIndex, columnName, DEFAULT_COLUMNS, DEFAULT_ROWS, DEFAULT_WIDTH, emptyBook, extentOf, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, mergeAt, mergesOf, NUMBER_FORMATS, parseAddress, parseRange, rangeName, rangeOf, sheetOf, type Book, type BookCell, type BookSheet, type BookSource, type BorderSide, type CellAlign, type CellPos, type CellRange, type CellStyle, type CellValue, type NumberFormat, } from './book.js';
|
|
2
2
|
export { ERRORS, evaluate, FUNCTIONS, isError, parseFormula, type ErrorCode, type FormulaError, type FormulaNode, type FormulaScope, type Scalar, } from './formula.js';
|
|
3
|
+
export { dateSerial, dateText, parseDateText, serialOf, serialParts } from './dates.js';
|
|
3
4
|
export { cellInput, computeBook, displayText, inputText, type BookValues } from './compute.js';
|
|
4
5
|
export { parseBook, type BookIssue, type ParsedBook } from './parse.js';
|
|
5
6
|
export { copied, openBook, savedBook, selectionOf, shownSheet, updateBook, type BookDirection, type BookMsg, type BookState, type BookUpdate, type SourceRows, type StylePatch, } from './session.js';
|
|
7
|
+
export { movedIndex, movedRefs, movedSpan, renamedRefs, rewriteRefs, sheetRef, shiftedRefs, type FormulaRef, type Move, type RefEnd, } from './refs.js';
|
|
6
8
|
export { fromTsv, toTsv } from './tsv.js';
|
|
7
9
|
export { bookWorkbook, type WorkbookCell, type WorkbookSheet } from './workbook.js';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
export { addressOf, ALIGNS, columnIndex, columnName, DEFAULT_COLUMNS, DEFAULT_ROWS, DEFAULT_WIDTH, emptyBook, extentOf, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, mergeAt, mergesOf, NUMBER_FORMATS, parseAddress, parseRange, rangeName, rangeOf, sheetOf, } from './book.js';
|
|
1
|
+
export { addressOf, ALIGNS, BORDER_SIDES, columnIndex, columnName, DEFAULT_COLUMNS, DEFAULT_ROWS, DEFAULT_WIDTH, emptyBook, extentOf, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, mergeAt, mergesOf, NUMBER_FORMATS, parseAddress, parseRange, rangeName, rangeOf, sheetOf, } from './book.js';
|
|
2
2
|
export { ERRORS, evaluate, FUNCTIONS, isError, parseFormula, } from './formula.js';
|
|
3
|
+
export { dateSerial, dateText, parseDateText, serialOf, serialParts } from './dates.js';
|
|
3
4
|
export { cellInput, computeBook, displayText, inputText } from './compute.js';
|
|
4
5
|
export { parseBook } from './parse.js';
|
|
5
6
|
export { copied, openBook, savedBook, selectionOf, shownSheet, updateBook, } from './session.js';
|
|
7
|
+
export { movedIndex, movedRefs, movedSpan, renamedRefs, rewriteRefs, sheetRef, shiftedRefs, } from './refs.js';
|
|
6
8
|
export { fromTsv, toTsv } from './tsv.js';
|
|
7
9
|
export { bookWorkbook } from './workbook.js';
|
package/dist/parse.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ALIGNS, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, NUMBER_FORMATS, overlaps, parseAddress, parseRange, } from './book.js';
|
|
1
|
+
import { ALIGNS, BORDER_SIDES, MAX_COLUMNS, MAX_ROWS, MAX_SHEETS, NUMBER_FORMATS, overlaps, parseAddress, parseRange, } from './book.js';
|
|
2
2
|
const COLOR = /^#[0-9a-fA-F]{6}$/;
|
|
3
3
|
const KEY = /^[A-Za-z0-9_-]{1,64}$/;
|
|
4
4
|
const UNSAFE = new Set(['__proto__', 'constructor', 'prototype']);
|
|
@@ -35,6 +35,16 @@ class Gate {
|
|
|
35
35
|
this.fail(path, 'must be true or false');
|
|
36
36
|
return value === true;
|
|
37
37
|
}
|
|
38
|
+
#sides(value, path) {
|
|
39
|
+
if (value === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
if (!Array.isArray(value) || value.some((side) => !BORDER_SIDES.some((s) => s === side))) {
|
|
42
|
+
this.fail(path, `must be a list of ${BORDER_SIDES.join(', ')}`);
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const sides = BORDER_SIDES.filter((side) => value.includes(side));
|
|
46
|
+
return sides.length ? sides : undefined;
|
|
47
|
+
}
|
|
38
48
|
style(value, path) {
|
|
39
49
|
if (value === undefined)
|
|
40
50
|
return undefined;
|
|
@@ -49,6 +59,7 @@ class Gate {
|
|
|
49
59
|
const fill = this.#colour(value.fill, `${path}.fill`);
|
|
50
60
|
const align = this.#oneOf(value.align, ALIGNS, `${path}.align`);
|
|
51
61
|
const format = this.#oneOf(value.format, NUMBER_FORMATS, `${path}.format`);
|
|
62
|
+
const border = this.#sides(value.border, `${path}.border`);
|
|
52
63
|
const style = {
|
|
53
64
|
...(bold ? { bold } : {}),
|
|
54
65
|
...(italic ? { italic } : {}),
|
|
@@ -57,6 +68,7 @@ class Gate {
|
|
|
57
68
|
...(fill === undefined ? {} : { fill }),
|
|
58
69
|
...(align === undefined ? {} : { align }),
|
|
59
70
|
...(format === undefined ? {} : { format }),
|
|
71
|
+
...(border === undefined ? {} : { border }),
|
|
60
72
|
};
|
|
61
73
|
return Object.keys(style).length ? style : undefined;
|
|
62
74
|
}
|
package/dist/refs.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** One end of a reference as written: `$B$3`, `B3`, or for whole columns `B` (no `row`). */
|
|
2
|
+
export interface RefEnd {
|
|
3
|
+
column: number;
|
|
4
|
+
/** From 0; unset on the end of whole columns (`A:C`). */
|
|
5
|
+
row?: number;
|
|
6
|
+
/** Written with `$`: copied to another cell, it stays. */
|
|
7
|
+
columnFixed: boolean;
|
|
8
|
+
rowFixed: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** A reference as a formula writes it: a cell, or a range from one end to the other. */
|
|
11
|
+
export interface FormulaRef {
|
|
12
|
+
/** The sheet's name, when the formula names one. */
|
|
13
|
+
sheet?: string;
|
|
14
|
+
from: RefEnd;
|
|
15
|
+
to?: RefEnd;
|
|
16
|
+
}
|
|
17
|
+
/** A sheet's name as a formula writes it: in quotes unless it is one plain word. */
|
|
18
|
+
export declare function sheetRef(name: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* The formula with every reference written again by `change`: the reference it gives, `null` for a
|
|
21
|
+
* place that is no more (`#REF!`), `undefined` to leave it as it is written.
|
|
22
|
+
*/
|
|
23
|
+
export declare function rewriteRefs(formula: string, change: (ref: FormulaRef) => FormulaRef | null | undefined): string;
|
|
24
|
+
/** Rows or columns put in before `at`, or taken out from `at`. */
|
|
25
|
+
export interface Move {
|
|
26
|
+
axis: 'row' | 'column';
|
|
27
|
+
at: number;
|
|
28
|
+
count: number;
|
|
29
|
+
remove: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Where a row or column is after the move; `null` when it was taken out or is past the grid. */
|
|
32
|
+
export declare function movedIndex(index: number, move: Move): number | null;
|
|
33
|
+
/** Where a span of rows or columns is after the move: it shrinks around what was taken out; `null` when all of it was. */
|
|
34
|
+
export declare function movedSpan(low: number, high: number, move: Move): [number, number] | null;
|
|
35
|
+
/**
|
|
36
|
+
* The formula after rows or columns moved on the sheet called `sheet`. `own`: the formula is on
|
|
37
|
+
* that sheet, so the references that name no sheet are its own.
|
|
38
|
+
*/
|
|
39
|
+
export declare function movedRefs(formula: string, move: Move, sheet: string, own: boolean): string;
|
|
40
|
+
/** The formula as it reads copied `rows` down and `columns` right: what is written with `$` stays. */
|
|
41
|
+
export declare function shiftedRefs(formula: string, rows: number, columns: number): string;
|
|
42
|
+
/** The formula with the sheet called `from` called `to`. */
|
|
43
|
+
export declare function renamedRefs(formula: string, from: string, to: string): string;
|