@univerjs/sheets-data-validation 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/es/facade.js CHANGED
@@ -1 +1,1759 @@
1
- import{CanceledError as e,DataValidationErrorStyle as t,DataValidationOperator as n,DataValidationStatus as r,DataValidationType as i,ICommandService as a,IUniverInstanceService as o,generateRandomId as s,toDisposable as c}from"@univerjs/core";import{AddSheetDataValidationCommand as l,ClearRangeDataValidationCommand as u,RemoveSheetAllDataValidationCommand as d,RemoveSheetDataValidationCommand as f,SheetDataValidationModel as p,SheetsDataValidationValidatorService as m,UpdateSheetDataValidationOptionsCommand as h,UpdateSheetDataValidationRangeCommand as g,UpdateSheetDataValidationSettingCommand as _}from"@univerjs/sheets-data-validation";import{FRange as v,FWorkbook as y,FWorksheet as b}from"@univerjs/sheets/facade";import{DataValidationModel as x,getRuleOptions as S}from"@univerjs/data-validation";import{serializeRangeToRefString as C}from"@univerjs/engine-formula";import{FEventName as w,FUniver as T}from"@univerjs/core/facade";import{filter as E}from"rxjs";function D(e){"@babel/helpers - typeof";return D=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},D(e)}function O(e,t){if(D(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(D(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function k(e){var t=O(e,`string`);return D(t)==`symbol`?t:t+``}function A(e,t,n){return(t=k(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var j=class e{constructor(e){A(this,`_rule`,void 0),this._rule=e==null?{uid:s(),ranges:void 0,type:i.CUSTOM}:e}build(){return new M(this._rule)}copy(){return new e({...this._rule,uid:s()})}getAllowInvalid(){return this._rule.errorStyle!==t.STOP}getCriteriaType(){return this._rule.type}getCriteriaValues(){return[this._rule.operator,this._rule.formula1,this._rule.formula2]}getHelpText(){return this._rule.error}requireCheckbox(e,t){return this._rule.type=i.CHECKBOX,this._rule.formula1=e,this._rule.formula2=t,this}requireDateAfter(e){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.operator=n.GREATER_THAN,this}requireDateBefore(e){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.formula2=void 0,this._rule.operator=n.LESS_THAN,this}requireDateBetween(e,t){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.formula2=t.toLocaleDateString(),this._rule.operator=n.BETWEEN,this}requireDateEqualTo(e){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.formula2=void 0,this._rule.operator=n.EQUAL,this}requireDateNotBetween(e,t){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.formula2=t.toLocaleDateString(),this._rule.operator=n.NOT_BETWEEN,this}requireDateOnOrAfter(e){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.formula2=void 0,this._rule.operator=n.GREATER_THAN_OR_EQUAL,this}requireDateOnOrBefore(e){return this._rule.type=i.DATE,this._rule.formula1=e.toLocaleDateString(),this._rule.formula2=void 0,this._rule.operator=n.LESS_THAN_OR_EQUAL,this}requireFormulaSatisfied(e){return this._rule.type=i.CUSTOM,this._rule.formula1=e,this._rule.formula2=void 0,this}requireNumberBetween(e,t,r){return this._rule.formula1=`${e}`,this._rule.formula2=`${t}`,this._rule.operator=n.BETWEEN,this._rule.type=r?i.WHOLE:i.DECIMAL,this}requireNumberEqualTo(e,t){return this._rule.formula1=`${e}`,this._rule.formula2=void 0,this._rule.operator=n.EQUAL,this._rule.type=t?i.WHOLE:i.DECIMAL,this}requireNumberGreaterThan(e,t){return this._rule.formula1=`${e}`,this._rule.formula2=void 0,this._rule.operator=n.GREATER_THAN,this._rule.type=t?i.WHOLE:i.DECIMAL,this}requireNumberGreaterThanOrEqualTo(e,t){return this._rule.formula1=`${e}`,this._rule.formula2=void 0,this._rule.operator=n.GREATER_THAN_OR_EQUAL,this._rule.type=t?i.WHOLE:i.DECIMAL,this}requireNumberLessThan(e,t){return this._rule.formula1=`${e}`,this._rule.formula2=void 0,this._rule.operator=n.LESS_THAN,this._rule.type=t?i.WHOLE:i.DECIMAL,this}requireNumberLessThanOrEqualTo(e,t){return this._rule.formula1=`${e}`,this._rule.formula2=void 0,this._rule.operator=n.LESS_THAN_OR_EQUAL,this._rule.type=t?i.WHOLE:i.DECIMAL,this}requireNumberNotBetween(e,t,r){return this._rule.formula1=`${e}`,this._rule.formula2=`${t}`,this._rule.operator=n.NOT_BETWEEN,this._rule.type=r?i.WHOLE:i.DECIMAL,this}requireNumberNotEqualTo(e,t){return this._rule.formula1=`${e}`,this._rule.formula2=void 0,this._rule.operator=n.NOT_EQUAL,this._rule.type=t?i.WHOLE:i.DECIMAL,this}requireValueInList(e,t,n){return this._rule.type=t?i.LIST_MULTIPLE:i.LIST,this._rule.formula1=e.join(`,`),this._rule.formula2=void 0,this._rule.showDropDown=n==null?!0:n,this}requireValueInRange(e,t,n){return this._rule.type=t?i.LIST_MULTIPLE:i.LIST,this._rule.formula1=`=${C({unitId:e.getUnitId(),sheetName:e.getSheetName(),range:e.getRange()})}`,this._rule.formula2=void 0,this._rule.showDropDown=n==null?!0:n,this}setAllowInvalid(e){return this._rule.errorStyle=e?t.WARNING:t.STOP,this}setAllowBlank(e){return this._rule.allowBlank=e,this}setOptions(e){return Object.assign(this._rule,e),this}},M=class{constructor(e,t,n){A(this,`rule`,void 0),A(this,`_worksheet`,void 0),A(this,`_injector`,void 0),this._injector=n,this.rule=e,this._worksheet=t}getAllowInvalid(){return this.rule.errorStyle!==t.STOP}getCriteriaType(){return this.rule.type}getCriteriaValues(){return[this.rule.operator,this.rule.formula1,this.rule.formula2]}getHelpText(){return this.rule.error}copy(){return new j(this.rule)}getApplied(){if(!this._worksheet)return!1;let e=this._injector.get(x).getRuleById(this._worksheet.getUnitId(),this._worksheet.getSheetId(),this.rule.uid);return!!(e&&e.ranges.length)}getRanges(){if(!this.getApplied())return[];let e=this._injector.get(o).getUnit(this._worksheet.getUnitId());return this.rule.ranges.map(t=>this._injector.createInstance(v,e,this._worksheet,t))}getUnitId(){var e;return(e=this._worksheet)==null?void 0:e.getUnitId()}getSheetId(){var e;return(e=this._worksheet)==null?void 0:e.getSheetId()}setCriteria(e,t,n=!0){if(this.getApplied()&&!this._injector.get(a).syncExecuteCommand(_.id,{unitId:this.getUnitId(),subUnitId:this.getSheetId(),ruleId:this.rule.uid,setting:{operator:t[0],formula1:t[1],formula2:t[2],type:this.rule.type,allowBlank:n}}))throw Error(`setCriteria failed`);return this.rule.operator=t[0],this.rule.formula1=t[1],this.rule.formula2=t[2],this.rule.type=e,this.rule.allowBlank=n,this}setOptions(e){if(this.getApplied()&&!this._injector.get(a).syncExecuteCommand(h.id,{unitId:this.getUnitId(),subUnitId:this.getSheetId(),ruleId:this.rule.uid,options:{...S(this.rule),...e}}))throw Error(`setOptions failed`);return Object.assign(this.rule,e),this}setRanges(e){if(this.getApplied()&&!this._injector.get(a).syncExecuteCommand(g.id,{unitId:this.getUnitId(),subUnitId:this.getSheetId(),ruleId:this.rule.uid,ranges:e.map(e=>e.getRange())}))throw Error(`setRanges failed`);return this.rule.ranges=e.map(e=>e.getRange()),this}delete(){return this.getApplied()?this._injector.get(a).syncExecuteCommand(f.id,{unitId:this.getUnitId(),subUnitId:this.getSheetId(),ruleId:this.rule.uid}):!1}},N=class extends v{setDataValidation(e){if(!e)return this._commandService.syncExecuteCommand(u.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),ranges:[this._range]}),this;let t={unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),rule:{...e.rule,ranges:[this._range]}};return this._commandService.syncExecuteCommand(l.id,t),this}getDataValidation(){let e=this._injector.get(m).getDataValidation(this._workbook.getUnitId(),this._worksheet.getSheetId(),[this._range]);return e&&new M(e,this._worksheet,this._injector)}getDataValidations(){return this._injector.get(m).getDataValidations(this._workbook.getUnitId(),this._worksheet.getSheetId(),[this._range]).map(e=>new M(e,this._worksheet,this._injector))}async getValidatorStatus(){return this._injector.get(m).validatorRanges(this._workbook.getUnitId(),this._worksheet.getSheetId(),[this._range])}async getDataValidationErrorAsync(){let e=this._workbook.getUnitId(),t=this._worksheet.getSheetId();return this._collectValidationErrorsForRange(e,t,[this._range])}async _collectValidationErrorsForRange(e,t,n){if(!n.length)return[];let i=this._injector.get(m),a=this._worksheet,o=a.getName(),s=[];for(let c of n){let n=[];for(let l=c.startRow;l<=c.endRow;l++)for(let u=c.startColumn;u<=c.endColumn;u++)n.push((async()=>{try{if(await i.validatorCell(e,t,l,u)!==r.VALID){let r=this._injector.get(p).getRuleByLocation(e,t,l,u);if(r){var n;let e=((n=a.getCell(l,u))==null?void 0:n.v)||null,t=this._createDataValidationError(o,l,u,r,e);s.push(t)}}}catch(e){console.warn(`Failed to validate cell [${l}, ${u}]:`,e)}})());await Promise.all(n)}return s}_createDataValidationError(e,t,n,r,i){return{sheetName:e,row:t,column:n,ruleId:r.uid,inputValue:i,rule:r}}};v.extend(N);var P=class extends T{static newDataValidation(){return new j}newDataValidation(){return new j}_initialize(t){let n=t.get(a);this.disposeWithMe(this.registerEventHandler(this.Event.SheetDataValidationChanged,()=>t.has(p)?t.get(p).ruleChange$.subscribe(e=>{let{unitId:t,subUnitId:n,rule:r,oldRule:i,type:a}=e,o=this.getSheetTarget(t,n);if(!o)return;let{workbook:s,worksheet:c}=o,l={origin:e,worksheet:c,workbook:s,changeType:a,oldRule:i,rule:new M(r,c.getSheet(),this._injector)};this.fireEvent(this.Event.SheetDataValidationChanged,l)}):{dispose:()=>{}})),this.disposeWithMe(this.registerEventHandler(this.Event.SheetDataValidatorStatusChanged,()=>t.has(p)?t.get(p).validStatusChange$.subscribe(e=>{let{unitId:t,subUnitId:n,ruleId:r,status:i,row:a,col:o}=e,s=this.getSheetTarget(t,n);if(!s)return;let{workbook:c,worksheet:l}=s,u=l.getDataValidation(r);if(!u)return;let d={workbook:c,worksheet:l,row:a,column:o,rule:u,status:i};this.fireEvent(this.Event.SheetDataValidatorStatusChanged,d)}):{dispose:()=>{}})),this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationAdd,()=>n.beforeCommandExecuted(t=>{if(t.id===l.id){let n=t.params,r=this.getSheetTarget(n.unitId,n.subUnitId);if(!r)return;let{workbook:i,worksheet:a}=r,o={worksheet:a,workbook:i,rule:n.rule};if(this.fireEvent(this.Event.BeforeSheetDataValidationAdd,o),o.cancel)throw new e}}))),this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationCriteriaUpdate,()=>n.beforeCommandExecuted(t=>{if(t.id===_.id){let n=t.params,r=this.getSheetTarget(n.unitId,n.subUnitId);if(!r)return;let{workbook:i,worksheet:a}=r,o=a.getDataValidation(n.ruleId);if(!o)return;let s={worksheet:a,workbook:i,rule:o,ruleId:n.ruleId,newCriteria:n.setting};if(this.fireEvent(this.Event.BeforeSheetDataValidationCriteriaUpdate,s),s.cancel)throw new e}}))),this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationRangeUpdate,()=>n.beforeCommandExecuted(t=>{if(t.id===g.id){let n=t.params,r=this.getSheetTarget(n.unitId,n.subUnitId);if(!r)return;let{workbook:i,worksheet:a}=r,o=a.getDataValidation(n.ruleId);if(!o)return;let s={worksheet:a,workbook:i,rule:o,ruleId:n.ruleId,newRanges:n.ranges};if(this.fireEvent(this.Event.BeforeSheetDataValidationRangeUpdate,s),s.cancel)throw new e}}))),this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationOptionsUpdate,()=>n.beforeCommandExecuted(t=>{if(t.id===h.id){let n=t.params,r=this.getSheetTarget(n.unitId,n.subUnitId);if(!r)return;let{workbook:i,worksheet:a}=r,o=a.getDataValidation(n.ruleId);if(!o)return;let s={worksheet:a,workbook:i,rule:o,ruleId:n.ruleId,newOptions:n.options};if(this.fireEvent(this.Event.BeforeSheetDataValidationOptionsUpdate,s),s.cancel)throw new e}}))),this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationDelete,()=>n.beforeCommandExecuted(t=>{if(t.id===f.id){let n=t.params,r=this.getSheetTarget(n.unitId,n.subUnitId);if(!r)return;let{workbook:i,worksheet:a}=r,o=a.getDataValidation(n.ruleId);if(!o)return;let s={worksheet:a,workbook:i,rule:o,ruleId:n.ruleId};if(this.fireEvent(this.Event.BeforeSheetDataValidationDelete,s),s.cancel)throw new e}}))),this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationDeleteAll,()=>n.beforeCommandExecuted(t=>{if(t.id===d.id){let n=t.params,r=this.getSheetTarget(n.unitId,n.subUnitId);if(!r)return;let{workbook:i,worksheet:a}=r,o={worksheet:a,workbook:i,rules:a.getDataValidations()};if(this.fireEvent(this.Event.BeforeSheetDataValidationDeleteAll,o),o.cancel)throw new e}})))}};T.extend(P);var F=class extends y{_initialize(){Object.defineProperty(this,`_dataValidationModel`,{get(){return this._injector.get(p)}})}getValidatorStatus(){return this._injector.get(m).validatorWorkbook(this._workbook.getUnitId())}async getAllDataValidationErrorAsync(){let e=this._workbook.getUnitId(),t=this._dataValidationModel.getSubUnitIds(e),n=[];for(let r of t){let t=await this._collectValidationErrorsForSheet(e,r);n.push(...t)}return n}async _collectValidationErrorsForSheet(e,t){let n=this._dataValidationModel.getRules(e,t);if(!n.length)return[];let r=n.flatMap(e=>e.ranges);return this._collectValidationErrorsForRange(e,t,r)}async _collectValidationErrorsForRange(e,t,n){if(!n.length)return[];let i=this._injector.get(m),a=this._workbook.getSheetBySheetId(t);if(!a)throw Error(`Cannot find worksheet with sheetId: ${t}`);let o=a.getName(),s=[];for(let c of n){let n=[];for(let l=c.startRow;l<=c.endRow;l++)for(let u=c.startColumn;u<=c.endColumn;u++)n.push((async()=>{try{if(await i.validatorCell(e,t,l,u)!==r.VALID){let r=this._dataValidationModel.getRuleByLocation(e,t,l,u);if(r){var n;let e=((n=a.getCell(l,u))==null?void 0:n.v)||null,t=this._createDataValidationError(o,l,u,r,e);s.push(t)}}}catch(e){console.warn(`Failed to validate cell [${l}, ${u}]:`,e)}})());await Promise.all(n)}return s}_createDataValidationError(e,t,n,r,i){return{sheetName:e,row:t,column:n,ruleId:r.uid,inputValue:i,rule:r}}onDataValidationChange(e){return c(this._dataValidationModel.ruleChange$.pipe(E(e=>e.unitId===this._workbook.getUnitId())).subscribe(e))}onDataValidationStatusChange(e){return c(this._dataValidationModel.validStatusChange$.pipe(E(e=>e.unitId===this._workbook.getUnitId())).subscribe(e))}onBeforeAddDataValidation(e){return c(this._commandService.beforeCommandExecuted((t,n)=>{let r=t.params;if(t.id===l.id){if(r.unitId!==this._workbook.getUnitId())return;if(e(r,n)===!1)throw Error(`Command is stopped by the hook onBeforeAddDataValidation`)}}))}onBeforeUpdateDataValidationCriteria(e){return c(this._commandService.beforeCommandExecuted((t,n)=>{let r=t.params;if(t.id===_.id){if(r.unitId!==this._workbook.getUnitId())return;if(e(r,n)===!1)throw Error(`Command is stopped by the hook onBeforeUpdateDataValidationCriteria`)}}))}onBeforeUpdateDataValidationRange(e){return c(this._commandService.beforeCommandExecuted((t,n)=>{let r=t.params;if(t.id===g.id){if(r.unitId!==this._workbook.getUnitId())return;if(e(r,n)===!1)throw Error(`Command is stopped by the hook onBeforeUpdateDataValidationRange`)}}))}onBeforeUpdateDataValidationOptions(e){return c(this._commandService.beforeCommandExecuted((t,n)=>{let r=t.params;if(t.id===h.id){if(r.unitId!==this._workbook.getUnitId())return;if(e(r,n)===!1)throw Error(`Command is stopped by the hook onBeforeUpdateDataValidationOptions`)}}))}onBeforeDeleteDataValidation(e){return c(this._commandService.beforeCommandExecuted((t,n)=>{let r=t.params;if(t.id===f.id){if(r.unitId!==this._workbook.getUnitId())return;if(e(r,n)===!1)throw Error(`Command is stopped by the hook onBeforeDeleteDataValidation`)}}))}onBeforeDeleteAllDataValidation(e){return c(this._commandService.beforeCommandExecuted((t,n)=>{let r=t.params;if(t.id===d.id){if(r.unitId!==this._workbook.getUnitId())return;if(e(r,n)===!1)throw Error(`Command is stopped by the hook onBeforeDeleteAllDataValidation`)}}))}};y.extend(F);var I=class extends b{getDataValidations(){return this._injector.get(x).getRules(this._workbook.getUnitId(),this._worksheet.getSheetId()).map(e=>new M(e,this._worksheet,this._injector))}getValidatorStatus(){return this._injector.get(m).validatorWorksheet(this._workbook.getUnitId(),this._worksheet.getSheetId())}getValidatorStatusAsync(){return this.getValidatorStatus()}getDataValidation(e){let t=this._injector.get(x).getRuleById(this._workbook.getUnitId(),this._worksheet.getSheetId(),e);return t?new M(t,this._worksheet,this._injector):null}async getAllDataValidationErrorAsync(){let e=this._workbook.getUnitId(),t=this._worksheet.getSheetId();return this._collectValidationErrorsForSheet(e,t)}async _collectValidationErrorsForSheet(e,t){let n=this._injector.get(x).getRules(e,t);if(!n.length)return[];let r=n.flatMap(e=>e.ranges);return this._collectValidationErrorsForRange(e,t,r)}async _collectValidationErrorsForRange(e,t,n){if(!n.length)return[];let i=this._injector.get(m),a=this._worksheet,o=a.getName(),s=[];for(let c of n){let n=[];for(let l=c.startRow;l<=c.endRow;l++)for(let u=c.startColumn;u<=c.endColumn;u++)n.push((async()=>{try{if(await i.validatorCell(e,t,l,u)!==r.VALID){let r=this._injector.get(p).getRuleByLocation(e,t,l,u);if(r){var n;let e=((n=a.getCell(l,u))==null?void 0:n.v)||null,t=this._createDataValidationError(o,l,u,r,e);s.push(t)}}}catch(e){console.warn(`Failed to validate cell [${l}, ${u}]:`,e)}})());await Promise.all(n)}return s}_createDataValidationError(e,t,n,r,i){return{sheetName:e,row:t,column:n,ruleId:r.uid,inputValue:i,rule:r}}};b.extend(I);var L=class extends w{get SheetDataValidationChanged(){return`SheetDataValidationChanged`}get SheetDataValidatorStatusChanged(){return`SheetDataValidatorStatusChanged`}get BeforeSheetDataValidationAdd(){return`BeforeSheetDataValidationAdd`}get BeforeSheetDataValidationDelete(){return`BeforeSheetDataValidationDelete`}get BeforeSheetDataValidationDeleteAll(){return`BeforeSheetDataValidationDeleteAll`}get BeforeSheetDataValidationCriteriaUpdate(){return`BeforeSheetDataValidationCriteriaUpdate`}get BeforeSheetDataValidationRangeUpdate(){return`BeforeSheetDataValidationRangeUpdate`}get BeforeSheetDataValidationOptionsUpdate(){return`BeforeSheetDataValidationOptionsUpdate`}};w.extend(L);export{M as FDataValidation,j as FDataValidationBuilder};
1
+ import { CanceledError, DataValidationErrorStyle, DataValidationOperator, DataValidationStatus, DataValidationType, ICommandService, IUniverInstanceService, generateRandomId, toDisposable } from "@univerjs/core";
2
+ import { AddSheetDataValidationCommand, ClearRangeDataValidationCommand, RemoveSheetAllDataValidationCommand, RemoveSheetDataValidationCommand, SheetDataValidationModel, SheetsDataValidationValidatorService, UpdateSheetDataValidationOptionsCommand, UpdateSheetDataValidationRangeCommand, UpdateSheetDataValidationSettingCommand } from "@univerjs/sheets-data-validation";
3
+ import { FRange, FWorkbook, FWorksheet } from "@univerjs/sheets/facade";
4
+ import { DataValidationModel, getRuleOptions } from "@univerjs/data-validation";
5
+ import { serializeRangeToRefString } from "@univerjs/engine-formula";
6
+ import { FEventName, FUniver } from "@univerjs/core/facade";
7
+ import { filter } from "rxjs";
8
+
9
+ //#region \0@oxc-project+runtime@0.124.0/helpers/typeof.js
10
+ function _typeof(o) {
11
+ "@babel/helpers - typeof";
12
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
13
+ return typeof o;
14
+ } : function(o) {
15
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
16
+ }, _typeof(o);
17
+ }
18
+
19
+ //#endregion
20
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPrimitive.js
21
+ function toPrimitive(t, r) {
22
+ if ("object" != _typeof(t) || !t) return t;
23
+ var e = t[Symbol.toPrimitive];
24
+ if (void 0 !== e) {
25
+ var i = e.call(t, r || "default");
26
+ if ("object" != _typeof(i)) return i;
27
+ throw new TypeError("@@toPrimitive must return a primitive value.");
28
+ }
29
+ return ("string" === r ? String : Number)(t);
30
+ }
31
+
32
+ //#endregion
33
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPropertyKey.js
34
+ function toPropertyKey(t) {
35
+ var i = toPrimitive(t, "string");
36
+ return "symbol" == _typeof(i) ? i : i + "";
37
+ }
38
+
39
+ //#endregion
40
+ //#region \0@oxc-project+runtime@0.124.0/helpers/defineProperty.js
41
+ function _defineProperty(e, r, t) {
42
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
43
+ value: t,
44
+ enumerable: !0,
45
+ configurable: !0,
46
+ writable: !0
47
+ }) : e[r] = t, e;
48
+ }
49
+
50
+ //#endregion
51
+ //#region src/facade/f-data-validation-builder.ts
52
+ /**
53
+ * Builder for data validation rules. use {@link FUniver} `univerAPI.newDataValidation()` to create a new builder.
54
+ * @example
55
+ * ```typescript
56
+ * // Set the data validation for cell A1 to require a value from B1:B10
57
+ * const fWorkbook = univerAPI.getActiveWorkbook();
58
+ * const fWorksheet = fWorkbook.getActiveSheet();
59
+ * const fRange = fWorksheet.getRange('B1:B2');
60
+ * fRange.setValues([
61
+ * ['Yes'],
62
+ * ['No']
63
+ * ]);
64
+ *
65
+ * const rule = univerAPI.newDataValidation()
66
+ * .requireValueInRange(fRange)
67
+ * .setOptions({
68
+ * allowBlank: false,
69
+ * showErrorMessage: true,
70
+ * error: 'Please enter a value from the list'
71
+ * })
72
+ * .build();
73
+ * const cell = fWorksheet.getRange('A1');
74
+ * cell.setDataValidation(rule);
75
+ * ```
76
+ * @hideconstructor
77
+ */
78
+ var FDataValidationBuilder = class FDataValidationBuilder {
79
+ constructor(rule) {
80
+ _defineProperty(this, "_rule", void 0);
81
+ this._rule = rule !== null && rule !== void 0 ? rule : {
82
+ uid: generateRandomId(),
83
+ ranges: void 0,
84
+ type: DataValidationType.CUSTOM
85
+ };
86
+ }
87
+ /**
88
+ * Builds an FDataValidation instance based on the _rule property of the current class
89
+ * @returns {FDataValidation} A new instance of the FDataValidation class
90
+ * @example
91
+ * ```typescript
92
+ * const fWorkbook = univerAPI.getActiveWorkbook();
93
+ * const fWorksheet = fWorkbook.getActiveSheet();
94
+ *
95
+ * // Create a new data validation rule that requires a number between 1 and 10 for the range A1:B10
96
+ * const fRange = fWorksheet.getRange('A1:B10');
97
+ * const rule = univerAPI.newDataValidation()
98
+ * .requireNumberBetween(1, 10)
99
+ * .setOptions({
100
+ * allowBlank: true,
101
+ * showErrorMessage: true,
102
+ * error: 'Please enter a number between 1 and 10'
103
+ * })
104
+ * .build();
105
+ * fRange.setDataValidation(rule);
106
+ * ```
107
+ */
108
+ build() {
109
+ return new FDataValidation(this._rule);
110
+ }
111
+ /**
112
+ * Creates a duplicate of the current DataValidationBuilder object
113
+ * @returns {FDataValidationBuilder} A new instance of the DataValidationBuilder class
114
+ * @example
115
+ * ```typescript
116
+ * const fWorkbook = univerAPI.getActiveWorkbook();
117
+ * const fWorksheet = fWorkbook.getActiveSheet();
118
+ *
119
+ * // Create a new data validation rule that requires a number between 1 and 10 for the range A1:B10
120
+ * const fRange = fWorksheet.getRange('A1:B10');
121
+ * const builder = univerAPI.newDataValidation()
122
+ * .requireNumberBetween(1, 10)
123
+ * .setOptions({
124
+ * allowBlank: true,
125
+ * showErrorMessage: true,
126
+ * error: 'Please enter a number between 1 and 10'
127
+ * });
128
+ * fRange.setDataValidation(builder.build());
129
+ *
130
+ * // Copy the builder applied to the new range F1:G10
131
+ * const newRange = fWorksheet.getRange('F1:G10');
132
+ * const copyBuilder = builder.copy();
133
+ * newRange.setDataValidation(copyBuilder.build());
134
+ * ```
135
+ */
136
+ copy() {
137
+ return new FDataValidationBuilder({
138
+ ...this._rule,
139
+ uid: generateRandomId()
140
+ });
141
+ }
142
+ /**
143
+ * Determines whether invalid data is allowed
144
+ * @returns {boolean} True if invalid data is allowed, False otherwise
145
+ * @example
146
+ * ```typescript
147
+ * const builder = univerAPI.newDataValidation().requireNumberBetween(1, 10);
148
+ * console.log(builder.getAllowInvalid());
149
+ * ```
150
+ */
151
+ getAllowInvalid() {
152
+ return this._rule.errorStyle !== DataValidationErrorStyle.STOP;
153
+ }
154
+ /**
155
+ * Gets the data validation type of the rule
156
+ * @returns {DataValidationType | string} The data validation type
157
+ * @example
158
+ * ```typescript
159
+ * const builder = univerAPI.newDataValidation();
160
+ * console.log(builder.getCriteriaType()); // custom
161
+ *
162
+ * builder.requireNumberBetween(1, 10);
163
+ * console.log(builder.getCriteriaType()); // decimal
164
+ *
165
+ * builder.requireValueInList(['Yes', 'No']);
166
+ * console.log(builder.getCriteriaType()); // list
167
+ * ```
168
+ */
169
+ getCriteriaType() {
170
+ return this._rule.type;
171
+ }
172
+ /**
173
+ * Gets the values used for criteria evaluation
174
+ * @returns {[string | undefined, string | undefined, string | undefined]} An array containing the operator, formula1, and formula2 values
175
+ * @example
176
+ * ```typescript
177
+ * const builder = univerAPI.newDataValidation().requireNumberBetween(1, 10);
178
+ * const [operator, formula1, formula2] = builder.getCriteriaValues();
179
+ * console.log(operator, formula1, formula2); // between 1 10
180
+ *
181
+ * builder.requireValueInList(['Yes', 'No']);
182
+ * console.log(builder.getCriteriaValues()); // undefined Yes,No undefined
183
+ * ```
184
+ */
185
+ getCriteriaValues() {
186
+ return [
187
+ this._rule.operator,
188
+ this._rule.formula1,
189
+ this._rule.formula2
190
+ ];
191
+ }
192
+ /**
193
+ * Gets the help text information, which is used to provide users with guidance and support
194
+ * @returns {string | undefined} Returns the help text information. If there is no error message, it returns an undefined value
195
+ * @example
196
+ * ```typescript
197
+ * const builder = univerAPI.newDataValidation().setOptions({
198
+ * showErrorMessage: true,
199
+ * error: 'Please enter a valid value'
200
+ * });
201
+ * console.log(builder.getHelpText()); // 'Please enter a valid value'
202
+ * ```
203
+ */
204
+ getHelpText() {
205
+ return this._rule.error;
206
+ }
207
+ /**
208
+ * Sets the data validation rule to require that the input is a boolean value; this value is rendered as a checkbox.
209
+ * @param {string} [checkedValue] - The value assigned to a checked box.
210
+ * @param {string} [uncheckedValue] - The value assigned to an unchecked box.
211
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
212
+ * @example
213
+ * ```typescript
214
+ * const fWorkbook = univerAPI.getActiveWorkbook();
215
+ * const fWorksheet = fWorkbook.getActiveSheet();
216
+ *
217
+ * // Set the data validation for cell A1:A10 to require a checkbox with default 1 and 0 values
218
+ * const fRange = fWorksheet.getRange('A1:A10');
219
+ * const rule = univerAPI.newDataValidation()
220
+ * .requireCheckbox()
221
+ * .build();
222
+ * fRange.setDataValidation(rule);
223
+ *
224
+ * // Set the data validation for cell B1:B10 to require a checkbox with 'Yes' and 'No' values
225
+ * const fRange2 = fWorksheet.getRange('B1:B10');
226
+ * const rule2 = univerAPI.newDataValidation()
227
+ * .requireCheckbox('Yes', 'No')
228
+ * .build();
229
+ * fRange2.setDataValidation(rule2);
230
+ * ```
231
+ */
232
+ requireCheckbox(checkedValue, uncheckedValue) {
233
+ this._rule.type = DataValidationType.CHECKBOX;
234
+ this._rule.formula1 = checkedValue;
235
+ this._rule.formula2 = uncheckedValue;
236
+ return this;
237
+ }
238
+ /**
239
+ * Set the data validation type to DATE and configure the validation rules to be after a specific date.
240
+ * @param {Date} date - The latest unacceptable date.
241
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
242
+ * @example
243
+ * ```typescript
244
+ * const fWorkbook = univerAPI.getActiveWorkbook();
245
+ * const fWorksheet = fWorkbook.getActiveSheet();
246
+ *
247
+ * // Set some date values in the range A1:B2
248
+ * const fRange = fWorksheet.getRange('A1:B2');
249
+ * fRange.setValues([
250
+ * ['2024-01-01', '2024-12-31'],
251
+ * ['2025-01-01', '2025-12-31']
252
+ * ]);
253
+ *
254
+ * // Create a data validation rule that requires a date after 2025-01-01
255
+ * const rule = univerAPI.newDataValidation()
256
+ * .requireDateAfter(new Date('2025-01-01'))
257
+ * .build();
258
+ * fRange.setDataValidation(rule);
259
+ *
260
+ * // Get the validation status of the range
261
+ * const status = await fRange.getValidatorStatus();
262
+ * console.log(status); // [['invalid', 'invalid', 'invalid', 'valid']]
263
+ * ```
264
+ */
265
+ requireDateAfter(date) {
266
+ this._rule.type = DataValidationType.DATE;
267
+ this._rule.formula1 = date.toLocaleDateString();
268
+ this._rule.operator = DataValidationOperator.GREATER_THAN;
269
+ return this;
270
+ }
271
+ /**
272
+ * Set the data validation type to DATE and configure the validation rules to be before a specific date.
273
+ * @param {Date} date - The earliest unacceptable date.
274
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
275
+ * @example
276
+ * ```typescript
277
+ * const fWorkbook = univerAPI.getActiveWorkbook();
278
+ * const fWorksheet = fWorkbook.getActiveSheet();
279
+ *
280
+ * // Set some date values in the range A1:B2
281
+ * const fRange = fWorksheet.getRange('A1:B2');
282
+ * fRange.setValues([
283
+ * ['2024-01-01', '2024-12-31'],
284
+ * ['2025-01-01', '2025-12-31']
285
+ * ]);
286
+ *
287
+ * // Create a data validation rule that requires a date before 2025-01-01
288
+ * const rule = univerAPI.newDataValidation()
289
+ * .requireDateBefore(new Date('2025-01-01'))
290
+ * .build();
291
+ * fRange.setDataValidation(rule);
292
+ *
293
+ * // Get the validation status of the range
294
+ * const status = await fRange.getValidatorStatus();
295
+ * console.log(status); // [['valid', 'valid', 'invalid', 'invalid']]
296
+ * ```
297
+ */
298
+ requireDateBefore(date) {
299
+ this._rule.type = DataValidationType.DATE;
300
+ this._rule.formula1 = date.toLocaleDateString();
301
+ this._rule.formula2 = void 0;
302
+ this._rule.operator = DataValidationOperator.LESS_THAN;
303
+ return this;
304
+ }
305
+ /**
306
+ * Set the data validation type to DATE and configure the validation rules to be within a specific date range.
307
+ * @param {Date} start - The earliest acceptable date.
308
+ * @param {Date} end - The latest acceptable date.
309
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
310
+ * @example
311
+ * ```typescript
312
+ * const fWorkbook = univerAPI.getActiveWorkbook();
313
+ * const fWorksheet = fWorkbook.getActiveSheet();
314
+ *
315
+ * // Set some date values in the range A1:B2
316
+ * const fRange = fWorksheet.getRange('A1:B2');
317
+ * fRange.setValues([
318
+ * ['2024-01-01', '2024-12-31'],
319
+ * ['2025-01-01', '2025-12-31']
320
+ * ]);
321
+ *
322
+ * // Create a data validation rule that requires a date between 2024-06-01 and 2025-06-01
323
+ * const rule = univerAPI.newDataValidation()
324
+ * .requireDateBetween(new Date('2024-06-01'), new Date('2025-06-01'))
325
+ * .build();
326
+ * fRange.setDataValidation(rule);
327
+ *
328
+ * // Get the validation status of the range
329
+ * const status = await fRange.getValidatorStatus();
330
+ * console.log(status); // [['invalid', 'valid', 'valid', 'invalid']]
331
+ * ```
332
+ */
333
+ requireDateBetween(start, end) {
334
+ this._rule.type = DataValidationType.DATE;
335
+ this._rule.formula1 = start.toLocaleDateString();
336
+ this._rule.formula2 = end.toLocaleDateString();
337
+ this._rule.operator = DataValidationOperator.BETWEEN;
338
+ return this;
339
+ }
340
+ /**
341
+ * Set the data validation type to DATE and configure the validation rules to be equal to a specific date.
342
+ * @param {Date} date - The sole acceptable date.
343
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
344
+ * @example
345
+ * ```typescript
346
+ * const fWorkbook = univerAPI.getActiveWorkbook();
347
+ * const fWorksheet = fWorkbook.getActiveSheet();
348
+ *
349
+ * // Set some date values in the range A1:B2
350
+ * const fRange = fWorksheet.getRange('A1:B2');
351
+ * fRange.setValues([
352
+ * ['2024-01-01', '2024-12-31'],
353
+ * ['2025-01-01', '2025-12-31']
354
+ * ]);
355
+ *
356
+ * // Create a data validation rule that requires a date equal to 2025-01-01
357
+ * const rule = univerAPI.newDataValidation()
358
+ * .requireDateEqualTo(new Date('2025-01-01'))
359
+ * .build();
360
+ * fRange.setDataValidation(rule);
361
+ *
362
+ * // Get the validation status of the cell A2
363
+ * const status = await fWorksheet.getRange('A2').getValidatorStatus();
364
+ * console.log(status?.[0]?.[0]); // 'valid'
365
+ *
366
+ * // Get the validation status of the cell B2
367
+ * const status2 = await fWorksheet.getRange('B2').getValidatorStatus();
368
+ * console.log(status2?.[0]?.[0]); // 'invalid'
369
+ * ```
370
+ */
371
+ requireDateEqualTo(date) {
372
+ this._rule.type = DataValidationType.DATE;
373
+ this._rule.formula1 = date.toLocaleDateString();
374
+ this._rule.formula2 = void 0;
375
+ this._rule.operator = DataValidationOperator.EQUAL;
376
+ return this;
377
+ }
378
+ /**
379
+ * Set the data validation type to DATE and configure the validation rules to be not within a specific date range.
380
+ * @param {Date} start - The earliest unacceptable date.
381
+ * @param {Date} end - The latest unacceptable date.
382
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
383
+ * @example
384
+ * ```typescript
385
+ * const fWorkbook = univerAPI.getActiveWorkbook();
386
+ * const fWorksheet = fWorkbook.getActiveSheet();
387
+ *
388
+ * // Set some date values in the range A1:B2
389
+ * const fRange = fWorksheet.getRange('A1:B2');
390
+ * fRange.setValues([
391
+ * ['2024-01-01', '2024-12-31'],
392
+ * ['2025-01-01', '2025-12-31']
393
+ * ]);
394
+ *
395
+ * // Create a data validation rule that requires a date not between 2024-06-01 and 2025-06-01
396
+ * const rule = univerAPI.newDataValidation()
397
+ * .requireDateNotBetween(new Date('2024-06-01'), new Date('2025-06-01'))
398
+ * .build();
399
+ * fRange.setDataValidation(rule);
400
+ *
401
+ * // Get the validation status of the range
402
+ * const status = await fRange.getValidatorStatus();
403
+ * console.log(status); // [['valid', 'invalid', 'invalid', 'valid']]
404
+ * ```
405
+ */
406
+ requireDateNotBetween(start, end) {
407
+ this._rule.type = DataValidationType.DATE;
408
+ this._rule.formula1 = start.toLocaleDateString();
409
+ this._rule.formula2 = end.toLocaleDateString();
410
+ this._rule.operator = DataValidationOperator.NOT_BETWEEN;
411
+ return this;
412
+ }
413
+ /**
414
+ * Set the data validation type to DATE and configure the validation rules to be on or after a specific date.
415
+ * @param {Date} date - The earliest acceptable date.
416
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
417
+ * @example
418
+ * ```typescript
419
+ * const fWorkbook = univerAPI.getActiveWorkbook();
420
+ * const fWorksheet = fWorkbook.getActiveSheet();
421
+ *
422
+ * // Set some date values in the range A1:B2
423
+ * const fRange = fWorksheet.getRange('A1:B2');
424
+ * fRange.setValues([
425
+ * ['2024-01-01', '2024-12-31'],
426
+ * ['2025-01-01', '2025-12-31']
427
+ * ]);
428
+ *
429
+ * // Create a data validation rule that requires a date on or after 2025-01-01
430
+ * const rule = univerAPI.newDataValidation()
431
+ * .requireDateOnOrAfter(new Date('2025-01-01'))
432
+ * .build();
433
+ * fRange.setDataValidation(rule);
434
+ *
435
+ * // Get the validation status of the range
436
+ * const status = await fRange.getValidatorStatus();
437
+ * console.log(status); // [['invalid', 'invalid', 'valid', 'valid']]
438
+ * ```
439
+ */
440
+ requireDateOnOrAfter(date) {
441
+ this._rule.type = DataValidationType.DATE;
442
+ this._rule.formula1 = date.toLocaleDateString();
443
+ this._rule.formula2 = void 0;
444
+ this._rule.operator = DataValidationOperator.GREATER_THAN_OR_EQUAL;
445
+ return this;
446
+ }
447
+ /**
448
+ * Set the data validation type to DATE and configure the validation rules to be on or before a specific date.
449
+ * @param {Date} date - The latest acceptable date.
450
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
451
+ * @example
452
+ * ```typescript
453
+ * const fWorkbook = univerAPI.getActiveWorkbook();
454
+ * const fWorksheet = fWorkbook.getActiveSheet();
455
+ *
456
+ * // Set some date values in the range A1:B2
457
+ * const fRange = fWorksheet.getRange('A1:B2');
458
+ * fRange.setValues([
459
+ * ['2024-01-01', '2024-12-31'],
460
+ * ['2025-01-01', '2025-12-31']
461
+ * ]);
462
+ *
463
+ * // Create a data validation rule that requires a date on or before 2025-01-01
464
+ * const rule = univerAPI.newDataValidation()
465
+ * .requireDateOnOrBefore(new Date('2025-01-01'))
466
+ * .build();
467
+ * fRange.setDataValidation(rule);
468
+ *
469
+ * // Get the validation status of the range
470
+ * const status = await fRange.getValidatorStatus();
471
+ * console.log(status); // [['valid', 'valid', 'valid', 'invalid']]
472
+ * ```
473
+ */
474
+ requireDateOnOrBefore(date) {
475
+ this._rule.type = DataValidationType.DATE;
476
+ this._rule.formula1 = date.toLocaleDateString();
477
+ this._rule.formula2 = void 0;
478
+ this._rule.operator = DataValidationOperator.LESS_THAN_OR_EQUAL;
479
+ return this;
480
+ }
481
+ /**
482
+ * Sets the data validation rule to require that the given formula evaluates to `true`.
483
+ * @param {string} formula - The formula string that needs to be satisfied, formula result should be TRUE or FALSE, and references range will relative offset.
484
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
485
+ * @example
486
+ * ```typescript
487
+ * const fWorkbook = univerAPI.getActiveWorkbook();
488
+ * const fWorksheet = fWorkbook.getActiveSheet();
489
+ *
490
+ * // Set some values in the range A1:B2 and C1:D2
491
+ * const cell = fWorksheet.getRange('A1:B2');
492
+ * cell.setValues([
493
+ * [4, 3],
494
+ * [2, 1]
495
+ * ]);
496
+ * const fRange = fWorksheet.getRange('C1:D2');
497
+ * fRange.setValues([
498
+ * [1, 2],
499
+ * [3, 4]
500
+ * ]);
501
+ *
502
+ * // Create a data validation rule that requires the formula '=A1>2' to be satisfied
503
+ * const rule = univerAPI.newDataValidation()
504
+ * .requireFormulaSatisfied('=A1>2')
505
+ * .setOptions({
506
+ * showErrorMessage: true,
507
+ * error: 'Please enter a value equal to A1'
508
+ * })
509
+ * .build();
510
+ * fRange.setDataValidation(rule);
511
+ *
512
+ * // Get the validation status of the range
513
+ * const status = await fRange.getValidatorStatus();
514
+ * console.log(status); // [['valid', 'valid', 'invalid', 'invalid']]
515
+ * ```
516
+ */
517
+ requireFormulaSatisfied(formula) {
518
+ this._rule.type = DataValidationType.CUSTOM;
519
+ this._rule.formula1 = formula;
520
+ this._rule.formula2 = void 0;
521
+ return this;
522
+ }
523
+ /**
524
+ * Sets the data validation rule to require a number that falls between, or is either of, two specified numbers.
525
+ * @param {number} start - The lowest acceptable value.
526
+ * @param {number} end - The highest acceptable value.
527
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
528
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
529
+ * @example
530
+ * ```typescript
531
+ * const fWorkbook = univerAPI.getActiveWorkbook();
532
+ * const fWorksheet = fWorkbook.getActiveSheet();
533
+ *
534
+ * // Create a new data validation rule that requires a number between 1 and 10 for the range A1:B10
535
+ * const fRange = fWorksheet.getRange('A1:B10');
536
+ * const rule = univerAPI.newDataValidation()
537
+ * .requireNumberBetween(1, 10)
538
+ * .setOptions({
539
+ * allowBlank: false,
540
+ * showErrorMessage: true,
541
+ * error: 'Please enter a number between 1 and 10'
542
+ * })
543
+ * .build();
544
+ * fRange.setDataValidation(rule);
545
+ * ```
546
+ */
547
+ requireNumberBetween(start, end, isInteger) {
548
+ this._rule.formula1 = `${start}`;
549
+ this._rule.formula2 = `${end}`;
550
+ this._rule.operator = DataValidationOperator.BETWEEN;
551
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
552
+ return this;
553
+ }
554
+ /**
555
+ * Sets the data validation rule to require a number equal to the given value.
556
+ * @param {number} num - The sole acceptable value.
557
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
558
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
559
+ * @example
560
+ * ```typescript
561
+ * const fWorkbook = univerAPI.getActiveWorkbook();
562
+ * const fWorksheet = fWorkbook.getActiveSheet();
563
+ *
564
+ * // Create a new data validation rule that requires a number equal to 10 for the range A1:B10
565
+ * const fRange = fWorksheet.getRange('A1:B10');
566
+ * const rule = univerAPI.newDataValidation()
567
+ * .requireNumberEqualTo(10)
568
+ * .setOptions({
569
+ * allowBlank: false,
570
+ * showErrorMessage: true,
571
+ * error: 'Please enter a number equal to 10'
572
+ * })
573
+ * .build();
574
+ * fRange.setDataValidation(rule);
575
+ * ```
576
+ */
577
+ requireNumberEqualTo(num, isInteger) {
578
+ this._rule.formula1 = `${num}`;
579
+ this._rule.formula2 = void 0;
580
+ this._rule.operator = DataValidationOperator.EQUAL;
581
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
582
+ return this;
583
+ }
584
+ /**
585
+ * Sets the data validation rule to require a number greater than the given value.
586
+ * @param {number} num - The highest unacceptable value.
587
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
588
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
589
+ * @example
590
+ * ```typescript
591
+ * const fWorkbook = univerAPI.getActiveWorkbook();
592
+ * const fWorksheet = fWorkbook.getActiveSheet();
593
+ *
594
+ * // Create a new data validation rule that requires a number greater than 10 for the range A1:B10
595
+ * const fRange = fWorksheet.getRange('A1:B10');
596
+ * const rule = univerAPI.newDataValidation()
597
+ * .requireNumberGreaterThan(10)
598
+ * .setOptions({
599
+ * allowBlank: false,
600
+ * showErrorMessage: true,
601
+ * error: 'Please enter a number greater than 10'
602
+ * })
603
+ * .build();
604
+ * fRange.setDataValidation(rule);
605
+ * ```
606
+ */
607
+ requireNumberGreaterThan(num, isInteger) {
608
+ this._rule.formula1 = `${num}`;
609
+ this._rule.formula2 = void 0;
610
+ this._rule.operator = DataValidationOperator.GREATER_THAN;
611
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
612
+ return this;
613
+ }
614
+ /**
615
+ * Sets the data validation rule to require a number greater than or equal to the given value.
616
+ * @param {number} num - The lowest acceptable value.
617
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
618
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
619
+ * @example
620
+ * ```typescript
621
+ * const fWorkbook = univerAPI.getActiveWorkbook();
622
+ * const fWorksheet = fWorkbook.getActiveSheet();
623
+ *
624
+ * // Create a new data validation rule that requires a number greater than 10 or equal to 10 for the range A1:B10
625
+ * const fRange = fWorksheet.getRange('A1:B10');
626
+ * const rule = univerAPI.newDataValidation()
627
+ * .requireNumberGreaterThanOrEqualTo(10)
628
+ * .setOptions({
629
+ * allowBlank: false,
630
+ * showErrorMessage: true,
631
+ * error: 'Please enter a number greater than 10 or equal to 10'
632
+ * })
633
+ * .build();
634
+ * fRange.setDataValidation(rule);
635
+ * ```
636
+ */
637
+ requireNumberGreaterThanOrEqualTo(num, isInteger) {
638
+ this._rule.formula1 = `${num}`;
639
+ this._rule.formula2 = void 0;
640
+ this._rule.operator = DataValidationOperator.GREATER_THAN_OR_EQUAL;
641
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
642
+ return this;
643
+ }
644
+ /**
645
+ * Sets the data validation rule to require a number less than the given value.
646
+ * @param {number} num - The lowest unacceptable value.
647
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
648
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
649
+ * @example
650
+ * ```typescript
651
+ * const fWorkbook = univerAPI.getActiveWorkbook();
652
+ * const fWorksheet = fWorkbook.getActiveSheet();
653
+ *
654
+ * // Create a new data validation rule that requires a number less than 10 for the range A1:B10
655
+ * const fRange = fWorksheet.getRange('A1:B10');
656
+ * const rule = univerAPI.newDataValidation()
657
+ * .requireNumberLessThan(10)
658
+ * .setOptions({
659
+ * allowBlank: false,
660
+ * showErrorMessage: true,
661
+ * error: 'Please enter a number less than 10'
662
+ * })
663
+ * .build();
664
+ * fRange.setDataValidation(rule);
665
+ * ```
666
+ */
667
+ requireNumberLessThan(num, isInteger) {
668
+ this._rule.formula1 = `${num}`;
669
+ this._rule.formula2 = void 0;
670
+ this._rule.operator = DataValidationOperator.LESS_THAN;
671
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
672
+ return this;
673
+ }
674
+ /**
675
+ * Sets the data validation rule to require a number less than or equal to the given value.
676
+ * @param {number} num - The highest acceptable value.
677
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
678
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
679
+ * @example
680
+ * ```typescript
681
+ * const fWorkbook = univerAPI.getActiveWorkbook();
682
+ * const fWorksheet = fWorkbook.getActiveSheet();
683
+ *
684
+ * // Create a new data validation rule that requires a number less than 10 or equal to 10 for the range A1:B10
685
+ * const fRange = fWorksheet.getRange('A1:B10');
686
+ * const rule = univerAPI.newDataValidation()
687
+ * .requireNumberLessThanOrEqualTo(10)
688
+ * .setOptions({
689
+ * allowBlank: false,
690
+ * showErrorMessage: true,
691
+ * error: 'Please enter a number less than 10 or equal to 10'
692
+ * })
693
+ * .build();
694
+ * fRange.setDataValidation(rule);
695
+ * ```
696
+ */
697
+ requireNumberLessThanOrEqualTo(num, isInteger) {
698
+ this._rule.formula1 = `${num}`;
699
+ this._rule.formula2 = void 0;
700
+ this._rule.operator = DataValidationOperator.LESS_THAN_OR_EQUAL;
701
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
702
+ return this;
703
+ }
704
+ /**
705
+ * Sets the data validation rule to require a number that does not fall between, and is neither of, two specified numbers.
706
+ * @param {number} start - The lowest unacceptable value.
707
+ * @param {number} end - The highest unacceptable value.
708
+ * @param {boolean} [isInteger] - Optional parameter, indicating whether the number to be verified is an integer.
709
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
710
+ * @example
711
+ * ```typescript
712
+ * const fWorkbook = univerAPI.getActiveWorkbook();
713
+ * const fWorksheet = fWorkbook.getActiveSheet();
714
+ *
715
+ * // Create a new data validation rule that requires a number not between 1 and 10 for the range A1:B10
716
+ * const fRange = fWorksheet.getRange('A1:B10');
717
+ * const rule = univerAPI.newDataValidation()
718
+ * .requireNumberNotBetween(1, 10)
719
+ * .setOptions({
720
+ * allowBlank: false,
721
+ * showErrorMessage: true,
722
+ * error: 'Please enter a number not between 1 and 10'
723
+ * })
724
+ * .build();
725
+ * fRange.setDataValidation(rule);
726
+ * ```
727
+ */
728
+ requireNumberNotBetween(start, end, isInteger) {
729
+ this._rule.formula1 = `${start}`;
730
+ this._rule.formula2 = `${end}`;
731
+ this._rule.operator = DataValidationOperator.NOT_BETWEEN;
732
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
733
+ return this;
734
+ }
735
+ /**
736
+ * Sets the data validation rule to require a number not equal to the given value.
737
+ * @param {number} num - The sole unacceptable value.
738
+ * @param {boolean} [isInteger] - Indicates whether the required number is an integer.
739
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
740
+ * @example
741
+ * ```typescript
742
+ * const fWorkbook = univerAPI.getActiveWorkbook();
743
+ * const fWorksheet = fWorkbook.getActiveSheet();
744
+ *
745
+ * // Create a new data validation rule that requires a number not equal to 10 for the range A1:B10
746
+ * const fRange = fWorksheet.getRange('A1:B10');
747
+ * const rule = univerAPI.newDataValidation()
748
+ * .requireNumberNotEqualTo(10)
749
+ * .setOptions({
750
+ * allowBlank: false,
751
+ * showErrorMessage: true,
752
+ * error: 'Please enter a number not equal to 10'
753
+ * })
754
+ * .build();
755
+ * fRange.setDataValidation(rule);
756
+ * ```
757
+ */
758
+ requireNumberNotEqualTo(num, isInteger) {
759
+ this._rule.formula1 = `${num}`;
760
+ this._rule.formula2 = void 0;
761
+ this._rule.operator = DataValidationOperator.NOT_EQUAL;
762
+ this._rule.type = isInteger ? DataValidationType.WHOLE : DataValidationType.DECIMAL;
763
+ return this;
764
+ }
765
+ /**
766
+ * Sets a data validation rule that requires the user to enter a value from a list of specific values.
767
+ * The list can be displayed in a dropdown, and the user can choose multiple values according to the settings.
768
+ * @param {string[]} values - An array of acceptable values.
769
+ * @param {boolean} [multiple] - Optional parameter indicating whether the user can select multiple values.
770
+ * @param {boolean} [showDropdown] - Optional parameter indicating whether to display the list in a dropdown.
771
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
772
+ * @example
773
+ * ```typescript
774
+ * const fWorkbook = univerAPI.getActiveWorkbook();
775
+ * const fWorksheet = fWorkbook.getActiveSheet();
776
+ *
777
+ * // Create a new data validation rule that requires the user to enter a value from the list ['Yes', 'No'] for the range A1:B10
778
+ * const fRange = fWorksheet.getRange('A1:B10');
779
+ * const rule = univerAPI.newDataValidation()
780
+ * .requireValueInList(['Yes', 'No'])
781
+ * .setOptions({
782
+ * allowBlank: true,
783
+ * showErrorMessage: true,
784
+ * error: 'Please enter a value from the list'
785
+ * })
786
+ * .build();
787
+ * fRange.setDataValidation(rule);
788
+ * ```
789
+ */
790
+ requireValueInList(values, multiple, showDropdown) {
791
+ this._rule.type = multiple ? DataValidationType.LIST_MULTIPLE : DataValidationType.LIST;
792
+ this._rule.formula1 = values.join(",");
793
+ this._rule.formula2 = void 0;
794
+ this._rule.showDropDown = showDropdown !== null && showDropdown !== void 0 ? showDropdown : true;
795
+ return this;
796
+ }
797
+ /**
798
+ * Sets a data validation rule that requires the user to enter a value within a specific range.
799
+ * The range is defined by an FRange object, which contains the unit ID, sheet name, and cell range.
800
+ * @param {FRange} range - An FRange object representing the range of values that the user can enter.
801
+ * @param {boolean} [multiple] - Optional parameter indicating whether the user can select multiple values.
802
+ * @param {boolean} [showDropdown] - Optional parameter indicating whether to display the list in a dropdown.
803
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
804
+ * @example
805
+ * ```typescript
806
+ * const fWorkbook = univerAPI.getActiveWorkbook();
807
+ * const fWorksheet = fWorkbook.getActiveSheet();
808
+ *
809
+ * // Set the values in the range B1:B2
810
+ * const fRange = fWorksheet.getRange('B1:B2');
811
+ * fRange.setValues([
812
+ * ['Yes'],
813
+ * ['No']
814
+ * ]);
815
+ *
816
+ * // Create a new data validation rule that requires the user to enter a value from the range B1:B2 for the range A1:A10
817
+ * const rule = univerAPI.newDataValidation()
818
+ * .requireValueInRange(fRange)
819
+ * .setOptions({
820
+ * allowBlank: false,
821
+ * showErrorMessage: true,
822
+ * error: 'Please enter a value from the list'
823
+ * })
824
+ * .build();
825
+ * const cell = fWorksheet.getRange('A1');
826
+ * cell.setDataValidation(rule);
827
+ * ```
828
+ */
829
+ requireValueInRange(range, multiple, showDropdown) {
830
+ this._rule.type = multiple ? DataValidationType.LIST_MULTIPLE : DataValidationType.LIST;
831
+ this._rule.formula1 = `=${serializeRangeToRefString({
832
+ unitId: range.getUnitId(),
833
+ sheetName: range.getSheetName(),
834
+ range: range.getRange()
835
+ })}`;
836
+ this._rule.formula2 = void 0;
837
+ this._rule.showDropDown = showDropdown !== null && showDropdown !== void 0 ? showDropdown : true;
838
+ return this;
839
+ }
840
+ /**
841
+ * Sets whether to allow invalid data and configures the error style.
842
+ * If invalid data is not allowed, the error style will be set to STOP, indicating that data entry must stop upon encountering an error.
843
+ * 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.
844
+ * @param {boolean} allowInvalidData - Whether to allow invalid data.
845
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
846
+ * @example
847
+ * ```typescript
848
+ * const fWorkbook = univerAPI.getActiveWorkbook();
849
+ * const fWorksheet = fWorkbook.getActiveSheet();
850
+ *
851
+ * // Set the data validation for cell A1:B2 to allow invalid data, so A1:B2 will display a warning when invalid data is entered
852
+ * const fRange = fWorksheet.getRange('A1:B2');
853
+ * const rule = univerAPI.newDataValidation()
854
+ * .requireValueInList(['Yes', 'No'])
855
+ * .setAllowInvalid(true)
856
+ * .build();
857
+ * fRange.setDataValidation(rule);
858
+ *
859
+ * // Set the data validation for cell C1:D2 to not allow invalid data, so C1:D2 will stop data entry when invalid data is entered
860
+ * const fRange2 = fWorksheet.getRange('C1:D2');
861
+ * const rule2 = univerAPI.newDataValidation()
862
+ * .requireValueInList(['Yes', 'No'])
863
+ * .setAllowInvalid(false)
864
+ * .build();
865
+ * fRange2.setDataValidation(rule2);
866
+ * ```
867
+ */
868
+ setAllowInvalid(allowInvalidData) {
869
+ this._rule.errorStyle = !allowInvalidData ? DataValidationErrorStyle.STOP : DataValidationErrorStyle.WARNING;
870
+ return this;
871
+ }
872
+ /**
873
+ * Sets whether to allow blank values.
874
+ * @param {boolean} allowBlank - Whether to allow blank values.
875
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
876
+ * @example
877
+ * ```typescript
878
+ * // Assume current sheet is empty data
879
+ * const fWorkbook = univerAPI.getActiveWorkbook();
880
+ * const fWorksheet = fWorkbook.getActiveSheet();
881
+ *
882
+ * // Set the data validation for cell A1:B2 to allow blank values
883
+ * const fRange = fWorksheet.getRange('A1:B2');
884
+ * const rule = univerAPI.newDataValidation()
885
+ * .requireValueInList(['Yes', 'No'])
886
+ * .setAllowBlank(true)
887
+ * .build();
888
+ * fRange.setDataValidation(rule);
889
+ *
890
+ * // Set the data validation for cell C1:D2 to not allow blank values
891
+ * const fRange2 = fWorksheet.getRange('C1:D2');
892
+ * const rule2 = univerAPI.newDataValidation()
893
+ * .requireValueInList(['Yes', 'No'])
894
+ * .setAllowBlank(false)
895
+ * .build();
896
+ * fRange2.setDataValidation(rule2);
897
+ * ```
898
+ */
899
+ setAllowBlank(allowBlank) {
900
+ this._rule.allowBlank = allowBlank;
901
+ return this;
902
+ }
903
+ /**
904
+ * Sets the options for the data validation rule.
905
+ * @param {Partial<IDataValidationRuleOptions>} options - The options to set for the data validation rule.
906
+ * @returns {FDataValidationBuilder} The current instance for method chaining.
907
+ * @example
908
+ * ```typescript
909
+ * const fWorkbook = univerAPI.getActiveWorkbook();
910
+ * const fWorksheet = fWorkbook.getActiveSheet();
911
+ *
912
+ * // Create a new data validation rule that requires the user to enter a value from the list ['Yes', 'No'] for the range A1:B10
913
+ * const fRange = fWorksheet.getRange('A1:B10');
914
+ * const rule = univerAPI.newDataValidation()
915
+ * .requireValueInList(['Yes', 'No'])
916
+ * .setOptions({
917
+ * allowBlank: true,
918
+ * showErrorMessage: true,
919
+ * error: 'Please enter a value from the list'
920
+ * })
921
+ * .build();
922
+ * fRange.setDataValidation(rule);
923
+ * ```
924
+ */
925
+ setOptions(options) {
926
+ Object.assign(this._rule, options);
927
+ return this;
928
+ }
929
+ };
930
+
931
+ //#endregion
932
+ //#region src/facade/f-data-validation.ts
933
+ /**
934
+ * @hideconstructor
935
+ */
936
+ var FDataValidation = class {
937
+ constructor(rule, worksheet, _injector) {
938
+ _defineProperty(this, "rule", void 0);
939
+ _defineProperty(this, "_worksheet", void 0);
940
+ _defineProperty(this, "_injector", void 0);
941
+ this._injector = _injector;
942
+ this.rule = rule;
943
+ this._worksheet = worksheet;
944
+ }
945
+ /**
946
+ * Gets whether invalid data is allowed based on the error style value
947
+ * @returns {boolean} true if invalid data is allowed, false otherwise
948
+ * @example
949
+ * ```typescript
950
+ * const fWorkbook = univerAPI.getActiveWorkbook();
951
+ * const fWorksheet = fWorkbook.getActiveSheet();
952
+ * const rules = fWorksheet.getDataValidations();
953
+ * rules.forEach((rule) => {
954
+ * console.log(rule, rule.getAllowInvalid());
955
+ * });
956
+ * ```
957
+ */
958
+ getAllowInvalid() {
959
+ return this.rule.errorStyle !== DataValidationErrorStyle.STOP;
960
+ }
961
+ /**
962
+ * Gets the data validation type of the rule
963
+ * @returns {DataValidationType | string} The data validation type
964
+ * @example
965
+ * ```typescript
966
+ * const fWorkbook = univerAPI.getActiveWorkbook();
967
+ * const fWorksheet = fWorkbook.getActiveSheet();
968
+ * const rules = fWorksheet.getDataValidations();
969
+ * rules.forEach((rule) => {
970
+ * console.log(rule, rule.getCriteriaType());
971
+ * });
972
+ * ```
973
+ */
974
+ getCriteriaType() {
975
+ return this.rule.type;
976
+ }
977
+ /**
978
+ * Gets the values used for criteria evaluation
979
+ * @returns {[string | undefined, string | undefined, string | undefined]} An array containing the operator, formula1, and formula2 values
980
+ * @example
981
+ * ```typescript
982
+ * const fWorkbook = univerAPI.getActiveWorkbook();
983
+ * const fWorksheet = fWorkbook.getActiveSheet();
984
+ * const rules = fWorksheet.getDataValidations();
985
+ * rules.forEach((rule) => {
986
+ * console.log(rule);
987
+ * const criteriaValues = rule.getCriteriaValues();
988
+ * const [operator, formula1, formula2] = criteriaValues;
989
+ * console.log(operator, formula1, formula2);
990
+ * });
991
+ * ```
992
+ */
993
+ getCriteriaValues() {
994
+ return [
995
+ this.rule.operator,
996
+ this.rule.formula1,
997
+ this.rule.formula2
998
+ ];
999
+ }
1000
+ /**
1001
+ * Gets the help text information, which is used to provide users with guidance and support
1002
+ * @returns {string | undefined} Returns the help text information. If there is no error message, it returns an undefined value
1003
+ * @example
1004
+ * ```typescript
1005
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1006
+ * const fWorksheet = fWorkbook.getActiveSheet();
1007
+ * const fRange = fWorksheet.getRange('A1:B10');
1008
+ * const rule = univerAPI.newDataValidation()
1009
+ * .requireNumberBetween(1, 10)
1010
+ * .setOptions({
1011
+ * allowBlank: true,
1012
+ * showErrorMessage: true,
1013
+ * error: 'Please enter a number between 1 and 10'
1014
+ * })
1015
+ * .build();
1016
+ * fRange.setDataValidation(rule);
1017
+ * console.log(fRange.getDataValidation().getHelpText()); // 'Please enter a number between 1 and 10'
1018
+ * ```
1019
+ */
1020
+ getHelpText() {
1021
+ return this.rule.error;
1022
+ }
1023
+ /**
1024
+ * Creates a new instance of FDataValidationBuilder using the current rule object
1025
+ * @returns {FDataValidationBuilder} A new FDataValidationBuilder instance with the same rule configuration
1026
+ * @example
1027
+ * ```typescript
1028
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1029
+ * const fWorksheet = fWorkbook.getActiveSheet();
1030
+ * const fRange = fWorksheet.getRange('A1:B10');
1031
+ * const rule = univerAPI.newDataValidation()
1032
+ * .requireNumberBetween(1, 10)
1033
+ * .setOptions({
1034
+ * allowBlank: true,
1035
+ * showErrorMessage: true,
1036
+ * error: 'Please enter a number between 1 and 10'
1037
+ * })
1038
+ * .build();
1039
+ * fRange.setDataValidation(rule);
1040
+ *
1041
+ * const builder = fRange.getDataValidation().copy();
1042
+ * const newRule = builder
1043
+ * .requireNumberBetween(1, 5)
1044
+ * .setOptions({
1045
+ * error: 'Please enter a number between 1 and 5'
1046
+ * })
1047
+ * .build();
1048
+ * fRange.setDataValidation(newRule);
1049
+ * ```
1050
+ */
1051
+ copy() {
1052
+ return new FDataValidationBuilder(this.rule);
1053
+ }
1054
+ /**
1055
+ * Gets whether the data validation rule is applied to the worksheet
1056
+ * @returns {boolean} true if the rule is applied, false otherwise
1057
+ * @example
1058
+ * ```typescript
1059
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1060
+ * const fWorksheet = fWorkbook.getActiveSheet();
1061
+ * const rules = fWorksheet.getDataValidations();
1062
+ * rules.forEach((rule) => {
1063
+ * console.log(rule, rule.getApplied());
1064
+ * });
1065
+ *
1066
+ * const fRange = fWorksheet.getRange('A1:B10');
1067
+ * console.log(fRange.getDataValidation()?.getApplied());
1068
+ * ```
1069
+ */
1070
+ getApplied() {
1071
+ if (!this._worksheet) return false;
1072
+ const currentRule = this._injector.get(DataValidationModel).getRuleById(this._worksheet.getUnitId(), this._worksheet.getSheetId(), this.rule.uid);
1073
+ if (currentRule && currentRule.ranges.length) return true;
1074
+ return false;
1075
+ }
1076
+ /**
1077
+ * Gets the ranges to which the data validation rule is applied
1078
+ * @returns {FRange[]} An array of FRange objects representing the ranges to which the data validation rule is applied
1079
+ * @example
1080
+ * ```typescript
1081
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1082
+ * const fWorksheet = fWorkbook.getActiveSheet();
1083
+ * const rules = fWorksheet.getDataValidations();
1084
+ * rules.forEach((rule) => {
1085
+ * console.log(rule);
1086
+ * const ranges = rule.getRanges();
1087
+ * ranges.forEach((range) => {
1088
+ * console.log(range.getA1Notation());
1089
+ * });
1090
+ * });
1091
+ * ```
1092
+ */
1093
+ getRanges() {
1094
+ if (!this.getApplied()) return [];
1095
+ const workbook = this._injector.get(IUniverInstanceService).getUnit(this._worksheet.getUnitId());
1096
+ return this.rule.ranges.map((range) => this._injector.createInstance(FRange, workbook, this._worksheet, range));
1097
+ }
1098
+ /**
1099
+ * Gets the unit ID of the worksheet
1100
+ * @returns {string | undefined} The unit ID of the worksheet
1101
+ * @example
1102
+ * ```typescript
1103
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1104
+ * const fWorksheet = fWorkbook.getActiveSheet();
1105
+ * const fRange = fWorksheet.getRange('A1:B10');
1106
+ * console.log(fRange.getDataValidation().getUnitId());
1107
+ * ```
1108
+ */
1109
+ getUnitId() {
1110
+ var _this$_worksheet;
1111
+ return (_this$_worksheet = this._worksheet) === null || _this$_worksheet === void 0 ? void 0 : _this$_worksheet.getUnitId();
1112
+ }
1113
+ /**
1114
+ * Gets the sheet ID of the worksheet
1115
+ * @returns {string | undefined} The sheet ID of the worksheet
1116
+ * @example
1117
+ * ```typescript
1118
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1119
+ * const fWorksheet = fWorkbook.getActiveSheet();
1120
+ * const fRange = fWorksheet.getRange('A1:B10');
1121
+ * console.log(fRange.getDataValidation().getSheetId());
1122
+ * ```
1123
+ */
1124
+ getSheetId() {
1125
+ var _this$_worksheet2;
1126
+ return (_this$_worksheet2 = this._worksheet) === null || _this$_worksheet2 === void 0 ? void 0 : _this$_worksheet2.getSheetId();
1127
+ }
1128
+ /**
1129
+ * Set Criteria for the data validation rule
1130
+ * @param {DataValidationType} type - The type of data validation criteria
1131
+ * @param {[DataValidationOperator, string, string]} values - An array containing the operator, formula1, and formula2 values
1132
+ * @param {boolean} [allowBlank] - Whether to allow blank values
1133
+ * @returns {FDataValidation} The current instance for method chaining
1134
+ * @example
1135
+ * ```typescript
1136
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1137
+ * const fWorksheet = fWorkbook.getActiveSheet();
1138
+ *
1139
+ * // Create a new data validation rule that requires a number equal to 20 for the range A1:B10
1140
+ * const fRange = fWorksheet.getRange('A1:B10');
1141
+ * const rule = univerAPI.newDataValidation()
1142
+ * .requireNumberEqualTo(20)
1143
+ * .build();
1144
+ * fRange.setDataValidation(rule);
1145
+ *
1146
+ * // Change the rule criteria to require a number between 1 and 10
1147
+ * fRange.getDataValidation().setCriteria(
1148
+ * univerAPI.Enum.DataValidationType.DECIMAL,
1149
+ * [univerAPI.Enum.DataValidationOperator.BETWEEN, '1', '10']
1150
+ * );
1151
+ * ```
1152
+ */
1153
+ setCriteria(type, values, allowBlank = true) {
1154
+ if (this.getApplied()) {
1155
+ if (!this._injector.get(ICommandService).syncExecuteCommand(UpdateSheetDataValidationSettingCommand.id, {
1156
+ unitId: this.getUnitId(),
1157
+ subUnitId: this.getSheetId(),
1158
+ ruleId: this.rule.uid,
1159
+ setting: {
1160
+ operator: values[0],
1161
+ formula1: values[1],
1162
+ formula2: values[2],
1163
+ type: this.rule.type,
1164
+ allowBlank
1165
+ }
1166
+ })) throw new Error("setCriteria failed");
1167
+ }
1168
+ this.rule.operator = values[0];
1169
+ this.rule.formula1 = values[1];
1170
+ this.rule.formula2 = values[2];
1171
+ this.rule.type = type;
1172
+ this.rule.allowBlank = allowBlank;
1173
+ return this;
1174
+ }
1175
+ /**
1176
+ * Set the options for the data validation rule
1177
+ * @param {Partial<IDataValidationRuleOptions>} options - The options to set for the data validation rule
1178
+ * @returns {FDataValidation} The current instance for method chaining
1179
+ * @example
1180
+ * ```typescript
1181
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1182
+ * const fWorksheet = fWorkbook.getActiveSheet();
1183
+ *
1184
+ * // Create a new data validation rule that requires a number equal to 20 for the range A1:B10
1185
+ * const fRange = fWorksheet.getRange('A1:B10');
1186
+ * const rule = univerAPI.newDataValidation()
1187
+ * .requireNumberEqualTo(20)
1188
+ * .build();
1189
+ * fRange.setDataValidation(rule);
1190
+ *
1191
+ * // Supplement the rule with additional options
1192
+ * fRange.getDataValidation().setOptions({
1193
+ * allowBlank: true,
1194
+ * showErrorMessage: true,
1195
+ * error: 'Please enter a valid value'
1196
+ * });
1197
+ * ```
1198
+ */
1199
+ setOptions(options) {
1200
+ if (this.getApplied()) {
1201
+ if (!this._injector.get(ICommandService).syncExecuteCommand(UpdateSheetDataValidationOptionsCommand.id, {
1202
+ unitId: this.getUnitId(),
1203
+ subUnitId: this.getSheetId(),
1204
+ ruleId: this.rule.uid,
1205
+ options: {
1206
+ ...getRuleOptions(this.rule),
1207
+ ...options
1208
+ }
1209
+ })) throw new Error("setOptions failed");
1210
+ }
1211
+ Object.assign(this.rule, options);
1212
+ return this;
1213
+ }
1214
+ /**
1215
+ * Set the ranges to the data validation rule
1216
+ * @param {FRange[]} ranges - New ranges array
1217
+ * @returns {FDataValidation} The current instance for method chaining
1218
+ * @example
1219
+ * ```typescript
1220
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1221
+ * const fWorksheet = fWorkbook.getActiveSheet();
1222
+ *
1223
+ * // Create a new data validation rule that requires a number equal to 20 for the range A1:B10
1224
+ * const fRange = fWorksheet.getRange('A1:B10');
1225
+ * const rule = univerAPI.newDataValidation()
1226
+ * .requireNumberEqualTo(20)
1227
+ * .build();
1228
+ * fRange.setDataValidation(rule);
1229
+ *
1230
+ * // Change the range to C1:D10
1231
+ * const newRuleRange = fWorksheet.getRange('C1:D10');
1232
+ * fRange.getDataValidation().setRanges([newRuleRange]);
1233
+ * ```
1234
+ */
1235
+ setRanges(ranges) {
1236
+ if (this.getApplied()) {
1237
+ if (!this._injector.get(ICommandService).syncExecuteCommand(UpdateSheetDataValidationRangeCommand.id, {
1238
+ unitId: this.getUnitId(),
1239
+ subUnitId: this.getSheetId(),
1240
+ ruleId: this.rule.uid,
1241
+ ranges: ranges.map((range) => range.getRange())
1242
+ })) throw new Error("setRanges failed");
1243
+ }
1244
+ this.rule.ranges = ranges.map((range) => range.getRange());
1245
+ return this;
1246
+ }
1247
+ /**
1248
+ * Delete the data validation rule from the worksheet
1249
+ * @returns {boolean} true if the rule is deleted successfully, false otherwise
1250
+ * @example
1251
+ * ```typescript
1252
+ * const fWorkbook = univerAPI.getActiveWorkbook();
1253
+ * const fWorksheet = fWorkbook.getActiveSheet();
1254
+ *
1255
+ * // Create a new data validation rule that requires a number equal to 20 for the range A1:B10
1256
+ * const fRange = fWorksheet.getRange('A1:B10');
1257
+ * const rule = univerAPI.newDataValidation()
1258
+ * .requireNumberEqualTo(20)
1259
+ * .build();
1260
+ * fRange.setDataValidation(rule);
1261
+ *
1262
+ * // Delete the data validation rule
1263
+ * fRange.getDataValidation().delete();
1264
+ * ```
1265
+ */
1266
+ delete() {
1267
+ if (!this.getApplied()) return false;
1268
+ return this._injector.get(ICommandService).syncExecuteCommand(RemoveSheetDataValidationCommand.id, {
1269
+ unitId: this.getUnitId(),
1270
+ subUnitId: this.getSheetId(),
1271
+ ruleId: this.rule.uid
1272
+ });
1273
+ }
1274
+ };
1275
+
1276
+ //#endregion
1277
+ //#region src/facade/f-range.ts
1278
+ /**
1279
+ * @ignore
1280
+ */
1281
+ var FRangeSheetsDataValidationMixin = class extends FRange {
1282
+ setDataValidation(rule) {
1283
+ if (!rule) {
1284
+ this._commandService.syncExecuteCommand(ClearRangeDataValidationCommand.id, {
1285
+ unitId: this._workbook.getUnitId(),
1286
+ subUnitId: this._worksheet.getSheetId(),
1287
+ ranges: [this._range]
1288
+ });
1289
+ return this;
1290
+ }
1291
+ const params = {
1292
+ unitId: this._workbook.getUnitId(),
1293
+ subUnitId: this._worksheet.getSheetId(),
1294
+ rule: {
1295
+ ...rule.rule,
1296
+ ranges: [this._range]
1297
+ }
1298
+ };
1299
+ this._commandService.syncExecuteCommand(AddSheetDataValidationCommand.id, params);
1300
+ return this;
1301
+ }
1302
+ getDataValidation() {
1303
+ const rule = this._injector.get(SheetsDataValidationValidatorService).getDataValidation(this._workbook.getUnitId(), this._worksheet.getSheetId(), [this._range]);
1304
+ if (rule) return new FDataValidation(rule, this._worksheet, this._injector);
1305
+ return rule;
1306
+ }
1307
+ getDataValidations() {
1308
+ return this._injector.get(SheetsDataValidationValidatorService).getDataValidations(this._workbook.getUnitId(), this._worksheet.getSheetId(), [this._range]).map((rule) => new FDataValidation(rule, this._worksheet, this._injector));
1309
+ }
1310
+ async getValidatorStatus() {
1311
+ return this._injector.get(SheetsDataValidationValidatorService).validatorRanges(this._workbook.getUnitId(), this._worksheet.getSheetId(), [this._range]);
1312
+ }
1313
+ async getDataValidationErrorAsync() {
1314
+ const unitId = this._workbook.getUnitId();
1315
+ const sheetId = this._worksheet.getSheetId();
1316
+ return this._collectValidationErrorsForRange(unitId, sheetId, [this._range]);
1317
+ }
1318
+ async _collectValidationErrorsForRange(unitId, sheetId, ranges) {
1319
+ if (!ranges.length) return [];
1320
+ const validatorService = this._injector.get(SheetsDataValidationValidatorService);
1321
+ const worksheet = this._worksheet;
1322
+ const sheetName = worksheet.getName();
1323
+ const errors = [];
1324
+ for (const range of ranges) {
1325
+ const promises = [];
1326
+ for (let row = range.startRow; row <= range.endRow; row++) for (let col = range.startColumn; col <= range.endColumn; col++) promises.push((async () => {
1327
+ try {
1328
+ if (await validatorService.validatorCell(unitId, sheetId, row, col) !== DataValidationStatus.VALID) {
1329
+ const rule = this._injector.get(SheetDataValidationModel).getRuleByLocation(unitId, sheetId, row, col);
1330
+ if (rule) {
1331
+ var _worksheet$getCell;
1332
+ const cellValue = ((_worksheet$getCell = worksheet.getCell(row, col)) === null || _worksheet$getCell === void 0 ? void 0 : _worksheet$getCell.v) || null;
1333
+ const error = this._createDataValidationError(sheetName, row, col, rule, cellValue);
1334
+ errors.push(error);
1335
+ }
1336
+ }
1337
+ } catch (e) {
1338
+ console.warn(`Failed to validate cell [${row}, ${col}]:`, e);
1339
+ }
1340
+ })());
1341
+ await Promise.all(promises);
1342
+ }
1343
+ return errors;
1344
+ }
1345
+ _createDataValidationError(sheetName, row, column, rule, inputValue) {
1346
+ return {
1347
+ sheetName,
1348
+ row,
1349
+ column,
1350
+ ruleId: rule.uid,
1351
+ inputValue,
1352
+ rule
1353
+ };
1354
+ }
1355
+ };
1356
+ FRange.extend(FRangeSheetsDataValidationMixin);
1357
+
1358
+ //#endregion
1359
+ //#region src/facade/f-univer.ts
1360
+ var FUniverSheetsDataValidationMixin = class extends FUniver {
1361
+ /**
1362
+ * @deprecated use `univerAPI.newDataValidation()` as instead.
1363
+ * @returns {FDataValidationBuilder} A new instance of the FDataValidationBuilder class
1364
+ */
1365
+ static newDataValidation() {
1366
+ return new FDataValidationBuilder();
1367
+ }
1368
+ newDataValidation() {
1369
+ return new FDataValidationBuilder();
1370
+ }
1371
+ /**
1372
+ * @ignore
1373
+ */
1374
+ _initialize(injector) {
1375
+ const commandService = injector.get(ICommandService);
1376
+ this.disposeWithMe(this.registerEventHandler(this.Event.SheetDataValidationChanged, () => {
1377
+ if (!injector.has(SheetDataValidationModel)) return { dispose: () => {} };
1378
+ return injector.get(SheetDataValidationModel).ruleChange$.subscribe((ruleChange) => {
1379
+ const { unitId, subUnitId, rule, oldRule, type } = ruleChange;
1380
+ const target = this.getSheetTarget(unitId, subUnitId);
1381
+ if (!target) return;
1382
+ const { workbook, worksheet } = target;
1383
+ const eventParams = {
1384
+ origin: ruleChange,
1385
+ worksheet,
1386
+ workbook,
1387
+ changeType: type,
1388
+ oldRule,
1389
+ rule: new FDataValidation(rule, worksheet.getSheet(), this._injector)
1390
+ };
1391
+ this.fireEvent(this.Event.SheetDataValidationChanged, eventParams);
1392
+ });
1393
+ }));
1394
+ this.disposeWithMe(this.registerEventHandler(this.Event.SheetDataValidatorStatusChanged, () => {
1395
+ if (!injector.has(SheetDataValidationModel)) return { dispose: () => {} };
1396
+ return injector.get(SheetDataValidationModel).validStatusChange$.subscribe((statusChange) => {
1397
+ const { unitId, subUnitId, ruleId, status, row, col } = statusChange;
1398
+ const target = this.getSheetTarget(unitId, subUnitId);
1399
+ if (!target) return;
1400
+ const { workbook, worksheet } = target;
1401
+ const rule = worksheet.getDataValidation(ruleId);
1402
+ if (!rule) return;
1403
+ const eventParams = {
1404
+ workbook,
1405
+ worksheet,
1406
+ row,
1407
+ column: col,
1408
+ rule,
1409
+ status
1410
+ };
1411
+ this.fireEvent(this.Event.SheetDataValidatorStatusChanged, eventParams);
1412
+ });
1413
+ }));
1414
+ this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationAdd, () => commandService.beforeCommandExecuted((commandInfo) => {
1415
+ if (commandInfo.id === AddSheetDataValidationCommand.id) {
1416
+ const params = commandInfo.params;
1417
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
1418
+ if (!target) return;
1419
+ const { workbook, worksheet } = target;
1420
+ const eventParams = {
1421
+ worksheet,
1422
+ workbook,
1423
+ rule: params.rule
1424
+ };
1425
+ this.fireEvent(this.Event.BeforeSheetDataValidationAdd, eventParams);
1426
+ if (eventParams.cancel) throw new CanceledError();
1427
+ }
1428
+ })));
1429
+ this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationCriteriaUpdate, () => commandService.beforeCommandExecuted((commandInfo) => {
1430
+ if (commandInfo.id === UpdateSheetDataValidationSettingCommand.id) {
1431
+ const params = commandInfo.params;
1432
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
1433
+ if (!target) return;
1434
+ const { workbook, worksheet } = target;
1435
+ const rule = worksheet.getDataValidation(params.ruleId);
1436
+ if (!rule) return;
1437
+ const eventParams = {
1438
+ worksheet,
1439
+ workbook,
1440
+ rule,
1441
+ ruleId: params.ruleId,
1442
+ newCriteria: params.setting
1443
+ };
1444
+ this.fireEvent(this.Event.BeforeSheetDataValidationCriteriaUpdate, eventParams);
1445
+ if (eventParams.cancel) throw new CanceledError();
1446
+ }
1447
+ })));
1448
+ this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationRangeUpdate, () => commandService.beforeCommandExecuted((commandInfo) => {
1449
+ if (commandInfo.id === UpdateSheetDataValidationRangeCommand.id) {
1450
+ const params = commandInfo.params;
1451
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
1452
+ if (!target) return;
1453
+ const { workbook, worksheet } = target;
1454
+ const rule = worksheet.getDataValidation(params.ruleId);
1455
+ if (!rule) return;
1456
+ const eventParams = {
1457
+ worksheet,
1458
+ workbook,
1459
+ rule,
1460
+ ruleId: params.ruleId,
1461
+ newRanges: params.ranges
1462
+ };
1463
+ this.fireEvent(this.Event.BeforeSheetDataValidationRangeUpdate, eventParams);
1464
+ if (eventParams.cancel) throw new CanceledError();
1465
+ }
1466
+ })));
1467
+ this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationOptionsUpdate, () => commandService.beforeCommandExecuted((commandInfo) => {
1468
+ if (commandInfo.id === UpdateSheetDataValidationOptionsCommand.id) {
1469
+ const params = commandInfo.params;
1470
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
1471
+ if (!target) return;
1472
+ const { workbook, worksheet } = target;
1473
+ const rule = worksheet.getDataValidation(params.ruleId);
1474
+ if (!rule) return;
1475
+ const eventParams = {
1476
+ worksheet,
1477
+ workbook,
1478
+ rule,
1479
+ ruleId: params.ruleId,
1480
+ newOptions: params.options
1481
+ };
1482
+ this.fireEvent(this.Event.BeforeSheetDataValidationOptionsUpdate, eventParams);
1483
+ if (eventParams.cancel) throw new CanceledError();
1484
+ }
1485
+ })));
1486
+ this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationDelete, () => commandService.beforeCommandExecuted((commandInfo) => {
1487
+ if (commandInfo.id === RemoveSheetDataValidationCommand.id) {
1488
+ const params = commandInfo.params;
1489
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
1490
+ if (!target) return;
1491
+ const { workbook, worksheet } = target;
1492
+ const rule = worksheet.getDataValidation(params.ruleId);
1493
+ if (!rule) return;
1494
+ const eventParams = {
1495
+ worksheet,
1496
+ workbook,
1497
+ rule,
1498
+ ruleId: params.ruleId
1499
+ };
1500
+ this.fireEvent(this.Event.BeforeSheetDataValidationDelete, eventParams);
1501
+ if (eventParams.cancel) throw new CanceledError();
1502
+ }
1503
+ })));
1504
+ this.disposeWithMe(this.registerEventHandler(this.Event.BeforeSheetDataValidationDeleteAll, () => commandService.beforeCommandExecuted((commandInfo) => {
1505
+ if (commandInfo.id === RemoveSheetAllDataValidationCommand.id) {
1506
+ const params = commandInfo.params;
1507
+ const target = this.getSheetTarget(params.unitId, params.subUnitId);
1508
+ if (!target) return;
1509
+ const { workbook, worksheet } = target;
1510
+ const eventParams = {
1511
+ worksheet,
1512
+ workbook,
1513
+ rules: worksheet.getDataValidations()
1514
+ };
1515
+ this.fireEvent(this.Event.BeforeSheetDataValidationDeleteAll, eventParams);
1516
+ if (eventParams.cancel) throw new CanceledError();
1517
+ }
1518
+ })));
1519
+ }
1520
+ };
1521
+ FUniver.extend(FUniverSheetsDataValidationMixin);
1522
+
1523
+ //#endregion
1524
+ //#region src/facade/f-workbook.ts
1525
+ /**
1526
+ * @ignore
1527
+ */
1528
+ var FWorkbookSheetsDataValidationMixin = class extends FWorkbook {
1529
+ _initialize() {
1530
+ Object.defineProperty(this, "_dataValidationModel", { get() {
1531
+ return this._injector.get(SheetDataValidationModel);
1532
+ } });
1533
+ }
1534
+ getValidatorStatus() {
1535
+ return this._injector.get(SheetsDataValidationValidatorService).validatorWorkbook(this._workbook.getUnitId());
1536
+ }
1537
+ async getAllDataValidationErrorAsync() {
1538
+ const unitId = this._workbook.getUnitId();
1539
+ const sheetIds = this._dataValidationModel.getSubUnitIds(unitId);
1540
+ const allErrors = [];
1541
+ for (const sheetId of sheetIds) {
1542
+ const sheetErrors = await this._collectValidationErrorsForSheet(unitId, sheetId);
1543
+ allErrors.push(...sheetErrors);
1544
+ }
1545
+ return allErrors;
1546
+ }
1547
+ async _collectValidationErrorsForSheet(unitId, sheetId) {
1548
+ const rules = this._dataValidationModel.getRules(unitId, sheetId);
1549
+ if (!rules.length) return [];
1550
+ const allRanges = rules.flatMap((rule) => rule.ranges);
1551
+ return this._collectValidationErrorsForRange(unitId, sheetId, allRanges);
1552
+ }
1553
+ async _collectValidationErrorsForRange(unitId, sheetId, ranges) {
1554
+ if (!ranges.length) return [];
1555
+ const validatorService = this._injector.get(SheetsDataValidationValidatorService);
1556
+ const worksheet = this._workbook.getSheetBySheetId(sheetId);
1557
+ if (!worksheet) throw new Error(`Cannot find worksheet with sheetId: ${sheetId}`);
1558
+ const sheetName = worksheet.getName();
1559
+ const errors = [];
1560
+ for (const range of ranges) {
1561
+ const promises = [];
1562
+ for (let row = range.startRow; row <= range.endRow; row++) for (let col = range.startColumn; col <= range.endColumn; col++) promises.push((async () => {
1563
+ try {
1564
+ if (await validatorService.validatorCell(unitId, sheetId, row, col) !== DataValidationStatus.VALID) {
1565
+ const rule = this._dataValidationModel.getRuleByLocation(unitId, sheetId, row, col);
1566
+ if (rule) {
1567
+ var _worksheet$getCell;
1568
+ const cellValue = ((_worksheet$getCell = worksheet.getCell(row, col)) === null || _worksheet$getCell === void 0 ? void 0 : _worksheet$getCell.v) || null;
1569
+ const error = this._createDataValidationError(sheetName, row, col, rule, cellValue);
1570
+ errors.push(error);
1571
+ }
1572
+ }
1573
+ } catch (e) {
1574
+ console.warn(`Failed to validate cell [${row}, ${col}]:`, e);
1575
+ }
1576
+ })());
1577
+ await Promise.all(promises);
1578
+ }
1579
+ return errors;
1580
+ }
1581
+ _createDataValidationError(sheetName, row, column, rule, inputValue) {
1582
+ return {
1583
+ sheetName,
1584
+ row,
1585
+ column,
1586
+ ruleId: rule.uid,
1587
+ inputValue,
1588
+ rule
1589
+ };
1590
+ }
1591
+ onDataValidationChange(callback) {
1592
+ return toDisposable(this._dataValidationModel.ruleChange$.pipe(filter((change) => change.unitId === this._workbook.getUnitId())).subscribe(callback));
1593
+ }
1594
+ onDataValidationStatusChange(callback) {
1595
+ return toDisposable(this._dataValidationModel.validStatusChange$.pipe(filter((change) => change.unitId === this._workbook.getUnitId())).subscribe(callback));
1596
+ }
1597
+ onBeforeAddDataValidation(callback) {
1598
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
1599
+ const params = commandInfo.params;
1600
+ if (commandInfo.id === AddSheetDataValidationCommand.id) {
1601
+ if (params.unitId !== this._workbook.getUnitId()) return;
1602
+ if (callback(params, options) === false) throw new Error("Command is stopped by the hook onBeforeAddDataValidation");
1603
+ }
1604
+ }));
1605
+ }
1606
+ onBeforeUpdateDataValidationCriteria(callback) {
1607
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
1608
+ const params = commandInfo.params;
1609
+ if (commandInfo.id === UpdateSheetDataValidationSettingCommand.id) {
1610
+ if (params.unitId !== this._workbook.getUnitId()) return;
1611
+ if (callback(params, options) === false) throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationCriteria");
1612
+ }
1613
+ }));
1614
+ }
1615
+ onBeforeUpdateDataValidationRange(callback) {
1616
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
1617
+ const params = commandInfo.params;
1618
+ if (commandInfo.id === UpdateSheetDataValidationRangeCommand.id) {
1619
+ if (params.unitId !== this._workbook.getUnitId()) return;
1620
+ if (callback(params, options) === false) throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationRange");
1621
+ }
1622
+ }));
1623
+ }
1624
+ onBeforeUpdateDataValidationOptions(callback) {
1625
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
1626
+ const params = commandInfo.params;
1627
+ if (commandInfo.id === UpdateSheetDataValidationOptionsCommand.id) {
1628
+ if (params.unitId !== this._workbook.getUnitId()) return;
1629
+ if (callback(params, options) === false) throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationOptions");
1630
+ }
1631
+ }));
1632
+ }
1633
+ onBeforeDeleteDataValidation(callback) {
1634
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
1635
+ const params = commandInfo.params;
1636
+ if (commandInfo.id === RemoveSheetDataValidationCommand.id) {
1637
+ if (params.unitId !== this._workbook.getUnitId()) return;
1638
+ if (callback(params, options) === false) throw new Error("Command is stopped by the hook onBeforeDeleteDataValidation");
1639
+ }
1640
+ }));
1641
+ }
1642
+ onBeforeDeleteAllDataValidation(callback) {
1643
+ return toDisposable(this._commandService.beforeCommandExecuted((commandInfo, options) => {
1644
+ const params = commandInfo.params;
1645
+ if (commandInfo.id === RemoveSheetAllDataValidationCommand.id) {
1646
+ if (params.unitId !== this._workbook.getUnitId()) return;
1647
+ if (callback(params, options) === false) throw new Error("Command is stopped by the hook onBeforeDeleteAllDataValidation");
1648
+ }
1649
+ }));
1650
+ }
1651
+ };
1652
+ FWorkbook.extend(FWorkbookSheetsDataValidationMixin);
1653
+
1654
+ //#endregion
1655
+ //#region src/facade/f-worksheet.ts
1656
+ /**
1657
+ * @ignore
1658
+ */
1659
+ var FWorksheetDataValidationMixin = class extends FWorksheet {
1660
+ getDataValidations() {
1661
+ return this._injector.get(DataValidationModel).getRules(this._workbook.getUnitId(), this._worksheet.getSheetId()).map((rule) => new FDataValidation(rule, this._worksheet, this._injector));
1662
+ }
1663
+ getValidatorStatus() {
1664
+ return this._injector.get(SheetsDataValidationValidatorService).validatorWorksheet(this._workbook.getUnitId(), this._worksheet.getSheetId());
1665
+ }
1666
+ getValidatorStatusAsync() {
1667
+ return this.getValidatorStatus();
1668
+ }
1669
+ getDataValidation(ruleId) {
1670
+ const rule = this._injector.get(DataValidationModel).getRuleById(this._workbook.getUnitId(), this._worksheet.getSheetId(), ruleId);
1671
+ if (rule) return new FDataValidation(rule, this._worksheet, this._injector);
1672
+ return null;
1673
+ }
1674
+ async getAllDataValidationErrorAsync() {
1675
+ const unitId = this._workbook.getUnitId();
1676
+ const sheetId = this._worksheet.getSheetId();
1677
+ return this._collectValidationErrorsForSheet(unitId, sheetId);
1678
+ }
1679
+ async _collectValidationErrorsForSheet(unitId, sheetId) {
1680
+ const rules = this._injector.get(DataValidationModel).getRules(unitId, sheetId);
1681
+ if (!rules.length) return [];
1682
+ const allRanges = rules.flatMap((rule) => rule.ranges);
1683
+ return this._collectValidationErrorsForRange(unitId, sheetId, allRanges);
1684
+ }
1685
+ async _collectValidationErrorsForRange(unitId, sheetId, ranges) {
1686
+ if (!ranges.length) return [];
1687
+ const validatorService = this._injector.get(SheetsDataValidationValidatorService);
1688
+ const worksheet = this._worksheet;
1689
+ const sheetName = worksheet.getName();
1690
+ const errors = [];
1691
+ for (const range of ranges) {
1692
+ const promises = [];
1693
+ for (let row = range.startRow; row <= range.endRow; row++) for (let col = range.startColumn; col <= range.endColumn; col++) promises.push((async () => {
1694
+ try {
1695
+ if (await validatorService.validatorCell(unitId, sheetId, row, col) !== DataValidationStatus.VALID) {
1696
+ const rule = this._injector.get(SheetDataValidationModel).getRuleByLocation(unitId, sheetId, row, col);
1697
+ if (rule) {
1698
+ var _worksheet$getCell;
1699
+ const cellValue = ((_worksheet$getCell = worksheet.getCell(row, col)) === null || _worksheet$getCell === void 0 ? void 0 : _worksheet$getCell.v) || null;
1700
+ const error = this._createDataValidationError(sheetName, row, col, rule, cellValue);
1701
+ errors.push(error);
1702
+ }
1703
+ }
1704
+ } catch (e) {
1705
+ console.warn(`Failed to validate cell [${row}, ${col}]:`, e);
1706
+ }
1707
+ })());
1708
+ await Promise.all(promises);
1709
+ }
1710
+ return errors;
1711
+ }
1712
+ _createDataValidationError(sheetName, row, column, rule, inputValue) {
1713
+ return {
1714
+ sheetName,
1715
+ row,
1716
+ column,
1717
+ ruleId: rule.uid,
1718
+ inputValue,
1719
+ rule
1720
+ };
1721
+ }
1722
+ };
1723
+ FWorksheet.extend(FWorksheetDataValidationMixin);
1724
+
1725
+ //#endregion
1726
+ //#region src/facade/f-event.ts
1727
+ /**
1728
+ * @ignore
1729
+ */
1730
+ var FSheetsDataValidationEventNameMixin = class extends FEventName {
1731
+ get SheetDataValidationChanged() {
1732
+ return "SheetDataValidationChanged";
1733
+ }
1734
+ get SheetDataValidatorStatusChanged() {
1735
+ return "SheetDataValidatorStatusChanged";
1736
+ }
1737
+ get BeforeSheetDataValidationAdd() {
1738
+ return "BeforeSheetDataValidationAdd";
1739
+ }
1740
+ get BeforeSheetDataValidationDelete() {
1741
+ return "BeforeSheetDataValidationDelete";
1742
+ }
1743
+ get BeforeSheetDataValidationDeleteAll() {
1744
+ return "BeforeSheetDataValidationDeleteAll";
1745
+ }
1746
+ get BeforeSheetDataValidationCriteriaUpdate() {
1747
+ return "BeforeSheetDataValidationCriteriaUpdate";
1748
+ }
1749
+ get BeforeSheetDataValidationRangeUpdate() {
1750
+ return "BeforeSheetDataValidationRangeUpdate";
1751
+ }
1752
+ get BeforeSheetDataValidationOptionsUpdate() {
1753
+ return "BeforeSheetDataValidationOptionsUpdate";
1754
+ }
1755
+ };
1756
+ FEventName.extend(FSheetsDataValidationEventNameMixin);
1757
+
1758
+ //#endregion
1759
+ export { FDataValidation, FDataValidationBuilder };