@odoo/o-spreadsheet 14.0.1 → 14.0.2

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 +17836 -0
  2. package/dist/o_spreadsheet.d.ts +4102 -0
  3. package/dist/o_spreadsheet.esm.js +17801 -0
  4. package/dist/o_spreadsheet.iife.js +17838 -0
  5. package/dist/o_spreadsheet.iife.min.js +1440 -0
  6. package/package.json +27 -7
  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,137 +0,0 @@
1
- import { composerTokenize } from "../../src/formulas/composer_tokenizer";
2
-
3
- describe("composerTokenizer", () => {
4
- test("only range", () => {
5
- expect(composerTokenize("=A1:A2")).toEqual([
6
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
7
- { start: 1, end: 6, length: 5, type: "SYMBOL", value: "A1:A2" },
8
- ]);
9
- });
10
- test("operation and no range", () => {
11
- expect(composerTokenize("=A3+A1")).toEqual([
12
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
13
- { start: 1, end: 3, length: 2, type: "SYMBOL", value: "A3" },
14
- { start: 3, end: 4, length: 1, type: "OPERATOR", value: "+" },
15
- { start: 4, end: 6, length: 2, type: "SYMBOL", value: "A1" },
16
- ]);
17
- });
18
- test("operation and range", () => {
19
- expect(composerTokenize("=A3+A1:A2")).toEqual([
20
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
21
- { start: 1, end: 3, length: 2, type: "SYMBOL", value: "A3" },
22
- { start: 3, end: 4, length: 1, type: "OPERATOR", value: "+" },
23
- { start: 4, end: 9, length: 5, type: "SYMBOL", value: "A1:A2" },
24
- ]);
25
- });
26
- test("operation and range with spaces", () => {
27
- expect(composerTokenize("=A3+ A1 : A2 ")).toEqual([
28
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
29
- { start: 1, end: 3, length: 2, type: "SYMBOL", value: "A3" },
30
- { start: 3, end: 4, length: 1, type: "OPERATOR", value: "+" },
31
- { start: 4, end: 16, length: 12, type: "SYMBOL", value: " A1 : A2 " },
32
- ]);
33
- });
34
-
35
- test("range with spaces then operation", () => {
36
- expect(composerTokenize("= A1 : A2 +a3")).toEqual([
37
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
38
- { start: 1, end: 13, length: 12, type: "SYMBOL", value: " A1 : A2 " },
39
- { start: 13, end: 14, length: 1, type: "OPERATOR", value: "+" },
40
- { start: 14, end: 16, length: 2, type: "SYMBOL", value: "a3" },
41
- ]);
42
- }); //"= SUM ( C4 : C5 )"
43
-
44
- test("= SUM ( C4 : C5 )", () => {
45
- expect(composerTokenize("= SUM ( C4 : C5 )")).toEqual([
46
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
47
- { start: 1, end: 2, length: 1, type: "SPACE", value: " " },
48
- { start: 2, end: 5, length: 3, type: "FUNCTION", value: "SUM" },
49
- { start: 5, end: 6, length: 1, type: "SPACE", value: " " },
50
- { start: 6, end: 7, length: 1, type: "LEFT_PAREN", value: "(", parenIndex: 1 },
51
- { start: 7, end: 16, length: 9, type: "SYMBOL", value: " C4 : C5 " },
52
- { start: 16, end: 17, length: 1, type: "RIGHT_PAREN", value: ")", parenIndex: 1 },
53
- ]);
54
- });
55
- });
56
-
57
- describe("composerTokenizer base tests", () => {
58
- test("simple token", () => {
59
- expect(composerTokenize("1")).toEqual([
60
- { start: 0, end: 1, length: 1, type: "NUMBER", value: "1" },
61
- ]);
62
- });
63
- test("formula token", () => {
64
- expect(composerTokenize("=1")).toEqual([
65
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
66
- { start: 1, end: 2, length: 1, type: "NUMBER", value: "1" },
67
- ]);
68
- });
69
- test("longer operators >=", () => {
70
- expect(composerTokenize("= >= <= <")).toEqual([
71
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
72
- { start: 1, end: 2, length: 1, type: "SPACE", value: " " },
73
- { start: 2, end: 4, length: 2, type: "OPERATOR", value: ">=" },
74
- { start: 4, end: 5, length: 1, type: "SPACE", value: " " },
75
- { start: 5, end: 7, length: 2, type: "OPERATOR", value: "<=" },
76
- { start: 7, end: 8, length: 1, type: "SPACE", value: " " },
77
- { start: 8, end: 9, length: 1, type: "OPERATOR", value: "<" },
78
- ]);
79
- });
80
-
81
- test("debug formula token", () => {
82
- expect(composerTokenize("=?1")).toEqual([
83
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
84
- { start: 1, end: 2, length: 1, type: "DEBUGGER", value: "?" },
85
- { start: 2, end: 3, length: 1, type: "NUMBER", value: "1" },
86
- ]);
87
- });
88
- test("String", () => {
89
- expect(composerTokenize('"hello"')).toEqual([
90
- { start: 0, end: 7, length: 7, type: "STRING", value: '"hello"' },
91
- ]);
92
- //expect(() => composerTokenize("'hello'")).toThrowError("kikou");
93
- expect(composerTokenize("'hello'")).toEqual([
94
- { start: 0, end: 7, length: 7, type: "SYMBOL", value: "'hello'" },
95
- ]);
96
- expect(composerTokenize('"he\\"l\\"lo"')).toEqual([
97
- { start: 0, end: 11, length: 11, type: "STRING", value: '"he\\"l\\"lo"' },
98
- ]);
99
- expect(composerTokenize("\"hel'l'o\"")).toEqual([
100
- { start: 0, end: 9, length: 9, type: "STRING", value: "\"hel'l'o\"" },
101
- ]);
102
- expect(composerTokenize('"hello""test"')).toEqual([
103
- { start: 0, end: 7, length: 7, type: "STRING", value: '"hello"' },
104
- { start: 7, end: 13, length: 6, type: "STRING", value: '"test"' },
105
- ]);
106
- });
107
-
108
- test("Function token", () => {
109
- expect(composerTokenize("SUM")).toEqual([
110
- { start: 0, end: 3, length: 3, type: "FUNCTION", value: "SUM" },
111
- ]);
112
- expect(composerTokenize("RAND")).toEqual([
113
- { start: 0, end: 4, length: 4, type: "FUNCTION", value: "RAND" },
114
- ]);
115
- });
116
- test("Boolean", () => {
117
- expect(composerTokenize("true")).toEqual([
118
- { start: 0, end: 4, length: 4, type: "SYMBOL", value: "true" },
119
- ]);
120
- expect(composerTokenize("false")).toEqual([
121
- { start: 0, end: 5, length: 5, type: "SYMBOL", value: "false" },
122
- ]);
123
- expect(composerTokenize("=AND(true,false)")).toEqual([
124
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
125
- { start: 1, end: 4, length: 3, type: "FUNCTION", value: "AND" },
126
- { start: 4, end: 5, length: 1, type: "LEFT_PAREN", value: "(", parenIndex: 1 },
127
- { start: 5, end: 9, length: 4, type: "SYMBOL", value: "true" },
128
- { start: 9, end: 10, length: 1, type: "COMMA", value: "," },
129
- { start: 10, end: 15, length: 5, type: "SYMBOL", value: "false" },
130
- { start: 15, end: 16, length: 1, type: "RIGHT_PAREN", parenIndex: 1, value: ")" },
131
- ]);
132
- expect(composerTokenize("=trueee")).toEqual([
133
- { start: 0, end: 1, length: 1, type: "OPERATOR", value: "=" },
134
- { start: 1, end: 7, length: 6, type: "SYMBOL", value: "trueee" },
135
- ]);
136
- });
137
- });
@@ -1,57 +0,0 @@
1
- import { evaluateCell } from "../helpers";
2
-
3
- /**
4
- * Tests in this file are supposed to test mostly the compiler/functions parts,
5
- * not the semantic of the recursive evaluation of formulas.
6
- */
7
-
8
- describe("expression evaluation", () => {
9
- test("arithmetic expressions", () => {
10
- expect(evaluateCell("A1", { A1: "=1" })).toBe(1);
11
- expect(evaluateCell("A1", { A1: "=2 + 3 * 4" })).toBe(14);
12
- });
13
-
14
- test("comparisons", () => {
15
- // eq
16
- expect(evaluateCell("A1", { A1: "=2 = 1" })).toBe(false);
17
- expect(evaluateCell("A1", { A1: "=2 = 2" })).toBe(true);
18
-
19
- // gt
20
- expect(evaluateCell("A1", { A1: "=2 > 1" })).toBe(true);
21
- expect(evaluateCell("A1", { A1: "=1 > 2" })).toBe(false);
22
- expect(evaluateCell("A1", { A1: "=1 > 1" })).toBe(false);
23
- // gte
24
- expect(evaluateCell("A1", { A1: "=1 >= 1" })).toBe(true);
25
- expect(evaluateCell("A1", { A1: "=1 >= 0" })).toBe(true);
26
- expect(evaluateCell("A1", { A1: "=-1 >= 3" })).toBe(false);
27
- // lt
28
- expect(evaluateCell("A1", { A1: "=1 < 2" })).toBe(true);
29
- expect(evaluateCell("A1", { A1: "=2 < 2" })).toBe(false);
30
- expect(evaluateCell("A1", { A1: "=3 < 2" })).toBe(false);
31
- // lte
32
- expect(evaluateCell("A1", { A1: "=1 <= 2" })).toBe(true);
33
- expect(evaluateCell("A1", { A1: "=2 <= 2" })).toBe(true);
34
- expect(evaluateCell("A1", { A1: "=3 <= 2" })).toBe(false);
35
- });
36
-
37
- test("priority of operations", () => {
38
- expect(evaluateCell("A1", { A1: "=4 > 1 + 2" })).toBe(true);
39
- expect(evaluateCell("A1", { A1: "=4 < 1 + 2" })).toBe(false);
40
- expect(evaluateCell("A1", { A1: "=4 >= 1 + 2" })).toBe(true);
41
- expect(evaluateCell("A1", { A1: "=4 <= 1 + 2" })).toBe(false);
42
- expect(evaluateCell("A1", { A1: "=2 = 1 + 1" })).toBe(true);
43
-
44
- expect(evaluateCell("A1", { A1: '="4" > 1 & 2' })).toBe(true);
45
- expect(evaluateCell("A1", { A1: '="4" < 1 & 2' })).toBe(false);
46
- expect(evaluateCell("A1", { A1: '="4" >= 1 & 2' })).toBe(true);
47
- expect(evaluateCell("A1", { A1: '="4" <= 1 & 2' })).toBe(false);
48
- expect(evaluateCell("A1", { A1: '="12" = 1 & 2' })).toBe(true);
49
- });
50
-
51
- test("miscellaneous formulas", () => {
52
- const r1 = evaluateCell("A2", { A2: "=SUM(A1,B1)", A1: "1", B1: "2" });
53
- expect(r1).toBe(3);
54
- const r2 = evaluateCell("A2", { A2: "=SUM(A1:B1)", A1: "1", B1: "2" });
55
- expect(r2).toBe(3);
56
- });
57
- });
@@ -1,80 +0,0 @@
1
- import { Model } from "../../src";
2
- import "../canvas.mock";
3
-
4
- describe("applyOffset", () => {
5
- test("simple changes", () => {
6
- const model = new Model({
7
- sheets: [
8
- {
9
- colNumber: 10,
10
- rowNumber: 10,
11
- },
12
- ],
13
- });
14
- expect(model.getters.applyOffset("=A1", 1, 1)).toEqual("=B2");
15
- expect(model.getters.applyOffset("=A1 + B3", 1, 1)).toEqual("=B2 + C4");
16
- });
17
-
18
- test("can handle negative offsets", () => {
19
- const model = new Model();
20
- expect(model.getters.applyOffset("=B2", 0, -1)).toEqual("=B1");
21
- expect(model.getters.applyOffset("=B2", -1, 0)).toEqual("=A2");
22
- expect(model.getters.applyOffset("=B2", -1, -1)).toEqual("=A1");
23
- expect(model.getters.applyOffset("=B2", 0, -4)).toEqual("=#REF");
24
- expect(model.getters.applyOffset("=B2", -4, 0)).toEqual("=#REF");
25
- });
26
-
27
- test("can handle offsets outside the sheet", () => {
28
- const model = new Model({
29
- sheets: [
30
- {
31
- colNumber: 10,
32
- rowNumber: 10,
33
- },
34
- ],
35
- });
36
- expect(model.getters.applyOffset("=B2", 0, -4)).toEqual("=#REF");
37
- expect(model.getters.applyOffset("=B10", 0, 2)).toEqual("=B12");
38
- expect(model.getters.applyOffset("=J1", 2, 0)).toEqual("=L1");
39
- });
40
-
41
- test("can handle other formulas", () => {
42
- const model = new Model({
43
- sheets: [
44
- {
45
- colNumber: 10,
46
- rowNumber: 10,
47
- },
48
- ],
49
- });
50
- expect(model.getters.applyOffset("=AND(true, B2)", 0, 1)).toEqual("=AND(true, B3)");
51
- });
52
-
53
- test("can handle cross-sheet formulas", () => {
54
- const model = new Model({
55
- sheets: [
56
- {
57
- colNumber: 10,
58
- rowNumber: 10,
59
- },
60
- {
61
- name: "Sheet2",
62
- colNumber: 5,
63
- rowNumber: 5,
64
- },
65
- ],
66
- });
67
- expect(model.getters.applyOffset("=Sheet2!B2", 0, 1)).toEqual("=Sheet2!B3");
68
- expect(model.getters.applyOffset("='Sheet2'!B2", 0, 1)).toEqual("=Sheet2!B3");
69
- expect(model.getters.applyOffset("=Sheet2!B2", 0, -2)).toEqual("=#REF");
70
- expect(model.getters.applyOffset("=Sheet2!B2", -2, 0)).toEqual("=#REF");
71
- expect(model.getters.applyOffset("=Sheet2!B2", 1, 1)).toEqual("=Sheet2!C3");
72
- expect(model.getters.applyOffset("=Sheet2!B2", 1, 10)).toEqual("=Sheet2!C12");
73
- });
74
-
75
- test("can handle sheet reference with names", () => {
76
- const model = new Model();
77
- model.dispatch("CREATE_SHEET", { id: "42", name: "Sheet 2" });
78
- expect(model.getters.applyOffset("='Sheet 2'!B2", 0, 1)).toEqual("='Sheet 2'!B3");
79
- });
80
- });
@@ -1,82 +0,0 @@
1
- import { normalize } from "../../src/formulas";
2
-
3
- test("basic formula with only boolean arguments should be unchanged", () => {
4
- expect(normalize("=Sum( TRUE , FALSE) + TRUE - ( FALSE * TRUE)")).toEqual({
5
- text: "=Sum( TRUE , FALSE) + TRUE - ( FALSE * TRUE)",
6
- dependencies: [],
7
- });
8
- });
9
-
10
- test("replace references with different syntax", () => {
11
- expect(normalize("=A1")).toEqual({
12
- text: "=|0|",
13
- dependencies: ["A1"],
14
- });
15
- expect(normalize("=$A$1")).toEqual({
16
- text: "=|0|",
17
- dependencies: ["$A$1"],
18
- });
19
- expect(normalize("=Sheet1!A1")).toEqual({
20
- text: "=|0|",
21
- dependencies: ["Sheet1!A1"],
22
- });
23
- expect(normalize("='Sheet1'!A1")).toEqual({
24
- text: "=|0|",
25
- dependencies: ["'Sheet1'!A1"],
26
- });
27
- expect(normalize("='Sheet1'!A$1")).toEqual({
28
- text: "=|0|",
29
- dependencies: ["'Sheet1'!A$1"],
30
- });
31
- expect(normalize("=A1:b2")).toEqual({
32
- text: "=|0|",
33
- dependencies: ["A1:b2"],
34
- });
35
- expect(normalize("=$A$1:$b$2")).toEqual({
36
- text: "=|0|",
37
- dependencies: ["$A$1:$b$2"],
38
- });
39
- expect(normalize("=Sheet1!A1:b2")).toEqual({
40
- text: "=|0|",
41
- dependencies: ["Sheet1!A1:b2"],
42
- });
43
- expect(normalize("='SHEET 1'!A1:b2")).toEqual({
44
- text: "=|0|",
45
- dependencies: ["'SHEET 1'!A1:b2"],
46
- });
47
- expect(normalize("='Sheet1'!A$1:$B2")).toEqual({
48
- text: "=|0|",
49
- dependencies: ["'Sheet1'!A$1:$B2"],
50
- });
51
- });
52
- test("replace multiple references", () => {
53
- expect(normalize("=sum(a1:a3, a1:a3) + a1")).toEqual({
54
- text: "=sum(|0|,|0|) + |1|",
55
- dependencies: ["a1:a3", "a1"],
56
- });
57
- });
58
- test("replace numbers in functions, numbers are parsed", () => {
59
- expect(normalize("=sum(1, 5%) + 7")).toEqual({
60
- text: "=sum(|N0|, |N1|) + |N2|",
61
- dependencies: [1, 0.05, 7],
62
- });
63
- });
64
- test("replace strings in functions", () => {
65
- expect(normalize('=CONCATENATE("a", "5", "b")')).toEqual({
66
- text: "=CONCATENATE(|S0|, |S1|, |S2|)",
67
- dependencies: ["a", "5", "b"],
68
- });
69
- });
70
- test("replace multiple on function", () => {
71
- expect(normalize('=CONCATENATE(A1:A3, 5, "b")')).toEqual({
72
- text: "=CONCATENATE(|0|, |N1|, |S2|)",
73
- dependencies: ["A1:A3", 5, "b"],
74
- });
75
- });
76
- test("do not replace normalized dependencies inside strings", () => {
77
- const formula = '=concat("|1|","|S0|","|N2|")';
78
- expect(normalize(formula)).toEqual({
79
- text: "=concat(|S0|,|S1|,|S2|)",
80
- dependencies: ["|1|", "|S0|", "|N2|"],
81
- });
82
- });
@@ -1,224 +0,0 @@
1
- import { astToFormula, normalize } from "../../src";
2
- import { parse } from "../../src/formulas";
3
-
4
- describe("parser", () => {
5
- test("can parse a function call with no argument", () => {
6
- expect(parse("RAND()")).toEqual({ type: "FUNCALL", value: "RAND", args: [] });
7
- });
8
-
9
- test("can parse a function call with one argument", () => {
10
- expect(parse("SUM(1)")).toEqual({
11
- type: "FUNCALL",
12
- value: "SUM",
13
- args: [{ type: "NUMBER", value: 1 }],
14
- });
15
- });
16
-
17
- test("can parse a function call with sub expressions as argument", () => {
18
- expect(parse("IF(|0| > 0, 1, 2)")).toEqual({
19
- type: "FUNCALL",
20
- value: "IF",
21
- args: [
22
- {
23
- type: "BIN_OPERATION",
24
- value: ">",
25
- left: { type: "REFERENCE", value: 0 },
26
- right: { type: "NUMBER", value: 0 },
27
- },
28
- { type: "NUMBER", value: 1 },
29
- { type: "NUMBER", value: 2 },
30
- ],
31
- });
32
- });
33
-
34
- test("function without a opening parenthesis", () => {
35
- expect(() => parse(`SUM 5`)).toThrow("wrong function call");
36
- });
37
-
38
- test("function without a closing parenthesis", () => {
39
- expect(() => parse(`SUM(5`)).toThrow("wrong function call");
40
- });
41
-
42
- test("function without argument nor a closing parenthesis", () => {
43
- expect(() => parse(`SUM(`)).toThrow("wrong function call");
44
- });
45
-
46
- test("function with empty first argument nor a closing parenthesis", () => {
47
- expect(() => parse(`SUM(,`)).toThrow("invalid expression");
48
- });
49
-
50
- test("add a unknown token for empty arguments", () => {
51
- expect(parse("SUM(1,)")).toEqual({
52
- type: "FUNCALL",
53
- value: "SUM",
54
- args: [
55
- { type: "NUMBER", value: 1 },
56
- { type: "UNKNOWN", value: "" },
57
- ],
58
- });
59
- expect(parse("SUM(,1)")).toEqual({
60
- type: "FUNCALL",
61
- value: "SUM",
62
- args: [
63
- { type: "UNKNOWN", value: "" },
64
- { type: "NUMBER", value: 1 },
65
- ],
66
- });
67
- expect(parse("SUM(,)")).toEqual({
68
- type: "FUNCALL",
69
- value: "SUM",
70
- args: [
71
- { type: "UNKNOWN", value: "" },
72
- { type: "UNKNOWN", value: "" },
73
- ],
74
- });
75
- expect(parse("SUM(,,)")).toEqual({
76
- type: "FUNCALL",
77
- value: "SUM",
78
- args: [
79
- { type: "UNKNOWN", value: "" },
80
- { type: "UNKNOWN", value: "" },
81
- { type: "UNKNOWN", value: "" },
82
- ],
83
- });
84
- expect(parse("SUM(,,,1)")).toEqual({
85
- type: "FUNCALL",
86
- value: "SUM",
87
- args: [
88
- { type: "UNKNOWN", value: "" },
89
- { type: "UNKNOWN", value: "" },
90
- { type: "UNKNOWN", value: "" },
91
- { type: "NUMBER", value: 1 },
92
- ],
93
- });
94
- });
95
-
96
- test("can parse unary operations", () => {
97
- expect(parse("-1")).toEqual({
98
- type: "UNARY_OPERATION",
99
- value: "-",
100
- right: { type: "NUMBER", value: 1 },
101
- });
102
- expect(parse("+1")).toEqual({
103
- type: "UNARY_OPERATION",
104
- value: "+",
105
- right: { type: "NUMBER", value: 1 },
106
- });
107
- });
108
- test("can parse numeric values", () => {
109
- expect(parse("1")).toEqual({ type: "NUMBER", value: 1 });
110
- expect(parse("1.5")).toEqual({ type: "NUMBER", value: 1.5 });
111
- expect(parse("1.")).toEqual({ type: "NUMBER", value: 1 });
112
- expect(parse(".5")).toEqual({ type: "NUMBER", value: 0.5 });
113
- });
114
-
115
- test("can parse string without ending double quotes", () => {
116
- expect(parse('"hello')).toEqual({ type: "STRING", value: '"hello' });
117
- });
118
-
119
- test("can parse number expressed as percent", () => {
120
- expect(parse("1%")).toEqual({ type: "NUMBER", value: 0.01 });
121
- expect(parse("100%")).toEqual({ type: "NUMBER", value: 1 });
122
- expect(parse("50.0%")).toEqual({ type: "NUMBER", value: 0.5 });
123
- });
124
-
125
- test("can parse binary operations", () => {
126
- expect(parse("2-3")).toEqual({
127
- type: "BIN_OPERATION",
128
- value: "-",
129
- left: { type: "NUMBER", value: 2 },
130
- right: { type: "NUMBER", value: 3 },
131
- });
132
- });
133
-
134
- test("can parse concat operator", () => {
135
- expect(parse("|0|&|1|")).toEqual({
136
- type: "BIN_OPERATION",
137
- value: "&",
138
- left: { type: "REFERENCE", value: 0 },
139
- right: { type: "REFERENCE", value: 1 },
140
- });
141
- });
142
-
143
- test("AND", () => {
144
- expect(parse("=AND(true, false)")).toEqual({
145
- type: "FUNCALL",
146
- value: "AND",
147
- args: [
148
- { type: "BOOLEAN", value: true },
149
- { type: "BOOLEAN", value: false },
150
- ],
151
- });
152
- expect(parse("=AND(0, tRuE)")).toEqual({
153
- type: "FUNCALL",
154
- value: "AND",
155
- args: [
156
- { type: "NUMBER", value: 0 },
157
- { type: "BOOLEAN", value: true },
158
- ],
159
- });
160
- });
161
-
162
- test("can parse async functions", () => {
163
- expect(parse("=WAIT(12)")).toEqual({
164
- type: "ASYNC_FUNCALL",
165
- value: "WAIT",
166
- args: [{ type: "NUMBER", value: 12 }],
167
- });
168
- });
169
- });
170
-
171
- describe("parsing other stuff", () => {
172
- test("arbitrary text", () => {
173
- expect(() => parse("=undefined")).toThrow();
174
- });
175
- });
176
- describe("Converting AST to string", () => {
177
- test("Convert number", () => {
178
- expect(astToFormula(parse("1"), [])).toBe("1");
179
- });
180
- test("Convert string", () => {
181
- expect(astToFormula(parse(`"hello"`), [])).toBe(`"hello"`);
182
- });
183
- test("Convert boolean", () => {
184
- expect(astToFormula(parse("TRUE"), [])).toBe("TRUE");
185
- expect(astToFormula(parse("FALSE"), [])).toBe("FALSE");
186
- });
187
- test("Convert unary operator", () => {
188
- expect(astToFormula(parse("-45"), [])).toBe("-45");
189
- expect(astToFormula(parse("+45"), [])).toBe("+45");
190
- });
191
- test("Convert binary operator", () => {
192
- expect(astToFormula(parse("89-45"), [])).toBe("89-45");
193
- });
194
- test("Convert function", () => {
195
- expect(astToFormula(parse("SUM(5,9,8)"), [])).toBe("SUM(5,9,8)");
196
- expect(astToFormula(parse("-SUM(5,9,SUM(5,9,8))"), [])).toBe("-SUM(5,9,SUM(5,9,8))");
197
- });
198
- test("Convert normalized references", () => {
199
- let { text, dependencies } = normalize("A10");
200
- expect(astToFormula(parse(text), dependencies)).toBe("A10");
201
- ({ text, dependencies } = normalize("Sheet1!A10"));
202
- expect(astToFormula(parse(text), dependencies)).toBe("Sheet1!A10");
203
- ({ text, dependencies } = normalize("'Sheet 1'!A10"));
204
- expect(astToFormula(parse(text), dependencies)).toBe("'Sheet 1'!A10");
205
- ({ text, dependencies } = normalize("'Sheet 1'!A10:A11"));
206
- expect(astToFormula(parse(text), dependencies)).toBe("'Sheet 1'!A10:A11");
207
- ({ text, dependencies } = normalize("SUM(A1,A2)"));
208
- expect(astToFormula(parse(text), dependencies)).toBe("SUM(A1,A2)");
209
- });
210
- test("Convert normalized strings", () => {
211
- let { text, dependencies } = normalize('"R"');
212
- expect(astToFormula(parse(text), dependencies)).toBe('"R"');
213
- ({ text, dependencies } = normalize('CONCAT("R", "EM")'));
214
- expect(astToFormula(parse(text), dependencies)).toBe('CONCAT("R","EM")');
215
- });
216
- test("Convert normalized numbers", () => {
217
- let { text, dependencies } = normalize("5");
218
- expect(astToFormula(parse(text), dependencies)).toBe("5");
219
- ({ text, dependencies } = normalize("5+4"));
220
- expect(astToFormula(parse(text), dependencies)).toBe("5+4");
221
- ({ text, dependencies } = normalize("+5"));
222
- expect(astToFormula(parse(text), dependencies)).toBe("+5");
223
- });
224
- });