@fileverse-dev/fortune-core 1.2.31 → 1.2.33

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.
@@ -378,10 +378,10 @@ export function handleArrowKey(ctx, e) {
378
378
  }
379
379
  }
380
380
  export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
381
- var _a;
381
+ var _a, _b, _c;
382
382
  return __awaiter(this, void 0, void 0, function () {
383
- var kcode, kstr, allowEdit, last, row_index, col_index, last, row_index, col_index;
384
- return __generator(this, function (_b) {
383
+ var kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, last, row_index, col_index;
384
+ return __generator(this, function (_d) {
385
385
  if (e.shiftKey && e.code === "Space") {
386
386
  e.stopImmediatePropagation();
387
387
  e.stopPropagation();
@@ -420,7 +420,10 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
420
420
  ctx.luckysheet_selection_range = [];
421
421
  }
422
422
  allowEdit = isAllowEdit(ctx);
423
- if (ctx.luckysheetCellUpdate.length > 0 && kstr !== "Enter" && kstr !== "Tab" && kstr !== "ArrowUp" && kstr !== "ArrowLeft" && kstr !== "ArrowRight") {
423
+ isFxInput = (_b = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.contains("fortune-fx-input");
424
+ ignoredKeys = new Set(isFxInput ? ["Enter", "Tab", "ArrowLeft", "ArrowRight"] : ["Enter", "Tab", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
425
+ restCod = !ignoredKeys.has(kstr);
426
+ if (ctx.luckysheetCellUpdate.length > 0 && restCod) {
424
427
  return [2];
425
428
  }
426
429
  if (kstr === "Enter") {
@@ -441,7 +444,7 @@ export function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUnd
441
444
  if (ctx.luckysheetCellUpdate.length > 0) {
442
445
  return [2];
443
446
  }
444
- last = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
447
+ last = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c[ctx.luckysheet_select_save.length - 1];
445
448
  if (!last) return [2];
446
449
  row_index = last.row_focus;
447
450
  col_index = last.column_focus;
@@ -1,4 +1,11 @@
1
1
  import { Context } from "../context";
2
+ export declare function columnLabelIndex(label: string): number;
3
+ export declare function indexToColumnLabel(index: number): string;
4
+ export declare class FormularCellRefError extends Error {
5
+ formula: string;
6
+ constructor(message: string, formula: string);
7
+ }
8
+ export declare function adjustFormulaForPaste(formula: string, srcCol: number, srcRow: number, destCol: number, destRow: number): string;
2
9
  export declare function parseAsLinkIfUrl(txtdata: string, ctx: Context): void;
3
10
  export declare function handlePaste(ctx: Context, e: ClipboardEvent): void;
4
11
  export declare function handlePasteByClick(ctx: Context, clipboardData: string, triggerType?: string): void;
@@ -1,4 +1,24 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ var __extends = this && this.__extends || function () {
3
+ var _extendStatics = function extendStatics(d, b) {
4
+ _extendStatics = Object.setPrototypeOf || {
5
+ __proto__: []
6
+ } instanceof Array && function (d, b) {
7
+ d.__proto__ = b;
8
+ } || function (d, b) {
9
+ for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
10
+ };
11
+ return _extendStatics(d, b);
12
+ };
13
+ return function (d, b) {
14
+ if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
15
+ _extendStatics(d, b);
16
+ function __() {
17
+ this.constructor = d;
18
+ }
19
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
+ };
21
+ }();
2
22
  var __assign = this && this.__assign || function () {
3
23
  __assign = Object.assign || function (t) {
4
24
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -9,10 +29,10 @@ var __assign = this && this.__assign || function () {
9
29
  };
10
30
  return __assign.apply(this, arguments);
11
31
  };
12
- import _ from "lodash";
32
+ import _, { isObject } from "lodash";
13
33
  import { handlePastedTable } from "../paste-table-helpers";
14
34
  import { getFlowdata } from "../context";
15
- import { execfunction, functionCopy } from "../modules/formula";
35
+ import { execfunction } from "../modules/formula";
16
36
  import { getdatabyselection } from "../modules/cell";
17
37
  import { genarate, update } from "../modules/format";
18
38
  import { normalizeSelection, selectionCache } from "../modules/selection";
@@ -21,9 +41,62 @@ import { hasPartMC, isRealNum } from "../modules/validation";
21
41
  import { getBorderInfoCompute } from "../modules/border";
22
42
  import { expandRowsAndColumns, storeSheetParamALL } from "../modules/sheet";
23
43
  import { jfrefreshgrid } from "../modules/refresh";
24
- import { CFSplitRange, sanitizeDuneUrl, saveHyperlink } from "../modules";
44
+ import { CFSplitRange, sanitizeDuneUrl, saveHyperlink, spillSortResult } from "../modules";
25
45
  import clipboard from "../modules/clipboard";
26
46
  import { calculateRangeCellSize, updateSheetCellSizes } from "../paste-helpers/calculate-range-cell-size";
47
+ export function columnLabelIndex(label) {
48
+ var index = 0;
49
+ var A = "A".charCodeAt(0);
50
+ for (var i = 0; i < label.length; i++) {
51
+ var charCode = label.charCodeAt(i) - A + 1;
52
+ index = index * 26 + charCode;
53
+ }
54
+ return index - 1;
55
+ }
56
+ export function indexToColumnLabel(index) {
57
+ var label = "";
58
+ while (index >= 0) {
59
+ var remainder = index % 26;
60
+ label = String.fromCharCode(65 + remainder) + label;
61
+ index = Math.floor(index / 26) - 1;
62
+ }
63
+ return label;
64
+ }
65
+ var FormularCellRefError = function (_super) {
66
+ __extends(FormularCellRefError, _super);
67
+ function FormularCellRefError(message, formula) {
68
+ var _this = _super.call(this, message) || this;
69
+ _this.name = "FormularCellRefError";
70
+ _this.formula = formula;
71
+ return _this;
72
+ }
73
+ return FormularCellRefError;
74
+ }(Error);
75
+ export { FormularCellRefError };
76
+ export function adjustFormulaForPaste(formula, srcCol, srcRow, destCol, destRow) {
77
+ var colOffset = destCol - srcCol;
78
+ var rowOffset = destRow - srcRow;
79
+ var hadInvalid = false;
80
+ var result = formula.replace(/(\$?)([A-Z]+)(\$?)(\d+)/g, function (__, absCol, colLetters, absRow, rowNum) {
81
+ var colIndex = columnLabelIndex(colLetters);
82
+ var rowIndex = parseInt(rowNum, 10);
83
+ if (!absCol) colIndex += colOffset;
84
+ if (!absRow) rowIndex += rowOffset;
85
+ if (colIndex < 0 || rowIndex <= 0) {
86
+ hadInvalid = true;
87
+ var invalidCol = colIndex < 0 ? "".concat(absCol ? "$" : "").concat(colLetters).concat(colIndex) : "".concat(absCol ? "$" : "").concat(indexToColumnLabel(colIndex));
88
+ var invalidRow = rowIndex.toString();
89
+ return "".concat(invalidCol).concat(invalidRow);
90
+ }
91
+ var newCol = indexToColumnLabel(colIndex);
92
+ return "".concat(absCol ? "$" : "").concat(newCol).concat(absRow ? "$" : "").concat(rowIndex);
93
+ });
94
+ if (hadInvalid) {
95
+ var brokenFormula = "=".concat(result.replace(/^=/, ""));
96
+ throw new FormularCellRefError("Invalid cell reference generated while pasting formula: ".concat(formula), brokenFormula);
97
+ }
98
+ return result;
99
+ }
27
100
  function postPasteCut(ctx, source, target, RowlChange) {
28
101
  var execF_rc = {};
29
102
  ctx.formulaCache.execFunctionExist = [];
@@ -108,10 +181,28 @@ function postPasteCut(ctx, source, target, RowlChange) {
108
181
  ctx.formulaCache.execFunctionGlobalData = null;
109
182
  storeSheetParamALL(ctx);
110
183
  }
184
+ var handleFormulaOnPaste = function handleFormulaOnPaste(ctx, d) {
185
+ var _a;
186
+ for (var r = 0; r < d.length; r += 1) {
187
+ var x = d[r];
188
+ for (var c = 0; c < d[0].length; c += 1) {
189
+ var value = isObject(d[r][c]) ? (_a = d[r][c]) === null || _a === void 0 ? void 0 : _a.v : d[r][c];
190
+ if (value && String(value).includes('=')) {
191
+ var cell = {};
192
+ var funcV = execfunction(ctx, String(value), r, c, undefined, undefined, true);
193
+ console.log(funcV);
194
+ cell.v = funcV[1];
195
+ cell.f = funcV[2];
196
+ cell.m = funcV[1].toString();
197
+ x[c] = cell;
198
+ }
199
+ d[r] = x;
200
+ }
201
+ }
202
+ };
111
203
  function pasteHandler(ctx, data, borderInfo) {
112
204
  var _a;
113
205
  var _b, _c, _d, _e, _f, _g;
114
- console.log("pasteHandler");
115
206
  if (ctx.luckysheet_selection_range) {
116
207
  ctx.luckysheet_selection_range = [];
117
208
  }
@@ -222,6 +313,7 @@ function pasteHandler(ctx, data, borderInfo) {
222
313
  ctx.luckysheetfile[getSheetIndex(ctx, ctx.currentSheetId)].config = cfg;
223
314
  }
224
315
  jfrefreshgrid(ctx, null, undefined);
316
+ handleFormulaOnPaste(ctx, d);
225
317
  } else {
226
318
  data = data.replace(/\r/g, "");
227
319
  var dataChe = [];
@@ -296,6 +388,7 @@ function pasteHandler(ctx, data, borderInfo) {
296
388
  last.row = [curR, curR + rlen - 1];
297
389
  last.column = [curC, curC + clen - 1];
298
390
  jfrefreshgrid(ctx, null, undefined);
391
+ handleFormulaOnPaste(ctx, d);
299
392
  }
300
393
  }
301
394
  function setCellHyperlink(ctx, id, r, c, link) {
@@ -792,8 +885,6 @@ function pasteHandlerOfCopyPaste(ctx, copyRange) {
792
885
  mtc = minc + (tc - 1) * copyc;
793
886
  maxrowCache = minh + th * copyh;
794
887
  maxcellCahe = minc + tc * copyc;
795
- var offsetRow = mth - c_r1;
796
- var offsetCol = mtc - c_c1;
797
888
  var offsetMC = {};
798
889
  for (var h = mth; h < maxrowCache; h += 1) {
799
890
  if (hiddenRows === null || hiddenRows === void 0 ? void 0 : hiddenRows.has(h.toString())) continue;
@@ -868,26 +959,56 @@ function pasteHandlerOfCopyPaste(ctx, copyRange) {
868
959
  }
869
960
  }
870
961
  if (!_.isNil(value) && !_.isNil(value.f)) {
871
- var func = value.f;
872
- if (offsetRow > 0) {
873
- func = "=".concat(functionCopy(ctx, func, "down", offsetRow));
874
- }
875
- if (offsetRow < 0) {
876
- func = "=".concat(functionCopy(ctx, func, "up", Math.abs(offsetRow)));
877
- }
878
- if (offsetCol > 0) {
879
- func = "=".concat(functionCopy(ctx, func, "right", offsetCol));
880
- }
881
- if (offsetCol < 0) {
882
- func = "=".concat(functionCopy(ctx, func, "left", Math.abs(offsetCol)));
962
+ var adjustedFormula = value.f;
963
+ var isError = false;
964
+ try {
965
+ adjustedFormula = adjustFormulaForPaste(value.f, c_c1, c_r1, c, h);
966
+ } catch (error) {
967
+ isError = true;
968
+ value.error = {
969
+ row_column: "".concat(h, "_").concat(c),
970
+ title: "Error",
971
+ message: (error === null || error === void 0 ? void 0 : error.message) || "Failed to adjust cell reference"
972
+ };
973
+ value.m = "#ERROR";
974
+ value.f = error === null || error === void 0 ? void 0 : error.formula;
975
+ delete value.ct;
976
+ delete value.v;
977
+ delete value.tb;
978
+ delete value.ht;
883
979
  }
884
- var funcV = execfunction(ctx, func, h, c, undefined, undefined, true);
885
- var afterUpdateCell = ctx.hooks.afterUpdateCell;
886
- if (afterUpdateCell) {
887
- afterUpdateCell(h, c, null, __assign(__assign({}, value), {
888
- v: arr.length === 1 ? funcV[1] : value.v,
889
- m: funcV[1] instanceof Promise ? "[object Promise]" : funcV[1]
890
- }));
980
+ if (!isError) {
981
+ var funcV = execfunction(ctx, adjustedFormula, h, c, undefined, undefined, true);
982
+ value.f = adjustedFormula;
983
+ if (!(funcV[1] instanceof Promise) && !funcV[3]) {
984
+ if (Array.isArray(funcV[1])) {
985
+ var formulaResultValue = funcV[1];
986
+ value.m = String(formulaResultValue[0][0]);
987
+ spillSortResult(ctx, h, c, {
988
+ m: String(formulaResultValue[0][0]),
989
+ f: value.f,
990
+ v: formulaResultValue
991
+ }, d);
992
+ } else {
993
+ value.m = String(funcV[1]);
994
+ value.v = String(funcV[1]);
995
+ }
996
+ } else if (funcV[3]) {
997
+ value.error = funcV[3];
998
+ value.m = "#ERROR";
999
+ value.f = adjustedFormula;
1000
+ delete value.ct;
1001
+ delete value.v;
1002
+ delete value.tb;
1003
+ delete value.ht;
1004
+ }
1005
+ var afterUpdateCell = ctx.hooks.afterUpdateCell;
1006
+ if (afterUpdateCell) {
1007
+ afterUpdateCell(h, c, null, __assign(__assign({}, value), {
1008
+ v: arr.length === 1 ? funcV[1] : value.v,
1009
+ m: funcV[1] instanceof Promise ? "[object Promise]" : funcV[1]
1010
+ }));
1011
+ }
891
1012
  }
892
1013
  if (!_.isNil(value.spl)) {}
893
1014
  }
@@ -740,7 +740,11 @@ export function execfunction(ctx, txt, r, c, id, calcChainSet, isrefresh, notIns
740
740
  } else {
741
741
  clearCellError(ctx, r, c);
742
742
  }
743
- return [true, !isError ? finalResult : "#ERROR", txt];
743
+ return [true, !isError ? finalResult : "#ERROR", txt, isError && {
744
+ row_column: "".concat(r, "_").concat(c),
745
+ title: "Error",
746
+ message: (formulaError === null || formulaError === void 0 ? void 0 : formulaError.toString()) || detectedErrorFromValue || "Unknown Error"
747
+ }];
744
748
  }
745
749
  function insertUpdateDynamicArray(ctx, dynamicArrayItem) {
746
750
  var r = dynamicArrayItem.r,
@@ -152,11 +152,13 @@ export function isLinkValid(ctx, linkType, linkAddress) {
152
152
  tooltip: ""
153
153
  };
154
154
  var insertLink = locale(ctx).insertLink;
155
- if (linkType === "webpage") {
156
- if (!/^http[s]?:\/\//.test(linkAddress)) {
157
- linkAddress = "https://".concat(linkAddress);
158
- }
159
- if (!/^http[s]?:\/\/([\w\-\.]+)+[\w-]*([\w\-\.\/\?%&=]+)?$/gi.test(linkAddress)) return {
155
+ if (!/^https?:\/\//i.test(linkAddress)) {
156
+ linkAddress = "https://".concat(linkAddress);
157
+ }
158
+ var urlPattern = /^(https?):\/\/[^\s$.?#].[^\s]*$/i;
159
+ var isValid = urlPattern.test(linkAddress);
160
+ if (!isValid) {
161
+ return {
160
162
  isValid: false,
161
163
  tooltip: insertLink.tooltipInfo1
162
164
  };
@@ -388,10 +388,10 @@ function handleArrowKey(ctx, e) {
388
388
  }
389
389
  }
390
390
  function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, handleRedo, canvas) {
391
- var _a;
391
+ var _a, _b, _c;
392
392
  return __awaiter(this, void 0, void 0, function () {
393
- var kcode, kstr, allowEdit, last, row_index, col_index, last, row_index, col_index;
394
- return __generator(this, function (_b) {
393
+ var kcode, kstr, allowEdit, isFxInput, ignoredKeys, restCod, last, row_index, col_index, last, row_index, col_index;
394
+ return __generator(this, function (_d) {
395
395
  if (e.shiftKey && e.code === "Space") {
396
396
  e.stopImmediatePropagation();
397
397
  e.stopPropagation();
@@ -430,7 +430,10 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
430
430
  ctx.luckysheet_selection_range = [];
431
431
  }
432
432
  allowEdit = (0, _utils.isAllowEdit)(ctx);
433
- if (ctx.luckysheetCellUpdate.length > 0 && kstr !== "Enter" && kstr !== "Tab" && kstr !== "ArrowUp" && kstr !== "ArrowLeft" && kstr !== "ArrowRight") {
433
+ isFxInput = (_b = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.classList) === null || _b === void 0 ? void 0 : _b.contains("fortune-fx-input");
434
+ ignoredKeys = new Set(isFxInput ? ["Enter", "Tab", "ArrowLeft", "ArrowRight"] : ["Enter", "Tab", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
435
+ restCod = !ignoredKeys.has(kstr);
436
+ if (ctx.luckysheetCellUpdate.length > 0 && restCod) {
434
437
  return [2];
435
438
  }
436
439
  if (kstr === "Enter") {
@@ -451,7 +454,7 @@ function handleGlobalKeyDown(ctx, cellInput, fxInput, e, cache, handleUndo, hand
451
454
  if (ctx.luckysheetCellUpdate.length > 0) {
452
455
  return [2];
453
456
  }
454
- last = (_a = ctx.luckysheet_select_save) === null || _a === void 0 ? void 0 : _a[ctx.luckysheet_select_save.length - 1];
457
+ last = (_c = ctx.luckysheet_select_save) === null || _c === void 0 ? void 0 : _c[ctx.luckysheet_select_save.length - 1];
455
458
  if (!last) return [2];
456
459
  row_index = last.row_focus;
457
460
  col_index = last.column_focus;
@@ -1,4 +1,11 @@
1
1
  import { Context } from "../context";
2
+ export declare function columnLabelIndex(label: string): number;
3
+ export declare function indexToColumnLabel(index: number): string;
4
+ export declare class FormularCellRefError extends Error {
5
+ formula: string;
6
+ constructor(message: string, formula: string);
7
+ }
8
+ export declare function adjustFormulaForPaste(formula: string, srcCol: number, srcRow: number, destCol: number, destRow: number): string;
2
9
  export declare function parseAsLinkIfUrl(txtdata: string, ctx: Context): void;
3
10
  export declare function handlePaste(ctx: Context, e: ClipboardEvent): void;
4
11
  export declare function handlePasteByClick(ctx: Context, clipboardData: string, triggerType?: string): void;
@@ -3,10 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.FormularCellRefError = void 0;
7
+ exports.adjustFormulaForPaste = adjustFormulaForPaste;
8
+ exports.columnLabelIndex = columnLabelIndex;
6
9
  exports.handlePaste = handlePaste;
7
10
  exports.handlePasteByClick = handlePasteByClick;
11
+ exports.indexToColumnLabel = indexToColumnLabel;
8
12
  exports.parseAsLinkIfUrl = parseAsLinkIfUrl;
9
- var _lodash = _interopRequireDefault(require("lodash"));
13
+ var _lodash = _interopRequireWildcard(require("lodash"));
10
14
  var _pasteTableHelpers = require("../paste-table-helpers");
11
15
  var _context = require("../context");
12
16
  var _formula = require("../modules/formula");
@@ -22,7 +26,28 @@ var _modules = require("../modules");
22
26
  var _clipboard = _interopRequireDefault(require("../modules/clipboard"));
23
27
  var _calculateRangeCellSize = require("../paste-helpers/calculate-range-cell-size");
24
28
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
29
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t2 in e) "default" !== _t2 && {}.hasOwnProperty.call(e, _t2) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t2)) && (i.get || i.set) ? o(f, _t2, i) : f[_t2] = e[_t2]); return f; })(e, t); }
25
30
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
31
+ var __extends = void 0 && (void 0).__extends || function () {
32
+ var _extendStatics = function extendStatics(d, b) {
33
+ _extendStatics = Object.setPrototypeOf || {
34
+ __proto__: []
35
+ } instanceof Array && function (d, b) {
36
+ d.__proto__ = b;
37
+ } || function (d, b) {
38
+ for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
39
+ };
40
+ return _extendStatics(d, b);
41
+ };
42
+ return function (d, b) {
43
+ if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
44
+ _extendStatics(d, b);
45
+ function __() {
46
+ this.constructor = d;
47
+ }
48
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
49
+ };
50
+ }();
26
51
  var __assign = void 0 && (void 0).__assign || function () {
27
52
  __assign = Object.assign || function (t) {
28
53
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -33,6 +58,58 @@ var __assign = void 0 && (void 0).__assign || function () {
33
58
  };
34
59
  return __assign.apply(this, arguments);
35
60
  };
61
+ function columnLabelIndex(label) {
62
+ var index = 0;
63
+ var A = "A".charCodeAt(0);
64
+ for (var i = 0; i < label.length; i++) {
65
+ var charCode = label.charCodeAt(i) - A + 1;
66
+ index = index * 26 + charCode;
67
+ }
68
+ return index - 1;
69
+ }
70
+ function indexToColumnLabel(index) {
71
+ var label = "";
72
+ while (index >= 0) {
73
+ var remainder = index % 26;
74
+ label = String.fromCharCode(65 + remainder) + label;
75
+ index = Math.floor(index / 26) - 1;
76
+ }
77
+ return label;
78
+ }
79
+ var FormularCellRefError = exports.FormularCellRefError = function (_super) {
80
+ __extends(FormularCellRefError, _super);
81
+ function FormularCellRefError(message, formula) {
82
+ var _this = _super.call(this, message) || this;
83
+ _this.name = "FormularCellRefError";
84
+ _this.formula = formula;
85
+ return _this;
86
+ }
87
+ return FormularCellRefError;
88
+ }(Error);
89
+ function adjustFormulaForPaste(formula, srcCol, srcRow, destCol, destRow) {
90
+ var colOffset = destCol - srcCol;
91
+ var rowOffset = destRow - srcRow;
92
+ var hadInvalid = false;
93
+ var result = formula.replace(/(\$?)([A-Z]+)(\$?)(\d+)/g, function (__, absCol, colLetters, absRow, rowNum) {
94
+ var colIndex = columnLabelIndex(colLetters);
95
+ var rowIndex = parseInt(rowNum, 10);
96
+ if (!absCol) colIndex += colOffset;
97
+ if (!absRow) rowIndex += rowOffset;
98
+ if (colIndex < 0 || rowIndex <= 0) {
99
+ hadInvalid = true;
100
+ var invalidCol = colIndex < 0 ? "".concat(absCol ? "$" : "").concat(colLetters).concat(colIndex) : "".concat(absCol ? "$" : "").concat(indexToColumnLabel(colIndex));
101
+ var invalidRow = rowIndex.toString();
102
+ return "".concat(invalidCol).concat(invalidRow);
103
+ }
104
+ var newCol = indexToColumnLabel(colIndex);
105
+ return "".concat(absCol ? "$" : "").concat(newCol).concat(absRow ? "$" : "").concat(rowIndex);
106
+ });
107
+ if (hadInvalid) {
108
+ var brokenFormula = "=".concat(result.replace(/^=/, ""));
109
+ throw new FormularCellRefError("Invalid cell reference generated while pasting formula: ".concat(formula), brokenFormula);
110
+ }
111
+ return result;
112
+ }
36
113
  function postPasteCut(ctx, source, target, RowlChange) {
37
114
  var execF_rc = {};
38
115
  ctx.formulaCache.execFunctionExist = [];
@@ -117,10 +194,28 @@ function postPasteCut(ctx, source, target, RowlChange) {
117
194
  ctx.formulaCache.execFunctionGlobalData = null;
118
195
  (0, _sheet.storeSheetParamALL)(ctx);
119
196
  }
197
+ var handleFormulaOnPaste = function handleFormulaOnPaste(ctx, d) {
198
+ var _a;
199
+ for (var r = 0; r < d.length; r += 1) {
200
+ var x = d[r];
201
+ for (var c = 0; c < d[0].length; c += 1) {
202
+ var value = (0, _lodash.isObject)(d[r][c]) ? (_a = d[r][c]) === null || _a === void 0 ? void 0 : _a.v : d[r][c];
203
+ if (value && String(value).includes('=')) {
204
+ var cell = {};
205
+ var funcV = (0, _formula.execfunction)(ctx, String(value), r, c, undefined, undefined, true);
206
+ console.log(funcV);
207
+ cell.v = funcV[1];
208
+ cell.f = funcV[2];
209
+ cell.m = funcV[1].toString();
210
+ x[c] = cell;
211
+ }
212
+ d[r] = x;
213
+ }
214
+ }
215
+ };
120
216
  function pasteHandler(ctx, data, borderInfo) {
121
217
  var _a;
122
218
  var _b, _c, _d, _e, _f, _g;
123
- console.log("pasteHandler");
124
219
  if (ctx.luckysheet_selection_range) {
125
220
  ctx.luckysheet_selection_range = [];
126
221
  }
@@ -231,6 +326,7 @@ function pasteHandler(ctx, data, borderInfo) {
231
326
  ctx.luckysheetfile[(0, _utils.getSheetIndex)(ctx, ctx.currentSheetId)].config = cfg;
232
327
  }
233
328
  (0, _refresh.jfrefreshgrid)(ctx, null, undefined);
329
+ handleFormulaOnPaste(ctx, d);
234
330
  } else {
235
331
  data = data.replace(/\r/g, "");
236
332
  var dataChe = [];
@@ -305,6 +401,7 @@ function pasteHandler(ctx, data, borderInfo) {
305
401
  last.row = [curR, curR + rlen - 1];
306
402
  last.column = [curC, curC + clen - 1];
307
403
  (0, _refresh.jfrefreshgrid)(ctx, null, undefined);
404
+ handleFormulaOnPaste(ctx, d);
308
405
  }
309
406
  }
310
407
  function setCellHyperlink(ctx, id, r, c, link) {
@@ -801,8 +898,6 @@ function pasteHandlerOfCopyPaste(ctx, copyRange) {
801
898
  mtc = minc + (tc - 1) * copyc;
802
899
  maxrowCache = minh + th * copyh;
803
900
  maxcellCahe = minc + tc * copyc;
804
- var offsetRow = mth - c_r1;
805
- var offsetCol = mtc - c_c1;
806
901
  var offsetMC = {};
807
902
  for (var h = mth; h < maxrowCache; h += 1) {
808
903
  if (hiddenRows === null || hiddenRows === void 0 ? void 0 : hiddenRows.has(h.toString())) continue;
@@ -877,26 +972,56 @@ function pasteHandlerOfCopyPaste(ctx, copyRange) {
877
972
  }
878
973
  }
879
974
  if (!_lodash.default.isNil(value) && !_lodash.default.isNil(value.f)) {
880
- var func = value.f;
881
- if (offsetRow > 0) {
882
- func = "=".concat((0, _formula.functionCopy)(ctx, func, "down", offsetRow));
883
- }
884
- if (offsetRow < 0) {
885
- func = "=".concat((0, _formula.functionCopy)(ctx, func, "up", Math.abs(offsetRow)));
886
- }
887
- if (offsetCol > 0) {
888
- func = "=".concat((0, _formula.functionCopy)(ctx, func, "right", offsetCol));
889
- }
890
- if (offsetCol < 0) {
891
- func = "=".concat((0, _formula.functionCopy)(ctx, func, "left", Math.abs(offsetCol)));
975
+ var adjustedFormula = value.f;
976
+ var isError = false;
977
+ try {
978
+ adjustedFormula = adjustFormulaForPaste(value.f, c_c1, c_r1, c, h);
979
+ } catch (error) {
980
+ isError = true;
981
+ value.error = {
982
+ row_column: "".concat(h, "_").concat(c),
983
+ title: "Error",
984
+ message: (error === null || error === void 0 ? void 0 : error.message) || "Failed to adjust cell reference"
985
+ };
986
+ value.m = "#ERROR";
987
+ value.f = error === null || error === void 0 ? void 0 : error.formula;
988
+ delete value.ct;
989
+ delete value.v;
990
+ delete value.tb;
991
+ delete value.ht;
892
992
  }
893
- var funcV = (0, _formula.execfunction)(ctx, func, h, c, undefined, undefined, true);
894
- var afterUpdateCell = ctx.hooks.afterUpdateCell;
895
- if (afterUpdateCell) {
896
- afterUpdateCell(h, c, null, __assign(__assign({}, value), {
897
- v: arr.length === 1 ? funcV[1] : value.v,
898
- m: funcV[1] instanceof Promise ? "[object Promise]" : funcV[1]
899
- }));
993
+ if (!isError) {
994
+ var funcV = (0, _formula.execfunction)(ctx, adjustedFormula, h, c, undefined, undefined, true);
995
+ value.f = adjustedFormula;
996
+ if (!(funcV[1] instanceof Promise) && !funcV[3]) {
997
+ if (Array.isArray(funcV[1])) {
998
+ var formulaResultValue = funcV[1];
999
+ value.m = String(formulaResultValue[0][0]);
1000
+ (0, _modules.spillSortResult)(ctx, h, c, {
1001
+ m: String(formulaResultValue[0][0]),
1002
+ f: value.f,
1003
+ v: formulaResultValue
1004
+ }, d);
1005
+ } else {
1006
+ value.m = String(funcV[1]);
1007
+ value.v = String(funcV[1]);
1008
+ }
1009
+ } else if (funcV[3]) {
1010
+ value.error = funcV[3];
1011
+ value.m = "#ERROR";
1012
+ value.f = adjustedFormula;
1013
+ delete value.ct;
1014
+ delete value.v;
1015
+ delete value.tb;
1016
+ delete value.ht;
1017
+ }
1018
+ var afterUpdateCell = ctx.hooks.afterUpdateCell;
1019
+ if (afterUpdateCell) {
1020
+ afterUpdateCell(h, c, null, __assign(__assign({}, value), {
1021
+ v: arr.length === 1 ? funcV[1] : value.v,
1022
+ m: funcV[1] instanceof Promise ? "[object Promise]" : funcV[1]
1023
+ }));
1024
+ }
900
1025
  }
901
1026
  if (!_lodash.default.isNil(value.spl)) {}
902
1027
  }
@@ -770,7 +770,11 @@ function execfunction(ctx, txt, r, c, id, calcChainSet, isrefresh, notInsertFunc
770
770
  } else {
771
771
  (0, _2.clearCellError)(ctx, r, c);
772
772
  }
773
- return [true, !isError ? finalResult : "#ERROR", txt];
773
+ return [true, !isError ? finalResult : "#ERROR", txt, isError && {
774
+ row_column: "".concat(r, "_").concat(c),
775
+ title: "Error",
776
+ message: (formulaError === null || formulaError === void 0 ? void 0 : formulaError.toString()) || detectedErrorFromValue || "Unknown Error"
777
+ }];
774
778
  }
775
779
  function insertUpdateDynamicArray(ctx, dynamicArrayItem) {
776
780
  var r = dynamicArrayItem.r,
@@ -168,11 +168,13 @@ function isLinkValid(ctx, linkType, linkAddress) {
168
168
  tooltip: ""
169
169
  };
170
170
  var insertLink = (0, _locale.locale)(ctx).insertLink;
171
- if (linkType === "webpage") {
172
- if (!/^http[s]?:\/\//.test(linkAddress)) {
173
- linkAddress = "https://".concat(linkAddress);
174
- }
175
- if (!/^http[s]?:\/\/([\w\-\.]+)+[\w-]*([\w\-\.\/\?%&=]+)?$/gi.test(linkAddress)) return {
171
+ if (!/^https?:\/\//i.test(linkAddress)) {
172
+ linkAddress = "https://".concat(linkAddress);
173
+ }
174
+ var urlPattern = /^(https?):\/\/[^\s$.?#].[^\s]*$/i;
175
+ var isValid = urlPattern.test(linkAddress);
176
+ if (!isValid) {
177
+ return {
176
178
  isValid: false,
177
179
  tooltip: insertLink.tooltipInfo1
178
180
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fileverse-dev/fortune-core",
3
- "version": "1.2.31",
3
+ "version": "1.2.33",
4
4
  "main": "lib/index.js",
5
5
  "module": "es/index.js",
6
6
  "typings": "lib/index.d.ts",