@odoo/o-spreadsheet 14.0.0 → 14.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 (198) 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 +3 -17
  12. package/rollup.config.js +23 -0
  13. package/src/base_plugin.ts +102 -0
  14. package/src/components/autofill.ts +146 -0
  15. package/src/components/bottom_bar.ts +199 -0
  16. package/src/components/color_picker.ts +150 -0
  17. package/src/components/composer/autocomplete_dropdown.ts +135 -0
  18. package/src/components/composer/composer.ts +487 -0
  19. package/src/components/composer/content_editable_helper.ts +146 -0
  20. package/src/components/figures/chart.ts +70 -0
  21. package/src/components/figures/container.ts +268 -0
  22. package/src/components/figures/text_figure.ts +36 -0
  23. package/src/components/grid.ts +635 -0
  24. package/src/components/helpers/dom_helpers.ts +7 -0
  25. package/src/components/helpers/drag_and_drop.ts +17 -0
  26. package/src/components/icons.ts +36 -0
  27. package/src/components/index.ts +1 -0
  28. package/src/components/menu.ts +288 -0
  29. package/src/components/overlay.ts +457 -0
  30. package/src/components/scrollbar.ts +22 -0
  31. package/src/components/selection_input.ts +168 -0
  32. package/src/components/side_panel/cell_is_rule_editor.ts +169 -0
  33. package/src/components/side_panel/chart_panel.ts +104 -0
  34. package/src/components/side_panel/color_scale_rule_editor.ts +145 -0
  35. package/src/components/side_panel/conditional_formatting.ts +396 -0
  36. package/src/components/side_panel/side_panel.ts +127 -0
  37. package/src/components/side_panel/translations_terms.ts +45 -0
  38. package/src/components/spreadsheet.ts +198 -0
  39. package/src/components/top_bar.ts +535 -0
  40. package/src/constants.ts +26 -0
  41. package/src/data.ts +149 -0
  42. package/src/fonts.ts +41 -0
  43. package/src/formulas/compiler.ts +258 -0
  44. package/src/formulas/composer_tokenizer.ts +70 -0
  45. package/src/formulas/index.ts +15 -0
  46. package/src/formulas/normalize.ts +52 -0
  47. package/src/formulas/parser.ts +280 -0
  48. package/src/formulas/range_tokenizer.ts +97 -0
  49. package/src/formulas/tokenizer.ts +242 -0
  50. package/src/functions/arguments.ts +114 -0
  51. package/src/functions/dates.ts +302 -0
  52. package/src/functions/helpers.ts +592 -0
  53. package/src/functions/index.ts +52 -0
  54. package/src/functions/module_database.ts +282 -0
  55. package/src/functions/module_date.ts +743 -0
  56. package/src/functions/module_info.ts +69 -0
  57. package/src/functions/module_logical.ts +208 -0
  58. package/src/functions/module_lookup.ts +185 -0
  59. package/src/functions/module_math.ts +1252 -0
  60. package/src/functions/module_operators.ts +291 -0
  61. package/src/functions/module_statistical.ts +882 -0
  62. package/src/functions/module_text.ts +370 -0
  63. package/src/helpers/color.ts +30 -0
  64. package/src/helpers/coordinates.ts +71 -0
  65. package/src/helpers/grid_manipulation.ts +68 -0
  66. package/src/helpers/index.ts +7 -0
  67. package/src/helpers/misc.ts +76 -0
  68. package/src/helpers/numbers.ts +117 -0
  69. package/src/helpers/uuid.ts +19 -0
  70. package/src/helpers/zones.ts +263 -0
  71. package/src/history.ts +186 -0
  72. package/src/index.ts +84 -0
  73. package/src/model.ts +261 -0
  74. package/src/plugins/autofill.ts +412 -0
  75. package/src/plugins/chart.ts +404 -0
  76. package/src/plugins/clipboard.ts +461 -0
  77. package/src/plugins/conditional_format.ts +445 -0
  78. package/src/plugins/core.ts +1502 -0
  79. package/src/plugins/edition.ts +157 -0
  80. package/src/plugins/evaluation.ts +428 -0
  81. package/src/plugins/figures.ts +123 -0
  82. package/src/plugins/formatting.ts +630 -0
  83. package/src/plugins/highlight.ts +187 -0
  84. package/src/plugins/index.ts +32 -0
  85. package/src/plugins/merge.ts +524 -0
  86. package/src/plugins/renderer.ts +571 -0
  87. package/src/plugins/selection.ts +562 -0
  88. package/src/plugins/selection_inputs.ts +326 -0
  89. package/src/registries/autofill_modifiers.ts +76 -0
  90. package/src/registries/autofill_rules.ts +98 -0
  91. package/src/registries/figure_registry.ts +30 -0
  92. package/src/registries/index.ts +7 -0
  93. package/src/registries/menu_items_registry.ts +110 -0
  94. package/src/registries/menus/cell_menu_registry.ts +79 -0
  95. package/src/registries/menus/col_menu_registry.ts +63 -0
  96. package/src/registries/menus/index.ts +6 -0
  97. package/src/registries/menus/menu_items_actions.ts +425 -0
  98. package/src/registries/menus/row_menu_registry.ts +63 -0
  99. package/src/registries/menus/sheet_menu_registry.ts +75 -0
  100. package/src/registries/menus/topbar_menu_registry.ts +204 -0
  101. package/src/registries/side_panel_registry.ts +26 -0
  102. package/src/registries/topbar_component_registry.ts +12 -0
  103. package/src/registry.ts +51 -0
  104. package/src/translation.ts +44 -0
  105. package/src/types/autofill.ts +48 -0
  106. package/src/types/chart.ts +26 -0
  107. package/src/types/commands.ts +716 -0
  108. package/src/types/conditional_formatting.ts +124 -0
  109. package/src/types/env.ts +13 -0
  110. package/src/types/functions.ts +35 -0
  111. package/src/types/getters.ts +103 -0
  112. package/src/types/index.ts +20 -0
  113. package/src/types/misc.ts +143 -0
  114. package/src/types/rendering.ts +41 -0
  115. package/src/types/workbook_data.ts +62 -0
  116. package/tests/__mocks__/uuid.ts +7 -0
  117. package/tests/canvas.mock.ts +34 -0
  118. package/tests/components/__mocks__/content_editable_helper.ts +79 -0
  119. package/tests/components/__mocks__/scrollbar.ts +18 -0
  120. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +29 -0
  121. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +75 -0
  122. package/tests/components/__snapshots__/context_menu_test.ts.snap +88 -0
  123. package/tests/components/__snapshots__/grid_test.ts.snap +60 -0
  124. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +455 -0
  125. package/tests/components/__snapshots__/top_bar_test.ts.snap +705 -0
  126. package/tests/components/autocomplete_dropdown_test.ts +315 -0
  127. package/tests/components/autofill_test.ts +167 -0
  128. package/tests/components/bottom_bar_test.ts +262 -0
  129. package/tests/components/chart_side_panel_test.ts +123 -0
  130. package/tests/components/composer_test.ts +400 -0
  131. package/tests/components/context_menu_test.ts +825 -0
  132. package/tests/components/grid_manipulation_test.ts +163 -0
  133. package/tests/components/grid_test.ts +581 -0
  134. package/tests/components/overlay_test.ts +541 -0
  135. package/tests/components/selection_input_test.ts +259 -0
  136. package/tests/components/side_panel_test.ts +148 -0
  137. package/tests/components/spreadsheet_test.ts +194 -0
  138. package/tests/components/top_bar_test.ts +427 -0
  139. package/tests/data_test.ts +175 -0
  140. package/tests/dom_helper.ts +42 -0
  141. package/tests/formulas/__snapshots__/compiler_test.ts.snap +302 -0
  142. package/tests/formulas/compiler_test.ts +266 -0
  143. package/tests/formulas/composer_tokenizer_test.ts +137 -0
  144. package/tests/formulas/expressions_test.ts +57 -0
  145. package/tests/formulas/formulas_test.ts +80 -0
  146. package/tests/formulas/normalize_test.ts +82 -0
  147. package/tests/formulas/parser_test.ts +224 -0
  148. package/tests/formulas/range_tokenizer_test.ts +187 -0
  149. package/tests/formulas/tokenizer_test.ts +164 -0
  150. package/tests/functions/arguments_test.ts +141 -0
  151. package/tests/functions/dates_test.ts +1081 -0
  152. package/tests/functions/functions_test.ts +53 -0
  153. package/tests/functions/helper_test.ts +98 -0
  154. package/tests/functions/module_database_test.ts +311 -0
  155. package/tests/functions/module_date_test.ts +941 -0
  156. package/tests/functions/module_info_test.ts +139 -0
  157. package/tests/functions/module_logical_test.ts +369 -0
  158. package/tests/functions/module_lookup_test.ts +541 -0
  159. package/tests/functions/module_math_test.ts +2482 -0
  160. package/tests/functions/module_operators_test.ts +955 -0
  161. package/tests/functions/module_statistical_test.ts +2014 -0
  162. package/tests/functions/module_text_test.ts +667 -0
  163. package/tests/helpers/coordinates_test.ts +25 -0
  164. package/tests/helpers/misc_test.ts +95 -0
  165. package/tests/helpers/numbers_test.ts +196 -0
  166. package/tests/helpers/zones_test.ts +97 -0
  167. package/tests/helpers.ts +384 -0
  168. package/tests/menu_items_registry_test.ts +540 -0
  169. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +90 -0
  170. package/tests/model/plugins/chart_test.ts +562 -0
  171. package/tests/model_test.ts +82 -0
  172. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +131 -0
  173. package/tests/plugins/__snapshots__/renderer_test.ts.snap +371 -0
  174. package/tests/plugins/autofill_test.ts +457 -0
  175. package/tests/plugins/borders_test.ts +295 -0
  176. package/tests/plugins/clipboard_test.ts +1180 -0
  177. package/tests/plugins/conditional_formatting_test.ts +1253 -0
  178. package/tests/plugins/core_test.ts +614 -0
  179. package/tests/plugins/edition_test.ts +100 -0
  180. package/tests/plugins/evaluation_async_test.ts +332 -0
  181. package/tests/plugins/evaluation_dates_test.ts +79 -0
  182. package/tests/plugins/evaluation_test.ts +901 -0
  183. package/tests/plugins/figures_test.ts +327 -0
  184. package/tests/plugins/formatting_test.ts +391 -0
  185. package/tests/plugins/grid_manipulation_test.ts +1366 -0
  186. package/tests/plugins/highlight_test.ts +413 -0
  187. package/tests/plugins/history_test.ts +142 -0
  188. package/tests/plugins/import_export_test.ts +276 -0
  189. package/tests/plugins/merges_test.ts +490 -0
  190. package/tests/plugins/navigation_test.ts +181 -0
  191. package/tests/plugins/plugins/evaluation_test.ts +111 -0
  192. package/tests/plugins/renderer_test.ts +598 -0
  193. package/tests/plugins/resizing_test.ts +152 -0
  194. package/tests/plugins/selection_input_test.ts +409 -0
  195. package/tests/plugins/selection_test.ts +442 -0
  196. package/tests/plugins/sheets_test.ts +643 -0
  197. package/tests/plugins/style_test.ts +90 -0
  198. package/tsconfig.json +22 -0
@@ -0,0 +1,137 @@
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
+ });
@@ -0,0 +1,57 @@
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
+ });
@@ -0,0 +1,80 @@
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
+ });
@@ -0,0 +1,82 @@
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
+ });
@@ -0,0 +1,224 @@
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
+ });