@univerjs/sheets-data-validation 0.4.1 → 0.4.2-nightly.202410301606

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.
@@ -0,0 +1,654 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
5
+ import { FRange, FWorkbook, FWorksheet } from "@univerjs/sheets/facade";
6
+ import { m as UpdateSheetDataValidationSettingCommand, n as UpdateSheetDataValidationOptionsCommand, U as UpdateSheetDataValidationRangeCommand, R as RemoveSheetDataValidationCommand, p as ClearRangeDataValidationCommand, A as AddSheetDataValidationCommand, l as SheetsDataValidationValidatorService, S as SheetDataValidationModel, o as RemoveSheetAllDataValidationCommand } from "../dv-validator-service-9VDfOza1.mjs";
7
+ import { generateRandomId, DataValidationType, DataValidationErrorStyle, DataValidationOperator, ICommandService, FUniver, toDisposable } from "@univerjs/core";
8
+ import { DataValidationModel, getRuleOptions } from "@univerjs/data-validation";
9
+ import { serializeRangeToRefString } from "@univerjs/engine-formula";
10
+ import { filter } from "rxjs";
11
+ const _FDataValidationBuilder = class _FDataValidationBuilder {
12
+ constructor(rule) {
13
+ __publicField(this, "_rule");
14
+ this._rule = rule != null ? rule : {
15
+ uid: generateRandomId(),
16
+ ranges: void 0,
17
+ type: DataValidationType.CUSTOM
18
+ };
19
+ }
20
+ /**
21
+ * Builds an FDataValidation instance based on the _rule property of the current class
22
+ *
23
+ * @returns {FDataValidation} A new instance of the FDataValidation class
24
+ */
25
+ build() {
26
+ return new FDataValidation(this._rule);
27
+ }
28
+ /**
29
+ * Creates a duplicate of the current DataValidationBuilder object
30
+ *
31
+ * @return {FDataValidationBuilder} A new instance of the DataValidationBuilder class
32
+ */
33
+ copy() {
34
+ return new _FDataValidationBuilder({
35
+ ...this._rule,
36
+ uid: generateRandomId()
37
+ });
38
+ }
39
+ /**
40
+ * Determines whether invalid data is allowed
41
+ *
42
+ * @returns {boolean} True if invalid data is allowed, False otherwise
43
+ */
44
+ getAllowInvalid() {
45
+ return this._rule.errorStyle !== DataValidationErrorStyle.STOP;
46
+ }
47
+ /**
48
+ * Gets the data validation type of the rule
49
+ *
50
+ * @returns {DataValidationType} The data validation type
51
+ */
52
+ getCriteriaType() {
53
+ return this._rule.type;
54
+ }
55
+ /**
56
+ * Gets the values used for criteria evaluation
57
+ *
58
+ * @returns {any[]} An array containing the operator, formula1, and formula2 values
59
+ */
60
+ getCriteriaValues() {
61
+ return [this._rule.operator, this._rule.formula1, this._rule.formula2];
62
+ }
63
+ /**
64
+ * Gets the help text information, which is used to provide users with guidance and support
65
+ *
66
+ * @returns {string | undefined} Returns the help text information. If there is no error message, it returns an undefined value.
67
+ */
68
+ getHelpText() {
69
+ return this._rule.error;
70
+ }
71
+ /**
72
+ * Sets the data validation type to CHECKBOX and sets the checked and unchecked values
73
+ *
74
+ * @param checkedValue The value when the checkbox is checked (Optional)
75
+ * @param uncheckedValue The value when the checkbox is unchecked (Optional)
76
+ * @returns The current instance of the FDataValidationBuilder class to allow for method chaining
77
+ */
78
+ requireCheckbox(checkedValue, uncheckedValue) {
79
+ return this._rule.type = DataValidationType.CHECKBOX, this._rule.formula1 = checkedValue, this._rule.formula2 = uncheckedValue, this;
80
+ }
81
+ /**
82
+ * Set the data validation type to DATE and configure the validation rules to be after a specific date
83
+ *
84
+ * @param date The date to compare against. The formatted date string will be set as formula1
85
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
86
+ */
87
+ requireDateAfter(date) {
88
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = date.toLocaleDateString(), this._rule.operator = DataValidationOperator.GREATER_THAN, this;
89
+ }
90
+ /**
91
+ * Set the data validation type to DATE and configure the validation rules to be before a specific date
92
+ *
93
+ * @param date The date to compare against. The formatted date string will be set as formula1
94
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
95
+ */
96
+ requireDateBefore(date) {
97
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = date.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.LESS_THAN, this;
98
+ }
99
+ /**
100
+ * Set the data validation type to DATE and configure the validation rules to be within a specific date range
101
+ *
102
+ * @param start The starting date of the range. The formatted date string will be set as formula1
103
+ * @param end The ending date of the range. The formatted date string will be set as formula2
104
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
105
+ */
106
+ requireDateBetween(start, end) {
107
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = start.toLocaleDateString(), this._rule.formula2 = end.toLocaleDateString(), this._rule.operator = DataValidationOperator.BETWEEN, this;
108
+ }
109
+ /**
110
+ * Set the data validation type to DATE and configure the validation rules to be equal to a specific date
111
+ *
112
+ * @param date The date to compare against. The formatted date string will be set as formula1
113
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
114
+ */
115
+ requireDateEqualTo(date) {
116
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = date.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.EQUAL, this;
117
+ }
118
+ /**
119
+ * Set the data validation type to DATE and configure the validation rules to be not within a specific date range
120
+ *
121
+ * @param start The starting date of the date range
122
+ * @param end The ending date of the date range
123
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
124
+ */
125
+ requireDateNotBetween(start, end) {
126
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = start.toLocaleDateString(), this._rule.formula2 = end.toLocaleDateString(), this._rule.operator = DataValidationOperator.NOT_BETWEEN, this;
127
+ }
128
+ /**
129
+ * Set the data validation type to DATE and configure the validation rules to be on or after a specific date
130
+ *
131
+ * @param date The date to compare against. The formatted date string will be set as formula1
132
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
133
+ */
134
+ requireDateOnOrAfter(date) {
135
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = date.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.GREATER_THAN_OR_EQUAL, this;
136
+ }
137
+ /**
138
+ * Set the data validation type to DATE and configure the validation rules to be on or before a specific date
139
+ *
140
+ * @param date The date to compare against. The formatted date string will be set as formula1
141
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
142
+ */
143
+ requireDateOnOrBefore(date) {
144
+ return this._rule.type = DataValidationType.DATE, this._rule.formula1 = date.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.LESS_THAN_OR_EQUAL, this;
145
+ }
146
+ /**
147
+ * Requires that a custom formula be satisfied.
148
+ * Sets the data validation type to CUSTOM and configures the validation rule based on the provided formula string.
149
+ *
150
+ * @param formula The formula string that needs to be satisfied.
151
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
152
+ */
153
+ requireFormulaSatisfied(formula) {
154
+ return this._rule.type = DataValidationType.CUSTOM, this._rule.formula1 = formula, this._rule.formula2 = void 0, this;
155
+ }
156
+ /**
157
+ * Requires the user to enter a number within a specific range, which can be integer or decimal.
158
+ * Sets the data validation type based on the isInteger parameter and configures the validation rules for the specified number range.
159
+ *
160
+ * @param start The starting value of the number range.
161
+ * @param end The ending value of the number range.
162
+ * @param isInteger Indicates whether the required number is an integer. Default is undefined, meaning it can be an integer or decimal.
163
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
164
+ */
165
+ requireNumberBetween(start, end, isInteger) {
166
+ return this._rule.formula1 = `${start}`, this._rule.formula2 = `${end}`, this._rule.operator = DataValidationOperator.BETWEEN, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
167
+ }
168
+ /**
169
+ * Requires the user to enter a number that is equal to a specific value, which can be an integer or a decimal.
170
+ * Sets the data validation type based on the isInteger parameter and configures the validation rules for the specified number.
171
+ *
172
+ * @param num The number to which the entered number should be equal.
173
+ * @param isInteger Indicates whether the required number is an integer. Default is undefined, meaning it can be an integer or a decimal.
174
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
175
+ */
176
+ requireNumberEqualTo(num, isInteger) {
177
+ return this._rule.formula1 = `${num}`, this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.EQUAL, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
178
+ }
179
+ /**
180
+ * Requires the user to enter a number that is greater than a specific value, which can be an integer or a decimal.
181
+ * Sets the data validation type based on the isInteger parameter and configures the validation rules for the specified number.
182
+ *
183
+ * @param num The number to which the entered number should be greater.
184
+ * @param isInteger Indicates whether the required number is an integer. Default is undefined, meaning it can be an integer or a decimal.
185
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
186
+ */
187
+ requireNumberGreaterThan(num, isInteger) {
188
+ return this._rule.formula1 = `${num}`, this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.GREATER_THAN, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
189
+ }
190
+ /**
191
+ * Requires the user to enter a number that is greater than or equal to a specific value, which can be an integer or a decimal.
192
+ * Sets the data validation type based on the isInteger parameter and configures the validation rules for the specified number.
193
+ *
194
+ * @param num The number to which the entered number should be greater than or equal.
195
+ * @param isInteger Indicates whether the required number is an integer. Default is undefined, meaning it can be an integer or a decimal.
196
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
197
+ */
198
+ requireNumberGreaterThanOrEqualTo(num, isInteger) {
199
+ return this._rule.formula1 = `${num}`, this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.GREATER_THAN_OR_EQUAL, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
200
+ }
201
+ /**
202
+ * Requires the user to enter a number that is less than a specific value, which can be an integer or a decimal.
203
+ * Sets the data validation type based on the isInteger parameter and configures the validation rules for the specified number.
204
+ *
205
+ * @param num The number to which the entered number should be less.
206
+ * @param isInteger Indicates whether the required number is an integer. Default is undefined, meaning it can be an integer or a decimal.
207
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
208
+ */
209
+ requireNumberLessThan(num, isInteger) {
210
+ return this._rule.formula1 = `${num}`, this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.LESS_THAN, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
211
+ }
212
+ /**
213
+ * Sets the data validation rule to require a number less than or equal to a specified value
214
+ * The specified value can be an integer or a decimal
215
+ *
216
+ * @param num The number to which the entered number should be less than or equal
217
+ * @param isInteger Indicates whether the required number is an integer
218
+ * @return The current instance of the DataValidationBuilder class, allowing for method chaining
219
+ */
220
+ requireNumberLessThanOrEqualTo(num, isInteger) {
221
+ return this._rule.formula1 = `${num}`, this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.LESS_THAN_OR_EQUAL, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
222
+ }
223
+ /**
224
+ * Sets a data validation rule that requires the user to enter a number outside a specified range
225
+ * The specified range includes all integers and decimals
226
+ *
227
+ * @param start The starting point of the specified range
228
+ * @param end The end point of the specified range
229
+ * @param isInteger Optional parameter, indicating whether the number to be verified is an integer. Default value is false
230
+ * @return An instance of the FDataValidationBuilder class, allowing for method chaining
231
+ */
232
+ requireNumberNotBetween(start, end, isInteger) {
233
+ return this._rule.formula1 = `${start}`, this._rule.formula2 = `${end}`, this._rule.operator = DataValidationOperator.NOT_BETWEEN, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
234
+ }
235
+ /**
236
+ * Creates a data validation rule that requires the user to enter a number that is not equal to a specific value
237
+ * The specific value can be an integer or a decimal
238
+ *
239
+ * @param num The number to which the entered number should not be equal
240
+ * @param isInteger Indicates whether the required number is an integer. Default is undefined, meaning it can be an integer or a decimal
241
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining
242
+ */
243
+ requireNumberNotEqualTo(num, isInteger) {
244
+ return this._rule.formula1 = `${num}`, this._rule.formula2 = void 0, this._rule.operator = DataValidationOperator.NOT_EQUAL, this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL, this;
245
+ }
246
+ /**
247
+ * Sets a data validation rule that requires the user to enter a value from a list of specific values.
248
+ * The list can be displayed in a dropdown, and the user can choose multiple values according to the settings.
249
+ *
250
+ * @param values An array containing the specific values that the user can enter.
251
+ * @param multiple Optional parameter indicating whether the user can select multiple values. Default is false, meaning only one value can be selected.
252
+ * @param showDropdown Optional parameter indicating whether to display the list in a dropdown. Default is true, meaning the list will be displayed as a dropdown.
253
+ * @return An instance of the FDataValidationBuilder class, allowing for method chaining.
254
+ */
255
+ requireValueInList(values, multiple, showDropdown) {
256
+ return this._rule.type = multiple ? DataValidationType.LIST_MULTIPLE : DataValidationType.LIST, this._rule.formula1 = values.join(","), this._rule.formula2 = void 0, this._rule.showDropDown = showDropdown != null ? showDropdown : !0, this;
257
+ }
258
+ /**
259
+ * Sets a data validation rule that requires the user to enter a value within a specific range.
260
+ * The range is defined by an FRange object, which contains the unit ID, sheet name, and cell range.
261
+ *
262
+ * @param range An FRange object representing the range of values that the user can enter.
263
+ * @param multiple Optional parameter indicating whether the user can select multiple values. Default is false, meaning only one value can be selected.
264
+ * @param showDropdown Optional parameter indicating whether to display the list in a dropdown. Default is true, meaning the list will be displayed as a dropdown.
265
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
266
+ */
267
+ requireValueInRange(range, multiple, showDropdown) {
268
+ return this._rule.type = multiple ? DataValidationType.LIST_MULTIPLE : DataValidationType.LIST, this._rule.formula1 = `=${serializeRangeToRefString({
269
+ unitId: range.getUnitId(),
270
+ sheetName: range.getSheetName(),
271
+ range: range.getRange()
272
+ })}`, this._rule.formula2 = void 0, this._rule.showDropDown = showDropdown != null ? showDropdown : !0, this;
273
+ }
274
+ /**
275
+ * Sets whether to allow invalid data and configures the error style for data validation.
276
+ * If invalid data is not allowed, the error style will be set to STOP, indicating that data entry must stop upon encountering an error.
277
+ * If invalid data is allowed, the error style will be set to WARNING, indicating that a warning will be displayed when invalid data is entered, but data entry can continue.
278
+ *
279
+ * @param allowInvalidData A boolean value indicating whether to allow invalid data.
280
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
281
+ */
282
+ setAllowInvalid(allowInvalidData) {
283
+ return this._rule.errorStyle = allowInvalidData ? DataValidationErrorStyle.WARNING : DataValidationErrorStyle.STOP, this;
284
+ }
285
+ /**
286
+ * Sets the help text and enables the display of error messages for data validation.
287
+ * This method allows you to set a custom help text that will be displayed when the user enters invalid data.
288
+ *
289
+ * @param helpText The text to display as help information.
290
+ * @return The current instance of the FDataValidationBuilder class to allow for method chaining.
291
+ */
292
+ setHelpText(helpText) {
293
+ return this._rule.error = helpText, this._rule.showErrorMessage = !0, this;
294
+ }
295
+ /**
296
+ * Sets the criteria values for data validation.
297
+ * This method is used to configure the validation rules based on specific criteria values.
298
+ *
299
+ * @param type The type of data validation.
300
+ * @param values An array containing the criteria values.
301
+ * The array should have three elements: [operator, formula1, formula2].
302
+ * operator is a DataValidationOperator enum value, formula1 is the first formula, and formula2 is the second formula.
303
+ * @return The current instance of the FDataValidationBuilder class, allowing for method chaining.
304
+ */
305
+ withCriteriaValues(type, values) {
306
+ return this._rule.type = type, this._rule.operator = values[0], this._rule.formula1 = values[1], this._rule.formula2 = values[2], this;
307
+ }
308
+ /**
309
+ * Sets the options for the data validation rule.
310
+ * For details of options, please refer to https://univer.ai/typedoc/@univerjs/core/interfaces/IDataValidationRuleOptions
311
+ *
312
+ * @param options The options to set for the data validation rule.
313
+ * @returns The current instance of the FDataValidationBuilder class to allow for method chaining.
314
+ */
315
+ setOptions(options) {
316
+ return Object.assign(this._rule, options), this;
317
+ }
318
+ };
319
+ __name(_FDataValidationBuilder, "FDataValidationBuilder");
320
+ let FDataValidationBuilder = _FDataValidationBuilder;
321
+ const _FDataValidation = class _FDataValidation {
322
+ constructor(rule, worksheet) {
323
+ __publicField(this, "rule");
324
+ __publicField(this, "_worksheet");
325
+ this.rule = rule, this._worksheet = worksheet;
326
+ }
327
+ /**
328
+ * Gets whether invalid data is allowed based on the error style value.
329
+ *
330
+ * @return true if invalid data is allowed, false otherwise.
331
+ */
332
+ getAllowInvalid() {
333
+ return this.rule.errorStyle !== DataValidationErrorStyle.STOP;
334
+ }
335
+ /**
336
+ * Gets the data validation type of the rule
337
+ *
338
+ * @returns The data validation type
339
+ */
340
+ getCriteriaType() {
341
+ return this.rule.type;
342
+ }
343
+ /**
344
+ * Gets the values used for criteria evaluation
345
+ *
346
+ * @returns An array containing the operator, formula1, and formula2 values
347
+ */
348
+ getCriteriaValues() {
349
+ return [this.rule.operator, this.rule.formula1, this.rule.formula2];
350
+ }
351
+ /**
352
+ * Gets the help text information, which is used to provide users with guidance and support
353
+ *
354
+ * @returns Returns the help text information. If there is no error message, it returns an undefined value.
355
+ */
356
+ getHelpText() {
357
+ return this.rule.error;
358
+ }
359
+ /**
360
+ * Creates a new instance of FDataValidationBuilder using the current rule object.
361
+ * This method is useful for copying an existing data validation rule configuration.
362
+ *
363
+ * @return A new FDataValidationBuilder instance with the same rule configuration.
364
+ */
365
+ copy() {
366
+ return new FDataValidationBuilder(this.rule);
367
+ }
368
+ /**
369
+ * Gets whether the data validation rule is applied to the worksheet.
370
+ *
371
+ * @returns true if the rule is applied, false otherwise.
372
+ */
373
+ getApplied() {
374
+ if (!this._worksheet)
375
+ return !1;
376
+ const currentRule = this._worksheet.getInject().get(DataValidationModel).getRuleById(this._worksheet.getWorkbook().getUnitId(), this._worksheet.getSheetId(), this.rule.uid);
377
+ return !!(currentRule && currentRule.ranges.length);
378
+ }
379
+ /**
380
+ * Gets the ranges to which the data validation rule is applied.
381
+ *
382
+ * @returns An array of IRange objects representing the ranges to which the data validation rule is applied.
383
+ */
384
+ getRanges() {
385
+ var _a;
386
+ if (!this.getAllowInvalid())
387
+ return [];
388
+ const workbook = (_a = this._worksheet) == null ? void 0 : _a.getWorkbook(), sheetId = this.getSheetId();
389
+ if (!sheetId)
390
+ return [];
391
+ const worksheet = workbook == null ? void 0 : workbook.getSheetBySheetId(sheetId);
392
+ return !workbook || !worksheet ? [] : this.rule.ranges.map((range) => {
393
+ var _a2;
394
+ return (_a2 = this._worksheet) == null ? void 0 : _a2.getInject().createInstance(FRange, workbook, worksheet, range);
395
+ });
396
+ }
397
+ /**
398
+ * Gets the title of the error message dialog box.
399
+ *
400
+ * @returns The title of the error message dialog box.
401
+ */
402
+ getUnitId() {
403
+ var _a;
404
+ return (_a = this._worksheet) == null ? void 0 : _a.getWorkbook().getUnitId();
405
+ }
406
+ /**
407
+ * Gets the sheetId of the worksheet.
408
+ *
409
+ * @returns The sheetId of the worksheet.
410
+ */
411
+ getSheetId() {
412
+ var _a;
413
+ return (_a = this._worksheet) == null ? void 0 : _a.getSheetId();
414
+ }
415
+ /**
416
+ * Set Criteria for the data validation rule.
417
+ * @param type The type of data validation criteria.
418
+ * @param values An array containing the operator, formula1, and formula2 values.
419
+ * @returns true if the criteria is set successfully, false otherwise.
420
+ */
421
+ setCriteria(type, values) {
422
+ return this.getApplied() && !this._worksheet.getInject().get(ICommandService).syncExecuteCommand(UpdateSheetDataValidationSettingCommand.id, {
423
+ unitId: this.getUnitId(),
424
+ subUnitId: this.getSheetId(),
425
+ ruleId: this.rule.uid,
426
+ setting: {
427
+ operator: values[0],
428
+ formula1: values[1],
429
+ formula2: values[2],
430
+ type: this.rule.type
431
+ }
432
+ }) ? !1 : (this.rule.operator = values[0], this.rule.formula1 = values[1], this.rule.formula2 = values[2], this.rule.type = type, !0);
433
+ }
434
+ /**
435
+ * Set the options for the data validation rule.
436
+ * For details of options, please refer to https://univer.ai/typedoc/@univerjs/core/interfaces/IDataValidationRuleOptions
437
+ * @param options An object containing the options to set. `IDataValidationRuleOptions`
438
+ * @returns true if the options are set successfully, false otherwise.
439
+ */
440
+ setOptions(options) {
441
+ return this.getApplied() && !this._worksheet.getInject().get(ICommandService).syncExecuteCommand(UpdateSheetDataValidationOptionsCommand.id, {
442
+ unitId: this.getUnitId(),
443
+ subUnitId: this.getSheetId(),
444
+ ruleId: this.rule.uid,
445
+ options: {
446
+ ...getRuleOptions(this.rule),
447
+ ...options
448
+ }
449
+ }) ? !1 : (Object.assign(this.rule, options), !0);
450
+ }
451
+ /**
452
+ * Set the ranges to the data validation rule.
453
+ * @param ranges new ranges array.
454
+ * @returns true if the ranges are set successfully, false otherwise.
455
+ */
456
+ setRanges(ranges) {
457
+ return this.getApplied() && !this._worksheet.getInject().get(ICommandService).syncExecuteCommand(UpdateSheetDataValidationRangeCommand.id, {
458
+ unitId: this.getUnitId(),
459
+ subUnitId: this.getSheetId(),
460
+ ruleId: this.rule.uid,
461
+ ranges: ranges.map((range) => range.getRange())
462
+ }) ? !1 : (this.rule.ranges = ranges, !0);
463
+ }
464
+ /**
465
+ * Delete the data validation rule from the worksheet.
466
+ * @returns true if the rule is deleted successfully, false otherwise.
467
+ */
468
+ delete() {
469
+ return this.getApplied() ? this._worksheet.getInject().get(ICommandService).syncExecuteCommand(RemoveSheetDataValidationCommand.id, {
470
+ unitId: this.getUnitId(),
471
+ subUnitId: this.getSheetId(),
472
+ ruleId: this.rule.uid
473
+ }) : !1;
474
+ }
475
+ };
476
+ __name(_FDataValidation, "FDataValidation");
477
+ let FDataValidation = _FDataValidation;
478
+ const _FRangeDataValidationMixin = class _FRangeDataValidationMixin extends FRange {
479
+ async setDataValidation(rule) {
480
+ if (!rule)
481
+ return this._commandService.executeCommand(ClearRangeDataValidationCommand.id, {
482
+ unitId: this._workbook.getUnitId(),
483
+ subUnitId: this._worksheet.getSheetId(),
484
+ ranges: [this._range]
485
+ }), this;
486
+ const params = {
487
+ unitId: this._workbook.getUnitId(),
488
+ subUnitId: this._worksheet.getSheetId(),
489
+ rule: {
490
+ ...rule.rule,
491
+ ranges: [this._range]
492
+ }
493
+ };
494
+ return await this._commandService.executeCommand(AddSheetDataValidationCommand.id, params), this;
495
+ }
496
+ getDataValidation() {
497
+ const rule = this._injector.get(SheetsDataValidationValidatorService).getDataValidation(
498
+ this._workbook.getUnitId(),
499
+ this._worksheet.getSheetId(),
500
+ [this._range]
501
+ );
502
+ return rule && new FDataValidation(rule);
503
+ }
504
+ getDataValidations() {
505
+ return this._injector.get(SheetsDataValidationValidatorService).getDataValidations(
506
+ this._workbook.getUnitId(),
507
+ this._worksheet.getSheetId(),
508
+ [this._range]
509
+ ).map((rule) => new FDataValidation(rule));
510
+ }
511
+ async getValidatorStatus() {
512
+ return this._injector.get(SheetsDataValidationValidatorService).validatorRanges(
513
+ this._workbook.getUnitId(),
514
+ this._worksheet.getSheetId(),
515
+ [this._range]
516
+ );
517
+ }
518
+ };
519
+ __name(_FRangeDataValidationMixin, "FRangeDataValidationMixin");
520
+ let FRangeDataValidationMixin = _FRangeDataValidationMixin;
521
+ FRange.extend(FRangeDataValidationMixin);
522
+ const _FUnvierDataValidationMixin = class _FUnvierDataValidationMixin {
523
+ static newDataValidation() {
524
+ return new FDataValidationBuilder();
525
+ }
526
+ };
527
+ __name(_FUnvierDataValidationMixin, "FUnvierDataValidationMixin");
528
+ let FUnvierDataValidationMixin = _FUnvierDataValidationMixin;
529
+ FUniver.extend(FUnvierDataValidationMixin);
530
+ const _FWorkbookDataValidationMixin = class _FWorkbookDataValidationMixin extends FWorkbook {
531
+ _initialize() {
532
+ Object.defineProperty(this, "_dataValidationModel", {
533
+ get() {
534
+ return this._injector.get(SheetDataValidationModel);
535
+ }
536
+ });
537
+ }
538
+ /**
539
+ * get data validation validator status for current workbook
540
+ * @returns matrix of validator status
541
+ */
542
+ getValidatorStatus() {
543
+ return this._injector.get(SheetsDataValidationValidatorService).validatorWorkbook(this._workbook.getUnitId());
544
+ }
545
+ // region DataValidationHooks
546
+ /**
547
+ * The onDataValidationChange event is fired when the data validation rule of this sheet is changed.
548
+ * @param callback Callback function that will be called when the event is fired
549
+ * @returns A disposable object that can be used to unsubscribe from the event
550
+ */
551
+ onDataValidationChange(callback) {
552
+ return toDisposable(this._dataValidationModel.ruleChange$.pipe(filter((change) => change.unitId === this._workbook.getUnitId())).subscribe(callback));
553
+ }
554
+ /**
555
+ * The onDataValidationStatusChange event is fired when the data validation status of this sheet is changed.
556
+ * @param callback Callback function that will be called when the event is fired
557
+ * @returns A disposable object that can be used to unsubscribe from the event
558
+ */
559
+ onDataValidationStatusChange(callback) {
560
+ return toDisposable(this._dataValidationModel.validStatusChange$.pipe(filter((change) => change.unitId === this._workbook.getUnitId())).subscribe(callback));
561
+ }
562
+ /**
563
+ * The onBeforeAddDataValidation event is fired before the data validation rule is added.
564
+ * @param callback Callback function that will be called when the event is fired
565
+ * @returns A disposable object that can be used to unsubscribe from the event
566
+ */
567
+ onBeforeAddDataValidation(callback) {
568
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
569
+ const params = commandInfo.params;
570
+ if (commandInfo.id === AddSheetDataValidationCommand.id) {
571
+ if (params.unitId !== this._workbook.getUnitId())
572
+ return;
573
+ if (callback(params, options) === !1)
574
+ throw new Error("Command is stopped by the hook onBeforeAddDataValidation");
575
+ }
576
+ }));
577
+ }
578
+ onBeforeUpdateDataValidationCriteria(callback) {
579
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
580
+ const params = commandInfo.params;
581
+ if (commandInfo.id === UpdateSheetDataValidationSettingCommand.id) {
582
+ if (params.unitId !== this._workbook.getUnitId())
583
+ return;
584
+ if (callback(params, options) === !1)
585
+ throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationCriteria");
586
+ }
587
+ }));
588
+ }
589
+ onBeforeUpdateDataValidationRange(callback) {
590
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
591
+ const params = commandInfo.params;
592
+ if (commandInfo.id === UpdateSheetDataValidationRangeCommand.id) {
593
+ if (params.unitId !== this._workbook.getUnitId())
594
+ return;
595
+ if (callback(params, options) === !1)
596
+ throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationRange");
597
+ }
598
+ }));
599
+ }
600
+ onBeforeUpdateDataValidationOptions(callback) {
601
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
602
+ const params = commandInfo.params;
603
+ if (commandInfo.id === UpdateSheetDataValidationOptionsCommand.id) {
604
+ if (params.unitId !== this._workbook.getUnitId())
605
+ return;
606
+ if (callback(params, options) === !1)
607
+ throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationOptions");
608
+ }
609
+ }));
610
+ }
611
+ onBeforeDeleteDataValidation(callback) {
612
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
613
+ const params = commandInfo.params;
614
+ if (commandInfo.id === RemoveSheetDataValidationCommand.id) {
615
+ if (params.unitId !== this._workbook.getUnitId())
616
+ return;
617
+ if (callback(params, options) === !1)
618
+ throw new Error("Command is stopped by the hook onBeforeDeleteDataValidation");
619
+ }
620
+ }));
621
+ }
622
+ onBeforeDeleteAllDataValidation(callback) {
623
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
624
+ const params = commandInfo.params;
625
+ if (commandInfo.id === RemoveSheetAllDataValidationCommand.id) {
626
+ if (params.unitId !== this._workbook.getUnitId())
627
+ return;
628
+ if (callback(params, options) === !1)
629
+ throw new Error("Command is stopped by the hook onBeforeDeleteAllDataValidation");
630
+ }
631
+ }));
632
+ }
633
+ };
634
+ __name(_FWorkbookDataValidationMixin, "FWorkbookDataValidationMixin");
635
+ let FWorkbookDataValidationMixin = _FWorkbookDataValidationMixin;
636
+ FWorkbook.extend(FWorkbookDataValidationMixin);
637
+ const _FWorksheetDataValidationMixin = class _FWorksheetDataValidationMixin extends FWorksheet {
638
+ getDataValidations() {
639
+ return this._injector.get(DataValidationModel).getRules(this._workbook.getUnitId(), this._worksheet.getSheetId()).map((rule) => new FDataValidation(rule));
640
+ }
641
+ getValidatorStatus() {
642
+ return this._injector.get(SheetsDataValidationValidatorService).validatorWorksheet(
643
+ this._workbook.getUnitId(),
644
+ this._worksheet.getSheetId()
645
+ );
646
+ }
647
+ };
648
+ __name(_FWorksheetDataValidationMixin, "FWorksheetDataValidationMixin");
649
+ let FWorksheetDataValidationMixin = _FWorksheetDataValidationMixin;
650
+ FWorksheet.extend(FWorksheetDataValidationMixin);
651
+ export {
652
+ FDataValidation,
653
+ FDataValidationBuilder
654
+ };