@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,882 +0,0 @@
1
- import { isNumber } from "../helpers/index";
2
- import { _lt } from "../translation";
3
- import { FunctionDescription } from "../types";
4
- import { args } from "./arguments";
5
- import { parseDateTime } from "./dates";
6
- import {
7
- dichotomicPredecessorSearch,
8
- extractDateValue,
9
- reduceArgs,
10
- reduceNumbers,
11
- reduceNumbersTextAs0,
12
- toNumber,
13
- visitAny,
14
- visitMatchingRanges,
15
- } from "./helpers";
16
-
17
- // Note: dataY and dataX may not have the same dimension
18
- function covariance(dataY: any[], dataX: any[], isSample: boolean): number {
19
- let flatDataY: any[] = [];
20
- let flatDataX: any[] = [];
21
- let lenY = 0;
22
- let lenX = 0;
23
-
24
- visitAny(dataY, (y) => {
25
- flatDataY.push(y);
26
- lenY += 1;
27
- });
28
-
29
- visitAny(dataX, (x) => {
30
- flatDataX.push(x);
31
- lenX += 1;
32
- });
33
-
34
- if (lenY !== lenX) {
35
- throw new Error(
36
- _lt(
37
- "[[FUNCTION_NAME]] has mismatched argument count %s vs %s.",
38
- lenY.toString(),
39
- lenX.toString()
40
- )
41
- );
42
- }
43
-
44
- let count = 0;
45
- let sumY = 0;
46
- let sumX = 0;
47
- for (let i = 0; i < lenY; i++) {
48
- const valueY = flatDataY[i];
49
- const valueX = flatDataX[i];
50
- if (typeof valueY === "number" && typeof valueX === "number") {
51
- count += 1;
52
- sumY += valueY;
53
- sumX += valueX;
54
- }
55
- }
56
-
57
- if (count === 0 || (isSample && count === 1)) {
58
- throw new Error(_lt(`Evaluation of function [[FUNCTION_NAME]] caused a divide by zero error.`));
59
- }
60
-
61
- const averageY = sumY / count;
62
- const averageX = sumX / count;
63
-
64
- let acc = 0;
65
- for (let i = 0; i < lenY; i++) {
66
- const valueY = flatDataY[i];
67
- const valueX = flatDataX[i];
68
- if (typeof valueY === "number" && typeof valueX === "number") {
69
- acc += (valueY - averageY) * (valueX - averageX);
70
- }
71
- }
72
-
73
- return acc / (count - (isSample ? 1 : 0));
74
- }
75
-
76
- function variance(args: IArguments | any[], isSample: boolean, textAs0: boolean): number {
77
- let count = 0;
78
- let sum = 0;
79
- const reduceFuction = textAs0 ? reduceNumbersTextAs0 : reduceNumbers;
80
-
81
- sum = reduceFuction(
82
- args,
83
- (acc, a) => {
84
- count += 1;
85
- return acc + a;
86
- },
87
- 0
88
- );
89
-
90
- if (count === 0 || (isSample && count === 1)) {
91
- throw new Error(_lt(`Evaluation of function [[FUNCTION_NAME]] caused a divide by zero error.`));
92
- }
93
-
94
- const average = sum / count;
95
- return (
96
- reduceFuction(args, (acc, a) => acc + Math.pow(a - average, 2), 0) /
97
- (count - (isSample ? 1 : 0))
98
- );
99
- }
100
-
101
- // -----------------------------------------------------------------------------
102
- // AVEDEV
103
- // -----------------------------------------------------------------------------
104
- export const AVEDEV: FunctionDescription = {
105
- description: _lt("Average magnitude of deviations from mean."),
106
- args: args(`
107
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
108
- value2 (number, range<number>, optional, repeating) ${_lt(
109
- "Additional values or ranges to include in the sample."
110
- )}
111
- `),
112
- returns: ["NUMBER"],
113
- compute: function (): number {
114
- let count = 0;
115
- const sum = reduceNumbers(
116
- arguments,
117
- (acc, a) => {
118
- count += 1;
119
- return acc + a;
120
- },
121
- 0
122
- );
123
- if (count === 0) {
124
- throw new Error(_lt(`Evaluation of function AVEDEV caused a divide by zero error.`));
125
- }
126
- const average = sum / count;
127
- return reduceNumbers(arguments, (acc, a) => acc + Math.abs(average - a), 0) / count;
128
- },
129
- };
130
-
131
- // -----------------------------------------------------------------------------
132
- // AVERAGE
133
- // -----------------------------------------------------------------------------
134
- export const AVERAGE: FunctionDescription = {
135
- description: _lt(`Numerical average value in a dataset, ignoring text.`),
136
- args: args(`
137
- value1 (number, range<number>) ${_lt(
138
- "The first value or range to consider when calculating the average value."
139
- )}
140
- value2 (number, range<number>, optional, repeating) ${_lt(
141
- "Additional values or ranges to consider when calculating the average value."
142
- )}
143
- `),
144
- returns: ["NUMBER"],
145
- compute: function (): number {
146
- let count = 0;
147
- const sum = reduceNumbers(
148
- arguments,
149
- (acc, a) => {
150
- count += 1;
151
- return acc + a;
152
- },
153
- 0
154
- );
155
- if (count === 0) {
156
- throw new Error(_lt(`Evaluation of function AVERAGE caused a divide by zero error.`));
157
- }
158
- return sum / count;
159
- },
160
- };
161
-
162
- // -----------------------------------------------------------------------------
163
- // AVERAGE.WEIGHTED
164
- // -----------------------------------------------------------------------------
165
- const rangeError = _lt(`AVERAGE.WEIGHTED has mismatched range sizes.`);
166
- const negativeWeightError = _lt(
167
- `AVERAGE.WEIGHTED expects the weight to be positive or equal to 0.`
168
- );
169
-
170
- export const AVERAGE_WEIGHTED: FunctionDescription = {
171
- description: _lt(`Weighted average.`),
172
- args: args(`
173
- values (number, range<number>) ${_lt("Values to average.")}
174
- weights (number, range<number>) ${_lt("Weights for each corresponding value.")}
175
- additional_values (number, range<number>, optional, repeating) ${_lt(
176
- "Additional values to average with weights."
177
- )}
178
- `),
179
- // @compatibility: on google sheets, args difinitions are next:
180
- // additional_values (number, range<number>, optional, repeating) Additional values to average.
181
- // additional_weights (number, range<number>, optional, repeating) Additional weights.
182
- returns: ["NUMBER"],
183
- compute: function (): number {
184
- let sum = 0;
185
- let count = 0;
186
- let value;
187
- let weight;
188
- if (arguments.length % 2 === 1) {
189
- throw new Error(_lt(`Wrong number of arguments. Expected an even number of arguments.`));
190
- }
191
- for (let n = 0; n < arguments.length - 1; n += 2) {
192
- value = arguments[n];
193
- weight = arguments[n + 1];
194
- // if (typeof value != typeof weight) {
195
- // throw new Error(rangeError);
196
- // }
197
- if (Array.isArray(value)) {
198
- if (!Array.isArray(weight)) {
199
- throw new Error(rangeError);
200
- }
201
- let dimColValue = value.length;
202
- let dimLinValue = value[0].length;
203
- if (dimColValue !== weight.length || dimLinValue != weight[0].length) {
204
- throw new Error(rangeError);
205
- }
206
- for (let i = 0; i < dimColValue; i++) {
207
- for (let j = 0; j < dimLinValue; j++) {
208
- let subValue = value[i][j];
209
- let subWeight = weight[i][j];
210
- let subValueIsNumber = typeof subValue === "number";
211
- let subWeightIsNumber = typeof subWeight === "number";
212
- // typeof subValue or subWeight can be 'number' or 'undefined'
213
- if (subValueIsNumber !== subWeightIsNumber) {
214
- throw new Error(_lt(`AVERAGE.WEIGHTED expects number values.`));
215
- }
216
- if (subWeightIsNumber) {
217
- if (subWeight < 0) {
218
- throw new Error(negativeWeightError);
219
- }
220
- sum += subValue * subWeight;
221
- count += subWeight;
222
- }
223
- }
224
- }
225
- } else {
226
- weight = toNumber(weight);
227
- value = toNumber(value);
228
- if (weight < 0) {
229
- throw new Error(negativeWeightError);
230
- }
231
- sum += value * weight;
232
- count += weight;
233
- }
234
- }
235
- if (count === 0) {
236
- throw new Error(
237
- _lt(`Evaluation of function AVERAGE.WEIGHTED caused a divide by zero error.`)
238
- );
239
- }
240
- return sum / count;
241
- },
242
- };
243
-
244
- // -----------------------------------------------------------------------------
245
- // AVERAGEA
246
- // -----------------------------------------------------------------------------
247
- export const AVERAGEA: FunctionDescription = {
248
- description: _lt(`Numerical average value in a dataset.`),
249
- args: args(`
250
- value1 (number, range<number>) ${_lt(
251
- "The first value or range to consider when calculating the average value."
252
- )}
253
- value2 (number, range<number>, optional, repeating) ${_lt(
254
- "Additional values or ranges to consider when calculating the average value."
255
- )}
256
- `),
257
- returns: ["NUMBER"],
258
- compute: function (): number {
259
- let count = 0;
260
- const sum = reduceNumbersTextAs0(
261
- arguments,
262
- (acc, a) => {
263
- count += 1;
264
- return acc + a;
265
- },
266
- 0
267
- );
268
- if (count === 0) {
269
- throw new Error(_lt(`Evaluation of function AVERAGEA caused a divide by zero error.`));
270
- }
271
- return sum / count;
272
- },
273
- };
274
-
275
- // -----------------------------------------------------------------------------
276
- // AVERAGEIF
277
- // -----------------------------------------------------------------------------
278
- export const AVERAGEIF: FunctionDescription = {
279
- description: _lt(`Average of values depending on criteria.`),
280
- args: args(`
281
- criteria_range (range) ${_lt("The range to check against criterion.")}
282
- criterion (string) ${_lt("The pattern or test to apply to criteria_range.")}
283
- average_range (range, optional, default=criteria_range) ${_lt(
284
- "The range to average. If not included, criteria_range is used for the average instead."
285
- )}
286
- `),
287
- returns: ["NUMBER"],
288
- compute: function (criteria_range: any, criterion: any, average_range: any = undefined): number {
289
- if (average_range === undefined) {
290
- average_range = criteria_range;
291
- }
292
-
293
- let count = 0;
294
- let sum = 0;
295
-
296
- visitMatchingRanges([criteria_range, criterion], (i, j) => {
297
- const value = average_range[i][j];
298
- if (typeof value === "number") {
299
- count += 1;
300
- sum += value;
301
- }
302
- });
303
-
304
- if (count === 0) {
305
- throw new Error(_lt(`Evaluation of function AVERAGEIF caused a divide by zero error.`));
306
- }
307
- return sum / count;
308
- },
309
- };
310
-
311
- // -----------------------------------------------------------------------------
312
- // AVERAGEIFS
313
- // -----------------------------------------------------------------------------
314
- export const AVERAGEIFS: FunctionDescription = {
315
- description: _lt(`Average of values depending on multiple criteria.`),
316
- args: args(`
317
- average_range (range) ${_lt("The range to average.")}
318
- criteria_range1 (range) ${_lt("The range to check against criterion1.")}
319
- criterion1 (string) ${_lt("The pattern or test to apply to criteria_range1.")}
320
- additional_values (any, range, optional, repeating) ${_lt(
321
- "Additional criteria_range and criterion to check."
322
- )}
323
- `),
324
- // @compatibility: on google sheets, args definitions are next:
325
- // average_range (any, range) The range to average.
326
- // criteria_range1 (any, range) The range to check against criterion1.
327
- // criterion1 (string) The pattern or test to apply to criteria_range1.
328
- // criteria_range2 (any, range, optional, repeating) Additional ranges to check.
329
- // criterion2 (string, optional, repeating) Additional criteria to check.
330
- returns: ["NUMBER"],
331
- compute: function (average_range, ...args): number {
332
- let count = 0;
333
- let sum = 0;
334
- visitMatchingRanges(args, (i, j) => {
335
- const value = average_range[i][j];
336
- if (typeof value === "number") {
337
- count += 1;
338
- sum += value;
339
- }
340
- });
341
- if (count === 0) {
342
- throw new Error(_lt(`Evaluation of function AVERAGEIFS caused a divide by zero error.`));
343
- }
344
- return sum / count;
345
- },
346
- };
347
-
348
- // -----------------------------------------------------------------------------
349
- // COUNT
350
- // -----------------------------------------------------------------------------
351
- export const COUNT: FunctionDescription = {
352
- description: _lt(`The number of numeric values in dataset.`),
353
- args: args(`
354
- value1 (number, range<number>) ${_lt("The first value or range to consider when counting.")}
355
- value2 (number, range<number>, optional, repeating) ${_lt(
356
- "Additional values or ranges to consider when counting."
357
- )}
358
- `),
359
- returns: ["NUMBER"],
360
- compute: function (): number {
361
- let count = 0;
362
- for (let n of arguments) {
363
- if (Array.isArray(n)) {
364
- for (let i of n) {
365
- for (let j of i) {
366
- if (typeof j === "number" || (typeof j === "object" && j !== null && j.jsDate)) {
367
- count += 1;
368
- }
369
- }
370
- }
371
- } else if (typeof n !== "string" || isNumber(n) || parseDateTime(n)) {
372
- count += 1;
373
- }
374
- }
375
- return count;
376
- },
377
- };
378
-
379
- // -----------------------------------------------------------------------------
380
- // COUNTA
381
- // -----------------------------------------------------------------------------
382
- export const COUNTA: FunctionDescription = {
383
- description: _lt(`The number of values in a dataset.`),
384
- args: args(`
385
- value1 (any, range) ${_lt("The first value or range to consider when counting.")}
386
- value2 (any, range, optional, repeating) ${_lt(
387
- "Additional values or ranges to consider when counting."
388
- )}
389
- `),
390
- returns: ["NUMBER"],
391
- compute: function (): number {
392
- return reduceArgs(arguments, (acc, a) => (a !== undefined && a !== null ? acc + 1 : acc), 0);
393
- },
394
- };
395
-
396
- // -----------------------------------------------------------------------------
397
- // COVAR
398
- // -----------------------------------------------------------------------------
399
-
400
- // Note: Unlike the VAR function which corresponds to the variance over a sample (VAR.S),
401
- // the COVAR function corresponds to the covariance over an entire population (COVAR.P)
402
- export const COVAR: FunctionDescription = {
403
- description: _lt(`The covariance of a dataset.`),
404
- args: args(`
405
- data_y (any, range) ${_lt("The range representing the array or matrix of dependent data.")}
406
- data_x (any, range) ${_lt("The range representing the array or matrix of independent data.")}
407
- `),
408
- returns: ["NUMBER"],
409
- compute: function (data_y: any[], data_x: any[]): number {
410
- return covariance(data_y, data_x, false);
411
- },
412
- };
413
-
414
- // -----------------------------------------------------------------------------
415
- // COVARIANCE.P
416
- // -----------------------------------------------------------------------------
417
- export const COVARIANCE_P: FunctionDescription = {
418
- description: _lt(`The covariance of a dataset.`),
419
- args: args(`
420
- data_y (any, range) ${_lt("The range representing the array or matrix of dependent data.")}
421
- data_x (any, range) ${_lt("The range representing the array or matrix of independent data.")}
422
- `),
423
- returns: ["NUMBER"],
424
- compute: function (data_y: any[], data_x: any[]): number {
425
- return covariance(data_y, data_x, false);
426
- },
427
- };
428
-
429
- // -----------------------------------------------------------------------------
430
- // COVARIANCE.S
431
- // -----------------------------------------------------------------------------
432
- export const COVARIANCE_S: FunctionDescription = {
433
- description: _lt(`The sample covariance of a dataset.`),
434
- args: args(`
435
- data_y (any, range) ${_lt("The range representing the array or matrix of dependent data.")}
436
- data_x (any, range) ${_lt("The range representing the array or matrix of independent data.")}
437
- `),
438
- returns: ["NUMBER"],
439
- compute: function (data_y: any[], data_x: any[]): number {
440
- return covariance(data_y, data_x, true);
441
- },
442
- };
443
-
444
- // -----------------------------------------------------------------------------
445
- // LARGE
446
- // -----------------------------------------------------------------------------
447
- export const LARGE: FunctionDescription = {
448
- description: _lt("Nth largest element from a data set."),
449
- args: args(`
450
- data (any, range) ${_lt("Array or range containing the dataset to consider.")}
451
- n (number) ${_lt("The rank from largest to smallest of the element to return.")}
452
- `),
453
- returns: ["NUMBER"],
454
- compute: function (data: any, n: any): number {
455
- n = Math.trunc(toNumber(n));
456
- let largests: number[] = [];
457
- let index: number;
458
- let count = 0;
459
- visitAny(data, (d) => {
460
- d = extractDateValue(d);
461
- if (typeof d === "number") {
462
- index = dichotomicPredecessorSearch(largests, d);
463
- largests.splice(index + 1, 0, d);
464
- count++;
465
- if (count > n) {
466
- largests.shift();
467
- count--;
468
- }
469
- }
470
- });
471
- const result = largests.shift();
472
- if (result === undefined) {
473
- throw new Error(_lt(`LARGE has no valid input data.`));
474
- }
475
- if (count < n) {
476
- throw new Error(_lt("Function LARGE parameter 2 value %s is out of range.", n.toString()));
477
- }
478
- return result;
479
- },
480
- };
481
-
482
- // -----------------------------------------------------------------------------
483
- // MAX
484
- // -----------------------------------------------------------------------------
485
- export const MAX: FunctionDescription = {
486
- description: _lt("Maximum value in a numeric dataset."),
487
- args: args(`
488
- value1 (number, range<number>) ${_lt(
489
- "The first value or range to consider when calculating the maximum value."
490
- )}
491
- value2 (number, range<number>, optional, repeating) ${_lt(
492
- "Additional values or ranges to consider when calculating the maximum value."
493
- )}
494
- `),
495
- returns: ["NUMBER"],
496
- compute: function (): number {
497
- const result = reduceNumbers(arguments, (acc, a) => (acc < a ? a : acc), -Infinity);
498
- return result === -Infinity ? 0 : result;
499
- },
500
- };
501
-
502
- // -----------------------------------------------------------------------------
503
- // MAXA
504
- // -----------------------------------------------------------------------------
505
- export const MAXA: FunctionDescription = {
506
- description: _lt("Maximum numeric value in a dataset."),
507
- args: args(`
508
- value1 (any, range) ${_lt(
509
- "The first value or range to consider when calculating the maximum value."
510
- )}
511
- value2 (any, range, optional, repeating) ${_lt(
512
- "Additional values or ranges to consider when calculating the maximum value."
513
- )}
514
- `),
515
- returns: ["NUMBER"],
516
- compute: function (): number {
517
- const maxa = reduceNumbersTextAs0(
518
- arguments,
519
- (acc, a) => {
520
- return Math.max(a, acc);
521
- },
522
- -Infinity
523
- );
524
- return maxa === -Infinity ? 0 : maxa;
525
- },
526
- };
527
-
528
- // -----------------------------------------------------------------------------
529
- // MAXIFS
530
- // -----------------------------------------------------------------------------
531
- export const MAXIFS: FunctionDescription = {
532
- description: _lt("Returns the maximum value in a range of cells, filtered by a set of criteria."),
533
- args: args(`
534
- range (range) ${_lt("The range of cells from which the maximum will be determined.")}
535
- criteria_range1 (range) ${_lt("The range of cells over which to evaluate criterion1.")}
536
- criterion1 (string) ${_lt(
537
- "The pattern or test to apply to criteria_range1, such that each cell that evaluates to TRUE will be included in the filtered set."
538
- )}
539
- additional_values (any, range, optional, repeating) ${_lt(
540
- "Additional criteria_range and criterion to check."
541
- )}
542
- `),
543
- // @compatibility: on google sheets, args definitions are next:
544
- // range (any, range) The range of cells from which the maximum will be determined.
545
- // criteria_range1 (any, range) The range of cells over which to evaluate criterion1.
546
- // criterion1 (string) The pattern or test to apply to criteria_range1, such that each cell that evaluates to TRUE will be included in the filtered set.
547
- // criteria_range2 (any, range, optional, repeating) Additional ranges over which to evaluate the additional criteria. The filtered set will be the intersection of the sets produced by each criterion-range pair.
548
- // criterion2 (string, optional, repeating) The pattern or test to apply to criteria_range2.
549
- returns: ["NUMBER"],
550
- compute: function (range, ...args): number {
551
- let result = -Infinity;
552
- visitMatchingRanges(args, (i, j) => {
553
- const value = range[i][j];
554
- if (typeof value === "number") {
555
- result = result < value ? value : result;
556
- }
557
- });
558
- return result === -Infinity ? 0 : result;
559
- },
560
- };
561
-
562
- // -----------------------------------------------------------------------------
563
- // MIN
564
- // -----------------------------------------------------------------------------
565
- export const MIN: FunctionDescription = {
566
- description: _lt("Minimum value in a numeric dataset."),
567
- args: args(`
568
- value1 (number, range<number>) ${_lt(
569
- "The first value or range to consider when calculating the minimum value."
570
- )}
571
- value2 (number, range<number>, optional, repeating) ${_lt(
572
- "Additional values or ranges to consider when calculating the minimum value."
573
- )}
574
- `),
575
- returns: ["NUMBER"],
576
- compute: function (): number {
577
- const result = reduceNumbers(arguments, (acc, a) => (a < acc ? a : acc), Infinity);
578
- return result === Infinity ? 0 : result;
579
- },
580
- };
581
-
582
- // -----------------------------------------------------------------------------
583
- // MINA
584
- // -----------------------------------------------------------------------------
585
- export const MINA: FunctionDescription = {
586
- description: _lt("Minimum numeric value in a dataset."),
587
- args: args(`
588
- value1 (number, range<number>) ${_lt(
589
- "The first value or range to consider when calculating the minimum value."
590
- )}
591
- value2 (number, range<number>, optional, repeating) ${_lt(
592
- "Additional values or ranges to consider when calculating the minimum value."
593
- )}
594
- `),
595
- returns: ["NUMBER"],
596
- compute: function (): number {
597
- const mina: number = reduceNumbersTextAs0(
598
- arguments,
599
- (acc, a) => {
600
- return Math.min(a, acc);
601
- },
602
- Infinity
603
- );
604
- return mina === Infinity ? 0 : mina;
605
- },
606
- };
607
-
608
- // -----------------------------------------------------------------------------
609
- // MINIFS
610
- // -----------------------------------------------------------------------------
611
- export const MINIFS: FunctionDescription = {
612
- description: _lt("Returns the minimum value in a range of cells, filtered by a set of criteria."),
613
- args: args(`
614
- range (range) ${_lt("The range of cells from which the minimum will be determined.")}
615
- criteria_range1 (range) ${_lt("The range of cells over which to evaluate criterion1.")}
616
- criterion1 (string) ${_lt(
617
- "The pattern or test to apply to criteria_range1, such that each cell that evaluates to TRUE will be included in the filtered set."
618
- )}
619
- additional_values (any, range, optional, repeating) ${_lt(
620
- "Additional criteria_range and criterion to check."
621
- )}
622
- `),
623
- // @compatibility: on google sheets, args definitions are next:
624
- // range (any, range) The range of cells from which the minimum will be determined.
625
- // criteria_range1 (any, range) The range of cells over which to evaluate criterion1.
626
- // criterion1 (string) The pattern or test to apply to criteria_range1, such that each cell that evaluates to TRUE will be included in the filtered set.
627
- // criteria_range2 (any, range, optional, repeating) Additional ranges over which to evaluate the additional criteria. The filtered set will be the intersection of the sets produced by each criterion-range pair.
628
- // criterion2 (string, optional, repeating) The pattern or test to apply to criteria_range2.
629
- returns: ["NUMBER"],
630
- compute: function (range, ...args): number {
631
- let result = Infinity;
632
- visitMatchingRanges(args, (i, j) => {
633
- const value = range[i][j];
634
- if (typeof value === "number") {
635
- result = result > value ? value : result;
636
- }
637
- });
638
- return result === Infinity ? 0 : result;
639
- },
640
- };
641
-
642
- // -----------------------------------------------------------------------------
643
- // SMALL
644
- // -----------------------------------------------------------------------------
645
- export const SMALL: FunctionDescription = {
646
- description: _lt("Nth smallest element in a data set."),
647
- args: args(`
648
- data (any, range) ${_lt("The array or range containing the dataset to consider.")}
649
- n (number) ${_lt("The rank from smallest to largest of the element to return.")}
650
- `),
651
- returns: ["NUMBER"],
652
- compute: function (data: any, n: any): number {
653
- n = Math.trunc(toNumber(n));
654
- let largests: number[] = [];
655
- let index: number;
656
- let count = 0;
657
- visitAny(data, (d) => {
658
- d = extractDateValue(d);
659
- if (typeof d === "number") {
660
- index = dichotomicPredecessorSearch(largests, d);
661
- largests.splice(index + 1, 0, d);
662
- count++;
663
- if (count > n) {
664
- largests.pop();
665
- count--;
666
- }
667
- }
668
- });
669
- const result = largests.pop();
670
- if (result === undefined) {
671
- throw new Error(_lt(`SMALL has no valid input data.`));
672
- }
673
- if (count < n) {
674
- throw new Error(_lt("Function SMALL parameter 2 value %s is out of range.", n.toString()));
675
- }
676
- return result;
677
- },
678
- };
679
-
680
- // -----------------------------------------------------------------------------
681
- // STDEV
682
- // -----------------------------------------------------------------------------
683
- export const STDEV: FunctionDescription = {
684
- description: _lt("Standard deviation."),
685
- args: args(`
686
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
687
- value2 (number, range<number>, optional, repeating) ${_lt(
688
- "Additional values or ranges to include in the sample."
689
- )}
690
- `),
691
- returns: ["NUMBER"],
692
- compute: function (): number {
693
- return Math.sqrt(VAR.compute(...arguments));
694
- },
695
- };
696
-
697
- // -----------------------------------------------------------------------------
698
- // STDEV.P
699
- // -----------------------------------------------------------------------------
700
- export const STDEV_P: FunctionDescription = {
701
- description: _lt("Standard deviation of entire population."),
702
- args: args(`
703
- value1 (number, range<number>) ${_lt("The first value or range of the population.")}
704
- value2 (number, range<number>, optional, repeating) ${_lt(
705
- "Additional values or ranges to include in the population."
706
- )}
707
- `),
708
- returns: ["NUMBER"],
709
- compute: function (): number {
710
- return Math.sqrt(VAR_P.compute(...arguments));
711
- },
712
- };
713
-
714
- // -----------------------------------------------------------------------------
715
- // STDEV.S
716
- // -----------------------------------------------------------------------------
717
- export const STDEV_S: FunctionDescription = {
718
- description: _lt("Standard deviation."),
719
- args: args(`
720
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
721
- value2 (number, range<number>, optional, repeating) ${_lt(
722
- "Additional values or ranges to include in the sample."
723
- )}
724
- `),
725
- returns: ["NUMBER"],
726
- compute: function (): number {
727
- return Math.sqrt(VAR_S.compute(...arguments));
728
- },
729
- };
730
-
731
- // -----------------------------------------------------------------------------
732
- // STDEVA
733
- // -----------------------------------------------------------------------------
734
- export const STDEVA: FunctionDescription = {
735
- description: _lt("Standard deviation of sample (text as 0)."),
736
- args: args(`
737
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
738
- value2 (number, range<number>, optional, repeating) ${_lt(
739
- "Additional values or ranges to include in the sample."
740
- )}
741
- `),
742
- returns: ["NUMBER"],
743
- compute: function (): number {
744
- return Math.sqrt(VARA.compute(...arguments));
745
- },
746
- };
747
-
748
- // -----------------------------------------------------------------------------
749
- // STDEVP
750
- // -----------------------------------------------------------------------------
751
- export const STDEVP: FunctionDescription = {
752
- description: _lt("Standard deviation of entire population."),
753
- args: args(`
754
- value1 (number, range<number>) ${_lt("The first value or range of the population.")}
755
- value2 (number, range<number>, optional, repeating) ${_lt(
756
- "Additional values or ranges to include in the population."
757
- )}
758
- `),
759
- returns: ["NUMBER"],
760
- compute: function (): number {
761
- return Math.sqrt(VARP.compute(...arguments));
762
- },
763
- };
764
-
765
- // -----------------------------------------------------------------------------
766
- // STDEVPA
767
- // -----------------------------------------------------------------------------
768
- export const STDEVPA: FunctionDescription = {
769
- description: _lt("Standard deviation of entire population (text as 0)."),
770
- args: args(`
771
- value1 (number, range<number>) ${_lt("The first value or range of the population.")}
772
- value2 (number, range<number>, optional, repeating) ${_lt(
773
- "Additional values or ranges to include in the population."
774
- )}
775
- `),
776
- returns: ["NUMBER"],
777
- compute: function (): number {
778
- return Math.sqrt(VARPA.compute(...arguments));
779
- },
780
- };
781
-
782
- // -----------------------------------------------------------------------------
783
- // VAR
784
- // -----------------------------------------------------------------------------
785
- export const VAR: FunctionDescription = {
786
- description: _lt("Variance."),
787
- args: args(`
788
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
789
- value2 (number, range<number>, optional, repeating) ${_lt(
790
- "Additional values or ranges to include in the sample."
791
- )}
792
- `),
793
- returns: ["NUMBER"],
794
- compute: function (): number {
795
- return variance(arguments, true, false);
796
- },
797
- };
798
-
799
- // -----------------------------------------------------------------------------
800
- // VAR.P
801
- // -----------------------------------------------------------------------------
802
- export const VAR_P: FunctionDescription = {
803
- description: _lt("Variance of entire population."),
804
- args: args(`
805
- value1 (number, range<number>) ${_lt("The first value or range of the population.")}
806
- value2 (number, range<number>, optional, repeating) ${_lt(
807
- "Additional values or ranges to include in the population."
808
- )}
809
- `),
810
- returns: ["NUMBER"],
811
- compute: function (): number {
812
- return variance(arguments, false, false);
813
- },
814
- };
815
-
816
- // -----------------------------------------------------------------------------
817
- // VAR.S
818
- // -----------------------------------------------------------------------------
819
- export const VAR_S: FunctionDescription = {
820
- description: _lt("Variance."),
821
- args: args(`
822
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
823
- value2 (number, range<number>, optional, repeating) ${_lt(
824
- "Additional values or ranges to include in the sample."
825
- )}
826
- `),
827
- returns: ["NUMBER"],
828
- compute: function (): number {
829
- return variance(arguments, true, false);
830
- },
831
- };
832
-
833
- // -----------------------------------------------------------------------------
834
- // VARA
835
- // -----------------------------------------------------------------------------
836
- export const VARA: FunctionDescription = {
837
- description: _lt("Variance of sample (text as 0)."),
838
- args: args(`
839
- value1 (number, range<number>) ${_lt("The first value or range of the sample.")}
840
- value2 (number, range<number>, optional, repeating) ${_lt(
841
- "Additional values or ranges to include in the sample."
842
- )}
843
- `),
844
- returns: ["NUMBER"],
845
- compute: function (): number {
846
- return variance(arguments, true, true);
847
- },
848
- };
849
-
850
- // -----------------------------------------------------------------------------
851
- // VARP
852
- // -----------------------------------------------------------------------------
853
- export const VARP: FunctionDescription = {
854
- description: _lt("Variance of entire population."),
855
- args: args(`
856
- value1 (number, range<number>) ${_lt("The first value or range of the population.")}
857
- value2 (number, range<number>, optional, repeating) ${_lt(
858
- "Additional values or ranges to include in the population."
859
- )}
860
- `),
861
- returns: ["NUMBER"],
862
- compute: function (): number {
863
- return variance(arguments, false, false);
864
- },
865
- };
866
-
867
- // -----------------------------------------------------------------------------
868
- // VARPA
869
- // -----------------------------------------------------------------------------
870
- export const VARPA: FunctionDescription = {
871
- description: _lt("Variance of entire population (text as 0)."),
872
- args: args(`
873
- value1 (number, range<number>) ${_lt("The first value or range of the population.")}
874
- value2 (number, range<number>, optional, repeating) ${_lt(
875
- "Additional values or ranges to include in the population."
876
- )}
877
- `),
878
- returns: ["NUMBER"],
879
- compute: function (): number {
880
- return variance(arguments, false, true);
881
- },
882
- };