@odoo/o-spreadsheet 14.0.1 → 14.0.3

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.
Files changed (204) hide show
  1. package/dist/o-spreadsheet.cjs.js +17880 -0
  2. package/dist/o-spreadsheet.d.ts +4119 -0
  3. package/dist/o-spreadsheet.esm.js +17845 -0
  4. package/dist/o-spreadsheet.iife.js +17882 -0
  5. package/dist/o-spreadsheet.iife.min.js +1446 -0
  6. package/package.json +32 -9
  7. package/readme.md +10 -0
  8. package/.eslintrc.json +0 -20
  9. package/.husky/post-checkout +0 -9
  10. package/.husky/pre-commit +0 -39
  11. package/.prettierignore +0 -1
  12. package/astToFormula.patch +0 -0
  13. package/demo/data.js +0 -255
  14. package/demo/index.html +0 -9
  15. package/demo/libs/Chart.bundle.js +0 -20755
  16. package/demo/main.js +0 -101
  17. package/doc/readme.md +0 -46
  18. package/rollup.config.js +0 -23
  19. package/src/base_plugin.ts +0 -102
  20. package/src/components/autofill.ts +0 -146
  21. package/src/components/bottom_bar.ts +0 -199
  22. package/src/components/color_picker.ts +0 -150
  23. package/src/components/composer/autocomplete_dropdown.ts +0 -135
  24. package/src/components/composer/composer.ts +0 -487
  25. package/src/components/composer/content_editable_helper.ts +0 -146
  26. package/src/components/figures/chart.ts +0 -70
  27. package/src/components/figures/container.ts +0 -268
  28. package/src/components/figures/text_figure.ts +0 -36
  29. package/src/components/grid.ts +0 -635
  30. package/src/components/helpers/dom_helpers.ts +0 -7
  31. package/src/components/helpers/drag_and_drop.ts +0 -17
  32. package/src/components/icons.ts +0 -36
  33. package/src/components/index.ts +0 -1
  34. package/src/components/menu.ts +0 -288
  35. package/src/components/overlay.ts +0 -457
  36. package/src/components/scrollbar.ts +0 -22
  37. package/src/components/selection_input.ts +0 -168
  38. package/src/components/side_panel/cell_is_rule_editor.ts +0 -169
  39. package/src/components/side_panel/chart_panel.ts +0 -104
  40. package/src/components/side_panel/color_scale_rule_editor.ts +0 -145
  41. package/src/components/side_panel/conditional_formatting.ts +0 -396
  42. package/src/components/side_panel/side_panel.ts +0 -127
  43. package/src/components/side_panel/translations_terms.ts +0 -45
  44. package/src/components/spreadsheet.ts +0 -198
  45. package/src/components/top_bar.ts +0 -535
  46. package/src/constants.ts +0 -26
  47. package/src/data.ts +0 -149
  48. package/src/fonts.ts +0 -41
  49. package/src/formulas/compiler.ts +0 -258
  50. package/src/formulas/composer_tokenizer.ts +0 -70
  51. package/src/formulas/index.ts +0 -15
  52. package/src/formulas/normalize.ts +0 -52
  53. package/src/formulas/parser.ts +0 -280
  54. package/src/formulas/range_tokenizer.ts +0 -97
  55. package/src/formulas/tokenizer.ts +0 -242
  56. package/src/functions/arguments.ts +0 -114
  57. package/src/functions/dates.ts +0 -302
  58. package/src/functions/helpers.ts +0 -592
  59. package/src/functions/index.ts +0 -52
  60. package/src/functions/module_database.ts +0 -282
  61. package/src/functions/module_date.ts +0 -743
  62. package/src/functions/module_info.ts +0 -69
  63. package/src/functions/module_logical.ts +0 -208
  64. package/src/functions/module_lookup.ts +0 -185
  65. package/src/functions/module_math.ts +0 -1252
  66. package/src/functions/module_operators.ts +0 -291
  67. package/src/functions/module_statistical.ts +0 -882
  68. package/src/functions/module_text.ts +0 -370
  69. package/src/helpers/color.ts +0 -30
  70. package/src/helpers/coordinates.ts +0 -71
  71. package/src/helpers/grid_manipulation.ts +0 -68
  72. package/src/helpers/index.ts +0 -7
  73. package/src/helpers/misc.ts +0 -76
  74. package/src/helpers/numbers.ts +0 -117
  75. package/src/helpers/uuid.ts +0 -19
  76. package/src/helpers/zones.ts +0 -263
  77. package/src/history.ts +0 -186
  78. package/src/index.ts +0 -84
  79. package/src/model.ts +0 -261
  80. package/src/plugins/autofill.ts +0 -412
  81. package/src/plugins/chart.ts +0 -404
  82. package/src/plugins/clipboard.ts +0 -461
  83. package/src/plugins/conditional_format.ts +0 -445
  84. package/src/plugins/core.ts +0 -1502
  85. package/src/plugins/edition.ts +0 -157
  86. package/src/plugins/evaluation.ts +0 -428
  87. package/src/plugins/figures.ts +0 -123
  88. package/src/plugins/formatting.ts +0 -630
  89. package/src/plugins/highlight.ts +0 -187
  90. package/src/plugins/index.ts +0 -32
  91. package/src/plugins/merge.ts +0 -524
  92. package/src/plugins/renderer.ts +0 -571
  93. package/src/plugins/selection.ts +0 -562
  94. package/src/plugins/selection_inputs.ts +0 -326
  95. package/src/registries/autofill_modifiers.ts +0 -76
  96. package/src/registries/autofill_rules.ts +0 -98
  97. package/src/registries/figure_registry.ts +0 -30
  98. package/src/registries/index.ts +0 -7
  99. package/src/registries/menu_items_registry.ts +0 -110
  100. package/src/registries/menus/cell_menu_registry.ts +0 -79
  101. package/src/registries/menus/col_menu_registry.ts +0 -63
  102. package/src/registries/menus/index.ts +0 -6
  103. package/src/registries/menus/menu_items_actions.ts +0 -425
  104. package/src/registries/menus/row_menu_registry.ts +0 -63
  105. package/src/registries/menus/sheet_menu_registry.ts +0 -75
  106. package/src/registries/menus/topbar_menu_registry.ts +0 -204
  107. package/src/registries/side_panel_registry.ts +0 -26
  108. package/src/registries/topbar_component_registry.ts +0 -12
  109. package/src/registry.ts +0 -51
  110. package/src/translation.ts +0 -44
  111. package/src/types/autofill.ts +0 -48
  112. package/src/types/chart.ts +0 -26
  113. package/src/types/commands.ts +0 -716
  114. package/src/types/conditional_formatting.ts +0 -124
  115. package/src/types/env.ts +0 -13
  116. package/src/types/functions.ts +0 -35
  117. package/src/types/getters.ts +0 -103
  118. package/src/types/index.ts +0 -20
  119. package/src/types/misc.ts +0 -143
  120. package/src/types/rendering.ts +0 -41
  121. package/src/types/workbook_data.ts +0 -62
  122. package/tests/__mocks__/uuid.ts +0 -7
  123. package/tests/canvas.mock.ts +0 -34
  124. package/tests/components/__mocks__/content_editable_helper.ts +0 -79
  125. package/tests/components/__mocks__/scrollbar.ts +0 -18
  126. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +0 -29
  127. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +0 -75
  128. package/tests/components/__snapshots__/context_menu_test.ts.snap +0 -88
  129. package/tests/components/__snapshots__/grid_test.ts.snap +0 -60
  130. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +0 -455
  131. package/tests/components/__snapshots__/top_bar_test.ts.snap +0 -705
  132. package/tests/components/autocomplete_dropdown_test.ts +0 -315
  133. package/tests/components/autofill_test.ts +0 -167
  134. package/tests/components/bottom_bar_test.ts +0 -262
  135. package/tests/components/chart_side_panel_test.ts +0 -123
  136. package/tests/components/composer_test.ts +0 -400
  137. package/tests/components/context_menu_test.ts +0 -825
  138. package/tests/components/grid_manipulation_test.ts +0 -163
  139. package/tests/components/grid_test.ts +0 -581
  140. package/tests/components/overlay_test.ts +0 -541
  141. package/tests/components/selection_input_test.ts +0 -259
  142. package/tests/components/side_panel_test.ts +0 -148
  143. package/tests/components/spreadsheet_test.ts +0 -194
  144. package/tests/components/top_bar_test.ts +0 -427
  145. package/tests/data_test.ts +0 -175
  146. package/tests/dom_helper.ts +0 -42
  147. package/tests/formulas/__snapshots__/compiler_test.ts.snap +0 -302
  148. package/tests/formulas/compiler_test.ts +0 -266
  149. package/tests/formulas/composer_tokenizer_test.ts +0 -137
  150. package/tests/formulas/expressions_test.ts +0 -57
  151. package/tests/formulas/formulas_test.ts +0 -80
  152. package/tests/formulas/normalize_test.ts +0 -82
  153. package/tests/formulas/parser_test.ts +0 -224
  154. package/tests/formulas/range_tokenizer_test.ts +0 -187
  155. package/tests/formulas/tokenizer_test.ts +0 -164
  156. package/tests/functions/arguments_test.ts +0 -141
  157. package/tests/functions/dates_test.ts +0 -1081
  158. package/tests/functions/functions_test.ts +0 -53
  159. package/tests/functions/helper_test.ts +0 -98
  160. package/tests/functions/module_database_test.ts +0 -311
  161. package/tests/functions/module_date_test.ts +0 -941
  162. package/tests/functions/module_info_test.ts +0 -139
  163. package/tests/functions/module_logical_test.ts +0 -369
  164. package/tests/functions/module_lookup_test.ts +0 -541
  165. package/tests/functions/module_math_test.ts +0 -2482
  166. package/tests/functions/module_operators_test.ts +0 -955
  167. package/tests/functions/module_statistical_test.ts +0 -2014
  168. package/tests/functions/module_text_test.ts +0 -667
  169. package/tests/helpers/coordinates_test.ts +0 -25
  170. package/tests/helpers/misc_test.ts +0 -95
  171. package/tests/helpers/numbers_test.ts +0 -196
  172. package/tests/helpers/zones_test.ts +0 -97
  173. package/tests/helpers.ts +0 -384
  174. package/tests/menu_items_registry_test.ts +0 -540
  175. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +0 -90
  176. package/tests/model/plugins/chart_test.ts +0 -562
  177. package/tests/model_test.ts +0 -82
  178. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +0 -131
  179. package/tests/plugins/__snapshots__/renderer_test.ts.snap +0 -371
  180. package/tests/plugins/autofill_test.ts +0 -457
  181. package/tests/plugins/borders_test.ts +0 -295
  182. package/tests/plugins/clipboard_test.ts +0 -1180
  183. package/tests/plugins/conditional_formatting_test.ts +0 -1253
  184. package/tests/plugins/core_test.ts +0 -614
  185. package/tests/plugins/edition_test.ts +0 -100
  186. package/tests/plugins/evaluation_async_test.ts +0 -332
  187. package/tests/plugins/evaluation_dates_test.ts +0 -79
  188. package/tests/plugins/evaluation_test.ts +0 -901
  189. package/tests/plugins/figures_test.ts +0 -327
  190. package/tests/plugins/formatting_test.ts +0 -391
  191. package/tests/plugins/grid_manipulation_test.ts +0 -1366
  192. package/tests/plugins/highlight_test.ts +0 -413
  193. package/tests/plugins/history_test.ts +0 -142
  194. package/tests/plugins/import_export_test.ts +0 -276
  195. package/tests/plugins/merges_test.ts +0 -490
  196. package/tests/plugins/navigation_test.ts +0 -181
  197. package/tests/plugins/plugins/evaluation_test.ts +0 -111
  198. package/tests/plugins/renderer_test.ts +0 -598
  199. package/tests/plugins/resizing_test.ts +0 -152
  200. package/tests/plugins/selection_input_test.ts +0 -409
  201. package/tests/plugins/selection_test.ts +0 -442
  202. package/tests/plugins/sheets_test.ts +0 -643
  203. package/tests/plugins/style_test.ts +0 -90
  204. package/tsconfig.json +0 -22
