@odoo/o-spreadsheet 14.0.1 → 15.0.0

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 (203) hide show
  1. package/dist/o_spreadsheet.cjs.js +31302 -0
  2. package/dist/o_spreadsheet.d.ts +6604 -0
  3. package/dist/o_spreadsheet.esm.js +31257 -0
  4. package/dist/o_spreadsheet.iife.js +31304 -0
  5. package/dist/o_spreadsheet.iife.min.js +3071 -0
  6. package/package.json +57 -15
  7. package/.eslintrc.json +0 -20
  8. package/.husky/post-checkout +0 -9
  9. package/.husky/pre-commit +0 -39
  10. package/.prettierignore +0 -1
  11. package/astToFormula.patch +0 -0
  12. package/demo/data.js +0 -255
  13. package/demo/index.html +0 -9
  14. package/demo/libs/Chart.bundle.js +0 -20755
  15. package/demo/main.js +0 -101
  16. package/doc/readme.md +0 -46
  17. package/rollup.config.js +0 -23
  18. package/src/base_plugin.ts +0 -102
  19. package/src/components/autofill.ts +0 -146
  20. package/src/components/bottom_bar.ts +0 -199
  21. package/src/components/color_picker.ts +0 -150
  22. package/src/components/composer/autocomplete_dropdown.ts +0 -135
  23. package/src/components/composer/composer.ts +0 -487
  24. package/src/components/composer/content_editable_helper.ts +0 -146
  25. package/src/components/figures/chart.ts +0 -70
  26. package/src/components/figures/container.ts +0 -268
  27. package/src/components/figures/text_figure.ts +0 -36
  28. package/src/components/grid.ts +0 -635
  29. package/src/components/helpers/dom_helpers.ts +0 -7
  30. package/src/components/helpers/drag_and_drop.ts +0 -17
  31. package/src/components/icons.ts +0 -36
  32. package/src/components/index.ts +0 -1
  33. package/src/components/menu.ts +0 -288
  34. package/src/components/overlay.ts +0 -457
  35. package/src/components/scrollbar.ts +0 -22
  36. package/src/components/selection_input.ts +0 -168
  37. package/src/components/side_panel/cell_is_rule_editor.ts +0 -169
  38. package/src/components/side_panel/chart_panel.ts +0 -104
  39. package/src/components/side_panel/color_scale_rule_editor.ts +0 -145
  40. package/src/components/side_panel/conditional_formatting.ts +0 -396
  41. package/src/components/side_panel/side_panel.ts +0 -127
  42. package/src/components/side_panel/translations_terms.ts +0 -45
  43. package/src/components/spreadsheet.ts +0 -198
  44. package/src/components/top_bar.ts +0 -535
  45. package/src/constants.ts +0 -26
  46. package/src/data.ts +0 -149
  47. package/src/fonts.ts +0 -41
  48. package/src/formulas/compiler.ts +0 -258
  49. package/src/formulas/composer_tokenizer.ts +0 -70
  50. package/src/formulas/index.ts +0 -15
  51. package/src/formulas/normalize.ts +0 -52
  52. package/src/formulas/parser.ts +0 -280
  53. package/src/formulas/range_tokenizer.ts +0 -97
  54. package/src/formulas/tokenizer.ts +0 -242
  55. package/src/functions/arguments.ts +0 -114
  56. package/src/functions/dates.ts +0 -302
  57. package/src/functions/helpers.ts +0 -592
  58. package/src/functions/index.ts +0 -52
  59. package/src/functions/module_database.ts +0 -282
  60. package/src/functions/module_date.ts +0 -743
  61. package/src/functions/module_info.ts +0 -69
  62. package/src/functions/module_logical.ts +0 -208
  63. package/src/functions/module_lookup.ts +0 -185
  64. package/src/functions/module_math.ts +0 -1252
  65. package/src/functions/module_operators.ts +0 -291
  66. package/src/functions/module_statistical.ts +0 -882
  67. package/src/functions/module_text.ts +0 -370
  68. package/src/helpers/color.ts +0 -30
  69. package/src/helpers/coordinates.ts +0 -71
  70. package/src/helpers/grid_manipulation.ts +0 -68
  71. package/src/helpers/index.ts +0 -7
  72. package/src/helpers/misc.ts +0 -76
  73. package/src/helpers/numbers.ts +0 -117
  74. package/src/helpers/uuid.ts +0 -19
  75. package/src/helpers/zones.ts +0 -263
  76. package/src/history.ts +0 -186
  77. package/src/index.ts +0 -84
  78. package/src/model.ts +0 -261
  79. package/src/plugins/autofill.ts +0 -412
  80. package/src/plugins/chart.ts +0 -404
  81. package/src/plugins/clipboard.ts +0 -461
  82. package/src/plugins/conditional_format.ts +0 -445
  83. package/src/plugins/core.ts +0 -1502
  84. package/src/plugins/edition.ts +0 -157
  85. package/src/plugins/evaluation.ts +0 -428
  86. package/src/plugins/figures.ts +0 -123
  87. package/src/plugins/formatting.ts +0 -630
  88. package/src/plugins/highlight.ts +0 -187
  89. package/src/plugins/index.ts +0 -32
  90. package/src/plugins/merge.ts +0 -524
  91. package/src/plugins/renderer.ts +0 -571
  92. package/src/plugins/selection.ts +0 -562
  93. package/src/plugins/selection_inputs.ts +0 -326
  94. package/src/registries/autofill_modifiers.ts +0 -76
  95. package/src/registries/autofill_rules.ts +0 -98
  96. package/src/registries/figure_registry.ts +0 -30
  97. package/src/registries/index.ts +0 -7
  98. package/src/registries/menu_items_registry.ts +0 -110
  99. package/src/registries/menus/cell_menu_registry.ts +0 -79
  100. package/src/registries/menus/col_menu_registry.ts +0 -63
  101. package/src/registries/menus/index.ts +0 -6
  102. package/src/registries/menus/menu_items_actions.ts +0 -425
  103. package/src/registries/menus/row_menu_registry.ts +0 -63
  104. package/src/registries/menus/sheet_menu_registry.ts +0 -75
  105. package/src/registries/menus/topbar_menu_registry.ts +0 -204
  106. package/src/registries/side_panel_registry.ts +0 -26
  107. package/src/registries/topbar_component_registry.ts +0 -12
  108. package/src/registry.ts +0 -51
  109. package/src/translation.ts +0 -44
  110. package/src/types/autofill.ts +0 -48
  111. package/src/types/chart.ts +0 -26
  112. package/src/types/commands.ts +0 -716
  113. package/src/types/conditional_formatting.ts +0 -124
  114. package/src/types/env.ts +0 -13
  115. package/src/types/functions.ts +0 -35
  116. package/src/types/getters.ts +0 -103
  117. package/src/types/index.ts +0 -20
  118. package/src/types/misc.ts +0 -143
  119. package/src/types/rendering.ts +0 -41
  120. package/src/types/workbook_data.ts +0 -62
  121. package/tests/__mocks__/uuid.ts +0 -7
  122. package/tests/canvas.mock.ts +0 -34
  123. package/tests/components/__mocks__/content_editable_helper.ts +0 -79
  124. package/tests/components/__mocks__/scrollbar.ts +0 -18
  125. package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +0 -29
  126. package/tests/components/__snapshots__/bottom_bar_test.ts.snap +0 -75
  127. package/tests/components/__snapshots__/context_menu_test.ts.snap +0 -88
  128. package/tests/components/__snapshots__/grid_test.ts.snap +0 -60
  129. package/tests/components/__snapshots__/spreadsheet_test.ts.snap +0 -455
  130. package/tests/components/__snapshots__/top_bar_test.ts.snap +0 -705
  131. package/tests/components/autocomplete_dropdown_test.ts +0 -315
  132. package/tests/components/autofill_test.ts +0 -167
  133. package/tests/components/bottom_bar_test.ts +0 -262
  134. package/tests/components/chart_side_panel_test.ts +0 -123
  135. package/tests/components/composer_test.ts +0 -400
  136. package/tests/components/context_menu_test.ts +0 -825
  137. package/tests/components/grid_manipulation_test.ts +0 -163
  138. package/tests/components/grid_test.ts +0 -581
  139. package/tests/components/overlay_test.ts +0 -541
  140. package/tests/components/selection_input_test.ts +0 -259
  141. package/tests/components/side_panel_test.ts +0 -148
  142. package/tests/components/spreadsheet_test.ts +0 -194
  143. package/tests/components/top_bar_test.ts +0 -427
  144. package/tests/data_test.ts +0 -175
  145. package/tests/dom_helper.ts +0 -42
  146. package/tests/formulas/__snapshots__/compiler_test.ts.snap +0 -302
  147. package/tests/formulas/compiler_test.ts +0 -266
  148. package/tests/formulas/composer_tokenizer_test.ts +0 -137
  149. package/tests/formulas/expressions_test.ts +0 -57
  150. package/tests/formulas/formulas_test.ts +0 -80
  151. package/tests/formulas/normalize_test.ts +0 -82
  152. package/tests/formulas/parser_test.ts +0 -224
  153. package/tests/formulas/range_tokenizer_test.ts +0 -187
  154. package/tests/formulas/tokenizer_test.ts +0 -164
  155. package/tests/functions/arguments_test.ts +0 -141
  156. package/tests/functions/dates_test.ts +0 -1081
  157. package/tests/functions/functions_test.ts +0 -53
  158. package/tests/functions/helper_test.ts +0 -98
  159. package/tests/functions/module_database_test.ts +0 -311
  160. package/tests/functions/module_date_test.ts +0 -941
  161. package/tests/functions/module_info_test.ts +0 -139
  162. package/tests/functions/module_logical_test.ts +0 -369
  163. package/tests/functions/module_lookup_test.ts +0 -541
  164. package/tests/functions/module_math_test.ts +0 -2482
  165. package/tests/functions/module_operators_test.ts +0 -955
  166. package/tests/functions/module_statistical_test.ts +0 -2014
  167. package/tests/functions/module_text_test.ts +0 -667
  168. package/tests/helpers/coordinates_test.ts +0 -25
  169. package/tests/helpers/misc_test.ts +0 -95
  170. package/tests/helpers/numbers_test.ts +0 -196
  171. package/tests/helpers/zones_test.ts +0 -97
  172. package/tests/helpers.ts +0 -384
  173. package/tests/menu_items_registry_test.ts +0 -540
  174. package/tests/model/plugins/__snapshots__/chart_test.ts.snap +0 -90
  175. package/tests/model/plugins/chart_test.ts +0 -562
  176. package/tests/model_test.ts +0 -82
  177. package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +0 -131
  178. package/tests/plugins/__snapshots__/renderer_test.ts.snap +0 -371
  179. package/tests/plugins/autofill_test.ts +0 -457
  180. package/tests/plugins/borders_test.ts +0 -295
  181. package/tests/plugins/clipboard_test.ts +0 -1180
  182. package/tests/plugins/conditional_formatting_test.ts +0 -1253
  183. package/tests/plugins/core_test.ts +0 -614
  184. package/tests/plugins/edition_test.ts +0 -100
  185. package/tests/plugins/evaluation_async_test.ts +0 -332
  186. package/tests/plugins/evaluation_dates_test.ts +0 -79
  187. package/tests/plugins/evaluation_test.ts +0 -901
  188. package/tests/plugins/figures_test.ts +0 -327
  189. package/tests/plugins/formatting_test.ts +0 -391
  190. package/tests/plugins/grid_manipulation_test.ts +0 -1366
  191. package/tests/plugins/highlight_test.ts +0 -413
  192. package/tests/plugins/history_test.ts +0 -142
  193. package/tests/plugins/import_export_test.ts +0 -276
  194. package/tests/plugins/merges_test.ts +0 -490
  195. package/tests/plugins/navigation_test.ts +0 -181
  196. package/tests/plugins/plugins/evaluation_test.ts +0 -111
  197. package/tests/plugins/renderer_test.ts +0 -598
  198. package/tests/plugins/resizing_test.ts +0 -152
  199. package/tests/plugins/selection_input_test.ts +0 -409
  200. package/tests/plugins/selection_test.ts +0 -442
  201. package/tests/plugins/sheets_test.ts +0 -643
  202. package/tests/plugins/style_test.ts +0 -90
  203. package/tsconfig.json +0 -22
