@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.
- package/.eslintrc.json +20 -0
- package/.husky/post-checkout +9 -0
- package/.husky/pre-commit +39 -0
- package/.prettierignore +1 -0
- package/astToFormula.patch +0 -0
- package/demo/data.js +255 -0
- package/demo/index.html +9 -0
- package/demo/libs/Chart.bundle.js +20755 -0
- package/demo/main.js +101 -0
- package/doc/readme.md +46 -0
- package/package.json +3 -17
- package/rollup.config.js +23 -0
- package/src/base_plugin.ts +102 -0
- package/src/components/autofill.ts +146 -0
- package/src/components/bottom_bar.ts +199 -0
- package/src/components/color_picker.ts +150 -0
- package/src/components/composer/autocomplete_dropdown.ts +135 -0
- package/src/components/composer/composer.ts +487 -0
- package/src/components/composer/content_editable_helper.ts +146 -0
- package/src/components/figures/chart.ts +70 -0
- package/src/components/figures/container.ts +268 -0
- package/src/components/figures/text_figure.ts +36 -0
- package/src/components/grid.ts +635 -0
- package/src/components/helpers/dom_helpers.ts +7 -0
- package/src/components/helpers/drag_and_drop.ts +17 -0
- package/src/components/icons.ts +36 -0
- package/src/components/index.ts +1 -0
- package/src/components/menu.ts +288 -0
- package/src/components/overlay.ts +457 -0
- package/src/components/scrollbar.ts +22 -0
- package/src/components/selection_input.ts +168 -0
- package/src/components/side_panel/cell_is_rule_editor.ts +169 -0
- package/src/components/side_panel/chart_panel.ts +104 -0
- package/src/components/side_panel/color_scale_rule_editor.ts +145 -0
- package/src/components/side_panel/conditional_formatting.ts +396 -0
- package/src/components/side_panel/side_panel.ts +127 -0
- package/src/components/side_panel/translations_terms.ts +45 -0
- package/src/components/spreadsheet.ts +198 -0
- package/src/components/top_bar.ts +535 -0
- package/src/constants.ts +26 -0
- package/src/data.ts +149 -0
- package/src/fonts.ts +41 -0
- package/src/formulas/compiler.ts +258 -0
- package/src/formulas/composer_tokenizer.ts +70 -0
- package/src/formulas/index.ts +15 -0
- package/src/formulas/normalize.ts +52 -0
- package/src/formulas/parser.ts +280 -0
- package/src/formulas/range_tokenizer.ts +97 -0
- package/src/formulas/tokenizer.ts +242 -0
- package/src/functions/arguments.ts +114 -0
- package/src/functions/dates.ts +302 -0
- package/src/functions/helpers.ts +592 -0
- package/src/functions/index.ts +52 -0
- package/src/functions/module_database.ts +282 -0
- package/src/functions/module_date.ts +743 -0
- package/src/functions/module_info.ts +69 -0
- package/src/functions/module_logical.ts +208 -0
- package/src/functions/module_lookup.ts +185 -0
- package/src/functions/module_math.ts +1252 -0
- package/src/functions/module_operators.ts +291 -0
- package/src/functions/module_statistical.ts +882 -0
- package/src/functions/module_text.ts +370 -0
- package/src/helpers/color.ts +30 -0
- package/src/helpers/coordinates.ts +71 -0
- package/src/helpers/grid_manipulation.ts +68 -0
- package/src/helpers/index.ts +7 -0
- package/src/helpers/misc.ts +76 -0
- package/src/helpers/numbers.ts +117 -0
- package/src/helpers/uuid.ts +19 -0
- package/src/helpers/zones.ts +263 -0
- package/src/history.ts +186 -0
- package/src/index.ts +84 -0
- package/src/model.ts +261 -0
- package/src/plugins/autofill.ts +412 -0
- package/src/plugins/chart.ts +404 -0
- package/src/plugins/clipboard.ts +461 -0
- package/src/plugins/conditional_format.ts +445 -0
- package/src/plugins/core.ts +1502 -0
- package/src/plugins/edition.ts +157 -0
- package/src/plugins/evaluation.ts +428 -0
- package/src/plugins/figures.ts +123 -0
- package/src/plugins/formatting.ts +630 -0
- package/src/plugins/highlight.ts +187 -0
- package/src/plugins/index.ts +32 -0
- package/src/plugins/merge.ts +524 -0
- package/src/plugins/renderer.ts +571 -0
- package/src/plugins/selection.ts +562 -0
- package/src/plugins/selection_inputs.ts +326 -0
- package/src/registries/autofill_modifiers.ts +76 -0
- package/src/registries/autofill_rules.ts +98 -0
- package/src/registries/figure_registry.ts +30 -0
- package/src/registries/index.ts +7 -0
- package/src/registries/menu_items_registry.ts +110 -0
- package/src/registries/menus/cell_menu_registry.ts +79 -0
- package/src/registries/menus/col_menu_registry.ts +63 -0
- package/src/registries/menus/index.ts +6 -0
- package/src/registries/menus/menu_items_actions.ts +425 -0
- package/src/registries/menus/row_menu_registry.ts +63 -0
- package/src/registries/menus/sheet_menu_registry.ts +75 -0
- package/src/registries/menus/topbar_menu_registry.ts +204 -0
- package/src/registries/side_panel_registry.ts +26 -0
- package/src/registries/topbar_component_registry.ts +12 -0
- package/src/registry.ts +51 -0
- package/src/translation.ts +44 -0
- package/src/types/autofill.ts +48 -0
- package/src/types/chart.ts +26 -0
- package/src/types/commands.ts +716 -0
- package/src/types/conditional_formatting.ts +124 -0
- package/src/types/env.ts +13 -0
- package/src/types/functions.ts +35 -0
- package/src/types/getters.ts +103 -0
- package/src/types/index.ts +20 -0
- package/src/types/misc.ts +143 -0
- package/src/types/rendering.ts +41 -0
- package/src/types/workbook_data.ts +62 -0
- package/tests/__mocks__/uuid.ts +7 -0
- package/tests/canvas.mock.ts +34 -0
- package/tests/components/__mocks__/content_editable_helper.ts +79 -0
- package/tests/components/__mocks__/scrollbar.ts +18 -0
- package/tests/components/__snapshots__/autocomplete_dropdown_test.ts.snap +29 -0
- package/tests/components/__snapshots__/bottom_bar_test.ts.snap +75 -0
- package/tests/components/__snapshots__/context_menu_test.ts.snap +88 -0
- package/tests/components/__snapshots__/grid_test.ts.snap +60 -0
- package/tests/components/__snapshots__/spreadsheet_test.ts.snap +455 -0
- package/tests/components/__snapshots__/top_bar_test.ts.snap +705 -0
- package/tests/components/autocomplete_dropdown_test.ts +315 -0
- package/tests/components/autofill_test.ts +167 -0
- package/tests/components/bottom_bar_test.ts +262 -0
- package/tests/components/chart_side_panel_test.ts +123 -0
- package/tests/components/composer_test.ts +400 -0
- package/tests/components/context_menu_test.ts +825 -0
- package/tests/components/grid_manipulation_test.ts +163 -0
- package/tests/components/grid_test.ts +581 -0
- package/tests/components/overlay_test.ts +541 -0
- package/tests/components/selection_input_test.ts +259 -0
- package/tests/components/side_panel_test.ts +148 -0
- package/tests/components/spreadsheet_test.ts +194 -0
- package/tests/components/top_bar_test.ts +427 -0
- package/tests/data_test.ts +175 -0
- package/tests/dom_helper.ts +42 -0
- package/tests/formulas/__snapshots__/compiler_test.ts.snap +302 -0
- package/tests/formulas/compiler_test.ts +266 -0
- package/tests/formulas/composer_tokenizer_test.ts +137 -0
- package/tests/formulas/expressions_test.ts +57 -0
- package/tests/formulas/formulas_test.ts +80 -0
- package/tests/formulas/normalize_test.ts +82 -0
- package/tests/formulas/parser_test.ts +224 -0
- package/tests/formulas/range_tokenizer_test.ts +187 -0
- package/tests/formulas/tokenizer_test.ts +164 -0
- package/tests/functions/arguments_test.ts +141 -0
- package/tests/functions/dates_test.ts +1081 -0
- package/tests/functions/functions_test.ts +53 -0
- package/tests/functions/helper_test.ts +98 -0
- package/tests/functions/module_database_test.ts +311 -0
- package/tests/functions/module_date_test.ts +941 -0
- package/tests/functions/module_info_test.ts +139 -0
- package/tests/functions/module_logical_test.ts +369 -0
- package/tests/functions/module_lookup_test.ts +541 -0
- package/tests/functions/module_math_test.ts +2482 -0
- package/tests/functions/module_operators_test.ts +955 -0
- package/tests/functions/module_statistical_test.ts +2014 -0
- package/tests/functions/module_text_test.ts +667 -0
- package/tests/helpers/coordinates_test.ts +25 -0
- package/tests/helpers/misc_test.ts +95 -0
- package/tests/helpers/numbers_test.ts +196 -0
- package/tests/helpers/zones_test.ts +97 -0
- package/tests/helpers.ts +384 -0
- package/tests/menu_items_registry_test.ts +540 -0
- package/tests/model/plugins/__snapshots__/chart_test.ts.snap +90 -0
- package/tests/model/plugins/chart_test.ts +562 -0
- package/tests/model_test.ts +82 -0
- package/tests/plugins/__snapshots__/conditional_formatting_test.ts.snap +131 -0
- package/tests/plugins/__snapshots__/renderer_test.ts.snap +371 -0
- package/tests/plugins/autofill_test.ts +457 -0
- package/tests/plugins/borders_test.ts +295 -0
- package/tests/plugins/clipboard_test.ts +1180 -0
- package/tests/plugins/conditional_formatting_test.ts +1253 -0
- package/tests/plugins/core_test.ts +614 -0
- package/tests/plugins/edition_test.ts +100 -0
- package/tests/plugins/evaluation_async_test.ts +332 -0
- package/tests/plugins/evaluation_dates_test.ts +79 -0
- package/tests/plugins/evaluation_test.ts +901 -0
- package/tests/plugins/figures_test.ts +327 -0
- package/tests/plugins/formatting_test.ts +391 -0
- package/tests/plugins/grid_manipulation_test.ts +1366 -0
- package/tests/plugins/highlight_test.ts +413 -0
- package/tests/plugins/history_test.ts +142 -0
- package/tests/plugins/import_export_test.ts +276 -0
- package/tests/plugins/merges_test.ts +490 -0
- package/tests/plugins/navigation_test.ts +181 -0
- package/tests/plugins/plugins/evaluation_test.ts +111 -0
- package/tests/plugins/renderer_test.ts +598 -0
- package/tests/plugins/resizing_test.ts +152 -0
- package/tests/plugins/selection_input_test.ts +409 -0
- package/tests/plugins/selection_test.ts +442 -0
- package/tests/plugins/sheets_test.ts +643 -0
- package/tests/plugins/style_test.ts +90 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,630 @@
|
|
|
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
|
+
}
|