@odoo/o-spreadsheet 1.0.1

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 (199) hide show
  1. package/.eslintrc.json +20 -0
  2. package/.husky/post-checkout +9 -0
  3. package/.husky/pre-commit +39 -0
  4. package/.prettierignore +1 -0
  5. package/astToFormula.patch +0 -0
  6. package/demo/data.js +255 -0
  7. package/demo/index.html +9 -0
  8. package/demo/libs/Chart.bundle.js +20755 -0
  9. package/demo/main.js +101 -0
  10. package/doc/readme.md +46 -0
  11. package/package.json +78 -0
  12. package/readme.md +39 -0
  13. package/rollup.config.js +23 -0
  14. package/src/base_plugin.ts +102 -0
  15. package/src/components/autofill.ts +146 -0
  16. package/src/components/bottom_bar.ts +199 -0
  17. package/src/components/color_picker.ts +150 -0
  18. package/src/components/composer/autocomplete_dropdown.ts +135 -0
  19. package/src/components/composer/composer.ts +487 -0
  20. package/src/components/composer/content_editable_helper.ts +146 -0
  21. package/src/components/figures/chart.ts +70 -0
  22. package/src/components/figures/container.ts +268 -0
  23. package/src/components/figures/text_figure.ts +36 -0
  24. package/src/components/grid.ts +635 -0
  25. package/src/components/helpers/dom_helpers.ts +7 -0
  26. package/src/components/helpers/drag_and_drop.ts +17 -0
  27. package/src/components/icons.ts +36 -0
  28. package/src/components/index.ts +1 -0
  29. package/src/components/menu.ts +288 -0
  30. package/src/components/overlay.ts +457 -0
  31. package/src/components/scrollbar.ts +22 -0
  32. package/src/components/selection_input.ts +168 -0
  33. package/src/components/side_panel/cell_is_rule_editor.ts +169 -0
  34. package/src/components/side_panel/chart_panel.ts +104 -0
  35. package/src/components/side_panel/color_scale_rule_editor.ts +145 -0
  36. package/src/components/side_panel/conditional_formatting.ts +396 -0
  37. package/src/components/side_panel/side_panel.ts +127 -0
  38. package/src/components/side_panel/translations_terms.ts +45 -0
  39. package/src/components/spreadsheet.ts +198 -0
  40. package/src/components/top_bar.ts +535 -0
  41. package/src/constants.ts +26 -0
  42. package/src/data.ts +149 -0
  43. package/src/fonts.ts +41 -0
  44. package/src/formulas/compiler.ts +258 -0
  45. package/src/formulas/composer_tokenizer.ts +70 -0
  46. package/src/formulas/index.ts +15 -0
  47. package/src/formulas/normalize.ts +52 -0
  48. package/src/formulas/parser.ts +280 -0
  49. package/src/formulas/range_tokenizer.ts +97 -0
  50. package/src/formulas/tokenizer.ts +242 -0
  51. package/src/functions/arguments.ts +114 -0
  52. package/src/functions/dates.ts +302 -0
  53. package/src/functions/helpers.ts +592 -0
  54. package/src/functions/index.ts +52 -0
  55. package/src/functions/module_database.ts +282 -0
  56. package/src/functions/module_date.ts +743 -0
  57. package/src/functions/module_info.ts +69 -0
  58. package/src/functions/module_logical.ts +208 -0
  59. package/src/functions/module_lookup.ts +185 -0
  60. package/src/functions/module_math.ts +1252 -0
  61. package/src/functions/module_operators.ts +291 -0
  62. package/src/functions/module_statistical.ts +882 -0
  63. package/src/functions/module_text.ts +370 -0
  64. package/src/helpers/color.ts +30 -0
  65. package/src/helpers/coordinates.ts +71 -0
  66. package/src/helpers/grid_manipulation.ts +68 -0
  67. package/src/helpers/index.ts +7 -0
  68. package/src/helpers/misc.ts +76 -0
  69. package/src/helpers/numbers.ts +117 -0
  70. package/src/helpers/uuid.ts +19 -0
  71. package/src/helpers/zones.ts +263 -0
  72. package/src/history.ts +186 -0
  73. package/src/index.ts +84 -0
  74. package/src/model.ts +261 -0
  75. package/src/plugins/autofill.ts +412 -0
  76. package/src/plugins/chart.ts +404 -0
  77. package/src/plugins/clipboard.ts +461 -0
  78. package/src/plugins/conditional_format.ts +445 -0
  79. package/src/plugins/core.ts +1502 -0
  80. package/src/plugins/edition.ts +157 -0
  81. package/src/plugins/evaluation.ts +428 -0
  82. package/src/plugins/figures.ts +123 -0
  83. package/src/plugins/formatting.ts +630 -0
  84. package/src/plugins/highlight.ts +187 -0
  85. package/src/plugins/index.ts +32 -0
  86. package/src/plugins/merge.ts +524 -0
  87. package/src/plugins/renderer.ts +571 -0
  88. package/src/plugins/selection.ts +562 -0
  89. package/src/plugins/selection_inputs.ts +326 -0
  90. package/src/registries/autofill_modifiers.ts +76 -0
  91. package/src/registries/autofill_rules.ts +98 -0
  92. package/src/registries/figure_registry.ts +30 -0
  93. package/src/registries/index.ts +7 -0
  94. package/src/registries/menu_items_registry.ts +110 -0
  95. package/src/registries/menus/cell_menu_registry.ts +79 -0
  96. package/src/registries/menus/col_menu_registry.ts +63 -0
  97. package/src/registries/menus/index.ts +6 -0
  98. package/src/registries/menus/menu_items_actions.ts +425 -0
  99. package/src/registries/menus/row_menu_registry.ts +63 -0
  100. package/src/registries/menus/sheet_menu_registry.ts +75 -0
  101. package/src/registries/menus/topbar_menu_registry.ts +204 -0
  102. package/src/registries/side_panel_registry.ts +26 -0
  103. package/src/registries/topbar_component_registry.ts +12 -0
  104. package/src/registry.ts +51 -0
  105. package/src/translation.ts +44 -0
  106. package/src/types/autofill.ts +48 -0
  107. package/src/types/chart.ts +26 -0
  108. package/src/types/commands.ts +716 -0
  109. package/src/types/conditional_formatting.ts +124 -0
  110. package/src/types/env.ts +13 -0
  111. package/src/types/functions.ts +35 -0
  112. package/src/types/getters.ts +103 -0
  113. package/src/types/index.ts +20 -0
  114. package/src/types/misc.ts +143 -0
  115. package/src/types/rendering.ts +41 -0
  116. package/src/types/workbook_data.ts +62 -0
  117. package/tests/__mocks__/uuid.ts +7 -0
  118. package/tests/canvas.mock.ts +34 -0
  119. package/tests/components/__mocks__/content_editable_helper.ts +79 -0
  120. package/tests/components/__mocks__/scrollbar.ts +18 -0
  121. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +29 -0
  122. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +75 -0
  123. package/tests/components/__snapshots__/context_menu_test.ts.snap +88 -0
  124. package/tests/components/__snapshots__/grid_test.ts.snap +60 -0
  125. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +455 -0
  126. package/tests/components/__snapshots__/top_bar_test.ts.snap +705 -0
  127. package/tests/components/autocomplete_dropdown_test.ts +315 -0
  128. package/tests/components/autofill_test.ts +167 -0
  129. package/tests/components/bottom_bar_test.ts +262 -0
  130. package/tests/components/chart_side_panel_test.ts +123 -0
  131. package/tests/components/composer_test.ts +400 -0
  132. package/tests/components/context_menu_test.ts +825 -0
  133. package/tests/components/grid_manipulation_test.ts +163 -0
  134. package/tests/components/grid_test.ts +581 -0
  135. package/tests/components/overlay_test.ts +541 -0
  136. package/tests/components/selection_input_test.ts +259 -0
  137. package/tests/components/side_panel_test.ts +148 -0
  138. package/tests/components/spreadsheet_test.ts +194 -0
  139. package/tests/components/top_bar_test.ts +427 -0
  140. package/tests/data_test.ts +175 -0
  141. package/tests/dom_helper.ts +42 -0
  142. package/tests/formulas/__snapshots__/compiler_test.ts.snap +302 -0
  143. package/tests/formulas/compiler_test.ts +266 -0
  144. package/tests/formulas/composer_tokenizer_test.ts +137 -0
  145. package/tests/formulas/expressions_test.ts +57 -0
  146. package/tests/formulas/formulas_test.ts +80 -0
  147. package/tests/formulas/normalize_test.ts +82 -0
  148. package/tests/formulas/parser_test.ts +224 -0
  149. package/tests/formulas/range_tokenizer_test.ts +187 -0
  150. package/tests/formulas/tokenizer_test.ts +164 -0
  151. package/tests/functions/arguments_test.ts +141 -0
  152. package/tests/functions/dates_test.ts +1081 -0
  153. package/tests/functions/functions_test.ts +53 -0
  154. package/tests/functions/helper_test.ts +98 -0
  155. package/tests/functions/module_database_test.ts +311 -0
  156. package/tests/functions/module_date_test.ts +941 -0
  157. package/tests/functions/module_info_test.ts +139 -0
  158. package/tests/functions/module_logical_test.ts +369 -0
  159. package/tests/functions/module_lookup_test.ts +541 -0
  160. package/tests/functions/module_math_test.ts +2482 -0
  161. package/tests/functions/module_operators_test.ts +955 -0
  162. package/tests/functions/module_statistical_test.ts +2014 -0
  163. package/tests/functions/module_text_test.ts +667 -0
  164. package/tests/helpers/coordinates_test.ts +25 -0
  165. package/tests/helpers/misc_test.ts +95 -0
  166. package/tests/helpers/numbers_test.ts +196 -0
  167. package/tests/helpers/zones_test.ts +97 -0
  168. package/tests/helpers.ts +384 -0
  169. package/tests/menu_items_registry_test.ts +540 -0
  170. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +90 -0
  171. package/tests/model/plugins/chart_test.ts +562 -0
  172. package/tests/model_test.ts +82 -0
  173. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +131 -0
  174. package/tests/plugins/__snapshots__/renderer_test.ts.snap +371 -0
  175. package/tests/plugins/autofill_test.ts +457 -0
  176. package/tests/plugins/borders_test.ts +295 -0
  177. package/tests/plugins/clipboard_test.ts +1180 -0
  178. package/tests/plugins/conditional_formatting_test.ts +1253 -0
  179. package/tests/plugins/core_test.ts +614 -0
  180. package/tests/plugins/edition_test.ts +100 -0
  181. package/tests/plugins/evaluation_async_test.ts +332 -0
  182. package/tests/plugins/evaluation_dates_test.ts +79 -0
  183. package/tests/plugins/evaluation_test.ts +901 -0
  184. package/tests/plugins/figures_test.ts +327 -0
  185. package/tests/plugins/formatting_test.ts +391 -0
  186. package/tests/plugins/grid_manipulation_test.ts +1366 -0
  187. package/tests/plugins/highlight_test.ts +413 -0
  188. package/tests/plugins/history_test.ts +142 -0
  189. package/tests/plugins/import_export_test.ts +276 -0
  190. package/tests/plugins/merges_test.ts +490 -0
  191. package/tests/plugins/navigation_test.ts +181 -0
  192. package/tests/plugins/plugins/evaluation_test.ts +111 -0
  193. package/tests/plugins/renderer_test.ts +598 -0
  194. package/tests/plugins/resizing_test.ts +152 -0
  195. package/tests/plugins/selection_input_test.ts +409 -0
  196. package/tests/plugins/selection_test.ts +442 -0
  197. package/tests/plugins/sheets_test.ts +643 -0
  198. package/tests/plugins/style_test.ts +90 -0
  199. package/tsconfig.json +22 -0
