@ricsam/formula-engine 0.2.8 → 0.2.10
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 +41 -3
- package/dist/cjs/core/engine.cjs +551 -166
- package/dist/cjs/core/engine.cjs.map +3 -3
- package/dist/cjs/core/managers/dependency-manager.cjs +151 -67
- package/dist/cjs/core/managers/dependency-manager.cjs.map +3 -3
- package/dist/cjs/core/managers/evaluation-manager.cjs.map +1 -1
- package/dist/cjs/core/managers/workbook-manager.cjs +197 -1
- package/dist/cjs/core/managers/workbook-manager.cjs.map +3 -3
- package/dist/cjs/core/mutation-invalidation.cjs +241 -0
- package/dist/cjs/core/mutation-invalidation.cjs.map +10 -0
- package/dist/cjs/core/types.cjs.map +2 -2
- package/dist/cjs/core/workbook-renamer.cjs.map +1 -1
- package/dist/cjs/evaluator/formula-evaluator.cjs +22 -48
- package/dist/cjs/evaluator/formula-evaluator.cjs.map +3 -3
- package/dist/cjs/functions/math/sum/summation-utils.cjs +2 -2
- package/dist/cjs/functions/math/sum/summation-utils.cjs.map +2 -2
- package/dist/cjs/lib.cjs +2 -13
- package/dist/cjs/lib.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/parser/formatter.cjs +3 -4
- package/dist/cjs/parser/formatter.cjs.map +3 -3
- package/dist/cjs/parser/parser.cjs +24 -216
- package/dist/cjs/parser/parser.cjs.map +3 -3
- package/dist/mjs/core/engine.mjs +565 -207
- package/dist/mjs/core/engine.mjs.map +3 -3
- package/dist/mjs/core/managers/dependency-manager.mjs +151 -67
- package/dist/mjs/core/managers/dependency-manager.mjs.map +3 -3
- package/dist/mjs/core/managers/evaluation-manager.mjs.map +1 -1
- package/dist/mjs/core/managers/workbook-manager.mjs +197 -1
- package/dist/mjs/core/managers/workbook-manager.mjs.map +3 -3
- package/dist/mjs/core/mutation-invalidation.mjs +201 -0
- package/dist/mjs/core/mutation-invalidation.mjs.map +10 -0
- package/dist/mjs/core/types.mjs.map +2 -2
- package/dist/mjs/core/workbook-renamer.mjs.map +1 -1
- package/dist/mjs/evaluator/formula-evaluator.mjs +22 -48
- package/dist/mjs/evaluator/formula-evaluator.mjs.map +3 -3
- package/dist/mjs/functions/math/sum/summation-utils.mjs +2 -2
- package/dist/mjs/functions/math/sum/summation-utils.mjs.map +2 -2
- package/dist/mjs/lib.mjs +2 -13
- package/dist/mjs/lib.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/parser/formatter.mjs +3 -4
- package/dist/mjs/parser/formatter.mjs.map +3 -3
- package/dist/mjs/parser/parser.mjs +24 -216
- package/dist/mjs/parser/parser.mjs.map +3 -3
- package/dist/types/core/engine.d.ts +31 -135
- package/dist/types/core/managers/dependency-manager.d.ts +5 -1
- package/dist/types/core/managers/evaluation-manager.d.ts +1 -1
- package/dist/types/core/managers/workbook-manager.d.ts +23 -1
- package/dist/types/core/mutation-invalidation.d.ts +46 -0
- package/dist/types/core/types.d.ts +35 -0
- package/dist/types/lib.d.ts +0 -8
- package/dist/types/parser/parser.d.ts +4 -6
- package/package.json +1 -1
- package/dist/cjs/core/commands/command-executor.cjs +0 -149
- package/dist/cjs/core/commands/command-executor.cjs.map +0 -10
- package/dist/cjs/core/commands/content-commands.cjs +0 -857
- package/dist/cjs/core/commands/content-commands.cjs.map +0 -10
- package/dist/cjs/core/commands/metadata-commands.cjs +0 -160
- package/dist/cjs/core/commands/metadata-commands.cjs.map +0 -10
- package/dist/cjs/core/commands/named-expression-commands.cjs +0 -363
- package/dist/cjs/core/commands/named-expression-commands.cjs.map +0 -10
- package/dist/cjs/core/commands/structure-commands.cjs +0 -696
- package/dist/cjs/core/commands/structure-commands.cjs.map +0 -10
- package/dist/cjs/core/commands/style-commands.cjs +0 -229
- package/dist/cjs/core/commands/style-commands.cjs.map +0 -10
- package/dist/cjs/core/commands/table-commands.cjs +0 -495
- package/dist/cjs/core/commands/table-commands.cjs.map +0 -10
- package/dist/cjs/core/commands/types.cjs +0 -99
- package/dist/cjs/core/commands/types.cjs.map +0 -10
- package/dist/cjs/core/managers/schema-manager.cjs +0 -374
- package/dist/cjs/core/managers/schema-manager.cjs.map +0 -10
- package/dist/cjs/core/schema/cell-orm.cjs +0 -76
- package/dist/cjs/core/schema/cell-orm.cjs.map +0 -10
- package/dist/cjs/core/schema/grid-orm.cjs +0 -125
- package/dist/cjs/core/schema/grid-orm.cjs.map +0 -10
- package/dist/cjs/core/schema/schema-builder.cjs +0 -74
- package/dist/cjs/core/schema/schema-builder.cjs.map +0 -10
- package/dist/cjs/core/schema/schema-helpers.cjs +0 -200
- package/dist/cjs/core/schema/schema-helpers.cjs.map +0 -10
- package/dist/cjs/core/schema/schema.cjs +0 -91
- package/dist/cjs/core/schema/schema.cjs.map +0 -10
- package/dist/cjs/core/schema/table-orm.cjs +0 -189
- package/dist/cjs/core/schema/table-orm.cjs.map +0 -10
- package/dist/mjs/core/commands/command-executor.mjs +0 -109
- package/dist/mjs/core/commands/command-executor.mjs.map +0 -10
- package/dist/mjs/core/commands/content-commands.mjs +0 -821
- package/dist/mjs/core/commands/content-commands.mjs.map +0 -10
- package/dist/mjs/core/commands/metadata-commands.mjs +0 -120
- package/dist/mjs/core/commands/metadata-commands.mjs.map +0 -10
- package/dist/mjs/core/commands/named-expression-commands.mjs +0 -323
- package/dist/mjs/core/commands/named-expression-commands.mjs.map +0 -10
- package/dist/mjs/core/commands/structure-commands.mjs +0 -661
- package/dist/mjs/core/commands/structure-commands.mjs.map +0 -10
- package/dist/mjs/core/commands/style-commands.mjs +0 -189
- package/dist/mjs/core/commands/style-commands.mjs.map +0 -10
- package/dist/mjs/core/commands/table-commands.mjs +0 -459
- package/dist/mjs/core/commands/table-commands.mjs.map +0 -10
- package/dist/mjs/core/commands/types.mjs +0 -59
- package/dist/mjs/core/commands/types.mjs.map +0 -10
- package/dist/mjs/core/managers/schema-manager.mjs +0 -334
- package/dist/mjs/core/managers/schema-manager.mjs.map +0 -10
- package/dist/mjs/core/schema/cell-orm.mjs +0 -35
- package/dist/mjs/core/schema/cell-orm.mjs.map +0 -10
- package/dist/mjs/core/schema/grid-orm.mjs +0 -84
- package/dist/mjs/core/schema/grid-orm.mjs.map +0 -10
- package/dist/mjs/core/schema/schema-builder.mjs +0 -34
- package/dist/mjs/core/schema/schema-builder.mjs.map +0 -10
- package/dist/mjs/core/schema/schema-helpers.mjs +0 -160
- package/dist/mjs/core/schema/schema-helpers.mjs.map +0 -10
- package/dist/mjs/core/schema/schema.mjs +0 -51
- package/dist/mjs/core/schema/schema.mjs.map +0 -10
- package/dist/mjs/core/schema/table-orm.mjs +0 -156
- package/dist/mjs/core/schema/table-orm.mjs.map +0 -10
- package/dist/types/core/commands/command-executor.d.ts +0 -81
- package/dist/types/core/commands/content-commands.d.ts +0 -172
- package/dist/types/core/commands/metadata-commands.d.ts +0 -55
- package/dist/types/core/commands/named-expression-commands.d.ts +0 -130
- package/dist/types/core/commands/structure-commands.d.ts +0 -155
- package/dist/types/core/commands/style-commands.d.ts +0 -76
- package/dist/types/core/commands/table-commands.d.ts +0 -119
- package/dist/types/core/commands/types.d.ts +0 -143
- package/dist/types/core/managers/schema-manager.d.ts +0 -142
- package/dist/types/core/schema/cell-orm.d.ts +0 -33
- package/dist/types/core/schema/grid-orm.d.ts +0 -63
- package/dist/types/core/schema/schema-builder.d.ts +0 -16
- package/dist/types/core/schema/schema-helpers.d.ts +0 -49
- package/dist/types/core/schema/schema.d.ts +0 -167
- package/dist/types/core/schema/schema.type-test.d.ts +0 -1
- package/dist/types/core/schema/table-orm.d.ts +0 -46
package/dist/cjs/core/engine.cjs
CHANGED
|
@@ -59,19 +59,9 @@ var import_dependency_manager = require("./managers/dependency-manager.cjs");
|
|
|
59
59
|
var import_style_manager = require("./managers/style-manager.cjs");
|
|
60
60
|
var import_copy_manager = require("./managers/copy-manager.cjs");
|
|
61
61
|
var import_reference_manager = require("./managers/reference-manager.cjs");
|
|
62
|
-
var import_schema_manager = require("./managers/schema-manager.cjs");
|
|
63
|
-
var import_schema_builder = require("./schema/schema-builder.cjs");
|
|
64
|
-
var import_table_orm = require("./schema/table-orm.cjs");
|
|
65
|
-
var import_cell_orm = require("./schema/cell-orm.cjs");
|
|
66
|
-
var import_grid_orm = require("./schema/grid-orm.cjs");
|
|
67
62
|
var import_engine_snapshot = require("./engine-snapshot.cjs");
|
|
68
|
-
var
|
|
69
|
-
var
|
|
70
|
-
var import_structure_commands = require("./commands/structure-commands.cjs");
|
|
71
|
-
var import_table_commands = require("./commands/table-commands.cjs");
|
|
72
|
-
var import_named_expression_commands = require("./commands/named-expression-commands.cjs");
|
|
73
|
-
var import_metadata_commands = require("./commands/metadata-commands.cjs");
|
|
74
|
-
var import_style_commands = require("./commands/style-commands.cjs");
|
|
63
|
+
var import_mutation_invalidation = require("./mutation-invalidation.cjs");
|
|
64
|
+
var import_resource_keys = require("./resource-keys.cjs");
|
|
75
65
|
|
|
76
66
|
class FormulaEngine {
|
|
77
67
|
workbookManager;
|
|
@@ -84,10 +74,6 @@ class FormulaEngine {
|
|
|
84
74
|
styleManager;
|
|
85
75
|
copyManager;
|
|
86
76
|
referenceManager;
|
|
87
|
-
schemaManager;
|
|
88
|
-
commandExecutor;
|
|
89
|
-
schema;
|
|
90
|
-
schemaDeclaration;
|
|
91
77
|
_workbookManager;
|
|
92
78
|
_namedExpressionManager;
|
|
93
79
|
_tableManager;
|
|
@@ -96,8 +82,7 @@ class FormulaEngine {
|
|
|
96
82
|
_autoFillManager;
|
|
97
83
|
_dependencyManager;
|
|
98
84
|
_styleManager;
|
|
99
|
-
constructor(
|
|
100
|
-
this.schemaDeclaration = schema?.declaration;
|
|
85
|
+
constructor() {
|
|
101
86
|
this.eventManager = new import_event_manager.EventManager;
|
|
102
87
|
this.workbookManager = new import_workbook_manager.WorkbookManager;
|
|
103
88
|
this.namedExpressionManager = new import_named_expression_manager.NamedExpressionManager;
|
|
@@ -110,13 +95,6 @@ class FormulaEngine {
|
|
|
110
95
|
this.copyManager = new import_copy_manager.CopyManager(this.workbookManager, this.evaluationManager, this.styleManager);
|
|
111
96
|
this.autoFillManager = new import_autofill_utils.AutoFill(this.workbookManager, this.styleManager);
|
|
112
97
|
this.referenceManager = new import_reference_manager.ReferenceManager;
|
|
113
|
-
this.schemaManager = new import_schema_manager.SchemaManager(this.tableManager);
|
|
114
|
-
this.commandExecutor = new import_command_executor.CommandExecutor(this.evaluationManager, this.eventManager, () => this.schemaManager.validateAllSchemaConstraints((cell) => this.getCellValue(cell), (cell) => this.getCellMetadata(cell), (table) => this.getTableDataCells(table)));
|
|
115
|
-
if (this.schemaDeclaration) {
|
|
116
|
-
this.schema = import_schema_builder.buildSchemaFromDeclaration(this, this.schemaDeclaration, this.schemaManager);
|
|
117
|
-
} else {
|
|
118
|
-
this.schema = {};
|
|
119
|
-
}
|
|
120
98
|
this._workbookManager = this.workbookManager;
|
|
121
99
|
this._namedExpressionManager = this.namedExpressionManager;
|
|
122
100
|
this._tableManager = this.tableManager;
|
|
@@ -126,46 +104,63 @@ class FormulaEngine {
|
|
|
126
104
|
this._dependencyManager = this.dependencyManager;
|
|
127
105
|
this._styleManager = this.styleManager;
|
|
128
106
|
}
|
|
129
|
-
static buildEmpty(
|
|
130
|
-
return new FormulaEngine
|
|
131
|
-
}
|
|
132
|
-
addTableSchema(namespace, address, headers) {
|
|
133
|
-
this.schemaManager.registerTableSchema(namespace, address.workbookName, address.tableName, headers);
|
|
134
|
-
const orm = new import_table_orm.TableOrm(this, address.workbookName, address.tableName, headers, namespace);
|
|
135
|
-
this.schema[namespace] = orm;
|
|
136
|
-
return orm;
|
|
137
|
-
}
|
|
138
|
-
addCellSchema(namespace, cellAddress, parse, write = (value) => ({ value })) {
|
|
139
|
-
this.schemaManager.registerCellSchema(namespace, cellAddress, parse);
|
|
140
|
-
const orm = new import_cell_orm.CellOrm(this, cellAddress, parse, write, namespace);
|
|
141
|
-
this.schema[namespace] = orm;
|
|
142
|
-
return orm;
|
|
143
|
-
}
|
|
144
|
-
addGridSchema(namespace, address, range, parse, write = (value) => ({ value })) {
|
|
145
|
-
this.schemaManager.registerGridSchema(namespace, address.workbookName, address.sheetName, range, parse);
|
|
146
|
-
const orm = new import_grid_orm.GridOrm(this, address.workbookName, address.sheetName, range, parse, write, namespace);
|
|
147
|
-
this.schema[namespace] = orm;
|
|
148
|
-
return orm;
|
|
107
|
+
static buildEmpty() {
|
|
108
|
+
return new FormulaEngine;
|
|
149
109
|
}
|
|
150
|
-
|
|
151
|
-
|
|
110
|
+
emitMutation(footprint) {
|
|
111
|
+
this.evaluationManager.invalidateFromMutation(footprint);
|
|
112
|
+
this.eventManager.emitUpdate();
|
|
152
113
|
}
|
|
153
|
-
|
|
154
|
-
|
|
114
|
+
emitUpdate() {
|
|
115
|
+
this.eventManager.emitUpdate();
|
|
155
116
|
}
|
|
156
|
-
|
|
157
|
-
|
|
117
|
+
getExistingSheetContent(opts) {
|
|
118
|
+
const sheet = this.workbookManager.getSheet(opts);
|
|
119
|
+
return sheet ? new Map(sheet.content) : undefined;
|
|
158
120
|
}
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
121
|
+
getWorkbookResourceKeys(workbookName) {
|
|
122
|
+
const resourceKeys = new Set([import_resource_keys.getWorkbookResourceKey(workbookName)]);
|
|
123
|
+
for (const sheetName of this.workbookManager.getWorkbooks().get(workbookName)?.sheets.keys() ?? []) {
|
|
124
|
+
resourceKeys.add(import_resource_keys.getSheetResourceKey({ workbookName, sheetName }));
|
|
163
125
|
}
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
delete this.schema[namespace];
|
|
126
|
+
for (const tableName of this.tableManager.getTables(workbookName).keys()) {
|
|
127
|
+
resourceKeys.add(import_resource_keys.getTableResourceKey({ workbookName, tableName }));
|
|
167
128
|
}
|
|
168
|
-
|
|
129
|
+
const namedExpressions = this.namedExpressionManager.getNamedExpressions();
|
|
130
|
+
for (const name of namedExpressions.workbookExpressions.get(workbookName)?.keys() ?? []) {
|
|
131
|
+
resourceKeys.add(import_resource_keys.getNamedExpressionResourceKey({ expressionName: name, workbookName }));
|
|
132
|
+
}
|
|
133
|
+
for (const [sheetName, expressions] of namedExpressions.sheetExpressions.get(workbookName) ?? []) {
|
|
134
|
+
resourceKeys.add(import_resource_keys.getSheetResourceKey({ workbookName, sheetName }));
|
|
135
|
+
for (const name of expressions.keys()) {
|
|
136
|
+
resourceKeys.add(import_resource_keys.getNamedExpressionResourceKey({
|
|
137
|
+
expressionName: name,
|
|
138
|
+
workbookName,
|
|
139
|
+
sheetName
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return Array.from(resourceKeys);
|
|
144
|
+
}
|
|
145
|
+
getSheetResourceKeys(opts) {
|
|
146
|
+
const resourceKeys = new Set([
|
|
147
|
+
import_resource_keys.getWorkbookResourceKey(opts.workbookName),
|
|
148
|
+
import_resource_keys.getSheetResourceKey(opts)
|
|
149
|
+
]);
|
|
150
|
+
for (const [tableName, table] of this.tableManager.getTables(opts.workbookName)) {
|
|
151
|
+
if (table.sheetName === opts.sheetName) {
|
|
152
|
+
resourceKeys.add(import_resource_keys.getTableResourceKey({ workbookName: opts.workbookName, tableName }));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const sheetExpressions = this.namedExpressionManager.getNamedExpressions().sheetExpressions.get(opts.workbookName)?.get(opts.sheetName);
|
|
156
|
+
for (const name of sheetExpressions?.keys() ?? []) {
|
|
157
|
+
resourceKeys.add(import_resource_keys.getNamedExpressionResourceKey({
|
|
158
|
+
expressionName: name,
|
|
159
|
+
workbookName: opts.workbookName,
|
|
160
|
+
sheetName: opts.sheetName
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
return Array.from(resourceKeys);
|
|
169
164
|
}
|
|
170
165
|
getCellEvaluationResult(cellAddress) {
|
|
171
166
|
return this.evaluationManager.getCellEvaluationResult(cellAddress);
|
|
@@ -178,7 +173,8 @@ class FormulaEngine {
|
|
|
178
173
|
return this.evaluationManager.evaluationResultToSerializedValue(result, cellAddress, debug);
|
|
179
174
|
}
|
|
180
175
|
setCellMetadata(address, metadata) {
|
|
181
|
-
this.
|
|
176
|
+
this.workbookManager.setCellMetadata(address, metadata);
|
|
177
|
+
this.emitUpdate();
|
|
182
178
|
}
|
|
183
179
|
getCellMetadata(address) {
|
|
184
180
|
const metadata = this.workbookManager.getCellMetadata(address);
|
|
@@ -188,13 +184,15 @@ class FormulaEngine {
|
|
|
188
184
|
return this.workbookManager.getSheetMetadataSerialized(opts);
|
|
189
185
|
}
|
|
190
186
|
setSheetMetadata(opts, metadata) {
|
|
191
|
-
this.
|
|
187
|
+
this.workbookManager.setSheetMetadata(opts, metadata);
|
|
188
|
+
this.emitUpdate();
|
|
192
189
|
}
|
|
193
190
|
getSheetMetadata(opts) {
|
|
194
191
|
return this.workbookManager.getSheetMetadata(opts);
|
|
195
192
|
}
|
|
196
193
|
setWorkbookMetadata(workbookName, metadata) {
|
|
197
|
-
this.
|
|
194
|
+
this.workbookManager.setWorkbookMetadata(workbookName, metadata);
|
|
195
|
+
this.emitUpdate();
|
|
198
196
|
}
|
|
199
197
|
getWorkbookMetadata(workbookName) {
|
|
200
198
|
return this.workbookManager.getWorkbookMetadata(workbookName);
|
|
@@ -221,10 +219,20 @@ class FormulaEngine {
|
|
|
221
219
|
throw new Error("Not implemented");
|
|
222
220
|
}
|
|
223
221
|
addNamedExpression(opts) {
|
|
224
|
-
this.
|
|
222
|
+
this.namedExpressionManager.addNamedExpression(opts);
|
|
223
|
+
this.emitMutation({
|
|
224
|
+
touchedCells: [],
|
|
225
|
+
resourceKeys: [import_resource_keys.getNamedExpressionResourceKey(opts)]
|
|
226
|
+
});
|
|
225
227
|
}
|
|
226
228
|
removeNamedExpression(opts) {
|
|
227
|
-
|
|
229
|
+
const removed = this.namedExpressionManager.removeNamedExpression(opts);
|
|
230
|
+
if (removed) {
|
|
231
|
+
this.emitMutation({
|
|
232
|
+
touchedCells: [],
|
|
233
|
+
resourceKeys: [import_resource_keys.getNamedExpressionResourceKey(opts)]
|
|
234
|
+
});
|
|
235
|
+
}
|
|
228
236
|
}
|
|
229
237
|
hasNamedExpression(opts) {
|
|
230
238
|
const scope = opts.sheetName && opts.workbookName ? {
|
|
@@ -238,25 +246,154 @@ class FormulaEngine {
|
|
|
238
246
|
});
|
|
239
247
|
}
|
|
240
248
|
updateNamedExpression(opts) {
|
|
241
|
-
this.
|
|
249
|
+
this.namedExpressionManager.updateNamedExpression(opts);
|
|
250
|
+
this.emitMutation({
|
|
251
|
+
touchedCells: [],
|
|
252
|
+
resourceKeys: [import_resource_keys.getNamedExpressionResourceKey(opts)]
|
|
253
|
+
});
|
|
242
254
|
}
|
|
243
255
|
renameNamedExpression(opts) {
|
|
244
|
-
this.
|
|
256
|
+
this.namedExpressionManager.renameNamedExpression(opts);
|
|
257
|
+
const changedCells = this.workbookManager.updateAllFormulas((formula) => import_named_expression_renamer.renameNamedExpressionInFormula(formula, opts.expressionName, opts.newName));
|
|
258
|
+
const changedNamedExpressions = this.namedExpressionManager.updateAllNamedExpressions((formula) => import_named_expression_renamer.renameNamedExpressionInFormula(formula, opts.expressionName, opts.newName));
|
|
259
|
+
this.emitMutation({
|
|
260
|
+
touchedCells: import_mutation_invalidation.buildFormulaTouchedCells(changedCells),
|
|
261
|
+
resourceKeys: [
|
|
262
|
+
import_resource_keys.getNamedExpressionResourceKey({
|
|
263
|
+
expressionName: opts.expressionName,
|
|
264
|
+
workbookName: opts.workbookName,
|
|
265
|
+
sheetName: opts.sheetName
|
|
266
|
+
}),
|
|
267
|
+
import_resource_keys.getNamedExpressionResourceKey({
|
|
268
|
+
expressionName: opts.newName,
|
|
269
|
+
workbookName: opts.workbookName,
|
|
270
|
+
sheetName: opts.sheetName
|
|
271
|
+
}),
|
|
272
|
+
...changedNamedExpressions
|
|
273
|
+
]
|
|
274
|
+
});
|
|
245
275
|
}
|
|
246
276
|
setNamedExpressions(opts) {
|
|
247
|
-
|
|
277
|
+
const allExpressions = this.namedExpressionManager.getNamedExpressions();
|
|
278
|
+
let previousExpressions;
|
|
279
|
+
if (opts.type === "global") {
|
|
280
|
+
previousExpressions = new Map(allExpressions.globalExpressions);
|
|
281
|
+
} else if (opts.type === "workbook") {
|
|
282
|
+
previousExpressions = new Map(allExpressions.workbookExpressions.get(opts.workbookName) || []);
|
|
283
|
+
} else {
|
|
284
|
+
const sheetExpressions = allExpressions.sheetExpressions.get(opts.workbookName)?.get(opts.sheetName);
|
|
285
|
+
previousExpressions = new Map(sheetExpressions || []);
|
|
286
|
+
}
|
|
287
|
+
this.namedExpressionManager.setNamedExpressions(opts);
|
|
288
|
+
const scope = opts.type === "global" ? {} : opts.type === "workbook" ? { workbookName: opts.workbookName } : {
|
|
289
|
+
workbookName: opts.workbookName,
|
|
290
|
+
sheetName: opts.sheetName
|
|
291
|
+
};
|
|
292
|
+
this.emitMutation({
|
|
293
|
+
touchedCells: [],
|
|
294
|
+
resourceKeys: [
|
|
295
|
+
...import_mutation_invalidation.getNamedExpressionScopeResourceKeys(previousExpressions.keys(), scope),
|
|
296
|
+
...import_mutation_invalidation.getNamedExpressionScopeResourceKeys(opts.expressions.keys(), scope)
|
|
297
|
+
]
|
|
298
|
+
});
|
|
248
299
|
}
|
|
249
300
|
addTable(props) {
|
|
250
|
-
|
|
301
|
+
const table = this.tableManager.addTable({
|
|
302
|
+
...props,
|
|
303
|
+
getCellValue: (cellAddress) => this.getCellValue(cellAddress)
|
|
304
|
+
});
|
|
305
|
+
this.emitMutation({
|
|
306
|
+
touchedCells: import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, [table]),
|
|
307
|
+
tableContextChangedCells: import_mutation_invalidation.buildTableContextChangedCells(this.workbookManager, [table]),
|
|
308
|
+
resourceKeys: [
|
|
309
|
+
import_resource_keys.getTableResourceKey({
|
|
310
|
+
workbookName: props.workbookName,
|
|
311
|
+
tableName: props.tableName
|
|
312
|
+
})
|
|
313
|
+
]
|
|
314
|
+
});
|
|
251
315
|
}
|
|
252
316
|
renameTable(workbookName, names) {
|
|
253
|
-
|
|
317
|
+
const oldTable = this.tableManager.getTable({
|
|
318
|
+
workbookName,
|
|
319
|
+
name: names.oldName
|
|
320
|
+
});
|
|
321
|
+
const oldTableSnapshot = oldTable ? { ...oldTable, headers: new Map(oldTable.headers) } : undefined;
|
|
322
|
+
this.tableManager.renameTable(workbookName, names);
|
|
323
|
+
const changedCells = this.workbookManager.updateAllFormulas((formula) => import_table_renamer.renameTableInFormula(formula, names.oldName, names.newName));
|
|
324
|
+
const changedNamedExpressions = this.namedExpressionManager.updateAllNamedExpressions((formula) => import_table_renamer.renameTableInFormula(formula, names.oldName, names.newName));
|
|
325
|
+
const newTable = this.tableManager.getTable({
|
|
326
|
+
workbookName,
|
|
327
|
+
name: names.newName
|
|
328
|
+
});
|
|
329
|
+
this.emitMutation({
|
|
330
|
+
touchedCells: import_mutation_invalidation.mergeTouchedCells(import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, [oldTableSnapshot]), import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, [newTable]), import_mutation_invalidation.buildFormulaTouchedCells(changedCells)),
|
|
331
|
+
tableContextChangedCells: import_mutation_invalidation.buildTableContextChangedCells(this.workbookManager, [oldTableSnapshot, newTable]),
|
|
332
|
+
resourceKeys: [
|
|
333
|
+
import_resource_keys.getTableResourceKey({
|
|
334
|
+
workbookName,
|
|
335
|
+
tableName: names.oldName
|
|
336
|
+
}),
|
|
337
|
+
import_resource_keys.getTableResourceKey({
|
|
338
|
+
workbookName,
|
|
339
|
+
tableName: names.newName
|
|
340
|
+
}),
|
|
341
|
+
...changedNamedExpressions
|
|
342
|
+
]
|
|
343
|
+
});
|
|
254
344
|
}
|
|
255
345
|
updateTable(opts) {
|
|
256
|
-
|
|
346
|
+
const oldTable = this.tableManager.getTable({
|
|
347
|
+
workbookName: opts.workbookName,
|
|
348
|
+
name: opts.tableName
|
|
349
|
+
});
|
|
350
|
+
const oldTableSnapshot = oldTable ? { ...oldTable, headers: new Map(oldTable.headers) } : undefined;
|
|
351
|
+
this.tableManager.updateTable({
|
|
352
|
+
...opts,
|
|
353
|
+
getCellValue: (cellAddress) => this.getCellValue(cellAddress)
|
|
354
|
+
});
|
|
355
|
+
const newTable = this.tableManager.getTable({
|
|
356
|
+
workbookName: opts.workbookName,
|
|
357
|
+
name: opts.tableName
|
|
358
|
+
});
|
|
359
|
+
this.emitMutation({
|
|
360
|
+
touchedCells: import_mutation_invalidation.mergeTouchedCells(import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, [oldTableSnapshot]), import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, [newTable])),
|
|
361
|
+
tableContextChangedCells: import_mutation_invalidation.buildTableContextChangedCells(this.workbookManager, [oldTableSnapshot, newTable]),
|
|
362
|
+
resourceKeys: [
|
|
363
|
+
import_resource_keys.getTableResourceKey({
|
|
364
|
+
workbookName: opts.workbookName,
|
|
365
|
+
tableName: opts.tableName
|
|
366
|
+
})
|
|
367
|
+
]
|
|
368
|
+
});
|
|
257
369
|
}
|
|
258
370
|
removeTable(opts) {
|
|
259
|
-
|
|
371
|
+
const oldTable = this.tableManager.getTable({
|
|
372
|
+
workbookName: opts.workbookName,
|
|
373
|
+
name: opts.tableName
|
|
374
|
+
});
|
|
375
|
+
const oldTableSnapshot = oldTable ? { ...oldTable, headers: new Map(oldTable.headers) } : undefined;
|
|
376
|
+
const found = this.tableManager.removeTable(opts);
|
|
377
|
+
if (found) {
|
|
378
|
+
this.emitMutation({
|
|
379
|
+
touchedCells: import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, [
|
|
380
|
+
oldTableSnapshot
|
|
381
|
+
]),
|
|
382
|
+
tableContextChangedCells: import_mutation_invalidation.buildTableContextChangedCells(this.workbookManager, [oldTableSnapshot]),
|
|
383
|
+
resourceKeys: [
|
|
384
|
+
import_resource_keys.getTableResourceKey({
|
|
385
|
+
workbookName: opts.workbookName,
|
|
386
|
+
tableName: opts.tableName
|
|
387
|
+
})
|
|
388
|
+
]
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
getAllTables() {
|
|
393
|
+
return Array.from(this.tableManager.tables.values()).flatMap((tables) => Array.from(tables.values()).map((table) => ({
|
|
394
|
+
...table,
|
|
395
|
+
headers: new Map(table.headers)
|
|
396
|
+
})));
|
|
260
397
|
}
|
|
261
398
|
hasTable(opts) {
|
|
262
399
|
return !!this.tableManager.getTable({
|
|
@@ -271,7 +408,21 @@ class FormulaEngine {
|
|
|
271
408
|
});
|
|
272
409
|
}
|
|
273
410
|
resetTables(tables) {
|
|
274
|
-
|
|
411
|
+
const oldTables = this.getAllTables();
|
|
412
|
+
const newTables = Array.from(tables.values()).flatMap((workbookTables) => Array.from(workbookTables.values()));
|
|
413
|
+
const resourceKeys = new Set;
|
|
414
|
+
for (const table of [...oldTables, ...newTables]) {
|
|
415
|
+
resourceKeys.add(import_resource_keys.getTableResourceKey({
|
|
416
|
+
workbookName: table.workbookName,
|
|
417
|
+
tableName: table.name
|
|
418
|
+
}));
|
|
419
|
+
}
|
|
420
|
+
this.tableManager.resetTables(tables);
|
|
421
|
+
this.emitMutation({
|
|
422
|
+
touchedCells: import_mutation_invalidation.mergeTouchedCells(import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, oldTables), import_mutation_invalidation.buildTableTouchedCells(this.workbookManager, newTables)),
|
|
423
|
+
tableContextChangedCells: import_mutation_invalidation.buildTableContextChangedCells(this.workbookManager, [...oldTables, ...newTables]),
|
|
424
|
+
resourceKeys: Array.from(resourceKeys)
|
|
425
|
+
});
|
|
275
426
|
}
|
|
276
427
|
getTables(workbookName) {
|
|
277
428
|
return this.tableManager.getTables(workbookName);
|
|
@@ -279,28 +430,15 @@ class FormulaEngine {
|
|
|
279
430
|
isCellInTable(cellAddress) {
|
|
280
431
|
return this.tableManager.isCellInTable(cellAddress);
|
|
281
432
|
}
|
|
282
|
-
getTableDataCells(table) {
|
|
283
|
-
const { start, endRow, headers } = table;
|
|
284
|
-
const dataStartRow = start.rowIndex + 1;
|
|
285
|
-
const endColIndex = start.colIndex + headers.size - 1;
|
|
286
|
-
const rangeAddress = {
|
|
287
|
-
workbookName: table.workbookName,
|
|
288
|
-
sheetName: table.sheetName,
|
|
289
|
-
range: {
|
|
290
|
-
start: { col: start.colIndex, row: dataStartRow },
|
|
291
|
-
end: {
|
|
292
|
-
col: { type: "number", value: endColIndex },
|
|
293
|
-
row: endRow
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
return this.workbookManager.getCellsInRange(rangeAddress);
|
|
298
|
-
}
|
|
299
433
|
addConditionalStyle(style) {
|
|
300
|
-
this.
|
|
434
|
+
this.styleManager.addConditionalStyle(style);
|
|
435
|
+
this.emitUpdate();
|
|
301
436
|
}
|
|
302
437
|
removeConditionalStyle(workbookName, index) {
|
|
303
|
-
this.
|
|
438
|
+
const removed = this.styleManager.removeConditionalStyle(workbookName, index);
|
|
439
|
+
if (removed) {
|
|
440
|
+
this.emitUpdate();
|
|
441
|
+
}
|
|
304
442
|
}
|
|
305
443
|
getConditionalStyleCount(workbookName) {
|
|
306
444
|
const allStyles = this.styleManager.getAllConditionalStyles();
|
|
@@ -319,10 +457,14 @@ class FormulaEngine {
|
|
|
319
457
|
return this.styleManager.getAllConditionalStyles();
|
|
320
458
|
}
|
|
321
459
|
addCellStyle(style) {
|
|
322
|
-
this.
|
|
460
|
+
this.styleManager.addCellStyle(style);
|
|
461
|
+
this.emitUpdate();
|
|
323
462
|
}
|
|
324
463
|
removeCellStyle(workbookName, index) {
|
|
325
|
-
this.
|
|
464
|
+
const removed = this.styleManager.removeCellStyle(workbookName, index);
|
|
465
|
+
if (removed) {
|
|
466
|
+
this.emitUpdate();
|
|
467
|
+
}
|
|
326
468
|
}
|
|
327
469
|
getCellStyleCount(workbookName) {
|
|
328
470
|
const allStyles = this.styleManager.getAllCellStyles();
|
|
@@ -335,13 +477,69 @@ class FormulaEngine {
|
|
|
335
477
|
return this.styleManager.getStyleForRange(range);
|
|
336
478
|
}
|
|
337
479
|
clearCellStyles(range) {
|
|
338
|
-
this.
|
|
480
|
+
this.styleManager.clearCellStyles(range);
|
|
481
|
+
this.emitUpdate();
|
|
482
|
+
}
|
|
483
|
+
getTopLeftCell(cells) {
|
|
484
|
+
let topLeft = cells[0];
|
|
485
|
+
for (const cell of cells) {
|
|
486
|
+
if (cell.rowIndex < topLeft.rowIndex || cell.rowIndex === topLeft.rowIndex && cell.colIndex < topLeft.colIndex) {
|
|
487
|
+
topLeft = cell;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return topLeft;
|
|
491
|
+
}
|
|
492
|
+
dedupeAddresses(addresses) {
|
|
493
|
+
return Array.from(new Map(addresses.map((address) => [import_mutation_invalidation.getMutationAddressKey(address), address])).values());
|
|
494
|
+
}
|
|
495
|
+
getPasteTouchedAddresses(source, target, options) {
|
|
496
|
+
if (source.length === 0) {
|
|
497
|
+
return [];
|
|
498
|
+
}
|
|
499
|
+
const topLeft = this.getTopLeftCell(source);
|
|
500
|
+
const colOffset = target.colIndex - topLeft.colIndex;
|
|
501
|
+
const rowOffset = target.rowIndex - topLeft.rowIndex;
|
|
502
|
+
const targetCells = source.map((sourceCell) => ({
|
|
503
|
+
workbookName: target.workbookName,
|
|
504
|
+
sheetName: target.sheetName,
|
|
505
|
+
colIndex: sourceCell.colIndex + colOffset,
|
|
506
|
+
rowIndex: sourceCell.rowIndex + rowOffset
|
|
507
|
+
}));
|
|
508
|
+
return this.dedupeAddresses(options.cut ? [...source, ...targetCells] : targetCells);
|
|
339
509
|
}
|
|
340
510
|
pasteCells(source, target, options) {
|
|
341
|
-
|
|
511
|
+
if (source.length === 0) {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const touchedAddresses = this.getPasteTouchedAddresses(source, target, options);
|
|
515
|
+
const before = import_mutation_invalidation.captureCellContents(this.workbookManager, touchedAddresses);
|
|
516
|
+
this.copyManager.pasteCells(source, target, options);
|
|
517
|
+
const after = import_mutation_invalidation.captureCellContents(this.workbookManager, touchedAddresses);
|
|
518
|
+
this.emitMutation({
|
|
519
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells(touchedAddresses.map((address) => ({
|
|
520
|
+
address,
|
|
521
|
+
before: before.get(import_mutation_invalidation.getMutationAddressKey(address)),
|
|
522
|
+
after: after.get(import_mutation_invalidation.getMutationAddressKey(address))
|
|
523
|
+
}))),
|
|
524
|
+
resourceKeys: []
|
|
525
|
+
});
|
|
342
526
|
}
|
|
343
527
|
fillAreas(seedRange, targetRanges, options) {
|
|
344
|
-
|
|
528
|
+
const touchedAddresses = this.dedupeAddresses([
|
|
529
|
+
...targetRanges.flatMap((targetRange) => import_mutation_invalidation.getFiniteRangeAddresses(targetRange)),
|
|
530
|
+
...options.cut ? import_mutation_invalidation.getFiniteRangeAddresses(seedRange) : []
|
|
531
|
+
]);
|
|
532
|
+
const before = import_mutation_invalidation.captureCellContents(this.workbookManager, touchedAddresses);
|
|
533
|
+
this.copyManager.fillAreas(seedRange, targetRanges, options);
|
|
534
|
+
const after = import_mutation_invalidation.captureCellContents(this.workbookManager, touchedAddresses);
|
|
535
|
+
this.emitMutation({
|
|
536
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells(touchedAddresses.map((address) => ({
|
|
537
|
+
address,
|
|
538
|
+
before: before.get(import_mutation_invalidation.getMutationAddressKey(address)),
|
|
539
|
+
after: after.get(import_mutation_invalidation.getMutationAddressKey(address))
|
|
540
|
+
}))),
|
|
541
|
+
resourceKeys: []
|
|
542
|
+
});
|
|
345
543
|
}
|
|
346
544
|
smartPaste(sourceCells, pasteSelection, options) {
|
|
347
545
|
if (sourceCells.length === 0) {
|
|
@@ -422,17 +620,27 @@ class FormulaEngine {
|
|
|
422
620
|
return { minCol, minRow, maxCol, maxRow };
|
|
423
621
|
}
|
|
424
622
|
moveCell(source, target) {
|
|
425
|
-
this.
|
|
623
|
+
this.pasteCells([source], target, {
|
|
624
|
+
cut: true,
|
|
625
|
+
type: "formula",
|
|
626
|
+
include: "all"
|
|
627
|
+
});
|
|
426
628
|
}
|
|
427
629
|
moveRange(sourceRange, target) {
|
|
428
|
-
|
|
630
|
+
const cells = this.copyManager.expandRangeToCells(sourceRange);
|
|
631
|
+
this.pasteCells(cells, target, {
|
|
632
|
+
cut: true,
|
|
633
|
+
type: "formula",
|
|
634
|
+
include: "all"
|
|
635
|
+
});
|
|
429
636
|
}
|
|
430
637
|
addSheet(opts) {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
638
|
+
const sheet = this.workbookManager.addSheet(opts);
|
|
639
|
+
this.namedExpressionManager.addSheet(opts);
|
|
640
|
+
this.emitMutation({
|
|
641
|
+
touchedCells: [],
|
|
642
|
+
resourceKeys: [import_resource_keys.getSheetResourceKey(opts)]
|
|
643
|
+
});
|
|
436
644
|
return sheet;
|
|
437
645
|
}
|
|
438
646
|
createSheet(opts) {
|
|
@@ -443,10 +651,40 @@ class FormulaEngine {
|
|
|
443
651
|
});
|
|
444
652
|
}
|
|
445
653
|
removeSheet(opts) {
|
|
446
|
-
|
|
654
|
+
const resourceKeys = this.getSheetResourceKeys(opts);
|
|
655
|
+
this.workbookManager.removeSheet(opts);
|
|
656
|
+
this.namedExpressionManager.removeSheet(opts);
|
|
657
|
+
this.tableManager.removeSheet(opts);
|
|
658
|
+
this.styleManager.removeSheetStyles(opts.workbookName, opts.sheetName);
|
|
659
|
+
this.referenceManager.invalidateSheet(opts.workbookName, opts.sheetName);
|
|
660
|
+
this.emitMutation({
|
|
661
|
+
touchedCells: [],
|
|
662
|
+
resourceKeys,
|
|
663
|
+
removedScopes: [{ type: "sheet", ...opts }]
|
|
664
|
+
});
|
|
447
665
|
}
|
|
448
666
|
renameSheet(opts) {
|
|
449
|
-
|
|
667
|
+
const oldResourceKeys = this.getSheetResourceKeys(opts);
|
|
668
|
+
this.workbookManager.renameSheet(opts);
|
|
669
|
+
this.namedExpressionManager.renameSheet(opts);
|
|
670
|
+
this.tableManager.updateTablesForSheetRename(opts);
|
|
671
|
+
this.styleManager.updateSheetName(opts.workbookName, opts.sheetName, opts.newSheetName);
|
|
672
|
+
const changedCells = this.workbookManager.updateAllFormulas((formula) => import_sheet_renamer.renameSheetInFormula({
|
|
673
|
+
formula,
|
|
674
|
+
oldSheetName: opts.sheetName,
|
|
675
|
+
newSheetName: opts.newSheetName
|
|
676
|
+
}));
|
|
677
|
+
this.referenceManager.updateSheetName(opts.workbookName, opts.sheetName, opts.newSheetName);
|
|
678
|
+
this.emitMutation({
|
|
679
|
+
touchedCells: import_mutation_invalidation.buildFormulaTouchedCells(changedCells),
|
|
680
|
+
resourceKeys: Array.from(new Set([
|
|
681
|
+
...oldResourceKeys,
|
|
682
|
+
...this.getSheetResourceKeys({
|
|
683
|
+
workbookName: opts.workbookName,
|
|
684
|
+
sheetName: opts.newSheetName
|
|
685
|
+
})
|
|
686
|
+
]))
|
|
687
|
+
});
|
|
450
688
|
}
|
|
451
689
|
hasSheet(opts) {
|
|
452
690
|
return !!this.workbookManager.getSheet(opts);
|
|
@@ -472,35 +710,234 @@ class FormulaEngine {
|
|
|
472
710
|
getSheetSerialized(opts) {
|
|
473
711
|
return this.workbookManager.getSheetSerialized(opts);
|
|
474
712
|
}
|
|
713
|
+
search(query, options) {
|
|
714
|
+
return this.workbookManager.search(query, options);
|
|
715
|
+
}
|
|
716
|
+
replace(query, replacement, target, options) {
|
|
717
|
+
const prepared = this.workbookManager.prepareReplace(query, replacement, target, options);
|
|
718
|
+
this.workbookManager.setCellContent(prepared.address, prepared.afterContent);
|
|
719
|
+
this.emitMutation({
|
|
720
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells([
|
|
721
|
+
{
|
|
722
|
+
address: prepared.address,
|
|
723
|
+
before: prepared.beforeContent,
|
|
724
|
+
after: prepared.afterContent
|
|
725
|
+
}
|
|
726
|
+
]),
|
|
727
|
+
resourceKeys: []
|
|
728
|
+
});
|
|
729
|
+
return prepared.change;
|
|
730
|
+
}
|
|
731
|
+
replaceAll(query, replacement, options) {
|
|
732
|
+
const preparedReplacements = this.workbookManager.prepareReplaceAll(query, replacement, options);
|
|
733
|
+
if (preparedReplacements.length === 0) {
|
|
734
|
+
return [];
|
|
735
|
+
}
|
|
736
|
+
for (const prepared of preparedReplacements) {
|
|
737
|
+
this.workbookManager.setCellContent(prepared.address, prepared.afterContent);
|
|
738
|
+
}
|
|
739
|
+
this.emitMutation({
|
|
740
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells(preparedReplacements.map((prepared) => ({
|
|
741
|
+
address: prepared.address,
|
|
742
|
+
before: prepared.beforeContent,
|
|
743
|
+
after: prepared.afterContent
|
|
744
|
+
}))),
|
|
745
|
+
resourceKeys: []
|
|
746
|
+
});
|
|
747
|
+
return preparedReplacements.flatMap((prepared) => prepared.changes);
|
|
748
|
+
}
|
|
475
749
|
addWorkbook(workbookName) {
|
|
476
|
-
this.
|
|
750
|
+
this.workbookManager.addWorkbook(workbookName);
|
|
751
|
+
this.namedExpressionManager.addWorkbook(workbookName);
|
|
752
|
+
this.tableManager.addWorkbook(workbookName);
|
|
753
|
+
this.emitMutation({
|
|
754
|
+
touchedCells: [],
|
|
755
|
+
resourceKeys: [import_resource_keys.getWorkbookResourceKey(workbookName)]
|
|
756
|
+
});
|
|
477
757
|
}
|
|
478
758
|
removeWorkbook(workbookName) {
|
|
479
|
-
|
|
759
|
+
const resourceKeys = this.getWorkbookResourceKeys(workbookName);
|
|
760
|
+
this.workbookManager.removeWorkbook(workbookName);
|
|
761
|
+
this.namedExpressionManager.removeWorkbook(workbookName);
|
|
762
|
+
this.tableManager.removeWorkbook(workbookName);
|
|
763
|
+
this.styleManager.removeWorkbookStyles(workbookName);
|
|
764
|
+
this.referenceManager.invalidateWorkbook(workbookName);
|
|
765
|
+
this.emitMutation({
|
|
766
|
+
touchedCells: [],
|
|
767
|
+
resourceKeys,
|
|
768
|
+
removedScopes: [{ type: "workbook", workbookName }]
|
|
769
|
+
});
|
|
480
770
|
}
|
|
481
771
|
hasWorkbook(workbookName) {
|
|
482
772
|
return this.workbookManager.getWorkbooks().has(workbookName);
|
|
483
773
|
}
|
|
484
774
|
cloneWorkbook(fromWorkbookName, toWorkbookName) {
|
|
485
|
-
|
|
775
|
+
const sourceWorkbook = this.workbookManager.getWorkbooks().get(fromWorkbookName);
|
|
776
|
+
if (!sourceWorkbook) {
|
|
777
|
+
throw new Error(`Source workbook "${fromWorkbookName}" not found`);
|
|
778
|
+
}
|
|
779
|
+
if (this.workbookManager.getWorkbooks().has(toWorkbookName)) {
|
|
780
|
+
throw new Error(`Target workbook "${toWorkbookName}" already exists`);
|
|
781
|
+
}
|
|
782
|
+
this.workbookManager.addWorkbook(toWorkbookName);
|
|
783
|
+
this.namedExpressionManager.addWorkbook(toWorkbookName);
|
|
784
|
+
this.tableManager.addWorkbook(toWorkbookName);
|
|
785
|
+
for (const [sheetName, sheet] of sourceWorkbook.sheets) {
|
|
786
|
+
this.workbookManager.addSheet({
|
|
787
|
+
workbookName: toWorkbookName,
|
|
788
|
+
sheetName
|
|
789
|
+
});
|
|
790
|
+
this.namedExpressionManager.addSheet({
|
|
791
|
+
workbookName: toWorkbookName,
|
|
792
|
+
sheetName
|
|
793
|
+
});
|
|
794
|
+
this.workbookManager.setSheetContent({ workbookName: toWorkbookName, sheetName }, new Map(sheet.content));
|
|
795
|
+
const targetSheet = this.workbookManager.getSheet({
|
|
796
|
+
workbookName: toWorkbookName,
|
|
797
|
+
sheetName
|
|
798
|
+
});
|
|
799
|
+
if (targetSheet) {
|
|
800
|
+
targetSheet.metadata = new Map(sheet.metadata);
|
|
801
|
+
if (sheet.sheetMetadata !== undefined) {
|
|
802
|
+
targetSheet.sheetMetadata = structuredClone(sheet.sheetMetadata);
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
const targetWorkbook = this.workbookManager.getWorkbooks().get(toWorkbookName);
|
|
807
|
+
if (targetWorkbook && sourceWorkbook.workbookMetadata !== undefined) {
|
|
808
|
+
targetWorkbook.workbookMetadata = structuredClone(sourceWorkbook.workbookMetadata);
|
|
809
|
+
}
|
|
810
|
+
const namedExpressions = this.namedExpressionManager.getNamedExpressions();
|
|
811
|
+
const sourceWorkbookExpressions = namedExpressions.workbookExpressions.get(fromWorkbookName);
|
|
812
|
+
if (sourceWorkbookExpressions) {
|
|
813
|
+
for (const [expressionName, expression] of sourceWorkbookExpressions) {
|
|
814
|
+
this.namedExpressionManager.addNamedExpression({
|
|
815
|
+
expressionName,
|
|
816
|
+
expression: expression.expression,
|
|
817
|
+
workbookName: toWorkbookName
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
const sourceSheetExpressions = namedExpressions.sheetExpressions.get(fromWorkbookName);
|
|
822
|
+
if (sourceSheetExpressions) {
|
|
823
|
+
for (const [sheetName, expressions] of sourceSheetExpressions) {
|
|
824
|
+
for (const [expressionName, expression] of expressions) {
|
|
825
|
+
this.namedExpressionManager.addNamedExpression({
|
|
826
|
+
expressionName,
|
|
827
|
+
expression: expression.expression,
|
|
828
|
+
workbookName: toWorkbookName,
|
|
829
|
+
sheetName
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
const sourceTables = this.tableManager.tables.get(fromWorkbookName);
|
|
835
|
+
if (sourceTables) {
|
|
836
|
+
for (const [tableName] of sourceTables) {
|
|
837
|
+
this.tableManager.copyTable({ workbookName: fromWorkbookName, tableName }, { workbookName: toWorkbookName, tableName });
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
for (const style of this.styleManager.getAllConditionalStyles()) {
|
|
841
|
+
if (style.areas.some((area) => area.workbookName === fromWorkbookName)) {
|
|
842
|
+
this.styleManager.addConditionalStyle({
|
|
843
|
+
...style,
|
|
844
|
+
areas: style.areas.map((area) => area.workbookName === fromWorkbookName ? { ...area, workbookName: toWorkbookName } : area)
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
for (const style of this.styleManager.getAllCellStyles()) {
|
|
849
|
+
if (style.areas.some((area) => area.workbookName === fromWorkbookName)) {
|
|
850
|
+
this.styleManager.addCellStyle({
|
|
851
|
+
...style,
|
|
852
|
+
areas: style.areas.map((area) => area.workbookName === fromWorkbookName ? { ...area, workbookName: toWorkbookName } : area)
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
this.workbookManager.updateFormulasForWorkbook(toWorkbookName, (formula) => import_workbook_renamer.renameWorkbookInFormula({
|
|
857
|
+
formula,
|
|
858
|
+
oldWorkbookName: fromWorkbookName,
|
|
859
|
+
newWorkbookName: toWorkbookName
|
|
860
|
+
}));
|
|
861
|
+
this.emitMutation({
|
|
862
|
+
touchedCells: [],
|
|
863
|
+
resourceKeys: [import_resource_keys.getWorkbookResourceKey(toWorkbookName)]
|
|
864
|
+
});
|
|
486
865
|
}
|
|
487
866
|
renameWorkbook(opts) {
|
|
488
|
-
|
|
867
|
+
const oldResourceKeys = this.getWorkbookResourceKeys(opts.workbookName);
|
|
868
|
+
this.workbookManager.renameWorkbook(opts);
|
|
869
|
+
this.namedExpressionManager.renameWorkbook(opts);
|
|
870
|
+
this.tableManager.updateTablesForWorkbookRename(opts);
|
|
871
|
+
this.styleManager.updateWorkbookName(opts.workbookName, opts.newWorkbookName);
|
|
872
|
+
const changedCells = this.workbookManager.updateAllFormulas((formula) => import_workbook_renamer.renameWorkbookInFormula({
|
|
873
|
+
formula,
|
|
874
|
+
oldWorkbookName: opts.workbookName,
|
|
875
|
+
newWorkbookName: opts.newWorkbookName
|
|
876
|
+
}));
|
|
877
|
+
this.referenceManager.updateWorkbookName(opts.workbookName, opts.newWorkbookName);
|
|
878
|
+
this.emitMutation({
|
|
879
|
+
touchedCells: import_mutation_invalidation.buildFormulaTouchedCells(changedCells),
|
|
880
|
+
resourceKeys: Array.from(new Set([
|
|
881
|
+
...oldResourceKeys,
|
|
882
|
+
...this.getWorkbookResourceKeys(opts.newWorkbookName)
|
|
883
|
+
]))
|
|
884
|
+
});
|
|
489
885
|
}
|
|
490
886
|
getWorkbooks() {
|
|
491
887
|
return this.workbookManager.getWorkbooks();
|
|
492
888
|
}
|
|
493
889
|
setSheetContent(opts, content) {
|
|
494
|
-
|
|
890
|
+
const previousContent = this.getExistingSheetContent(opts);
|
|
891
|
+
this.workbookManager.setSheetContent(opts, content);
|
|
892
|
+
this.emitMutation({
|
|
893
|
+
touchedCells: import_mutation_invalidation.buildSheetContentTouchedCells(opts, previousContent, content),
|
|
894
|
+
resourceKeys: []
|
|
895
|
+
});
|
|
495
896
|
}
|
|
496
897
|
setCellContent(address, content) {
|
|
497
|
-
|
|
898
|
+
const previousValue = this.workbookManager.getCellContent(address);
|
|
899
|
+
this.workbookManager.setCellContent(address, content);
|
|
900
|
+
this.emitMutation({
|
|
901
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells([
|
|
902
|
+
{
|
|
903
|
+
address,
|
|
904
|
+
before: previousValue,
|
|
905
|
+
after: content
|
|
906
|
+
}
|
|
907
|
+
]),
|
|
908
|
+
resourceKeys: []
|
|
909
|
+
});
|
|
498
910
|
}
|
|
499
911
|
autoFill(opts, seedRange, fillRanges, direction) {
|
|
500
|
-
|
|
912
|
+
const touchedAddresses = this.dedupeAddresses(fillRanges.flatMap((range) => import_mutation_invalidation.getFiniteRangeAddresses({
|
|
913
|
+
workbookName: opts.workbookName,
|
|
914
|
+
sheetName: opts.sheetName,
|
|
915
|
+
range
|
|
916
|
+
})));
|
|
917
|
+
const before = import_mutation_invalidation.captureCellContents(this.workbookManager, touchedAddresses);
|
|
918
|
+
this.autoFillManager.fill(opts, seedRange, fillRanges, direction);
|
|
919
|
+
const after = import_mutation_invalidation.captureCellContents(this.workbookManager, touchedAddresses);
|
|
920
|
+
this.emitMutation({
|
|
921
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells(touchedAddresses.map((address) => ({
|
|
922
|
+
address,
|
|
923
|
+
before: before.get(import_mutation_invalidation.getMutationAddressKey(address)),
|
|
924
|
+
after: after.get(import_mutation_invalidation.getMutationAddressKey(address))
|
|
925
|
+
}))),
|
|
926
|
+
resourceKeys: []
|
|
927
|
+
});
|
|
501
928
|
}
|
|
502
929
|
clearSpreadsheetRange(address) {
|
|
503
|
-
|
|
930
|
+
const clearedCells = Array.from(this.workbookManager.iterateCellsInRange(address));
|
|
931
|
+
const before = import_mutation_invalidation.captureCellContents(this.workbookManager, clearedCells);
|
|
932
|
+
this.workbookManager.clearSpreadsheetRange(address);
|
|
933
|
+
this.emitMutation({
|
|
934
|
+
touchedCells: import_mutation_invalidation.buildTouchedCells(clearedCells.map((cellAddress) => ({
|
|
935
|
+
address: cellAddress,
|
|
936
|
+
before: before.get(import_mutation_invalidation.getMutationAddressKey(cellAddress)),
|
|
937
|
+
after: undefined
|
|
938
|
+
}))),
|
|
939
|
+
resourceKeys: []
|
|
940
|
+
});
|
|
504
941
|
}
|
|
505
942
|
getState() {
|
|
506
943
|
return {
|
|
@@ -556,60 +993,8 @@ class FormulaEngine {
|
|
|
556
993
|
dependency: deserialized.managers.dependency,
|
|
557
994
|
cache: deserialized.managers.cache
|
|
558
995
|
}, this.evaluationManager);
|
|
559
|
-
this.commandExecutor.clearHistory();
|
|
560
|
-
this.commandExecutor.clearActionLog();
|
|
561
996
|
this.eventManager.emitUpdate();
|
|
562
997
|
}
|
|
563
|
-
undo() {
|
|
564
|
-
return this.commandExecutor.undo();
|
|
565
|
-
}
|
|
566
|
-
redo() {
|
|
567
|
-
return this.commandExecutor.redo();
|
|
568
|
-
}
|
|
569
|
-
canUndo() {
|
|
570
|
-
return this.commandExecutor.canUndo();
|
|
571
|
-
}
|
|
572
|
-
canRedo() {
|
|
573
|
-
return this.commandExecutor.canRedo();
|
|
574
|
-
}
|
|
575
|
-
getActionLog() {
|
|
576
|
-
return this.commandExecutor.getActionLog();
|
|
577
|
-
}
|
|
578
|
-
clearActionLog() {
|
|
579
|
-
this.commandExecutor.clearActionLog();
|
|
580
|
-
}
|
|
581
|
-
clearHistory() {
|
|
582
|
-
this.commandExecutor.clearHistory();
|
|
583
|
-
}
|
|
584
|
-
getStructureCommandDeps() {
|
|
585
|
-
return {
|
|
586
|
-
workbookManager: this.workbookManager,
|
|
587
|
-
namedExpressionManager: this.namedExpressionManager,
|
|
588
|
-
tableManager: this.tableManager,
|
|
589
|
-
styleManager: this.styleManager,
|
|
590
|
-
referenceManager: this.referenceManager,
|
|
591
|
-
apiSchemaManager: this.schemaManager,
|
|
592
|
-
renameSheetInFormula: import_sheet_renamer.renameSheetInFormula,
|
|
593
|
-
renameWorkbookInFormula: import_workbook_renamer.renameWorkbookInFormula
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
getTableCommandDeps() {
|
|
597
|
-
return {
|
|
598
|
-
tableManager: this.tableManager,
|
|
599
|
-
namedExpressionManager: this.namedExpressionManager,
|
|
600
|
-
workbookManager: this.workbookManager,
|
|
601
|
-
apiSchemaManager: this.schemaManager,
|
|
602
|
-
getCellValue: (cell) => this.getCellValue(cell),
|
|
603
|
-
renameTableInFormula: import_table_renamer.renameTableInFormula
|
|
604
|
-
};
|
|
605
|
-
}
|
|
606
|
-
getNamedExpressionCommandDeps() {
|
|
607
|
-
return {
|
|
608
|
-
namedExpressionManager: this.namedExpressionManager,
|
|
609
|
-
workbookManager: this.workbookManager,
|
|
610
|
-
renameNamedExpressionInFormula: import_named_expression_renamer.renameNamedExpressionInFormula
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
998
|
}
|
|
614
999
|
|
|
615
|
-
//# debugId=
|
|
1000
|
+
//# debugId=6B63B5DF523C393264756E2164756E21
|