@truecalc/workbook 1.0.2

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 ADDED
@@ -0,0 +1,234 @@
1
+ # @truecalc/workbook
2
+
3
+ [![npm](https://img.shields.io/npm/v/@truecalc/workbook)](https://www.npmjs.com/package/@truecalc/workbook)
4
+ [![crates.io](https://img.shields.io/crates/v/truecalc-core)](https://crates.io/crates/truecalc-core)
5
+ [![docs.rs](https://img.shields.io/docsrs/truecalc-core)](https://docs.rs/truecalc-core)
6
+ [![license](https://img.shields.io/crates/l/truecalc-core)](LICENSE)
7
+
8
+ Spreadsheet workbook for JavaScript — full recalculation engine compiled to WebAssembly.
9
+ Manage multiple sheets, set cell values and formulas, and trigger recalculation with a
10
+ single call.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ npm install @truecalc/workbook
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### Node.js (ESM)
21
+
22
+ ```js
23
+ import init, { JsWorkbook } from '@truecalc/workbook';
24
+
25
+ await init();
26
+
27
+ const wb = new JsWorkbook('sheets');
28
+ wb.addSheet('Sheet1');
29
+ wb.set('Sheet1', 'A1', '10');
30
+ wb.set('Sheet1', 'A2', '=A1*2');
31
+ wb.recalc(JSON.stringify({ timestamp_ms: 0, timezone: 'UTC', rng_seed: 0 }));
32
+
33
+ const val = wb.resolved('Sheet1', 'A2');
34
+ // => { type: 'number', value: 20 }
35
+ ```
36
+
37
+ ### Node.js (CJS)
38
+
39
+ Node.js CJS projects can use a dynamic import:
40
+
41
+ ```js
42
+ async function main() {
43
+ const { default: init, JsWorkbook } = await import('@truecalc/workbook');
44
+ await init();
45
+
46
+ const wb = new JsWorkbook('sheets');
47
+ wb.addSheet('Sheet1');
48
+ wb.set('Sheet1', 'B1', '=SUM(A1:A3)');
49
+ wb.set('Sheet1', 'A1', '1');
50
+ wb.set('Sheet1', 'A2', '2');
51
+ wb.set('Sheet1', 'A3', '3');
52
+ wb.recalc(JSON.stringify({ timestamp_ms: 0, timezone: 'UTC', rng_seed: 0 }));
53
+
54
+ console.log(wb.resolved('Sheet1', 'B1'));
55
+ // => { type: 'number', value: 6 }
56
+ }
57
+
58
+ main();
59
+ ```
60
+
61
+ ### Serialization
62
+
63
+ ```js
64
+ const json = wb.toJSON();
65
+ const wb2 = JsWorkbook.fromJSON(json);
66
+
67
+ wb2.recalc(JSON.stringify({ timestamp_ms: 0, timezone: 'UTC', rng_seed: 0 }));
68
+ console.log(wb2.resolved('Sheet1', 'A2'));
69
+ // => { type: 'number', value: 20 }
70
+ ```
71
+
72
+ ### Vite
73
+
74
+ Install the wasm plugin first:
75
+
76
+ ```sh
77
+ npm install -D vite-plugin-wasm
78
+ ```
79
+
80
+ Add it to `vite.config.js`:
81
+
82
+ ```js
83
+ import wasm from 'vite-plugin-wasm';
84
+
85
+ export default {
86
+ plugins: [wasm()],
87
+ };
88
+ ```
89
+
90
+ Then import and use normally:
91
+
92
+ ```js
93
+ import init, { JsWorkbook } from '@truecalc/workbook';
94
+
95
+ await init();
96
+
97
+ const wb = new JsWorkbook('sheets');
98
+ wb.addSheet('Sheet1');
99
+ wb.set('Sheet1', 'A1', '=IF(B1 > 0, "positive", "non-positive")');
100
+ wb.set('Sheet1', 'B1', '5');
101
+ wb.recalc(JSON.stringify({ timestamp_ms: 0, timezone: 'UTC', rng_seed: 0 }));
102
+
103
+ const result = wb.resolved('Sheet1', 'A1');
104
+ // => { type: 'text', value: 'positive' }
105
+ ```
106
+
107
+ ### webpack 5
108
+
109
+ webpack 5 supports WebAssembly natively. Enable the experiment in `webpack.config.js`:
110
+
111
+ ```js
112
+ module.exports = {
113
+ experiments: {
114
+ asyncWebAssembly: true,
115
+ },
116
+ };
117
+ ```
118
+
119
+ ## API Reference
120
+
121
+ ### `new JsWorkbook(flavor)`
122
+
123
+ Creates a new empty workbook.
124
+
125
+ - `flavor` — engine flavor string (use `'sheets'` for Google Sheets-compatible behavior).
126
+
127
+ ```js
128
+ const wb = new JsWorkbook('sheets');
129
+ ```
130
+
131
+ ### `wb.addSheet(name)`
132
+
133
+ Adds a new sheet to the workbook.
134
+
135
+ - `name` — sheet name, e.g. `'Sheet1'`.
136
+
137
+ ```js
138
+ wb.addSheet('Sheet1');
139
+ wb.addSheet('Summary');
140
+ ```
141
+
142
+ ### `wb.set(sheet, cell, value)`
143
+
144
+ Sets a cell to a literal value or formula. Formulas start with `=`.
145
+
146
+ - `sheet` — sheet name
147
+ - `cell` — A1-style cell reference, e.g. `'B2'`
148
+ - `value` — string literal or formula string
149
+
150
+ ```js
151
+ wb.set('Sheet1', 'A1', '42');
152
+ wb.set('Sheet1', 'A2', '=A1 * 2');
153
+ wb.set('Sheet1', 'A3', '=SUM(A1:A2)');
154
+ ```
155
+
156
+ ### `wb.clear(sheet, cell)`
157
+
158
+ Clears a cell (removes its value or formula).
159
+
160
+ - `sheet` — sheet name
161
+ - `cell` — A1-style cell reference
162
+
163
+ ```js
164
+ wb.clear('Sheet1', 'A1');
165
+ ```
166
+
167
+ ### `wb.defineName(name, expression)`
168
+
169
+ Defines a named range or formula that can be referenced by name across sheets.
170
+
171
+ - `name` — name identifier
172
+ - `expression` — formula expression string
173
+
174
+ ```js
175
+ wb.defineName('TaxRate', '0.2');
176
+ wb.set('Sheet1', 'B1', '=A1 * TaxRate');
177
+ ```
178
+
179
+ ### `wb.recalc(context_json)`
180
+
181
+ Recalculates all formulas in the workbook. Must be called after any `set`/`clear`/`defineName`
182
+ operations before reading results.
183
+
184
+ - `context_json` — JSON string with evaluation context:
185
+ - `timestamp_ms` — Unix timestamp in milliseconds (for `TODAY()`, `NOW()`)
186
+ - `timezone` — IANA timezone string, e.g. `'UTC'` or `'America/New_York'`
187
+ - `rng_seed` — integer seed for random functions (`RAND`, `RANDBETWEEN`)
188
+
189
+ ```js
190
+ wb.recalc(JSON.stringify({ timestamp_ms: Date.now(), timezone: 'UTC', rng_seed: 0 }));
191
+ ```
192
+
193
+ ### `wb.resolved(sheet, cell)`
194
+
195
+ Returns the evaluated result for a cell after recalculation.
196
+
197
+ - `sheet` — sheet name
198
+ - `cell` — A1-style cell reference
199
+
200
+ Returns a discriminated union object tagged by `type`:
201
+
202
+ | `type` | Shape |
203
+ |----------|--------------------------------------------------------|
204
+ | `number` | `{ type: 'number', value: 6 }` |
205
+ | `text` | `{ type: 'text', value: 'yes' }` |
206
+ | `bool` | `{ type: 'bool', value: true }` |
207
+ | `date` | `{ type: 'date', value: 46180 }` |
208
+ | `error` | `{ type: 'error', error: '#NAME?' }` |
209
+ | `empty` | `{ type: 'empty' }` |
210
+ | `array` | `{ type: 'array', value: [ /* EvalResult cells */ ] }` |
211
+
212
+ ```js
213
+ const result = wb.resolved('Sheet1', 'A2');
214
+ // => { type: 'number', value: 20 }
215
+ ```
216
+
217
+ ### `wb.toJSON()`
218
+
219
+ Serializes the entire workbook state (sheets, cell values, formulas, named ranges)
220
+ to a JSON string.
221
+
222
+ ```js
223
+ const json = wb.toJSON();
224
+ ```
225
+
226
+ ### `JsWorkbook.fromJSON(json)`
227
+
228
+ Deserializes a workbook from a JSON string produced by `toJSON()`. Returns a new
229
+ `JsWorkbook` instance.
230
+
231
+ ```js
232
+ const wb2 = JsWorkbook.fromJSON(json);
233
+ wb2.recalc(JSON.stringify({ timestamp_ms: 0, timezone: 'UTC', rng_seed: 0 }));
234
+ ```
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@truecalc/workbook",
3
+ "type": "module",
4
+ "description": "Spreadsheet workbook for the browser — full Workbook API compiled to WebAssembly",
5
+ "version": "1.0.2",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/truecalc/core"
10
+ },
11
+ "files": [
12
+ "truecalc_wasm_workbook_bg.wasm",
13
+ "truecalc_wasm_workbook.js",
14
+ "truecalc_wasm_workbook.d.ts"
15
+ ],
16
+ "main": "truecalc_wasm_workbook.js",
17
+ "types": "truecalc_wasm_workbook.d.ts",
18
+ "sideEffects": [
19
+ "./snippets/*"
20
+ ]
21
+ }
@@ -0,0 +1,116 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A spreadsheet workbook exposed to JavaScript.
6
+ *
7
+ * Create with `new JsWorkbook("sheets")` or `new JsWorkbook("excel")`,
8
+ * or deserialize from a JSON string with `JsWorkbook.fromJSON(s)`.
9
+ */
10
+ export class JsWorkbook {
11
+ free(): void;
12
+ [Symbol.dispose](): void;
13
+ /**
14
+ * Adds a new sheet with the given name and returns on success.
15
+ */
16
+ addSheet(name: string): void;
17
+ /**
18
+ * Clears the cell at `a1` on `sheet`.
19
+ */
20
+ clear(sheet: string, a1: string): void;
21
+ /**
22
+ * Defines a workbook-scoped named range.
23
+ */
24
+ defineName(name: string, ref_str: string): void;
25
+ /**
26
+ * Deserializes a workbook from its canonical JSON string.
27
+ */
28
+ static fromJSON(s: string): JsWorkbook;
29
+ /**
30
+ * Creates a new empty workbook locked to the given engine flavor.
31
+ *
32
+ * Pass `"sheets"` for Google-Sheets-compatible behavior, or any other
33
+ * string (e.g. `"excel"`) for Excel-compatible behavior.
34
+ */
35
+ constructor(engine: string);
36
+ /**
37
+ * Runs a full recalculation against the given context JSON.
38
+ *
39
+ * `context_json` must be a JSON object:
40
+ * `{"timestamp_ms": 0, "timezone": "UTC", "rng_seed": 0}`
41
+ *
42
+ * Returns a JSON array of change objects:
43
+ * `[{"sheet":"Sheet1","addr":"A1","old":{...},"new":{...}}, ...]`
44
+ */
45
+ recalc(context_json: string): string;
46
+ /**
47
+ * Redefines (renames target of) a workbook-scoped named range.
48
+ */
49
+ redefineName(name: string, ref_str: string): void;
50
+ /**
51
+ * Returns the resolved value at `a1` on `sheet` as a JS value.
52
+ *
53
+ * Returns `null` if the cell has no value; otherwise returns a tagged
54
+ * JSON object: `{"type":"number","value":1.5}`, `{"type":"text","value":"hello"}`,
55
+ * `{"type":"bool","value":true}`, `{"type":"date","value":46180}`,
56
+ * `{"type":"error","error":"#REF!"}`, `{"type":"empty"}`,
57
+ * or `{"type":"array","value":[[...],...]}`.
58
+ */
59
+ resolved(sheet: string, a1: string): any;
60
+ /**
61
+ * Sets the cell at `a1` on `sheet` to the given `input`.
62
+ *
63
+ * If `input` starts with `=` it is treated as a formula; otherwise the
64
+ * string is coerced: a numeric literal becomes a `Number`, `"true"` /
65
+ * `"false"` (case-insensitive) become a `Boolean`, and everything else
66
+ * becomes a `Text` value.
67
+ */
68
+ set(sheet: string, a1: string, input: string): void;
69
+ /**
70
+ * Serializes the workbook to its canonical JSON string.
71
+ */
72
+ toJSON(): string;
73
+ }
74
+
75
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
76
+
77
+ export interface InitOutput {
78
+ readonly memory: WebAssembly.Memory;
79
+ readonly __wbg_jsworkbook_free: (a: number, b: number) => void;
80
+ readonly jsworkbook_addSheet: (a: number, b: number, c: number, d: number) => void;
81
+ readonly jsworkbook_clear: (a: number, b: number, c: number, d: number, e: number) => void;
82
+ readonly jsworkbook_defineName: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
83
+ readonly jsworkbook_fromJSON: (a: number, b: number, c: number) => void;
84
+ readonly jsworkbook_new: (a: number, b: number) => number;
85
+ readonly jsworkbook_recalc: (a: number, b: number, c: number, d: number) => void;
86
+ readonly jsworkbook_redefineName: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
87
+ readonly jsworkbook_resolved: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
88
+ readonly jsworkbook_set: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
89
+ readonly jsworkbook_toJSON: (a: number, b: number) => void;
90
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
91
+ readonly __wbindgen_export: (a: number, b: number) => number;
92
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
93
+ readonly __wbindgen_export3: (a: number, b: number, c: number) => void;
94
+ }
95
+
96
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
97
+
98
+ /**
99
+ * Instantiates the given `module`, which can either be bytes or
100
+ * a precompiled `WebAssembly.Module`.
101
+ *
102
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
103
+ *
104
+ * @returns {InitOutput}
105
+ */
106
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
107
+
108
+ /**
109
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
110
+ * for everything else, calls `WebAssembly.instantiate` directly.
111
+ *
112
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
113
+ *
114
+ * @returns {Promise<InitOutput>}
115
+ */
116
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,498 @@
1
+ /* @ts-self-types="./truecalc_wasm_workbook.d.ts" */
2
+
3
+ /**
4
+ * A spreadsheet workbook exposed to JavaScript.
5
+ *
6
+ * Create with `new JsWorkbook("sheets")` or `new JsWorkbook("excel")`,
7
+ * or deserialize from a JSON string with `JsWorkbook.fromJSON(s)`.
8
+ */
9
+ export class JsWorkbook {
10
+ static __wrap(ptr) {
11
+ ptr = ptr >>> 0;
12
+ const obj = Object.create(JsWorkbook.prototype);
13
+ obj.__wbg_ptr = ptr;
14
+ JsWorkbookFinalization.register(obj, obj.__wbg_ptr, obj);
15
+ return obj;
16
+ }
17
+ __destroy_into_raw() {
18
+ const ptr = this.__wbg_ptr;
19
+ this.__wbg_ptr = 0;
20
+ JsWorkbookFinalization.unregister(this);
21
+ return ptr;
22
+ }
23
+ free() {
24
+ const ptr = this.__destroy_into_raw();
25
+ wasm.__wbg_jsworkbook_free(ptr, 0);
26
+ }
27
+ /**
28
+ * Adds a new sheet with the given name and returns on success.
29
+ * @param {string} name
30
+ */
31
+ addSheet(name) {
32
+ try {
33
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
34
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_export, wasm.__wbindgen_export2);
35
+ const len0 = WASM_VECTOR_LEN;
36
+ wasm.jsworkbook_addSheet(retptr, this.__wbg_ptr, ptr0, len0);
37
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
38
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
39
+ if (r1) {
40
+ throw takeObject(r0);
41
+ }
42
+ } finally {
43
+ wasm.__wbindgen_add_to_stack_pointer(16);
44
+ }
45
+ }
46
+ /**
47
+ * Clears the cell at `a1` on `sheet`.
48
+ * @param {string} sheet
49
+ * @param {string} a1
50
+ */
51
+ clear(sheet, a1) {
52
+ const ptr0 = passStringToWasm0(sheet, wasm.__wbindgen_export, wasm.__wbindgen_export2);
53
+ const len0 = WASM_VECTOR_LEN;
54
+ const ptr1 = passStringToWasm0(a1, wasm.__wbindgen_export, wasm.__wbindgen_export2);
55
+ const len1 = WASM_VECTOR_LEN;
56
+ wasm.jsworkbook_clear(this.__wbg_ptr, ptr0, len0, ptr1, len1);
57
+ }
58
+ /**
59
+ * Defines a workbook-scoped named range.
60
+ * @param {string} name
61
+ * @param {string} ref_str
62
+ */
63
+ defineName(name, ref_str) {
64
+ try {
65
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
66
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_export, wasm.__wbindgen_export2);
67
+ const len0 = WASM_VECTOR_LEN;
68
+ const ptr1 = passStringToWasm0(ref_str, wasm.__wbindgen_export, wasm.__wbindgen_export2);
69
+ const len1 = WASM_VECTOR_LEN;
70
+ wasm.jsworkbook_defineName(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
71
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
72
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
73
+ if (r1) {
74
+ throw takeObject(r0);
75
+ }
76
+ } finally {
77
+ wasm.__wbindgen_add_to_stack_pointer(16);
78
+ }
79
+ }
80
+ /**
81
+ * Deserializes a workbook from its canonical JSON string.
82
+ * @param {string} s
83
+ * @returns {JsWorkbook}
84
+ */
85
+ static fromJSON(s) {
86
+ try {
87
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
88
+ const ptr0 = passStringToWasm0(s, wasm.__wbindgen_export, wasm.__wbindgen_export2);
89
+ const len0 = WASM_VECTOR_LEN;
90
+ wasm.jsworkbook_fromJSON(retptr, ptr0, len0);
91
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
92
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
93
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
94
+ if (r2) {
95
+ throw takeObject(r1);
96
+ }
97
+ return JsWorkbook.__wrap(r0);
98
+ } finally {
99
+ wasm.__wbindgen_add_to_stack_pointer(16);
100
+ }
101
+ }
102
+ /**
103
+ * Creates a new empty workbook locked to the given engine flavor.
104
+ *
105
+ * Pass `"sheets"` for Google-Sheets-compatible behavior, or any other
106
+ * string (e.g. `"excel"`) for Excel-compatible behavior.
107
+ * @param {string} engine
108
+ */
109
+ constructor(engine) {
110
+ const ptr0 = passStringToWasm0(engine, wasm.__wbindgen_export, wasm.__wbindgen_export2);
111
+ const len0 = WASM_VECTOR_LEN;
112
+ const ret = wasm.jsworkbook_new(ptr0, len0);
113
+ this.__wbg_ptr = ret >>> 0;
114
+ JsWorkbookFinalization.register(this, this.__wbg_ptr, this);
115
+ return this;
116
+ }
117
+ /**
118
+ * Runs a full recalculation against the given context JSON.
119
+ *
120
+ * `context_json` must be a JSON object:
121
+ * `{"timestamp_ms": 0, "timezone": "UTC", "rng_seed": 0}`
122
+ *
123
+ * Returns a JSON array of change objects:
124
+ * `[{"sheet":"Sheet1","addr":"A1","old":{...},"new":{...}}, ...]`
125
+ * @param {string} context_json
126
+ * @returns {string}
127
+ */
128
+ recalc(context_json) {
129
+ let deferred3_0;
130
+ let deferred3_1;
131
+ try {
132
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
133
+ const ptr0 = passStringToWasm0(context_json, wasm.__wbindgen_export, wasm.__wbindgen_export2);
134
+ const len0 = WASM_VECTOR_LEN;
135
+ wasm.jsworkbook_recalc(retptr, this.__wbg_ptr, ptr0, len0);
136
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
137
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
138
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
139
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
140
+ var ptr2 = r0;
141
+ var len2 = r1;
142
+ if (r3) {
143
+ ptr2 = 0; len2 = 0;
144
+ throw takeObject(r2);
145
+ }
146
+ deferred3_0 = ptr2;
147
+ deferred3_1 = len2;
148
+ return getStringFromWasm0(ptr2, len2);
149
+ } finally {
150
+ wasm.__wbindgen_add_to_stack_pointer(16);
151
+ wasm.__wbindgen_export3(deferred3_0, deferred3_1, 1);
152
+ }
153
+ }
154
+ /**
155
+ * Redefines (renames target of) a workbook-scoped named range.
156
+ * @param {string} name
157
+ * @param {string} ref_str
158
+ */
159
+ redefineName(name, ref_str) {
160
+ try {
161
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
162
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_export, wasm.__wbindgen_export2);
163
+ const len0 = WASM_VECTOR_LEN;
164
+ const ptr1 = passStringToWasm0(ref_str, wasm.__wbindgen_export, wasm.__wbindgen_export2);
165
+ const len1 = WASM_VECTOR_LEN;
166
+ wasm.jsworkbook_redefineName(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
167
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
168
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
169
+ if (r1) {
170
+ throw takeObject(r0);
171
+ }
172
+ } finally {
173
+ wasm.__wbindgen_add_to_stack_pointer(16);
174
+ }
175
+ }
176
+ /**
177
+ * Returns the resolved value at `a1` on `sheet` as a JS value.
178
+ *
179
+ * Returns `null` if the cell has no value; otherwise returns a tagged
180
+ * JSON object: `{"type":"number","value":1.5}`, `{"type":"text","value":"hello"}`,
181
+ * `{"type":"bool","value":true}`, `{"type":"date","value":46180}`,
182
+ * `{"type":"error","error":"#REF!"}`, `{"type":"empty"}`,
183
+ * or `{"type":"array","value":[[...],...]}`.
184
+ * @param {string} sheet
185
+ * @param {string} a1
186
+ * @returns {any}
187
+ */
188
+ resolved(sheet, a1) {
189
+ try {
190
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
191
+ const ptr0 = passStringToWasm0(sheet, wasm.__wbindgen_export, wasm.__wbindgen_export2);
192
+ const len0 = WASM_VECTOR_LEN;
193
+ const ptr1 = passStringToWasm0(a1, wasm.__wbindgen_export, wasm.__wbindgen_export2);
194
+ const len1 = WASM_VECTOR_LEN;
195
+ wasm.jsworkbook_resolved(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
196
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
197
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
198
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
199
+ if (r2) {
200
+ throw takeObject(r1);
201
+ }
202
+ return takeObject(r0);
203
+ } finally {
204
+ wasm.__wbindgen_add_to_stack_pointer(16);
205
+ }
206
+ }
207
+ /**
208
+ * Sets the cell at `a1` on `sheet` to the given `input`.
209
+ *
210
+ * If `input` starts with `=` it is treated as a formula; otherwise the
211
+ * string is coerced: a numeric literal becomes a `Number`, `"true"` /
212
+ * `"false"` (case-insensitive) become a `Boolean`, and everything else
213
+ * becomes a `Text` value.
214
+ * @param {string} sheet
215
+ * @param {string} a1
216
+ * @param {string} input
217
+ */
218
+ set(sheet, a1, input) {
219
+ try {
220
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
221
+ const ptr0 = passStringToWasm0(sheet, wasm.__wbindgen_export, wasm.__wbindgen_export2);
222
+ const len0 = WASM_VECTOR_LEN;
223
+ const ptr1 = passStringToWasm0(a1, wasm.__wbindgen_export, wasm.__wbindgen_export2);
224
+ const len1 = WASM_VECTOR_LEN;
225
+ const ptr2 = passStringToWasm0(input, wasm.__wbindgen_export, wasm.__wbindgen_export2);
226
+ const len2 = WASM_VECTOR_LEN;
227
+ wasm.jsworkbook_set(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2);
228
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
229
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
230
+ if (r1) {
231
+ throw takeObject(r0);
232
+ }
233
+ } finally {
234
+ wasm.__wbindgen_add_to_stack_pointer(16);
235
+ }
236
+ }
237
+ /**
238
+ * Serializes the workbook to its canonical JSON string.
239
+ * @returns {string}
240
+ */
241
+ toJSON() {
242
+ let deferred2_0;
243
+ let deferred2_1;
244
+ try {
245
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
246
+ wasm.jsworkbook_toJSON(retptr, this.__wbg_ptr);
247
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
248
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
249
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
250
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
251
+ var ptr1 = r0;
252
+ var len1 = r1;
253
+ if (r3) {
254
+ ptr1 = 0; len1 = 0;
255
+ throw takeObject(r2);
256
+ }
257
+ deferred2_0 = ptr1;
258
+ deferred2_1 = len1;
259
+ return getStringFromWasm0(ptr1, len1);
260
+ } finally {
261
+ wasm.__wbindgen_add_to_stack_pointer(16);
262
+ wasm.__wbindgen_export3(deferred2_0, deferred2_1, 1);
263
+ }
264
+ }
265
+ }
266
+ if (Symbol.dispose) JsWorkbook.prototype[Symbol.dispose] = JsWorkbook.prototype.free;
267
+ function __wbg_get_imports() {
268
+ const import0 = {
269
+ __proto__: null,
270
+ __wbg_Error_960c155d3d49e4c2: function(arg0, arg1) {
271
+ const ret = Error(getStringFromWasm0(arg0, arg1));
272
+ return addHeapObject(ret);
273
+ },
274
+ __wbg___wbindgen_throw_6b64449b9b9ed33c: function(arg0, arg1) {
275
+ throw new Error(getStringFromWasm0(arg0, arg1));
276
+ },
277
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
278
+ // Cast intrinsic for `Ref(String) -> Externref`.
279
+ const ret = getStringFromWasm0(arg0, arg1);
280
+ return addHeapObject(ret);
281
+ },
282
+ };
283
+ return {
284
+ __proto__: null,
285
+ "./truecalc_wasm_workbook_bg.js": import0,
286
+ };
287
+ }
288
+
289
+ const JsWorkbookFinalization = (typeof FinalizationRegistry === 'undefined')
290
+ ? { register: () => {}, unregister: () => {} }
291
+ : new FinalizationRegistry(ptr => wasm.__wbg_jsworkbook_free(ptr >>> 0, 1));
292
+
293
+ function addHeapObject(obj) {
294
+ if (heap_next === heap.length) heap.push(heap.length + 1);
295
+ const idx = heap_next;
296
+ heap_next = heap[idx];
297
+
298
+ heap[idx] = obj;
299
+ return idx;
300
+ }
301
+
302
+ function dropObject(idx) {
303
+ if (idx < 1028) return;
304
+ heap[idx] = heap_next;
305
+ heap_next = idx;
306
+ }
307
+
308
+ let cachedDataViewMemory0 = null;
309
+ function getDataViewMemory0() {
310
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
311
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
312
+ }
313
+ return cachedDataViewMemory0;
314
+ }
315
+
316
+ function getStringFromWasm0(ptr, len) {
317
+ ptr = ptr >>> 0;
318
+ return decodeText(ptr, len);
319
+ }
320
+
321
+ let cachedUint8ArrayMemory0 = null;
322
+ function getUint8ArrayMemory0() {
323
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
324
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
325
+ }
326
+ return cachedUint8ArrayMemory0;
327
+ }
328
+
329
+ function getObject(idx) { return heap[idx]; }
330
+
331
+ let heap = new Array(1024).fill(undefined);
332
+ heap.push(undefined, null, true, false);
333
+
334
+ let heap_next = heap.length;
335
+
336
+ function passStringToWasm0(arg, malloc, realloc) {
337
+ if (realloc === undefined) {
338
+ const buf = cachedTextEncoder.encode(arg);
339
+ const ptr = malloc(buf.length, 1) >>> 0;
340
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
341
+ WASM_VECTOR_LEN = buf.length;
342
+ return ptr;
343
+ }
344
+
345
+ let len = arg.length;
346
+ let ptr = malloc(len, 1) >>> 0;
347
+
348
+ const mem = getUint8ArrayMemory0();
349
+
350
+ let offset = 0;
351
+
352
+ for (; offset < len; offset++) {
353
+ const code = arg.charCodeAt(offset);
354
+ if (code > 0x7F) break;
355
+ mem[ptr + offset] = code;
356
+ }
357
+ if (offset !== len) {
358
+ if (offset !== 0) {
359
+ arg = arg.slice(offset);
360
+ }
361
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
362
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
363
+ const ret = cachedTextEncoder.encodeInto(arg, view);
364
+
365
+ offset += ret.written;
366
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
367
+ }
368
+
369
+ WASM_VECTOR_LEN = offset;
370
+ return ptr;
371
+ }
372
+
373
+ function takeObject(idx) {
374
+ const ret = getObject(idx);
375
+ dropObject(idx);
376
+ return ret;
377
+ }
378
+
379
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
380
+ cachedTextDecoder.decode();
381
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
382
+ let numBytesDecoded = 0;
383
+ function decodeText(ptr, len) {
384
+ numBytesDecoded += len;
385
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
386
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
387
+ cachedTextDecoder.decode();
388
+ numBytesDecoded = len;
389
+ }
390
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
391
+ }
392
+
393
+ const cachedTextEncoder = new TextEncoder();
394
+
395
+ if (!('encodeInto' in cachedTextEncoder)) {
396
+ cachedTextEncoder.encodeInto = function (arg, view) {
397
+ const buf = cachedTextEncoder.encode(arg);
398
+ view.set(buf);
399
+ return {
400
+ read: arg.length,
401
+ written: buf.length
402
+ };
403
+ };
404
+ }
405
+
406
+ let WASM_VECTOR_LEN = 0;
407
+
408
+ let wasmModule, wasm;
409
+ function __wbg_finalize_init(instance, module) {
410
+ wasm = instance.exports;
411
+ wasmModule = module;
412
+ cachedDataViewMemory0 = null;
413
+ cachedUint8ArrayMemory0 = null;
414
+ return wasm;
415
+ }
416
+
417
+ async function __wbg_load(module, imports) {
418
+ if (typeof Response === 'function' && module instanceof Response) {
419
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
420
+ try {
421
+ return await WebAssembly.instantiateStreaming(module, imports);
422
+ } catch (e) {
423
+ const validResponse = module.ok && expectedResponseType(module.type);
424
+
425
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
426
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
427
+
428
+ } else { throw e; }
429
+ }
430
+ }
431
+
432
+ const bytes = await module.arrayBuffer();
433
+ return await WebAssembly.instantiate(bytes, imports);
434
+ } else {
435
+ const instance = await WebAssembly.instantiate(module, imports);
436
+
437
+ if (instance instanceof WebAssembly.Instance) {
438
+ return { instance, module };
439
+ } else {
440
+ return instance;
441
+ }
442
+ }
443
+
444
+ function expectedResponseType(type) {
445
+ switch (type) {
446
+ case 'basic': case 'cors': case 'default': return true;
447
+ }
448
+ return false;
449
+ }
450
+ }
451
+
452
+ function initSync(module) {
453
+ if (wasm !== undefined) return wasm;
454
+
455
+
456
+ if (module !== undefined) {
457
+ if (Object.getPrototypeOf(module) === Object.prototype) {
458
+ ({module} = module)
459
+ } else {
460
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
461
+ }
462
+ }
463
+
464
+ const imports = __wbg_get_imports();
465
+ if (!(module instanceof WebAssembly.Module)) {
466
+ module = new WebAssembly.Module(module);
467
+ }
468
+ const instance = new WebAssembly.Instance(module, imports);
469
+ return __wbg_finalize_init(instance, module);
470
+ }
471
+
472
+ async function __wbg_init(module_or_path) {
473
+ if (wasm !== undefined) return wasm;
474
+
475
+
476
+ if (module_or_path !== undefined) {
477
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
478
+ ({module_or_path} = module_or_path)
479
+ } else {
480
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
481
+ }
482
+ }
483
+
484
+ if (module_or_path === undefined) {
485
+ module_or_path = new URL('truecalc_wasm_workbook_bg.wasm', import.meta.url);
486
+ }
487
+ const imports = __wbg_get_imports();
488
+
489
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
490
+ module_or_path = fetch(module_or_path);
491
+ }
492
+
493
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
494
+
495
+ return __wbg_finalize_init(instance, module);
496
+ }
497
+
498
+ export { initSync, __wbg_init as default };
Binary file