@@ -0,0 +1,370 @@
1
+ import { _lt } from "../translation";
2
+ import { FunctionDescription } from "../types";
3
+ import { args } from "./arguments";
4
+ import { reduceArgs, toBoolean, toNumber, toString } from "./helpers";
5
+
6
+ // -----------------------------------------------------------------------------
7
+ // CHAR
8
+ // -----------------------------------------------------------------------------
9
+ export const CHAR: FunctionDescription = {
10
+ description: _lt("Gets character associated with number."),
11
+ args: args(`
12
+ table_number (number) ${_lt(
13
+ "The number of the character to look up from the current Unicode table in decimal format."
14
+ )}
15
+ `),
16
+ returns: ["STRING"],
17
+ compute: function (table_number: any): string {
18
+ const _tableNumber = Math.trunc(toNumber(table_number));
19
+ if (_tableNumber < 1) {
20
+ throw new Error(
21
+ _lt("Function CHAR parameter 1 value %s is out of range.", _tableNumber.toString())
22
+ );
23
+ }
24
+ return String.fromCharCode(_tableNumber);
25
+ },
26
+ };
27
+
28
+ // -----------------------------------------------------------------------------
29
+ // CONCATENATE
30
+ // -----------------------------------------------------------------------------
31
+ export const CONCATENATE: FunctionDescription = {
32
+ description: _lt("Appends strings to one another."),
33
+ args: args(`
34
+ string1 (string, range<string>) ${_lt("The initial string.")}
35
+ string2 (string, range<string>, optional, repeating) ${_lt(
36
+ "More strings to append in sequence."
37
+ )}
38
+ `),
39
+ returns: ["STRING"],
40
+ compute: function (): string {
41
+ return reduceArgs(arguments, (acc, a) => acc + toString(a), "");
42
+ },
43
+ };
44
+
45
+ // -----------------------------------------------------------------------------
46
+ // EXACT
47
+ // -----------------------------------------------------------------------------
48
+ export const EXACT: FunctionDescription = {
49
+ description: _lt("Tests whether two strings are identical."),
50
+ args: args(`
51
+ string1 (string) ${_lt("The first string to compare.")}
52
+ string2 (string) ${_lt("The second string to compare.")}
53
+ `),
54
+ returns: ["BOOLEAN"],
55
+ compute: function (string1: any, string2: any): boolean {
56
+ return toString(string1) === toString(string2);
57
+ },
58
+ };
59
+
60
+ // -----------------------------------------------------------------------------
61
+ // FIND
62
+ // -----------------------------------------------------------------------------
63
+ export const FIND: FunctionDescription = {
64
+ description: _lt("First position of string found in text, case-sensitive."),
65
+ args: args(`
66
+ search_for (string) ${_lt("The string to look for within text_to_search.")}
67
+ text_to_search (string) ${_lt("The text to search for the first occurrence of search_for.")}
68
+ starting_at (number, optional, default=1 ) ${_lt(
69
+ "The character within text_to_search at which to start the search."
70
+ )}
71
+ `),
72
+ returns: ["NUMBER"],
73
+ compute: function (search_for: any, text_to_search: any, starting_at: any = 1): number {
74
+ const _textToSearch = toString(text_to_search);
75
+ if (_textToSearch === "") {
76
+ throw new Error(_lt(`Function FIND parameter 2 value should be non-empty.`));
77
+ }
78
+
79
+ const _startingAt = toNumber(starting_at);
80
+ if (_startingAt === 0) {
81
+ throw new Error(
82
+ _lt(`Function FIND parameter 3 value is 0. It should be greater than or equal to 1.`)
83
+ );
84
+ }
85
+
86
+ const _searchFor = toString(search_for);
87
+ const result = _textToSearch.indexOf(_searchFor, _startingAt - 1);
88
+ if (result < 0) {
89
+ throw new Error(
90
+ _lt(
91
+ "In FIND evaluation, cannot find '%s' within '%s'.",
92
+ _searchFor.toString(),
93
+ _textToSearch
94
+ )
95
+ );
96
+ }
97
+ return result + 1;
98
+ },
99
+ };
100
+
101
+ // -----------------------------------------------------------------------------
102
+ // JOIN
103
+ // -----------------------------------------------------------------------------
104
+ export const JOIN: FunctionDescription = {
105
+ description: _lt("Concatenates elements of arrays with delimiter."),
106
+ args: args(`
107
+ delimiter (string) ${_lt("The character or string to place between each concatenated value.")}
108
+ value_or_array1 (string, range<string>) ${_lt(
109
+ "The value or values to be appended using delimiter."
110
+ )}
111
+ value_or_array2 (string, range<string>, optional, repeating) ${_lt(
112
+ "More values to be appended using delimiter."
113
+ )}
114
+ `),
115
+ returns: ["STRING"],
116
+ compute: function (delimiter: any, ...values_or_arrays: any): string {
117
+ const _delimiter = toString(delimiter);
118
+ return reduceArgs(
119
+ values_or_arrays,
120
+ (acc, a) => (acc ? acc + _delimiter : "") + toString(a),
121
+ ""
122
+ );
123
+ },
124
+ };
125
+
126
+ // -----------------------------------------------------------------------------
127
+ // LEFT
128
+ // -----------------------------------------------------------------------------
129
+ export const LEFT: FunctionDescription = {
130
+ description: _lt("Substring from beginning of specified string."),
131
+ args: args(`
132
+ text (string) ${_lt("The string from which the left portion will be returned.")}
133
+ number_of_characters (number, optional, default=1) ${_lt(
134
+ "The number of characters to return from the left side of string."
135
+ )}
136
+ `),
137
+ returns: ["STRING"],
138
+ compute: function (text: any, number_of_characters: any = 1): string {
139
+ const _numberOfCharacters = toNumber(number_of_characters);
140
+ if (_numberOfCharacters < 0) {
141
+ throw new Error(
142
+ _lt(`Function LEFT parameter 2 value is negative. It should be positive or zero.`)
143
+ );
144
+ }
145
+ return toString(text).substring(0, _numberOfCharacters);
146
+ },
147
+ };
148
+
149
+ // -----------------------------------------------------------------------------
150
+ // LEN
151
+ // -----------------------------------------------------------------------------
152
+ export const LEN: FunctionDescription = {
153
+ description: _lt("Length of a string."),
154
+ args: args(`
155
+ text (string) ${_lt("The string whose length will be returned.")}
156
+ `),
157
+ returns: ["NUMBER"],
158
+ compute: function (text: any): number {
159
+ return toString(text).length;
160
+ },
161
+ };
162
+
163
+ // -----------------------------------------------------------------------------
164
+ // LOWER
165
+ // -----------------------------------------------------------------------------
166
+ export const LOWER: FunctionDescription = {
167
+ description: _lt("Converts a specified string to lowercase."),
168
+ args: args(`
169
+ text (string) ${_lt("The string to convert to lowercase.")}
170
+ `),
171
+ returns: ["STRING"],
172
+ compute: function (text: any): string {
173
+ return toString(text).toLowerCase();
174
+ },
175
+ };
176
+
177
+ // -----------------------------------------------------------------------------
178
+ // REPLACE
179
+ // -----------------------------------------------------------------------------
180
+ export const REPLACE: FunctionDescription = {
181
+ description: _lt("Replaces part of a text string with different text."),
182
+ args: args(`
183
+ text (string) ${_lt("The text, a part of which will be replaced.")}
184
+ position (number) ${_lt("The position where the replacement will begin (starting from 1).")}
185
+ length (number) ${_lt("The number of characters in the text to be replaced.")}
186
+ new_text (string) ${_lt("The text which will be inserted into the original text.")}
187
+ `),
188
+ returns: ["STRING"],
189
+ compute: function (text: any, position: any, length: any, new_text: any): string {
190
+ const _position = toNumber(position);
191
+ if (_position < 1) {
192
+ throw new Error(
193
+ _lt(
194
+ "Function REPLACE parameter 2 value is %s. It should be greater than or equal to 1.",
195
+ toString(_position)
196
+ )
197
+ );
198
+ }
199
+
200
+ const _text = toString(text);
201
+ const _length = toNumber(length);
202
+ const _newText = toString(new_text);
203
+ return _text.substring(0, _position - 1) + _newText + _text.substring(_position - 1 + _length);
204
+ },
205
+ };
206
+
207
+ // -----------------------------------------------------------------------------
208
+ // RIGHT
209
+ // -----------------------------------------------------------------------------
210
+ export const RIGHT: FunctionDescription = {
211
+ description: _lt("A substring from the end of a specified string."),
212
+ args: args(`
213
+ text (string) ${_lt("The string from which the right portion will be returned.")}
214
+ number_of_characters (number, optional, default=1) ${_lt(
215
+ "The number of characters to return from the right side of string."
216
+ )}
217
+ `),
218
+ returns: ["STRING"],
219
+ compute: function (text: any, number_of_characters: any = 1): string {
220
+ const _numberOfCharacters = toNumber(number_of_characters);
221
+ if (_numberOfCharacters < 0) {
222
+ throw new Error(
223
+ _lt(`Function RIGHT parameter 2 value is negative. It should be positive or zero.`)
224
+ );
225
+ }
226
+ const _text = toString(text);
227
+ const stringLength = _text.length;
228
+ return _text.substring(stringLength - _numberOfCharacters, stringLength);
229
+ },
230
+ };
231
+
232
+ // -----------------------------------------------------------------------------
233
+ // SEARCH
234
+ // -----------------------------------------------------------------------------
235
+ export const SEARCH: FunctionDescription = {
236
+ description: _lt("First position of string found in text, ignoring case."),
237
+ args: args(`
238
+ search_for (string) ${_lt("The string to look for within text_to_search.")}
239
+ text_to_search (string) ${_lt("The text to search for the first occurrence of search_for.")}
240
+ starting_at (number, optional, default=1 ) ${_lt(
241
+ "The character within text_to_search at which to start the search."
242
+ )}
243
+ `),
244
+ returns: ["NUMBER"],
245
+ compute: function (search_for: any, text_to_search: any, starting_at: any = 1): number {
246
+ const _textToSearch = toString(text_to_search).toLowerCase();
247
+ if (_textToSearch === "") {
248
+ throw new Error(_lt(`Function SEARCH parameter 2 value should be non-empty.`));
249
+ }
250
+
251
+ const _startingAt = toNumber(starting_at);
252
+ if (_startingAt === 0) {
253
+ throw new Error(
254
+ _lt(`Function SEARCH parameter 3 value is 0. It should be greater than or equal to 1.`)
255
+ );
256
+ }
257
+
258
+ const _searchFor = toString(search_for).toLowerCase();
259
+ const result = _textToSearch.indexOf(_searchFor, _startingAt - 1);
260
+ if (result < 0) {
261
+ throw new Error(
262
+ _lt("In SEARCH evaluation, cannot find '%s' within '%s'.", _searchFor, _textToSearch)
263
+ );
264
+ }
265
+ return result + 1;
266
+ },
267
+ };
268
+
269
+ // -----------------------------------------------------------------------------
270
+ // SUBSTITUTE
271
+ // -----------------------------------------------------------------------------
272
+ export const SUBSTITUTE: FunctionDescription = {
273
+ description: _lt("Replaces existing text with new text in a string."),
274
+ args: args(`
275
+ text_to_search (string) ${_lt("The text within which to search and replace.")}
276
+ search_for (string) ${_lt("The string to search for within text_to_search.")}
277
+ replace_with (string) ${_lt("The string that will replace search_for.")}
278
+ occurrence_number (number, optional) ${_lt(
279
+ "The instance of search_for within text_to_search to replace with replace_with. By default, all occurrences of search_for are replaced; however, if occurrence_number is specified, only the indicated instance of search_for is replaced."
280
+ )}
281
+ `),
282
+ returns: ["NUMBER"],
283
+ compute: function (
284
+ text_to_search: any,
285
+ search_for: any,
286
+ replace_with: any,
287
+ occurrence_number: any = undefined
288
+ ): string {
289
+ const _occurrenceNumber = toNumber(occurrence_number);
290
+ if (_occurrenceNumber < 0) {
291
+ throw new Error(
292
+ _lt(`Function SUBSTITUTE parameter 4 value is negative. It should be positive or zero.`)
293
+ );
294
+ }
295
+
296
+ const _textToSearch = toString(text_to_search);
297
+ const _searchFor = toString(search_for);
298
+ if (_searchFor === "") {
299
+ return _textToSearch;
300
+ }
301
+
302
+ const _replaceWith = toString(replace_with);
303
+ const reg = new RegExp(_searchFor, "g");
304
+ if (_occurrenceNumber === 0) {
305
+ return _textToSearch.replace(reg, _replaceWith);
306
+ }
307
+
308
+ let n = 0;
309
+ return _textToSearch.replace(reg, (text) => (++n === _occurrenceNumber ? _replaceWith : text));
310
+ },
311
+ };
312
+
313
+ // -----------------------------------------------------------------------------
314
+ // TEXTJOIN
315
+ // -----------------------------------------------------------------------------
316
+ export const TEXTJOIN: FunctionDescription = {
317
+ description: _lt("Combines text from multiple strings and/or arrays."),
318
+ args: args(`
319
+ delimiter (string) ${_lt(
320
+ " A string, possible empty, or a reference to a valid string. If empty, the text will be simply concatenated."
321
+ )}
322
+ ignore_empty (boolean) ${_lt(
323
+ "A boolean; if TRUE, empty cells selected in the text arguments won't be included in the result."
324
+ )}
325
+ text1 (string, range<string>) ${_lt(
326
+ "Any text item. This could be a string, or an array of strings in a range."
327
+ )}
328
+ text2 (string, range<string>, optional, repeating) ${_lt("Additional text item(s).")}
329
+ `),
330
+ returns: ["STRING"],
331
+ compute: function (delimiter: any, ignore_empty: any, ...texts_or_arrays: any): string {
332
+ const _delimiter = toString(delimiter);
333
+ const _ignoreEmpty = toBoolean(ignore_empty);
334
+ let n = 0;
335
+ return reduceArgs(
336
+ texts_or_arrays,
337
+ (acc, a) =>
338
+ !(_ignoreEmpty && toString(a) === "") ? (n++ ? acc + _delimiter : "") + toString(a) : acc,
339
+ ""
340
+ );
341
+ },
342
+ };
343
+
344
+ // -----------------------------------------------------------------------------
345
+ // TRIM
346
+ // -----------------------------------------------------------------------------
347
+ export const TRIM: FunctionDescription = {
348
+ description: _lt("Removes space characters."),
349
+ args: args(`
350
+ text (string) ${_lt("The text or reference to a cell containing text to be trimmed.")}
351
+ `),
352
+ returns: ["STRING"],
353
+ compute: function (text: any): string {
354
+ return toString(text).trim();
355
+ },
356
+ };
357
+
358
+ // -----------------------------------------------------------------------------
359
+ // UPPER
360
+ // -----------------------------------------------------------------------------
361
+ export const UPPER: FunctionDescription = {
362
+ description: _lt("Converts a specified string to uppercase."),
363
+ args: args(`
364
+ text (string) ${_lt("The string to convert to uppercase.")}
365
+ `),
366
+ returns: ["STRING"],
367
+ compute: function (text: any): string {
368
+ return toString(text).toUpperCase();
369
+ },
370
+ };
@@ -0,0 +1,30 @@
1
+ export const colors = [
2
+ "#ff851b",
3
+ "#0074d9",
4
+ "#ffdc00",
5
+ "#7fdbff",
6
+ "#b10dc9",
7
+ "#0ecc40",
8
+ "#39cccc",
9
+ "#f012be",
10
+ "#3d9970",
11
+ "#111111",
12
+ "#01ff70",
13
+ "#ff4136",
14
+ "#aaaaaa",
15
+ "#85144b",
16
+ "#001f3f",
17
+ ];
18
+
19
+ /*
20
+ * transform a color number (R * 256^2 + G * 256 + B) into classic RGB
21
+ * */
22
+ export function colorNumberString(color: number): string {
23
+ return color.toString(16).padStart(6, "0");
24
+ }
25
+
26
+ let colorIndex = 0;
27
+ export function getNextColor() {
28
+ colorIndex = ++colorIndex % colors.length;
29
+ return colors[colorIndex];
30
+ }
@@ -0,0 +1,71 @@
1
+ //------------------------------------------------------------------------------
2
+ // Coordinate
3
+ //------------------------------------------------------------------------------
4
+
5
+ /**
6
+ * Convert a (col) number to the corresponding letter.
7
+ *
8
+ * Examples:
9
+ * 0 => 'A'
10
+ * 25 => 'Z'
11
+ * 26 => 'AA'
12
+ * 27 => 'AB'
13
+ */
14
+ export function numberToLetters(n: number): string {
15
+ if (n < 26) {
16
+ return String.fromCharCode(65 + n);
17
+ } else {
18
+ return numberToLetters(Math.floor(n / 26) - 1) + numberToLetters(n % 26);
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Convert a string (describing a column) to its number value.
24
+ *
25
+ * Examples:
26
+ * 'A' => 0
27
+ * 'Z' => 25
28
+ * 'AA' => 26
29
+ */
30
+ export function lettersToNumber(letters: string): number {
31
+ let result = 0;
32
+ const l = letters.length;
33
+ for (let i = 0; i < l; i++) {
34
+ let n = letters.charCodeAt(i) - 65 + (i < l - 1 ? 1 : 0);
35
+ result += n * 26 ** (l - i - 1);
36
+ }
37
+ return result;
38
+ }
39
+
40
+ /**
41
+ * Convert a "XC" coordinate to cartesian coordinates.
42
+ *
43
+ * Examples:
44
+ * A1 => [0,0]
45
+ * B3 => [1,2]
46
+ *
47
+ * Note: it also accepts lowercase coordinates, but not fixed references
48
+ */
49
+ export function toCartesian(xc: string): [number, number] {
50
+ xc = xc.toUpperCase();
51
+ const [m, letters, numbers] = xc.match(/\$?([A-Z]*)\$?([0-9]*)/)!;
52
+ if (m !== xc) {
53
+ throw new Error(`Invalid cell description: ${xc}`);
54
+ }
55
+ const col = lettersToNumber(letters);
56
+ const row = parseInt(numbers, 10) - 1;
57
+ return [col, row];
58
+ }
59
+
60
+ /**
61
+ * Convert from cartesian coordinate to the "XC" coordinate system.
62
+ *
63
+ * Examples:
64
+ * [0,0] => A1
65
+ * [1,2] => B3
66
+ *
67
+ * Note: it does not support fixed references
68
+ */
69
+ export function toXC(col: number, row: number): string {
70
+ return numberToLetters(col) + String(row + 1);
71
+ }
@@ -0,0 +1,68 @@
1
+ /*
2
+ * Contains all method to update ranges with grid_manipulation
3
+ */
4
+
5
+ import { toXC } from "./coordinates";
6
+ import { toZone } from "./zones";
7
+
8
+ export function updateRemoveColumns(range: string, columns: number[]): string | null {
9
+ let { left, right, top, bottom } = toZone(range);
10
+ columns = columns.slice().sort((a, b) => b - a);
11
+ for (let column of columns) {
12
+ if (left > column) {
13
+ left -= 1;
14
+ }
15
+ if (left >= column || right >= column) {
16
+ right -= 1;
17
+ }
18
+ }
19
+ if (left > right) {
20
+ return null;
21
+ }
22
+ return toXC(left, top) + ":" + toXC(right, bottom);
23
+ }
24
+
25
+ export function updateRemoveRows(range: string, rows: number[]): string | null {
26
+ let { left, right, top, bottom } = toZone(range);
27
+ rows = rows.slice().sort((a, b) => b - a);
28
+ for (let row of rows) {
29
+ if (top > row) {
30
+ top -= 1;
31
+ }
32
+ if (top >= row || bottom >= row) {
33
+ bottom -= 1;
34
+ }
35
+ }
36
+ if (top > bottom) {
37
+ return null;
38
+ }
39
+ return toXC(left, top) + ":" + toXC(right, bottom);
40
+ }
41
+
42
+ export function updateAddColumns(range: string, column: number, step: number): string | null {
43
+ let { left, right, top, bottom } = toZone(range);
44
+ if (left >= column) {
45
+ left += step;
46
+ }
47
+ if (left >= column || right >= column) {
48
+ right += step;
49
+ }
50
+ if (left > right) {
51
+ return null;
52
+ }
53
+ return toXC(left, top) + ":" + toXC(right, bottom);
54
+ }
55
+
56
+ export function updateAddRows(range: string, row: number, step: number): string | null {
57
+ let { left, right, top, bottom } = toZone(range);
58
+ if (top >= row) {
59
+ top += step;
60
+ }
61
+ if (top >= row || bottom >= row) {
62
+ bottom += step;
63
+ }
64
+ if (top > bottom) {
65
+ return null;
66
+ }
67
+ return toXC(left, top) + ":" + toXC(right, bottom);
68
+ }
@@ -0,0 +1,7 @@
1
+ export * from "./color";
2
+ export * from "./coordinates";
3
+ export * from "./grid_manipulation";
4
+ export * from "./misc";
5
+ export * from "./numbers";
6
+ export * from "./uuid";
7
+ export * from "./zones";
@@ -0,0 +1,76 @@
1
+ //------------------------------------------------------------------------------
2
+ // Miscellaneous
3
+ //------------------------------------------------------------------------------
4
+
5
+ /**
6
+ * Stringify an object, like JSON.stringify, except that the first level of keys
7
+ * is ordered.
8
+ */
9
+ export function stringify(obj: any): string {
10
+ return JSON.stringify(obj, Object.keys(obj).sort());
11
+ }
12
+
13
+ /**
14
+ * Deep copy arrays, plain objects and primitive values.
15
+ * Throws an error for other types such as class instances.
16
+ * Sparse arrays remain sparse.
17
+ */
18
+ export function deepCopy<T>(obj: T): T {
19
+ const result: any = Array.isArray(obj) ? [] : {};
20
+ switch (typeof obj) {
21
+ case "object": {
22
+ if (obj === null) {
23
+ return obj;
24
+ } else if (!(isPlainObject(obj) || obj instanceof Array)) {
25
+ throw new Error("Unsupported type: only objects and arrays are supported");
26
+ }
27
+ for (const key in obj) {
28
+ result[key] = deepCopy(obj[key]);
29
+ }
30
+ return result;
31
+ }
32
+ case "number":
33
+ case "string":
34
+ case "boolean":
35
+ case "function":
36
+ case "undefined":
37
+ return obj;
38
+ default:
39
+ throw new Error(`Unsupported type: ${typeof obj}`);
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Check if the object is a plain old javascript object.
45
+ */
46
+ function isPlainObject(obj: unknown): boolean {
47
+ return typeof obj === "object" && obj?.constructor === Object;
48
+ }
49
+
50
+ /**
51
+ * Sanitize the name of a sheet, by eventually removing quotes
52
+ * @param sheetName name of the sheet, potentially quoted with single quotes
53
+ */
54
+ export function getUnquotedSheetName(sheetName: string): string {
55
+ if (sheetName.startsWith("'")) {
56
+ sheetName = sheetName.slice(1, -1).replace(/''/g, "'");
57
+ }
58
+ return sheetName;
59
+ }
60
+
61
+ /**
62
+ * Add quotes around the sheet name if it contains a space
63
+ * @param sheetName Name of the sheet
64
+ */
65
+ export function getComposerSheetName(sheetName: string): string {
66
+ if (sheetName.includes(" ")) {
67
+ sheetName = `'${sheetName}'`;
68
+ }
69
+ return sheetName;
70
+ }
71
+
72
+ export function clip(val: number, min: number, max: number): number {
73
+ return val < min ? min : val > max ? max : val;
74
+ }
75
+
76
+ export const DEBUG: { [key: string]: any } = {};