@@ -1,69 +0,0 @@
1
- import { _lt } from "../translation";
2
- import { FunctionDescription } from "../types";
3
- import { args } from "./arguments";
4
-
5
- // -----------------------------------------------------------------------------
6
- // ISERROR
7
- // -----------------------------------------------------------------------------
8
- export const ISERROR: FunctionDescription = {
9
- description: _lt("Whether a value is an error."),
10
- args: args(`value (any, lazy) ${_lt("The value to be verified as an error type.")}`),
11
- returns: ["BOOLEAN"],
12
- compute: function (value: () => any): boolean {
13
- try {
14
- value();
15
- return false;
16
- } catch (e) {
17
- return true;
18
- }
19
- },
20
- };
21
-
22
- // -----------------------------------------------------------------------------
23
- // ISLOGICAL
24
- // -----------------------------------------------------------------------------
25
- export const ISLOGICAL: FunctionDescription = {
26
- description: _lt("Whether a value is `true` or `false`."),
27
- args: args(`value (any) ${_lt("The value to be verified as a logical TRUE or FALSE.")}`),
28
- returns: ["BOOLEAN"],
29
- compute: function (value: any): boolean {
30
- return typeof value === "boolean";
31
- },
32
- };
33
-
34
- // -----------------------------------------------------------------------------
35
- // ISNONTEXT
36
- // -----------------------------------------------------------------------------
37
- export const ISNONTEXT: FunctionDescription = {
38
- description: _lt("Whether a value is non-textual."),
39
- args: args(`value (any) ${_lt("The value to be checked.")}`),
40
- returns: ["BOOLEAN"],
41
- compute: function (value: any): boolean {
42
- return typeof value !== "string";
43
- },
44
- };
45
-
46
- // -----------------------------------------------------------------------------
47
- // ISNUMBER
48
- // -----------------------------------------------------------------------------
49
-
50
- export const ISNUMBER: FunctionDescription = {
51
- description: _lt("Whether a value is a number."),
52
- args: args(`value (any) ${_lt("The value to be verified as a number.")}`),
53
- returns: ["BOOLEAN"],
54
- compute: function (value: any): boolean {
55
- return typeof value === "number";
56
- },
57
- };
58
-
59
- // -----------------------------------------------------------------------------
60
- // ISTEXT
61
- // -----------------------------------------------------------------------------
62
- export const ISTEXT: FunctionDescription = {
63
- description: _lt("Whether a value is text."),
64
- args: args(`value (any) ${_lt("The value to be verified as text.")}`),
65
- returns: ["BOOLEAN"],
66
- compute: function (value: any): boolean {
67
- return typeof value === "string";
68
- },
69
- };
@@ -1,208 +0,0 @@
1
- import { _lt } from "../translation";
2
- import { FunctionDescription } from "../types";
3
- import { args } from "./arguments";
4
- import { toBoolean, visitBooleans } from "./helpers";
5
-
6
- // -----------------------------------------------------------------------------
7
- // WAIT
8
- // -----------------------------------------------------------------------------
9
- export const WAIT: FunctionDescription = {
10
- description: _lt("Wait"),
11
- args: args(`ms (number) ${_lt("wait time in milliseconds")}`),
12
- returns: ["ANY"],
13
- async: true,
14
- compute: function (delay) {
15
- return new Promise(function (resolve, reject) {
16
- setTimeout(function () {
17
- resolve(delay);
18
- }, delay);
19
- });
20
- },
21
- };
22
-
23
- // -----------------------------------------------------------------------------
24
- // AND
25
- // -----------------------------------------------------------------------------
26
- export const AND: FunctionDescription = {
27
- description: _lt("Logical `and` operator."),
28
- args: args(`
29
- logical_expression1 (boolean, range<boolean>) ${_lt(
30
- "An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE, or an expression that can be coerced to a logical value."
31
- )}
32
- logical_expression2 (boolean, range<boolean>, optional, repeating) ${_lt(
33
- "More expressions that represent logical values."
34
- )}
35
- `),
36
- returns: ["BOOLEAN"],
37
- compute: function (): boolean {
38
- let result = true;
39
- let foundBoolean = false;
40
- visitBooleans(arguments, (b) => {
41
- result = result && b;
42
- foundBoolean = true;
43
- return result;
44
- });
45
- if (!foundBoolean) {
46
- throw new Error(_lt(`AND has no valid input data.`));
47
- }
48
- return result;
49
- },
50
- };
51
-
52
- // -----------------------------------------------------------------------------
53
- // IF
54
- // -----------------------------------------------------------------------------
55
- export const IF: FunctionDescription = {
56
- description: _lt("Returns value depending on logical expression."),
57
- args: args(`
58
- logical_expression (boolean) ${_lt(
59
- "An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE."
60
- )}
61
- value_if_true (any, lazy) ${_lt(
62
- "The value the function returns if logical_expression is TRUE."
63
- )}
64
- value_if_false (any, lazy, optional, default=FALSE) ${_lt(
65
- "The value the function returns if logical_expression is FALSE."
66
- )}
67
- `),
68
- returns: ["ANY"],
69
- compute: function (
70
- logical_expression: any,
71
- value_if_true: () => any,
72
- value_if_false: () => any = () => false
73
- ): any {
74
- const result = toBoolean(logical_expression) ? value_if_true() : value_if_false();
75
- return result === null ? "" : result;
76
- },
77
- };
78
-
79
- // -----------------------------------------------------------------------------
80
- // IFERROR
81
- // -----------------------------------------------------------------------------
82
- export const IFERROR: FunctionDescription = {
83
- description: _lt("Value if it is not an error, otherwise 2nd argument."),
84
- args: args(`
85
- value (any, lazy) ${_lt("The value to return if value itself is not an error.")}
86
- value_if_error (any, lazy, optional, default="") ${_lt(
87
- "The value the function returns if value is an error."
88
- )}
89
- `),
90
- returns: ["ANY"],
91
- compute: function (value: () => any, value_if_error: () => any = () => ""): any {
92
- let result;
93
- try {
94
- result = value();
95
- } catch (e) {
96
- result = value_if_error();
97
- }
98
- return result === null ? "" : result;
99
- },
100
- };
101
-
102
- // -----------------------------------------------------------------------------
103
- // IFS
104
- // -----------------------------------------------------------------------------
105
- export const IFS: FunctionDescription = {
106
- description: _lt("Returns a value depending on multiple logical expressions."),
107
- args: args(`
108
- condition1 (boolean, lazy) ${_lt(
109
- "The first condition to be evaluated. This can be a boolean, a number, an array, or a reference to any of those."
110
- )}
111
- value1 (any, lazy) ${_lt("The returned value if condition1 is TRUE.")}
112
- additional_values (any, lazy, optional, repeating) ${_lt(
113
- "Additional conditions and values to be evaluated if the previous ones are FALSE."
114
- )}
115
- `),
116
- // @compatibility: on google sheets, args definitions are next:
117
- // condition1 (boolean) The first condition to be evaluated. This can be a boolean, a number, an array, or a reference to any of those.
118
- // value1 (any) The returned value if condition1 is TRUE.
119
- // condition2 (boolean, optional, repeating) Additional conditions to be evaluated if the previous ones are FALSE.
120
- // value2 (any, optional, repeating) Additional values to be returned if their corresponding conditions are TRUE.
121
- returns: ["ANY"],
122
- compute: function (): any {
123
- if (arguments.length % 2 === 1) {
124
- throw new Error(_lt(`Wrong number of arguments. Expected an even number of arguments.`));
125
- }
126
- for (let n = 0; n < arguments.length - 1; n += 2) {
127
- if (toBoolean(arguments[n]())) {
128
- return arguments[n + 1]();
129
- }
130
- }
131
- throw new Error(_lt(`No match.`));
132
- },
133
- };
134
-
135
- // -----------------------------------------------------------------------------
136
- // NOT
137
- // -----------------------------------------------------------------------------
138
- export const NOT: FunctionDescription = {
139
- description: _lt("Returns opposite of provided logical value."),
140
- args: args(
141
- `logical_expression (boolean) ${_lt(
142
- "An expression or reference to a cell holding an expression that represents some logical value."
143
- )}
144
- `
145
- ),
146
- returns: ["BOOLEAN"],
147
- compute: function (logical_expression: any): boolean {
148
- return !toBoolean(logical_expression);
149
- },
150
- };
151
-
152
- // -----------------------------------------------------------------------------
153
- // OR
154
- // -----------------------------------------------------------------------------
155
- export const OR: FunctionDescription = {
156
- description: _lt("Logical `or` operator."),
157
- args: args(`
158
- logical_expression1 (boolean, range<boolean>) ${_lt(
159
- "An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE, or an expression that can be coerced to a logical value."
160
- )}
161
- logical_expression2 (boolean, range<boolean>, optional, repeating) ${_lt(
162
- "More expressions that evaluate to logical values."
163
- )}
164
- `),
165
- returns: ["BOOLEAN"],
166
- compute: function (): boolean {
167
- let result = false;
168
- let foundBoolean = false;
169
- visitBooleans(arguments, (b) => {
170
- result = result || b;
171
- foundBoolean = true;
172
- return !result;
173
- });
174
- if (!foundBoolean) {
175
- throw new Error(_lt(`OR has no valid input data.`));
176
- }
177
- return result;
178
- },
179
- };
180
-
181
- // -----------------------------------------------------------------------------
182
- // XOR
183
- // -----------------------------------------------------------------------------
184
- export const XOR: FunctionDescription = {
185
- description: _lt("Logical `xor` operator."),
186
- args: args(`
187
- logical_expression1 (boolean, range<boolean>) ${_lt(
188
- "An expression or reference to a cell containing an expression that represents some logical value, i.e. TRUE or FALSE, or an expression that can be coerced to a logical value."
189
- )}
190
- logical_expression2 (boolean, range<boolean>, optional, repeating) ${_lt(
191
- "More expressions that evaluate to logical values."
192
- )}
193
- `),
194
- returns: ["BOOLEAN"],
195
- compute: function (): boolean {
196
- let result = false;
197
- let foundBoolean = false;
198
- visitBooleans(arguments, (b) => {
199
- result = result ? !b : b;
200
- foundBoolean = true;
201
- return true;
202
- });
203
- if (!foundBoolean) {
204
- throw new Error(_lt(`XOR has no valid input data.`));
205
- }
206
- return result;
207
- },
208
- };
@@ -1,185 +0,0 @@
1
- import { _lt } from "../translation";
2
- import { FunctionDescription } from "../types";
3
- import { args } from "./arguments";
4
- import { formatDateTime } from "./dates";
5
- import {
6
- dichotomicPredecessorSearch,
7
- dichotomicSuccessorSearch,
8
- extractDateValue,
9
- toBoolean,
10
- toNumber,
11
- } from "./helpers";
12
-
13
- /**
14
- * Perform a linear search and return the index of the perfect match.
15
- * -1 is returned if no value is found.
16
- *
17
- * Example:
18
- * - [3, 6, 10], 3 => 0
19
- * - [3, 6, 10], 6 => 1
20
- * - [3, 6, 10], 9 => -1
21
- * - [3, 6, 10], 2 => -1
22
- */
23
- function linearSearch(range: any[], target: any): number {
24
- for (let i = 0; i < range.length; i++) {
25
- if (range[i] === target) {
26
- return i;
27
- }
28
- }
29
- // no value is found, -1 is returned
30
- return -1;
31
- }
32
-
33
- // -----------------------------------------------------------------------------
34
- // LOOKUP
35
- // -----------------------------------------------------------------------------
36
-
37
- export const LOOKUP: FunctionDescription = {
38
- description: _lt(`Look up a value.`),
39
- args: args(`
40
- search_key (any) ${_lt("The value to search for. For example, 42, 'Cats', or I24.")}
41
- search_array (any, range) ${_lt(
42
- "One method of using this function is to provide a single sorted row or column search_array to look through for the search_key with a second argument result_range. The other way is to combine these two arguments into one search_array where the first row or column is searched and a value is returned from the last row or column in the array. If search_key is not found, a non-exact match may be returned."
43
- )}
44
- result_range (any, range, optional) ${_lt(
45
- "The range from which to return a result. The value returned corresponds to the location where search_key is found in search_range. This range must be only a single row or column and should not be used if using the search_result_array method."
46
- )}
47
- `),
48
- returns: ["ANY"],
49
- compute: function (search_key: any, search_array: any, result_range: any = undefined): any {
50
- const verticalSearch = search_array[0].length >= search_array.length;
51
- const searchRange = (verticalSearch ? search_array[0] : search_array.map((c) => c[0])).map(
52
- (el) => extractDateValue(el)
53
- );
54
-
55
- const key = extractDateValue(search_key);
56
- const index = dichotomicPredecessorSearch(searchRange, key);
57
- if (index === -1) {
58
- const value =
59
- typeof search_key === "object" && search_key.jsDate
60
- ? formatDateTime(search_key)
61
- : search_key;
62
- throw new Error(_lt("Did not find value '%s' in LOOKUP evaluation.", value));
63
- }
64
- if (result_range === undefined) {
65
- return verticalSearch ? search_array.pop()[index] : search_array[index].pop();
66
- }
67
-
68
- const nbCol = result_range.length;
69
- const nbRow = result_range[0].length;
70
- if (nbCol > 1 && nbRow > 1) {
71
- throw new Error(_lt(`LOOKUP range must be a single row or a single column.`));
72
- }
73
-
74
- if (nbCol > 1) {
75
- if (nbCol - 1 < index) {
76
- throw new Error(
77
- _lt("LOOKUP evaluates to an out of range row value %s.", (index + 1).toString())
78
- );
79
- }
80
- return result_range[index][0];
81
- }
82
-
83
- if (nbRow - 1 < index) {
84
- throw new Error(
85
- _lt("LOOKUP evaluates to an out of range column value %s.", (index + 1).toString())
86
- );
87
- }
88
- return result_range[0][index];
89
- },
90
- };
91
-
92
- // -----------------------------------------------------------------------------
93
- // MATCH
94
- // -----------------------------------------------------------------------------
95
-
96
- export const MATCH: FunctionDescription = {
97
- description: _lt(`Position of item in range that matches value.`),
98
- args: args(`
99
- search_key (any) ${_lt("The value to search for. For example, 42, 'Cats', or I24.")}
100
- range (any, range) ${_lt("The one-dimensional array to be searched.")}
101
- search_type (number, optional, default=1) ${_lt(
102
- "The search method. 1 (default) finds the largest value less than or equal to search_key when range is sorted in ascending order. 0 finds the exact value when range is unsorted. -1 finds the smallest value greater than or equal to search_key when range is sorted in descending order."
103
- )}
104
- `),
105
- returns: ["NUMBER"],
106
- compute: function (search_key: any, range: any[], search_type: any = 1): number {
107
- let _searchType = toNumber(search_type);
108
- const nbCol = range.length;
109
- const nbRow = range[0].length;
110
- if (nbCol > 1 && nbRow > 1) {
111
- throw new Error(_lt(`MATCH range must be a single row or a single column.`));
112
- }
113
- let index = -1;
114
- range = range.flat().map((el) => extractDateValue(el));
115
- const key = extractDateValue(search_key);
116
- _searchType = Math.sign(_searchType);
117
- switch (_searchType) {
118
- case 1:
119
- index = dichotomicPredecessorSearch(range, key);
120
- break;
121
- case 0:
122
- index = linearSearch(range, key);
123
- break;
124
- case -1:
125
- index = dichotomicSuccessorSearch(range, key);
126
- break;
127
- }
128
- if (index > -1) {
129
- return index + 1;
130
- } else {
131
- const value =
132
- typeof search_key === "object" && search_key.jsDate
133
- ? formatDateTime(search_key)
134
- : search_key;
135
- throw new Error(_lt("Did not find value '%s' in MATCH evaluation.", value));
136
- }
137
- },
138
- };
139
-
140
- // -----------------------------------------------------------------------------
141
- // VLOOKUP
142
- // -----------------------------------------------------------------------------
143
-
144
- export const VLOOKUP: FunctionDescription = {
145
- description: _lt(`Vertical lookup.`),
146
- args: args(`
147
- search_key (any) ${_lt("The value to search for. For example, 42, 'Cats', or I24.")}
148
- range (any, range) ${_lt(
149
- "The range to consider for the search. The first column in the range is searched for the key specified in search_key."
150
- )}
151
- index (number) ${_lt(
152
- "The column index of the value to be returned, where the first column in range is numbered 1."
153
- )}
154
- is_sorted (boolean, optional, default = TRUE) ${_lt(
155
- "Indicates whether the column to be searched [the first column of the specified range] is sorted, in which case the closest match for search_key will be returned."
156
- )}
157
- `),
158
- returns: ["ANY"],
159
- compute: function (search_key: any, range: any[], index: any, is_sorted: any = true): any {
160
- const _index = Math.trunc(toNumber(index));
161
- if (_index < 1 || range.length < _index) {
162
- throw new Error(_lt(`VLOOKUP evaluates to an out of bounds range.`));
163
- }
164
-
165
- const _isSorted = toBoolean(is_sorted);
166
- const firstCol = range[0].map((el) => extractDateValue(el));
167
- const key = extractDateValue(search_key);
168
- let lineIndex;
169
- if (_isSorted) {
170
- lineIndex = dichotomicPredecessorSearch(firstCol, key);
171
- } else {
172
- lineIndex = linearSearch(firstCol, key);
173
- }
174
-
175
- if (lineIndex > -1) {
176
- return range[_index - 1][lineIndex];
177
- } else {
178
- const value =
179
- typeof search_key === "object" && search_key.jsDate
180
- ? formatDateTime(search_key)
181
- : search_key;
182
- throw new Error(_lt("Did not find value '%s' in VLOOKUP evaluation.", value));
183
- }
184
- },
185
- };