facade.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. var j = Object.defineProperty;
  2. var q = (c, e, t) => e in c ? j(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
  3. var f = (c, e, t) => q(c, typeof e != "symbol" ? e + "" : e, t);
  4. import { UpdateSheetDataValidationSettingCommand as U, UpdateSheetDataValidationOptionsCommand as v, UpdateSheetDataValidationRangeCommand as C, RemoveSheetDataValidationCommand as T, ClearRangeDataValidationCommand as W, AddSheetDataValidationCommand as A, SheetsDataValidationValidatorService as S, SheetDataValidationModel as V, RemoveSheetAllDataValidationCommand as O } from "@univerjs/sheets-data-validation";
  5. import { FRange as y, FWorkbook as M, FWorksheet as N } from "@univerjs/sheets/facade";
  6. import { generateRandomId as B, DataValidationType as o, DataValidationErrorStyle as w, DataValidationOperator as h, IUniverInstanceService as H, ICommandService as p, FUniver as x, CanceledError as _, toDisposable as g, FEventName as $ } from "@univerjs/core";
  7. import { DataValidationModel as b, getRuleOptions as F } from "@univerjs/data-validation";
  8. import { serializeRangeToRefString as P } from "@univerjs/engine-formula";
  9. import { filter as R } from "rxjs";
  10. class D {
  11. constructor(e) {
  12. f(this, "_rule");
  13. this._rule = e != null ? e : {
  14. uid: B(),
  15. ranges: void 0,
  16. type: o.CUSTOM
  17. };
  18. }
  19. /**
  20. * Builds an FDataValidation instance based on the _rule property of the current class
  21. * @returns {FDataValidation} A new instance of the FDataValidation class
  22. * @example
  23. * ```typescript
  24. * const builder = univerAPI.newDataValidation();
  25. * const validation = builder.requireNumberBetween(1, 10).build();
  26. * ```
  27. */
  28. build() {
  29. return new m(this._rule);
  30. }
  31. /**
  32. * Creates a duplicate of the current DataValidationBuilder object
  33. * @returns {FDataValidationBuilder} A new instance of the DataValidationBuilder class
  34. * @example
  35. * ```typescript
  36. * const builder = univerAPI.newDataValidation();
  37. * const copy = builder.requireNumberBetween(1, 10).copy();
  38. * ```
  39. */
  40. copy() {
  41. return new D({
  42. ...this._rule,
  43. uid: B()
  44. });
  45. }
  46. /**
  47. * Determines whether invalid data is allowed
  48. * @returns {boolean} True if invalid data is allowed, False otherwise
  49. * @example
  50. * ```typescript
  51. * const builder = univerAPI.newDataValidation();
  52. * const allowsInvalid = builder.getAllowInvalid();
  53. * ```
  54. */
  55. getAllowInvalid() {
  56. return this._rule.errorStyle !== w.STOP;
  57. }
  58. /**
  59. * Gets the data validation type of the rule
  60. * @returns {DataValidationType | string} The data validation type
  61. * @example
  62. * ```typescript
  63. * const builder = univerAPI.newDataValidation();
  64. * const type = builder.getCriteriaType();
  65. * ```
  66. */
  67. getCriteriaType() {
  68. return this._rule.type;
  69. }
  70. /**
  71. * Gets the values used for criteria evaluation
  72. * @returns {[string | undefined, string | undefined, string | undefined]} An array containing the operator, formula1, and formula2 values
  73. * @example
  74. * ```typescript
  75. * const builder = univerAPI.newDataValidation();
  76. * const [operator, formula1, formula2] = builder.getCriteriaValues();
  77. * ```
  78. */
  79. getCriteriaValues() {
  80. return [this._rule.operator, this._rule.formula1, this._rule.formula2];
  81. }
  82. /**
  83. * Gets the help text information, which is used to provide users with guidance and support
  84. * @returns {string | undefined} Returns the help text information. If there is no error message, it returns an undefined value
  85. * @example
  86. * ```typescript
  87. * const builder = univerAPI.newDataValidation();
  88. * const helpText = builder.getHelpText();
  89. * ```
  90. */
  91. getHelpText() {
  92. return this._rule.error;
  93. }
  94. /**
  95. * Sets the data validation type to CHECKBOX and sets the checked and unchecked values
  96. * @param {string} [checkedValue] - The value when the checkbox is checked
  97. * @param {string} [uncheckedValue] - The value when the checkbox is unchecked
  98. * @returns {FDataValidationBuilder} The current instance for method chaining
  99. * @example
  100. * ```typescript
  101. * const builder = univerAPI.newDataValidation();
  102. * const rule = builder.requireCheckbox('Yes', 'No').build();
  103. * ```
  104. */
  105. requireCheckbox(e, t) {
  106. return this._rule.type = o.CHECKBOX, this._rule.formula1 = e, this._rule.formula2 = t, this;
  107. }
  108. /**
  109. * Set the data validation type to DATE and configure the validation rules to be after a specific date
  110. * @param {Date} date - The date to compare against
  111. * @returns {FDataValidationBuilder} The current instance for method chaining
  112. * @example
  113. * ```typescript
  114. * const builder = univerAPI.newDataValidation();
  115. * const rule = builder.requireDateAfter(new Date('2024-01-01')).build();
  116. * ```
  117. */
  118. requireDateAfter(e) {
  119. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.operator = h.GREATER_THAN, this;
  120. }
  121. /**
  122. * Set the data validation type to DATE and configure the validation rules to be before a specific date
  123. * @param {Date} date - The date to compare against
  124. * @returns {FDataValidationBuilder} The current instance for method chaining
  125. * @example
  126. * ```typescript
  127. * const builder = univerAPI.newDataValidation();
  128. * const rule = builder.requireDateBefore(new Date('2024-12-31')).build();
  129. * ```
  130. */
  131. requireDateBefore(e) {
  132. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = h.LESS_THAN, this;
  133. }
  134. /**
  135. * Set the data validation type to DATE and configure the validation rules to be within a specific date range
  136. * @param {Date} start - The starting date of the range
  137. * @param {Date} end - The ending date of the range
  138. * @returns {FDataValidationBuilder} The current instance for method chaining
  139. * @example
  140. * ```typescript
  141. * const builder = univerAPI.newDataValidation();
  142. * const rule = builder
  143. * .requireDateBetween(new Date('2024-01-01'), new Date('2024-12-31'))
  144. * .build();
  145. * ```
  146. */
  147. requireDateBetween(e, t) {
  148. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.formula2 = t.toLocaleDateString(), this._rule.operator = h.BETWEEN, this;
  149. }
  150. /**
  151. * Set the data validation type to DATE and configure the validation rules to be equal to a specific date
  152. * @param {Date} date - The date to compare against
  153. * @returns {FDataValidationBuilder} The current instance for method chaining
  154. * @example
  155. * ```typescript
  156. * const builder = univerAPI.newDataValidation();
  157. * const rule = builder.requireDateEqualTo(new Date('2024-01-01')).build();
  158. * ```
  159. */
  160. requireDateEqualTo(e) {
  161. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = h.EQUAL, this;
  162. }
  163. /**
  164. * Set the data validation type to DATE and configure the validation rules to be not within a specific date range
  165. * @param {Date} start - The starting date of the date range
  166. * @param {Date} end - The ending date of the date range
  167. * @returns {FDataValidationBuilder} The current instance for method chaining
  168. * @example
  169. * ```typescript
  170. * const builder = univerAPI.newDataValidation();
  171. * const rule = builder
  172. * .requireDateNotBetween(new Date('2024-01-01'), new Date('2024-12-31'))
  173. * .build();
  174. * ```
  175. */
  176. requireDateNotBetween(e, t) {
  177. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.formula2 = t.toLocaleDateString(), this._rule.operator = h.NOT_BETWEEN, this;
  178. }
  179. /**
  180. * Set the data validation type to DATE and configure the validation rules to be on or after a specific date
  181. * @param {Date} date - The date to compare against
  182. * @returns {FDataValidationBuilder} The current instance for method chaining
  183. * @example
  184. * ```typescript
  185. * const builder = univerAPI.newDataValidation();
  186. * const rule = builder.requireDateOnOrAfter(new Date('2024-01-01')).build();
  187. * ```
  188. */
  189. requireDateOnOrAfter(e) {
  190. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = h.GREATER_THAN_OR_EQUAL, this;
  191. }
  192. /**
  193. * Set the data validation type to DATE and configure the validation rules to be on or before a specific date
  194. * @param {Date} date - The date to compare against
  195. * @returns {FDataValidationBuilder} The current instance for method chaining
  196. * @example
  197. * ```typescript
  198. * const builder = univerAPI.newDataValidation();
  199. * const rule = builder.requireDateOnOrBefore(new Date('2024-12-31')).build();
  200. * ```
  201. */
  202. requireDateOnOrBefore(e) {
  203. return this._rule.type = o.DATE, this._rule.formula1 = e.toLocaleDateString(), this._rule.formula2 = void 0, this._rule.operator = h.LESS_THAN_OR_EQUAL, this;
  204. }
  205. /**
  206. * Requires that a custom formula be satisfied
  207. * @param {string} formula - The formula string that needs to be satisfied
  208. * @returns {FDataValidationBuilder} The current instance for method chaining
  209. * @example
  210. * ```typescript
  211. * const builder = univerAPI.newDataValidation();
  212. * const rule = builder.requireFormulaSatisfied('=A1>0').build();
  213. * ```
  214. */
  215. requireFormulaSatisfied(e) {
  216. return this._rule.type = o.CUSTOM, this._rule.formula1 = e, this._rule.formula2 = void 0, this;
  217. }
  218. /**
  219. * Requires the user to enter a number within a specific range, which can be integer or decimal
  220. * @param {number} start - The starting value of the number range
  221. * @param {number} end - The ending value of the number range
  222. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  223. * @returns {FDataValidationBuilder} The current instance for method chaining
  224. * @example
  225. * ```typescript
  226. * const builder = univerAPI.newDataValidation();
  227. * const rule = builder.requireNumberBetween(1, 10).build();
  228. * ```
  229. */
  230. requireNumberBetween(e, t, r) {
  231. return this._rule.formula1 = `${e}`, this._rule.formula2 = `${t}`, this._rule.operator = h.BETWEEN, this._rule.type = r ? o.WHOLE : o.DECIMAL, this;
  232. }
  233. /**
  234. * Requires the user to enter a number that is equal to a specific value, which can be an integer or a decimal
  235. * @param {number} num - The number to which the entered number should be equal
  236. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  237. * @returns {FDataValidationBuilder} The current instance for method chaining
  238. * @example
  239. * ```typescript
  240. * const builder = univerAPI.newDataValidation();
  241. * const rule = builder.requireNumberEqualTo(10).build();
  242. * ```
  243. */
  244. requireNumberEqualTo(e, t) {
  245. return this._rule.formula1 = `${e}`, this._rule.formula2 = void 0, this._rule.operator = h.EQUAL, this._rule.type = t ? o.WHOLE : o.DECIMAL, this;
  246. }
  247. /**
  248. * Requires the user to enter a number that is greater than a specific value, which can be an integer or a decimal
  249. * @param {number} num - The number to which the entered number should be greater
  250. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  251. * @returns {FDataValidationBuilder} The current instance for method chaining
  252. * @example
  253. * ```typescript
  254. * const builder = univerAPI.newDataValidation();
  255. * const rule = builder.requireNumberGreaterThan(10).build();
  256. * ```
  257. */
  258. requireNumberGreaterThan(e, t) {
  259. return this._rule.formula1 = `${e}`, this._rule.formula2 = void 0, this._rule.operator = h.GREATER_THAN, this._rule.type = t ? o.WHOLE : o.DECIMAL, this;
  260. }
  261. /**
  262. * Requires the user to enter a number that is greater than or equal to a specific value, which can be an integer or a decimal
  263. * @param {number} num - The number to which the entered number should be greater than or equal
  264. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  265. * @returns {FDataValidationBuilder} The current instance for method chaining
  266. * @example
  267. * ```typescript
  268. * const builder = univerAPI.newDataValidation();
  269. * const rule = builder.requireNumberGreaterThanOrEqualTo(10).build();
  270. * ```
  271. */
  272. requireNumberGreaterThanOrEqualTo(e, t) {
  273. return this._rule.formula1 = `${e}`, this._rule.formula2 = void 0, this._rule.operator = h.GREATER_THAN_OR_EQUAL, this._rule.type = t ? o.WHOLE : o.DECIMAL, this;
  274. }
  275. /**
  276. * Requires the user to enter a number that is less than a specific value, which can be an integer or a decimal
  277. * @param {number} num - The number to which the entered number should be less
  278. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  279. * @returns {FDataValidationBuilder} The current instance for method chaining
  280. * @example
  281. * ```typescript
  282. * const builder = univerAPI.newDataValidation();
  283. * const rule = builder.requireNumberLessThan(10).build();
  284. * ```
  285. */
  286. requireNumberLessThan(e, t) {
  287. return this._rule.formula1 = `${e}`, this._rule.formula2 = void 0, this._rule.operator = h.LESS_THAN, this._rule.type = t ? o.WHOLE : o.DECIMAL, this;
  288. }
  289. /**
  290. * Sets the data validation rule to require a number less than or equal to a specified value
  291. * The specified value can be an integer or a decimal
  292. * @param {number} num - The number to which the entered number should be less than or equal
  293. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  294. * @returns {FDataValidationBuilder} The current instance for method chaining
  295. * @example
  296. * ```typescript
  297. * const builder = univerAPI.newDataValidation();
  298. * const rule = builder.requireNumberLessThanOrEqualTo(10).build();
  299. * ```
  300. */
  301. requireNumberLessThanOrEqualTo(e, t) {
  302. return this._rule.formula1 = `${e}`, this._rule.formula2 = void 0, this._rule.operator = h.LESS_THAN_OR_EQUAL, this._rule.type = t ? o.WHOLE : o.DECIMAL, this;
  303. }
  304. /**
  305. * Sets a data validation rule that requires the user to enter a number outside a specified range
  306. * The specified range includes all integers and decimals
  307. * @param {number} start - The starting point of the specified range
  308. * @param {number} end - The end point of the specified range
  309. * @param {boolean} [isInteger] - Optional parameter, indicating whether the number to be verified is an integer
  310. * @returns {FDataValidationBuilder} The current instance for method chaining
  311. * @example
  312. * ```typescript
  313. * const builder = univerAPI.newDataValidation();
  314. * const rule = builder.requireNumberNotBetween(1, 10).build();
  315. * ```
  316. */
  317. requireNumberNotBetween(e, t, r) {
  318. return this._rule.formula1 = `${e}`, this._rule.formula2 = `${t}`, this._rule.operator = h.NOT_BETWEEN, this._rule.type = r ? o.WHOLE : o.DECIMAL, this;
  319. }
  320. /**
  321. * Creates a data validation rule that requires the user to enter a number that is not equal to a specific value
  322. * The specific value can be an integer or a decimal
  323. * @param {number} num - The number to which the entered number should not be equal
  324. * @param {boolean} [isInteger] - Indicates whether the required number is an integer
  325. * @returns {FDataValidationBuilder} The current instance for method chaining
  326. * @example
  327. * ```typescript
  328. * const builder = univerAPI.newDataValidation();
  329. * const rule = builder.requireNumberNotEqualTo(10).build();
  330. * ```
  331. */
  332. requireNumberNotEqualTo(e, t) {
  333. return this._rule.formula1 = `${e}`, this._rule.formula2 = void 0, this._rule.operator = h.NOT_EQUAL, this._rule.type = t ? o.WHOLE : o.DECIMAL, this;
  334. }
  335. /**
  336. * Sets a data validation rule that requires the user to enter a value from a list of specific values
  337. * The list can be displayed in a dropdown, and the user can choose multiple values according to the settings
  338. * @param {string[]} values - An array containing the specific values that the user can enter
  339. * @param {boolean} [multiple] - Optional parameter indicating whether the user can select multiple values
  340. * @param {boolean} [showDropdown] - Optional parameter indicating whether to display the list in a dropdown
  341. * @returns {FDataValidationBuilder} The current instance for method chaining
  342. * @example
  343. * ```typescript
  344. * const builder = univerAPI.newDataValidation();
  345. * const rule = builder.requireValueInList(['Yes', 'No']).build();
  346. * ```
  347. */
  348. requireValueInList(e, t, r) {
  349. return this._rule.type = t ? o.LIST_MULTIPLE : o.LIST, this._rule.formula1 = e.join(","), this._rule.formula2 = void 0, this._rule.showDropDown = r != null ? r : !0, this;
  350. }
  351. /**
  352. * Sets a data validation rule that requires the user to enter a value within a specific range
  353. * The range is defined by an FRange object, which contains the unit ID, sheet name, and cell range
  354. * @param {FRange} range - An FRange object representing the range of values that the user can enter
  355. * @param {boolean} [multiple] - Optional parameter indicating whether the user can select multiple values
  356. * @param {boolean} [showDropdown] - Optional parameter indicating whether to display the list in a dropdown
  357. * @returns {FDataValidationBuilder} The current instance for method chaining
  358. * @example
  359. * ```typescript
  360. * const builder = univerAPI.newDataValidation();
  361. * const range = FRange.create('Sheet1', 'B1:B10');
  362. * const rule = builder.requireValueInRange(range).build();
  363. * ```
  364. */
  365. requireValueInRange(e, t, r) {
  366. return this._rule.type = t ? o.LIST_MULTIPLE : o.LIST, this._rule.formula1 = `=${P({
  367. unitId: e.getUnitId(),
  368. sheetName: e.getSheetName(),
  369. range: e.getRange()
  370. })}`, this._rule.formula2 = void 0, this._rule.showDropDown = r != null ? r : !0, this;
  371. }
  372. /**
  373. * Sets whether to allow invalid data and configures the error style
  374. * If invalid data is not allowed, the error style will be set to STOP, indicating that data entry must stop upon encountering an error
  375. * 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
  376. * @param {boolean} allowInvalidData - Whether to allow invalid data
  377. * @returns {FDataValidationBuilder} The current instance for method chaining
  378. * @example
  379. * ```typescript
  380. * const builder = univerAPI.newDataValidation();
  381. * const rule = builder.setAllowInvalid(true).build();
  382. * ```
  383. */
  384. setAllowInvalid(e) {
  385. return this._rule.errorStyle = e ? w.WARNING : w.STOP, this;
  386. }
  387. /**
  388. * Sets whether to allow blank values
  389. * @param {boolean} allowBlank - Whether to allow blank values
  390. * @returns {FDataValidationBuilder} The current instance for method chaining
  391. * @example
  392. * ```typescript
  393. * const builder = univerAPI.newDataValidation();
  394. * const rule = builder.setAllowBlank(true).build();
  395. * ```
  396. */
  397. setAllowBlank(e) {
  398. return this._rule.allowBlank = e, this;
  399. }
  400. /**
  401. * Sets the options for the data validation rule
  402. * @param {Partial<IDataValidationRuleOptions>} options - The options to set for the data validation rule
  403. * @returns {FDataValidationBuilder} The current instance for method chaining
  404. * @example
  405. * ```typescript
  406. * const builder = univerAPI.newDataValidation();
  407. * const rule = builder.setOptions({
  408. * allowBlank: true,
  409. * showErrorMessage: true,
  410. * error: 'Please enter a valid value'
  411. * }).build();
  412. * ```
  413. */
  414. setOptions(e) {
  415. return Object.assign(this._rule, e), this;
  416. }
  417. }
  418. class m {
  419. constructor(e, t, r) {
  420. f(this, "rule");
  421. f(this, "_worksheet");
  422. f(this, "_injector");
  423. this._injector = r, this.rule = e, this._worksheet = t;
  424. }
  425. /**
  426. * Gets whether invalid data is allowed based on the error style value
  427. * @returns {boolean} true if invalid data is allowed, false otherwise
  428. * @example
  429. * ```typescript
  430. * const dataValidation = univerAPI
  431. * .getActiveWorkbook()
  432. * .getActiveWorksheet()
  433. * .getActiveRange()
  434. * .getDataValidation();
  435. * const allowsInvalid = dataValidation.getAllowInvalid();
  436. * ```
  437. */
  438. getAllowInvalid() {
  439. return this.rule.errorStyle !== w.STOP;
  440. }
  441. /**
  442. * Gets the data validation type of the rule
  443. * @returns {DataValidationType | string} The data validation type
  444. * @example
  445. * ```typescript
  446. * const dataValidation = univerAPI
  447. * .getActiveWorkbook()
  448. * .getActiveWorksheet()
  449. * .getActiveRange()
  450. * .getDataValidation();
  451. * const type = dataValidation.getCriteriaType();
  452. * ```
  453. */
  454. getCriteriaType() {
  455. return this.rule.type;
  456. }
  457. /**
  458. * Gets the values used for criteria evaluation
  459. * @returns {[string | undefined, string | undefined, string | undefined]} An array containing the operator, formula1, and formula2 values
  460. * @example
  461. * ```typescript
  462. * const dataValidation = univerAPI
  463. * .getActiveWorkbook()
  464. * .getActiveWorksheet()
  465. * .getActiveRange()
  466. * .getDataValidation();
  467. * const [operator, formula1, formula2] = dataValidation.getCriteriaValues();
  468. * ```
  469. */
  470. getCriteriaValues() {
  471. return [this.rule.operator, this.rule.formula1, this.rule.formula2];
  472. }
  473. /**
  474. * Gets the help text information, which is used to provide users with guidance and support
  475. * @returns {string | undefined} Returns the help text information. If there is no error message, it returns an undefined value
  476. * @example
  477. * ```typescript
  478. * const dataValidation = univerAPI
  479. * .getActiveWorkbook()
  480. * .getActiveWorksheet()
  481. * .getActiveRange()
  482. * .getDataValidation();
  483. * const helpText = dataValidation.getHelpText();
  484. * ```
  485. */
  486. getHelpText() {
  487. return this.rule.error;
  488. }
  489. /**
  490. * Creates a new instance of FDataValidationBuilder using the current rule object
  491. * @returns {FDataValidationBuilder} A new FDataValidationBuilder instance with the same rule configuration
  492. * @example
  493. * ```typescript
  494. * const dataValidation = univerAPI
  495. * .getActiveWorkbook()
  496. * .getActiveWorksheet()
  497. * .getActiveRange()
  498. * .getDataValidation();
  499. * const builder = dataValidation.copy();
  500. * const newRule = builder.setAllowInvalid(true).build();
  501. * ```
  502. */
  503. copy() {
  504. return new D(this.rule);
  505. }
  506. /**
  507. * Gets whether the data validation rule is applied to the worksheet
  508. * @returns {boolean} true if the rule is applied, false otherwise
  509. * @example
  510. * ```typescript
  511. * const dataValidation = univerAPI
  512. * .getActiveWorkbook()
  513. * .getActiveWorksheet()
  514. * .getActiveRange()
  515. * .getDataValidation();
  516. * const isApplied = dataValidation.getApplied();
  517. * ```
  518. */
  519. getApplied() {
  520. if (!this._worksheet)
  521. return !1;
  522. const t = this._injector.get(b).getRuleById(this._worksheet.getUnitId(), this._worksheet.getSheetId(), this.rule.uid);
  523. return !!(t && t.ranges.length);
  524. }
  525. /**
  526. * Gets the ranges to which the data validation rule is applied
  527. * @returns {FRange[]} An array of FRange objects representing the ranges to which the data validation rule is applied
  528. * @example
  529. * ```typescript
  530. * const dataValidation = univerAPI
  531. * .getActiveWorkbook()
  532. * .getActiveWorksheet()
  533. * .getActiveRange()
  534. * .getDataValidation();
  535. * const ranges = dataValidation.getRanges();
  536. * ```
  537. */
  538. getRanges() {
  539. if (!this.getApplied())
  540. return [];
  541. const e = this._injector.get(H).getUnit(this._worksheet.getUnitId());
  542. return this.rule.ranges.map((t) => this._injector.createInstance(y, e, this._worksheet, t));
  543. }
  544. /**
  545. * Gets the unit ID of the worksheet
  546. * @returns {string | undefined} The unit ID of the worksheet
  547. * @example
  548. * ```typescript
  549. * const dataValidation = univerAPI
  550. * .getActiveWorkbook()
  551. * .getActiveWorksheet()
  552. * .getActiveRange()
  553. * .getDataValidation();
  554. * const unitId = dataValidation.getUnitId();
  555. * ```
  556. */
  557. getUnitId() {
  558. var e;
  559. return (e = this._worksheet) == null ? void 0 : e.getUnitId();
  560. }
  561. /**
  562. * Gets the sheet ID of the worksheet
  563. * @returns {string | undefined} The sheet ID of the worksheet
  564. * @example
  565. * ```typescript
  566. * const dataValidation = univerAPI
  567. * .getActiveWorkbook()
  568. * .getActiveWorksheet()
  569. * .getActiveRange()
  570. * .getDataValidation();
  571. * const sheetId = dataValidation.getSheetId();
  572. * ```
  573. */
  574. getSheetId() {
  575. var e;
  576. return (e = this._worksheet) == null ? void 0 : e.getSheetId();
  577. }
  578. /**
  579. * Set Criteria for the data validation rule
  580. * @param {DataValidationType} type - The type of data validation criteria
  581. * @param {[DataValidationOperator, string, string]} values - An array containing the operator, formula1, and formula2 values
  582. * @param {boolean} [allowBlank] - Whether to allow blank values
  583. * @returns {FDataValidation} The current instance for method chaining
  584. * @example
  585. * ```typescript
  586. * const dataValidation = univerAPI
  587. * .getActiveWorkbook()
  588. * .getActiveWorksheet()
  589. * .getActiveRange()
  590. * .getDataValidation();
  591. * dataValidation.setCriteria(
  592. * DataValidationType.DECIMAL,
  593. * [DataValidationOperator.BETWEEN, '1', '10'],
  594. * true
  595. * );
  596. * ```
  597. */
  598. setCriteria(e, t, r = !0) {
  599. if (this.getApplied() && !this._injector.get(p).syncExecuteCommand(U.id, {
  600. unitId: this.getUnitId(),
  601. subUnitId: this.getSheetId(),
  602. ruleId: this.rule.uid,
  603. setting: {
  604. operator: t[0],
  605. formula1: t[1],
  606. formula2: t[2],
  607. type: this.rule.type,
  608. allowBlank: r
  609. }
  610. }))
  611. throw new Error("setCriteria failed");
  612. return this.rule.operator = t[0], this.rule.formula1 = t[1], this.rule.formula2 = t[2], this.rule.type = e, this.rule.allowBlank = r, this;
  613. }
  614. /**
  615. * Set the options for the data validation rule
  616. * @param {Partial<IDataValidationRuleOptions>} options - The options to set for the data validation rule
  617. * @returns {FDataValidation} The current instance for method chaining
  618. * @example
  619. * ```typescript
  620. * const dataValidation = univerAPI
  621. * .getActiveWorkbook()
  622. * .getActiveWorksheet()
  623. * .getActiveRange()
  624. * .getDataValidation();
  625. * dataValidation.setOptions({
  626. * allowBlank: true,
  627. * showErrorMessage: true,
  628. * error: 'Please enter a valid value'
  629. * });
  630. * ```
  631. */
  632. setOptions(e) {
  633. if (this.getApplied() && !this._injector.get(p).syncExecuteCommand(v.id, {
  634. unitId: this.getUnitId(),
  635. subUnitId: this.getSheetId(),
  636. ruleId: this.rule.uid,
  637. options: {
  638. ...F(this.rule),
  639. ...e
  640. }
  641. }))
  642. throw new Error("setOptions failed");
  643. return Object.assign(this.rule, e), this;
  644. }
  645. /**
  646. * Set the ranges to the data validation rule
  647. * @param {FRange[]} ranges - New ranges array
  648. * @returns {FDataValidation} The current instance for method chaining
  649. * @example
  650. * ```typescript
  651. * const dataValidation = univerAPI
  652. * .getActiveWorkbook()
  653. * .getActiveWorksheet()
  654. * .getActiveRange()
  655. * .getDataValidation();
  656. * const range = FRange.create('Sheet1', 'A1:B10');
  657. * dataValidation.setRanges([range]);
  658. * ```
  659. */
  660. setRanges(e) {
  661. if (this.getApplied() && !this._injector.get(p).syncExecuteCommand(C.id, {
  662. unitId: this.getUnitId(),
  663. subUnitId: this.getSheetId(),
  664. ruleId: this.rule.uid,
  665. ranges: e.map((a) => a.getRange())
  666. }))
  667. throw new Error("setRanges failed");
  668. return this.rule.ranges = e.map((t) => t.getRange()), this;
  669. }
  670. /**
  671. * Delete the data validation rule from the worksheet
  672. * @returns {boolean} true if the rule is deleted successfully, false otherwise
  673. * @example
  674. * ```typescript
  675. * const dataValidation = univerAPI
  676. * .getActiveWorkbook()
  677. * .getActiveWorksheet()
  678. * .getActiveRange()
  679. * .getDataValidation();
  680. * const isDeleted = dataValidation.delete();
  681. * ```
  682. */
  683. delete() {
  684. return this.getApplied() ? this._injector.get(p).syncExecuteCommand(T.id, {
  685. unitId: this.getUnitId(),
  686. subUnitId: this.getSheetId(),
  687. ruleId: this.rule.uid
  688. }) : !1;
  689. }
  690. }
  691. class G extends y {
  692. setDataValidation(e) {
  693. if (!e)
  694. return this._commandService.syncExecuteCommand(W.id, {
  695. unitId: this._workbook.getUnitId(),
  696. subUnitId: this._worksheet.getSheetId(),
  697. ranges: [this._range]
  698. }), this;
  699. const t = {
  700. unitId: this._workbook.getUnitId(),
  701. subUnitId: this._worksheet.getSheetId(),
  702. rule: {
  703. ...e.rule,
  704. ranges: [this._range]
  705. }
  706. };
  707. return this._commandService.syncExecuteCommand(A.id, t), this;
  708. }
  709. getDataValidation() {
  710. const t = this._injector.get(S).getDataValidation(
  711. this._workbook.getUnitId(),
  712. this._worksheet.getSheetId(),
  713. [this._range]
  714. );
  715. return t && new m(t, this._worksheet, this._injector);
  716. }
  717. getDataValidations() {
  718. return this._injector.get(S).getDataValidations(
  719. this._workbook.getUnitId(),
  720. this._worksheet.getSheetId(),
  721. [this._range]
  722. ).map((t) => new m(t, this._worksheet, this._injector));
  723. }
  724. async getValidatorStatus() {
  725. return this._injector.get(S).validatorRanges(
  726. this._workbook.getUnitId(),
  727. this._worksheet.getSheetId(),
  728. [this._range]
  729. );
  730. }
  731. }
  732. y.extend(G);
  733. class Q extends x {
  734. // eslint-disable-next-line jsdoc/require-returns
  735. /**
  736. /**
  737. * @deprecated use `univerAPI.newDataValidation()` as instead.
  738. */
  739. static newDataValidation() {
  740. return new D();
  741. }
  742. /**
  743. * Creates a new instance of FDataValidationBuilder
  744. * @returns {FDataValidationBuilder} A new instance of the FDataValidationBuilder class
  745. * @example
  746. * ```ts
  747. * const rule = FUnvier.newDataValidation();
  748. * cell.setDataValidation(rule.requireValueInRange(range));
  749. * ```
  750. */
  751. newDataValidation() {
  752. return new D();
  753. }
  754. // eslint-disable-next-line max-lines-per-function
  755. _initialize(e) {
  756. if (!e.has(V)) return;
  757. const t = e.get(V), r = e.get(p);
  758. this.disposeWithMe(t.ruleChange$.subscribe((a) => {
  759. const { unitId: i, subUnitId: n, rule: d, oldRule: u, type: s } = a, l = this.getSheetTarget(i, n);
  760. if (!l)
  761. return;
  762. const { workbook: I, worksheet: k } = l, E = new m(d, k.getSheet(), this._injector);
  763. this.fireEvent(this.Event.SheetDataValidationChanged, {
  764. origin: a,
  765. worksheet: k,
  766. workbook: I,
  767. changeType: s,
  768. oldRule: u,
  769. rule: E
  770. });
  771. })), this.disposeWithMe(t.validStatusChange$.subscribe((a) => {
  772. const { unitId: i, subUnitId: n, ruleId: d, status: u, row: s, col: l } = a, I = this.getSheetTarget(i, n);
  773. if (!I)
  774. return;
  775. const { workbook: k, worksheet: E } = I, L = E.getDataValidation(d);
  776. L && this.fireEvent(this.Event.SheetDataValidatorStatusChanged, {
  777. workbook: k,
  778. worksheet: E,
  779. row: s,
  780. column: l,
  781. rule: L,
  782. status: u
  783. });
  784. })), this.disposeWithMe(r.beforeCommandExecuted((a) => {
  785. switch (a.id) {
  786. case A.id: {
  787. const i = a.params, n = this.getSheetTarget(i.unitId, i.subUnitId);
  788. if (!n)
  789. return;
  790. const { workbook: d, worksheet: u } = n, s = {
  791. worksheet: u,
  792. workbook: d,
  793. rule: i.rule
  794. };
  795. if (this.fireEvent(this.Event.BeforeSheetDataValidationAdd, s), s.cancel)
  796. throw new _();
  797. break;
  798. }
  799. case U.id: {
  800. const i = a.params, n = this.getSheetTarget(i.unitId, i.subUnitId);
  801. if (!n)
  802. return;
  803. const { workbook: d, worksheet: u } = n, s = u.getDataValidation(i.ruleId);
  804. if (!s)
  805. return;
  806. const l = {
  807. worksheet: u,
  808. workbook: d,
  809. rule: s,
  810. ruleId: i.ruleId,
  811. newCriteria: i.setting
  812. };
  813. if (this.fireEvent(this.Event.BeforeSheetDataValidationCriteriaUpdate, l), l.cancel)
  814. throw new _();
  815. break;
  816. }
  817. case C.id: {
  818. const i = a.params, n = this.getSheetTarget(i.unitId, i.subUnitId);
  819. if (!n)
  820. return;
  821. const { workbook: d, worksheet: u } = n, s = u.getDataValidation(i.ruleId);
  822. if (!s)
  823. return;
  824. const l = {
  825. worksheet: u,
  826. workbook: d,
  827. rule: s,
  828. ruleId: i.ruleId,
  829. newRanges: i.ranges
  830. };
  831. if (this.fireEvent(this.Event.BeforeSheetDataValidationRangeUpdate, l), l.cancel)
  832. throw new _();
  833. break;
  834. }
  835. case v.id: {
  836. const i = a.params, n = this.getSheetTarget(i.unitId, i.subUnitId);
  837. if (!n)
  838. return;
  839. const { workbook: d, worksheet: u } = n, s = u.getDataValidation(i.ruleId);
  840. if (!s)
  841. return;
  842. const l = {
  843. worksheet: u,
  844. workbook: d,
  845. rule: s,
  846. ruleId: i.ruleId,
  847. newOptions: i.options
  848. };
  849. if (this.fireEvent(this.Event.BeforeSheetDataValidationOptionsUpdate, l), l.cancel)
  850. throw new _();
  851. break;
  852. }
  853. case T.id: {
  854. const i = a.params, n = this.getSheetTarget(i.unitId, i.subUnitId);
  855. if (!n)
  856. return;
  857. const { workbook: d, worksheet: u } = n, s = u.getDataValidation(i.ruleId);
  858. if (!s)
  859. return;
  860. const l = {
  861. worksheet: u,
  862. workbook: d,
  863. rule: s,
  864. ruleId: i.ruleId
  865. };
  866. if (this.fireEvent(this.Event.BeforeSheetDataValidationDelete, l), l.cancel)
  867. throw new _();
  868. break;
  869. }
  870. case O.id: {
  871. const i = a.params, n = this.getSheetTarget(i.unitId, i.subUnitId);
  872. if (!n)
  873. return;
  874. const { workbook: d, worksheet: u } = n, s = {
  875. worksheet: u,
  876. workbook: d,
  877. rules: u.getDataValidations()
  878. };
  879. if (this.fireEvent(this.Event.BeforeSheetDataValidationDeleteAll, s), s.cancel)
  880. throw new _();
  881. break;
  882. }
  883. }
  884. }));
  885. }
  886. }
  887. x.extend(Q);
  888. class z extends M {
  889. _initialize() {
  890. Object.defineProperty(this, "_dataValidationModel", {
  891. get() {
  892. return this._injector.get(V);
  893. }
  894. });
  895. }
  896. getValidatorStatus() {
  897. return this._injector.get(S).validatorWorkbook(this._workbook.getUnitId());
  898. }
  899. // region DataValidationHooks
  900. onDataValidationChange(e) {
  901. return g(this._dataValidationModel.ruleChange$.pipe(R((t) => t.unitId === this._workbook.getUnitId())).subscribe(e));
  902. }
  903. onDataValidationStatusChange(e) {
  904. return g(this._dataValidationModel.validStatusChange$.pipe(R((t) => t.unitId === this._workbook.getUnitId())).subscribe(e));
  905. }
  906. onBeforeAddDataValidation(e) {
  907. return g(this._commandService.beforeCommandExecuted((t, r) => {
  908. const a = t.params;
  909. if (t.id === A.id) {
  910. if (a.unitId !== this._workbook.getUnitId())
  911. return;
  912. if (e(a, r) === !1)
  913. throw new Error("Command is stopped by the hook onBeforeAddDataValidation");
  914. }
  915. }));
  916. }
  917. onBeforeUpdateDataValidationCriteria(e) {
  918. return g(this._commandService.beforeCommandExecuted((t, r) => {
  919. const a = t.params;
  920. if (t.id === U.id) {
  921. if (a.unitId !== this._workbook.getUnitId())
  922. return;
  923. if (e(a, r) === !1)
  924. throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationCriteria");
  925. }
  926. }));
  927. }
  928. onBeforeUpdateDataValidationRange(e) {
  929. return g(this._commandService.beforeCommandExecuted((t, r) => {
  930. const a = t.params;
  931. if (t.id === C.id) {
  932. if (a.unitId !== this._workbook.getUnitId())
  933. return;
  934. if (e(a, r) === !1)
  935. throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationRange");
  936. }
  937. }));
  938. }
  939. onBeforeUpdateDataValidationOptions(e) {
  940. return g(this._commandService.beforeCommandExecuted((t, r) => {
  941. const a = t.params;
  942. if (t.id === v.id) {
  943. if (a.unitId !== this._workbook.getUnitId())
  944. return;
  945. if (e(a, r) === !1)
  946. throw new Error("Command is stopped by the hook onBeforeUpdateDataValidationOptions");
  947. }
  948. }));
  949. }
  950. onBeforeDeleteDataValidation(e) {
  951. return g(this._commandService.beforeCommandExecuted((t, r) => {
  952. const a = t.params;
  953. if (t.id === T.id) {
  954. if (a.unitId !== this._workbook.getUnitId())
  955. return;
  956. if (e(a, r) === !1)
  957. throw new Error("Command is stopped by the hook onBeforeDeleteDataValidation");
  958. }
  959. }));
  960. }
  961. onBeforeDeleteAllDataValidation(e) {
  962. return g(this._commandService.beforeCommandExecuted((t, r) => {
  963. const a = t.params;
  964. if (t.id === O.id) {
  965. if (a.unitId !== this._workbook.getUnitId())
  966. return;
  967. if (e(a, r) === !1)
  968. throw new Error("Command is stopped by the hook onBeforeDeleteAllDataValidation");
  969. }
  970. }));
  971. }
  972. }
  973. M.extend(z);
  974. class K extends N {
  975. getDataValidations() {
  976. return this._injector.get(b).getRules(this._workbook.getUnitId(), this._worksheet.getSheetId()).map((t) => new m(t, this._worksheet, this._injector));
  977. }
  978. getValidatorStatus() {
  979. return this._injector.get(S).validatorWorksheet(
  980. this._workbook.getUnitId(),
  981. this._worksheet.getSheetId()
  982. );
  983. }
  984. getValidatorStatusAsync() {
  985. return this.getValidatorStatus();
  986. }
  987. getDataValidation(e) {
  988. const r = this._injector.get(b).getRuleById(this._workbook.getUnitId(), this._worksheet.getSheetId(), e);
  989. return r ? new m(r, this._worksheet, this._injector) : null;
  990. }
  991. }
  992. N.extend(K);
  993. class X {
  994. get SheetDataValidationChanged() {
  995. return "SheetDataValidationChanged";
  996. }
  997. get SheetDataValidatorStatusChanged() {
  998. return "SheetDataValidatorStatusChanged";
  999. }
  1000. get BeforeSheetDataValidationAdd() {
  1001. return "BeforeSheetDataValidationAdd";
  1002. }
  1003. get BeforeSheetDataValidationDelete() {
  1004. return "BeforeSheetDataValidationDelete";
  1005. }
  1006. get BeforeSheetDataValidationDeleteAll() {
  1007. return "BeforeSheetDataValidationDeleteAll";
  1008. }
  1009. get BeforeSheetDataValidationCriteriaUpdate() {
  1010. return "BeforeSheetDataValidationCriteriaUpdate";
  1011. }
  1012. get BeforeSheetDataValidationRangeUpdate() {
  1013. return "BeforeSheetDataValidationRangeUpdate";
  1014. }
  1015. get BeforeSheetDataValidationOptionsUpdate() {
  1016. return "BeforeSheetDataValidationOptionsUpdate";
  1017. }
  1018. }
  1019. $.extend(X);
  1020. export {
  1021. m as FDataValidation,
  1022. D as FDataValidationBuilder
  1023. };