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