@@ -1,630 +0,0 @@
1
- import { BasePlugin } from "../base_plugin";
2
- import { DEFAULT_FONT, DEFAULT_FONT_SIZE, DEFAULT_FONT_WEIGHT } from "../constants";
3
- import { fontSizeMap } from "../fonts";
4
- import { maximumDecimalPlaces, stringify, toCartesian, toXC } from "../helpers/index";
5
- import { Border, BorderCommand, Cell, Command, Style, WorkbookData, Zone } from "../types/index";
6
-
7
- // -----------------------------------------------------------------------------
8
- // Constants / Types / Helpers
9
- // -----------------------------------------------------------------------------
10
-
11
- const commandToSides = {
12
- top: ["top"],
13
- left: ["left"],
14
- right: ["right"],
15
- bottom: ["bottom"],
16
- all: ["top", "left", "bottom", "right"],
17
- };
18
-
19
- const DEFAULT_STYLE: Style = {
20
- fillColor: "white",
21
- textColor: "black",
22
- fontSize: 11,
23
- };
24
-
25
- type FormatInfo = {
26
- border?: number;
27
- style?: number;
28
- format?: string;
29
- };
30
-
31
- function getTargetZone(zone: Zone, side: string): Zone {
32
- const { left, right, top, bottom } = zone;
33
- switch (side) {
34
- case "left":
35
- return { left, top, right: left, bottom };
36
- case "top":
37
- return { left, top, right, bottom: top };
38
- case "right":
39
- return { left: right, top, right, bottom };
40
- case "bottom":
41
- return { left, top: bottom, right, bottom };
42
- }
43
- return zone;
44
- }
45
-
46
- /**
47
- * Formatting plugin.
48
- *
49
- * This plugin manages all things related to a cell look:
50
- * - styles
51
- * - borders
52
- * - value formatters
53
- */
54
- export class FormattingPlugin extends BasePlugin {
55
- static getters = [
56
- "getCurrentStyle",
57
- "getCellWidth",
58
- "getTextWidth",
59
- "getCellHeight",
60
- "getCellStyle",
61
- "getCellBorder",
62
- ];
63
- private ctx = document.createElement("canvas").getContext("2d")!;
64
-
65
- private styles: { [key: number]: Style } = {};
66
- private borders: { [key: number]: Border } = {};
67
- private nextId: number = 1;
68
-
69
- // ---------------------------------------------------------------------------
70
- // Command Handling
71
- // ---------------------------------------------------------------------------
72
-
73
- handle(cmd: Command) {
74
- switch (cmd.type) {
75
- case "SET_FORMATTING":
76
- if (cmd.style) {
77
- this.setStyle(cmd.sheet, cmd.target, cmd.style);
78
- }
79
- if (cmd.border) {
80
- this.setBorder(cmd.sheet, cmd.target, cmd.border);
81
- }
82
- break;
83
- case "CLEAR_FORMATTING":
84
- this.clearFormatting(cmd.target);
85
- break;
86
- case "SET_FORMATTER":
87
- this.setFormatter(cmd.sheet, cmd.target, cmd.formatter);
88
- break;
89
- case "SET_DECIMAL":
90
- this.setDecimal(cmd.sheet, cmd.target, cmd.step);
91
- break;
92
- case "ADD_COLUMNS":
93
- const start_col = cmd.position === "before" ? cmd.column - 1 : cmd.column;
94
- const end_col = start_col + cmd.quantity + 1;
95
- this.onAddElements(start_col, end_col, true, cmd.position === "before");
96
- break;
97
- case "ADD_ROWS":
98
- const start_row = cmd.position === "before" ? cmd.row - 1 : cmd.row;
99
- const end_row = start_row + cmd.quantity + 1;
100
- this.onAddElements(start_row, end_row, false, cmd.position === "before");
101
- break;
102
- }
103
- }
104
-
105
- // ---------------------------------------------------------------------------
106
- // Getters
107
- // ---------------------------------------------------------------------------
108
-
109
- getCellWidth(cell: Cell): number {
110
- const styleId = cell.style || 0;
111
- const text = this.getters.getCellText(cell);
112
- return this.getTextWidth(text, styleId);
113
- }
114
-
115
- getTextWidth(text: string, styleId: number = 0): number {
116
- const style = this.styles[styleId];
117
- const italic = style.italic ? "italic " : "";
118
- const weight = style.bold ? "bold" : DEFAULT_FONT_WEIGHT;
119
- const sizeInPt = style.fontSize || DEFAULT_FONT_SIZE;
120
- const size = fontSizeMap[sizeInPt];
121
- this.ctx.font = `${italic}${weight} ${size}px ${DEFAULT_FONT}`;
122
- return this.ctx.measureText(text).width;
123
- }
124
-
125
- getCellHeight(cell: Cell): number {
126
- const style = this.styles[cell ? cell.style || 0 : 0];
127
- const sizeInPt = style.fontSize || DEFAULT_FONT_SIZE;
128
- return fontSizeMap[sizeInPt];
129
- }
130
-
131
- getCellStyle(cell: Cell): Style {
132
- return cell.style ? this.styles[cell.style] : {};
133
- }
134
-
135
- getCellBorder(cell: Cell): Border | null {
136
- return cell.border ? this.borders[cell.border] : null;
137
- }
138
-
139
- getCurrentStyle(): Style {
140
- const cell = this.getters.getActiveCell();
141
- return cell && cell.style ? this.styles[cell.style] : {};
142
- }
143
-
144
- // ---------------------------------------------------------------------------
145
- // Styles
146
- // ---------------------------------------------------------------------------
147
-
148
- private setStyle(sheet: string, target: Zone[], style: Style) {
149
- for (let zone of target) {
150
- for (let col = zone.left; col <= zone.right; col++) {
151
- for (let row = zone.top; row <= zone.bottom; row++) {
152
- this.setStyleToCell(col, row, style);
153
- }
154
- }
155
- }
156
- }
157
-
158
- private setStyleToCell(col: number, row: number, style) {
159
- const cell = this.getters.getCell(col, row);
160
- const currentStyle = cell && cell.style ? this.styles[cell.style] : {};
161
- const nextStyle = Object.assign({}, currentStyle, style);
162
- const id = this.registerStyle(nextStyle);
163
- this.dispatch("UPDATE_CELL", {
164
- sheet: this.workbook.activeSheet.id,
165
- col,
166
- row,
167
- style: id,
168
- });
169
- }
170
-
171
- private registerStyle(style) {
172
- const strStyle = stringify(style);
173
- for (let k in this.styles) {
174
- if (stringify(this.styles[k]) === strStyle) {
175
- return parseInt(k, 10);
176
- }
177
- }
178
- const id = this.nextId++;
179
- this.styles[id] = style;
180
- return id;
181
- }
182
-
183
- // ---------------------------------------------------------------------------
184
- // Borders
185
- // ---------------------------------------------------------------------------
186
-
187
- private setBorder(sheet: string, zones: Zone[], command: BorderCommand) {
188
- // this object aggregate the desired final border command for a cell
189
- const borderMap: { [xc: string]: number } = {};
190
- for (let zone of zones) {
191
- this.aggregateBorderCommands(sheet, zone, command, borderMap);
192
- }
193
- for (let [xc, borderId] of Object.entries(borderMap)) {
194
- const [col, row] = toCartesian(xc);
195
- const cell = this.getters.getCell(col, row);
196
- const current = (cell && cell.border) || 0;
197
- if (current !== borderId) {
198
- this.dispatch("UPDATE_CELL", {
199
- sheet: sheet,
200
- col,
201
- row,
202
- border: borderId,
203
- });
204
- }
205
- }
206
- }
207
-
208
- private aggregateBorderCommands(
209
- sheet: string,
210
- zone: Zone,
211
- command: BorderCommand,
212
- borderMap: { [xc: string]: number }
213
- ) {
214
- if (command === "clear") {
215
- for (let row = zone.top; row <= zone.bottom; row++) {
216
- for (let col = zone.left; col <= zone.right; col++) {
217
- this.clearBorder(sheet, col, row, borderMap);
218
- }
219
- }
220
- return;
221
- }
222
- if (command === "external") {
223
- this.aggregateBorderCommands(sheet, zone, "left", borderMap);
224
- this.aggregateBorderCommands(sheet, zone, "right", borderMap);
225
- this.aggregateBorderCommands(sheet, zone, "top", borderMap);
226
- this.aggregateBorderCommands(sheet, zone, "bottom", borderMap);
227
- return;
228
- }
229
- if (command === "hv") {
230
- this.aggregateBorderCommands(sheet, zone, "h", borderMap);
231
- this.aggregateBorderCommands(sheet, zone, "v", borderMap);
232
- return;
233
- }
234
- const { left, top, right, bottom } = zone;
235
- if (command === "h") {
236
- for (let r = top + 1; r <= bottom; r++) {
237
- this.aggregateBorderCommands(sheet, { left, top: r, right, bottom: r }, "top", borderMap);
238
- }
239
- return;
240
- }
241
- if (command === "v") {
242
- for (let c = left + 1; c <= right; c++) {
243
- this.aggregateBorderCommands(sheet, { left: c, top, right: c, bottom }, "left", borderMap);
244
- }
245
- return;
246
- }
247
-
248
- const border: Border = {};
249
- for (let side of commandToSides[command]) {
250
- border[side] = ["thin", "#000"];
251
- }
252
-
253
- const target = getTargetZone(zone, command);
254
- for (let row = target.top; row <= target.bottom; row++) {
255
- for (let col = target.left; col <= target.right; col++) {
256
- this.setBorderToMap(sheet, col, row, border, borderMap);
257
- }
258
- }
259
- }
260
-
261
- private clearBorder(
262
- sheet: string,
263
- col: number,
264
- row: number,
265
- borderMap: { [xc: string]: number }
266
- ) {
267
- const cell = this.getters.getCell(col, row);
268
- const xc = cell ? cell.xc : toXC(col, row);
269
- borderMap[xc] = 0;
270
-
271
- if (col > 0) {
272
- this.clearSide(sheet, col - 1, row, "right", borderMap);
273
- }
274
- if (row > 0) {
275
- this.clearSide(sheet, col, row - 1, "bottom", borderMap);
276
- }
277
- if (col < this.workbook.activeSheet.cols.length - 1) {
278
- this.clearSide(sheet, col + 1, row, "left", borderMap);
279
- }
280
- if (row < this.workbook.activeSheet.rows.length - 1) {
281
- this.clearSide(sheet, col, row + 1, "top", borderMap);
282
- }
283
- }
284
-
285
- private clearSide(
286
- sheet: string,
287
- col: number,
288
- row: number,
289
- side: string,
290
- borderMap: { [xc: string]: number }
291
- ) {
292
- const cell = this.getters.getCell(col, row);
293
- const xc = cell ? cell.xc : toXC(col, row);
294
- const currentBorderId = xc in borderMap ? borderMap[xc] : cell && cell.border ? cell.border : 0;
295
- const currentBorder = this.borders[currentBorderId] || {};
296
- if (side in currentBorder) {
297
- const newBorder = Object.assign({}, currentBorder);
298
- delete newBorder[side];
299
- borderMap[xc] = this.registerBorder(newBorder);
300
- }
301
- }
302
-
303
- private setBorderToMap(
304
- sheet: string,
305
- col: number,
306
- row: number,
307
- border: Border,
308
- borderMap: { [xc: string]: number }
309
- ) {
310
- const cell = this.getters.getCell(col, row);
311
- const xc = cell ? cell.xc : toXC(col, row);
312
- const currentBorderId = xc in borderMap ? borderMap[xc] : cell && cell.border ? cell.border : 0;
313
- const currentBorder = this.borders[currentBorderId] || {};
314
- const nextBorder = Object.assign({}, currentBorder, border);
315
- const id = this.registerBorder(nextBorder);
316
- borderMap[xc] = id;
317
- }
318
-
319
- private registerBorder(border: Border): number {
320
- if (!Object.keys(border).length) {
321
- return 0;
322
- }
323
- const strBorder = stringify(border);
324
- for (let k in this.borders) {
325
- if (stringify(this.borders[k]) === strBorder) {
326
- return parseInt(k, 10);
327
- }
328
- }
329
- const id = this.nextId++;
330
- this.borders[id] = border;
331
- return id;
332
- }
333
-
334
- // ---------------------------------------------------------------------------
335
- // Clear formatting
336
- // ---------------------------------------------------------------------------
337
-
338
- /**
339
- * Note that here, formatting refers to styles+border, not value formatters
340
- */
341
- private clearFormatting(zones: Zone[]) {
342
- for (let zone of zones) {
343
- for (let col = zone.left; col <= zone.right; col++) {
344
- for (let row = zone.top; row <= zone.bottom; row++) {
345
- this.dispatch("UPDATE_CELL", {
346
- sheet: this.workbook.activeSheet.id,
347
- col,
348
- row,
349
- style: 0,
350
- border: 0,
351
- });
352
- }
353
- }
354
- }
355
- }
356
-
357
- // ---------------------------------------------------------------------------
358
- // Formatters
359
- // ---------------------------------------------------------------------------
360
-
361
- private setFormatter(sheet: string, zones: Zone[], format: string) {
362
- for (let zone of zones) {
363
- for (let row = zone.top; row <= zone.bottom; row++) {
364
- for (let col = zone.left; col <= zone.right; col++) {
365
- this.dispatch("UPDATE_CELL", {
366
- sheet,
367
- col,
368
- row,
369
- format,
370
- });
371
- }
372
- }
373
- }
374
- }
375
-
376
- /**
377
- * This function allows to adjust the quantity of decimal places after a decimal
378
- * point on cells containing number value. It does this by changing the cells
379
- * format. Values aren't modified.
380
- *
381
- * The change of the decimal quantity is done one by one, the sign of the step
382
- * variable indicates whether we are increasing or decreasing.
383
- *
384
- * If several cells are in the zone, the format resulting from the change of the
385
- * first cell (with number type) will be applied to the whole zone.
386
- */
387
- private setDecimal(sheet: string, zones: Zone[], step: number) {
388
- // Find the first cell with a number value and get the format
389
- const numberFormat = this.searchNumberFormat(zones);
390
- if (numberFormat !== undefined) {
391
- // Depending on the step sign, increase or decrease the decimal representation
392
- // of the format
393
- const newFormat = this.changeDecimalFormat(numberFormat, step);
394
- // Aply the new format on the whole zone
395
- this.setFormatter(sheet, zones, newFormat!);
396
- }
397
- }
398
-
399
- /**
400
- * Take a range of cells and return the format of the first cell containing a
401
- * number value. Returns a default format if the cell hasn't format. Returns
402
- * undefined if no number value in the range.
403
- */
404
- private searchNumberFormat(zones: Zone[]): string | undefined {
405
- for (let zone of zones) {
406
- for (let row = zone.top; row <= zone.bottom; row++) {
407
- for (let col = zone.left; col <= zone.right; col++) {
408
- const cell = this.getters.getCell(col, row);
409
- if (
410
- cell &&
411
- (cell.type === "number" || (cell.type === "formula" && typeof cell.value === "number"))
412
- ) {
413
- return cell.format || this.setDefaultNumberFormat(cell.value);
414
- }
415
- }
416
- }
417
- }
418
- return undefined;
419
- }
420
-
421
- /**
422
- * Function used to give the default format of a cell with a number for value.
423
- * It is considered that the default format of a number is 0 followed by as many
424
- * 0 as there are decimal places.
425
- *
426
- * Exemple:
427
- * - 1 --> '0'
428
- * - 123 --> '0'
429
- * - 12345 --> '0'
430
- * - 42.1 --> '0.0'
431
- * - 456.0001 --> '0.0000'
432
- */
433
- private setDefaultNumberFormat(cellValue: number): string {
434
- const strValue = cellValue.toString();
435
- const parts = strValue.split(".");
436
- if (parts.length === 1) {
437
- return "0";
438
- }
439
- return "0." + Array(parts[1].length + 1).join("0");
440
- }
441
-
442
- /**
443
- * This function take a cell format representation and return a new format representtion
444
- * with more or lesse decimal places.
445
- *
446
- * If the format doesn't look like a digital format (means that not contain '0')
447
- * or if this one cannot be increased or decreased, the returned format will be
448
- * the same.
449
- *
450
- * This function aims to work with all possible formats as well as custom formats.
451
- *
452
- * Examples of format changed by this function:
453
- * - "0" (step = 1) --> "0.0"
454
- * - "0.000%" (step = 1) --> "0.0000%"
455
- * - "0.00" (step = -1) --> "0.0"
456
- * - "0%" (step = -1) --> "0%"
457
- * - "#,##0.0" (step = -1) --> "#,##0"
458
- * - "#,##0;0.0%;0.000" (step = 1) --> "#,##0.0;0.00%;0.0000"
459
- */
460
- private changeDecimalFormat(format: string, step: number): string {
461
- const sign = Math.sign(step);
462
- // According to the representation of the cell format. A format can contain
463
- // up to 4 sub-formats which can be applied depending on the value of the cell
464
- // (among positive / negative / zero / text), each of these sub-format is separated
465
- // by ';' in the format. We need to make the change on each sub-format.
466
- const subFormats = format.split(";");
467
- let newSubFormats: string[] = [];
468
-
469
- for (let subFormat of subFormats) {
470
- const decimalPointPosition = subFormat.indexOf(".");
471
- const exponentPosition = subFormat.toUpperCase().indexOf("E");
472
- let newSubFormat: string;
473
-
474
- // the 1st step is to find the part of the zeros located before the
475
- // exponent (when existed)
476
- const subPart = exponentPosition > -1 ? subFormat.slice(0, exponentPosition) : subFormat;
477
- const zerosAfterDecimal =
478
- decimalPointPosition > -1 ? subPart.slice(decimalPointPosition).match(/0/g)!.length : 0;
479
-
480
- // the 2nd step is to add (or remove) zero after the last zeros obtained in
481
- // step 1
482
- const lastZeroPosition = subPart.lastIndexOf("0");
483
- if (lastZeroPosition > -1) {
484
- if (sign > 0) {
485
- // in this case we want to add decimal information
486
- if (zerosAfterDecimal < maximumDecimalPlaces) {
487
- newSubFormat =
488
- subFormat.slice(0, lastZeroPosition + 1) +
489
- (zerosAfterDecimal === 0 ? ".0" : "0") +
490
- subFormat.slice(lastZeroPosition + 1);
491
- } else {
492
- newSubFormat = subFormat;
493
- }
494
- } else {
495
- // in this case we want to remove decimal information
496
- if (zerosAfterDecimal > 0) {
497
- // remove last zero
498
- newSubFormat =
499
- subFormat.slice(0, lastZeroPosition) + subFormat.slice(lastZeroPosition + 1);
500
- // if a zero always exist after decimal point else remove decimal point
501
- if (zerosAfterDecimal === 1) {
502
- newSubFormat =
503
- newSubFormat.slice(0, decimalPointPosition) +
504
- newSubFormat.slice(decimalPointPosition + 1);
505
- }
506
- } else {
507
- // zero after decimal isn't present, we can't remove zero
508
- newSubFormat = subFormat;
509
- }
510
- }
511
- } else {
512
- // no zeros are present in this format, we do nothing
513
- newSubFormat = subFormat;
514
- }
515
- newSubFormats.push(newSubFormat);
516
- }
517
- return newSubFormats.join(";");
518
- }
519
-
520
- // ---------------------------------------------------------------------------
521
- // Grid Manipulation
522
- // ---------------------------------------------------------------------------
523
-
524
- /**
525
- * This function computes the style/border of a row/col based on the neighbours.
526
- *
527
- * @param index the index of the row/col of which we will change the style
528
- * @param isColumn true if element is a column, false if row
529
- * @param upper true if the style of the upper row/col should be used, false, if the lower should be used
530
- */
531
- private onAddElements(start: number, end: number, isColumn: boolean, upper: boolean) {
532
- const length = isColumn
533
- ? this.workbook.activeSheet.rows.length
534
- : this.workbook.activeSheet.cols.length;
535
- const index = start + 1;
536
- for (let x = 0; x < length; x++) {
537
- const xc = isColumn ? toXC(index, x) : toXC(x, index);
538
- if (this.getters.isInMerge(xc)) {
539
- continue;
540
- }
541
- const format: FormatInfo = {};
542
- let lowerFormat: FormatInfo = isColumn
543
- ? this.getFormat(toXC(start, x))
544
- : this.getFormat(toXC(x, start));
545
- let upperFormat: FormatInfo = isColumn
546
- ? this.getFormat(toXC(end, x))
547
- : this.getFormat(toXC(x, end));
548
- if (upper) {
549
- if (upperFormat.style) {
550
- format["style"] = upperFormat.style;
551
- }
552
- if (upperFormat.format) {
553
- format["format"] = upperFormat.format;
554
- }
555
- } else {
556
- if (lowerFormat.style) {
557
- format["style"] = lowerFormat.style;
558
- }
559
- if (lowerFormat.format) {
560
- format["format"] = lowerFormat.format;
561
- }
562
- }
563
- if (upperFormat.border && upperFormat.border === lowerFormat.border) {
564
- format["border"] = upperFormat.border;
565
- }
566
- if (Object.keys(format).length !== 0) {
567
- for (let i = index; i < end; i++) {
568
- this.dispatch("UPDATE_CELL", {
569
- sheet: this.workbook.activeSheet.id,
570
- col: isColumn ? i : x,
571
- row: isColumn ? x : i,
572
- style: format.style,
573
- border: format.border,
574
- format: format.format,
575
- });
576
- }
577
- }
578
- }
579
- }
580
-
581
- /**
582
- * gets the currently used style/border of a cell based on it's coordinates
583
- *
584
- * @param x column number of a cell
585
- * @param y row number of a cell
586
- */
587
- private getFormat(xc: string): FormatInfo {
588
- const format: FormatInfo = {};
589
- xc = this.getters.getMainCell(xc);
590
- if (xc in this.workbook.activeSheet.cells) {
591
- if (this.workbook.activeSheet.cells[xc].border) {
592
- format["border"] = this.workbook.activeSheet.cells[xc].border;
593
- }
594
- if (this.workbook.activeSheet.cells[xc].style) {
595
- format["style"] = this.workbook.activeSheet.cells[xc].style;
596
- }
597
- if (this.workbook.activeSheet.cells[xc].format) {
598
- format["format"] = this.workbook.activeSheet.cells[xc].format;
599
- }
600
- }
601
- return format;
602
- }
603
-
604
- // ---------------------------------------------------------------------------
605
- // Import/Export
606
- // ---------------------------------------------------------------------------
607
-
608
- import(data: WorkbookData) {
609
- if (data.styles) {
610
- this.styles = data.styles;
611
- }
612
- this.styles[0] = Object.assign({}, DEFAULT_STYLE, this.styles[0]);
613
- if (data.borders) {
614
- this.borders = data.borders;
615
- }
616
- let nextId = 1;
617
- for (let k in this.styles) {
618
- nextId = Math.max(k as any, nextId);
619
- }
620
- for (let k in this.borders) {
621
- nextId = Math.max(k as any, nextId);
622
- }
623
- this.nextId = nextId + 1;
624
- }
625
-
626
- export(data: WorkbookData) {
627
- data.styles = this.styles;
628
- data.borders = this.borders;
629
- }
630
- }