@univerjs/sheets-numfmt 1.5.2 → 1.6.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.
- package/lib/cjs/index.js +118 -55
- package/lib/es/index.js +119 -54
- package/lib/index.js +119 -54
- package/lib/types/base/const/currency-symbols.d.ts +0 -5
- package/lib/types/index.d.ts +1 -1
- package/lib/types/utils/decimal.d.ts +6 -1
- package/lib/umd/index.js +1 -1
- package/package.json +7 -7
package/lib/cjs/index.js
CHANGED
|
@@ -20,27 +20,6 @@ let rxjs = require("rxjs");
|
|
|
20
20
|
* See the License for the specific language governing permissions and
|
|
21
21
|
* limitations under the License.
|
|
22
22
|
*/
|
|
23
|
-
const localeCurrencySymbolMap = /* @__PURE__ */ new Map([
|
|
24
|
-
[_univerjs_core.LocaleType.EN_US, "$"],
|
|
25
|
-
[_univerjs_core.LocaleType.RU_RU, "₽"],
|
|
26
|
-
[_univerjs_core.LocaleType.VI_VN, "₫"],
|
|
27
|
-
[_univerjs_core.LocaleType.ZH_CN, "¥"],
|
|
28
|
-
[_univerjs_core.LocaleType.ZH_TW, "NT$"],
|
|
29
|
-
[_univerjs_core.LocaleType.ZH_HK, "HK$"],
|
|
30
|
-
[_univerjs_core.LocaleType.FR_FR, "€"],
|
|
31
|
-
[_univerjs_core.LocaleType.FA_IR, "﷼"],
|
|
32
|
-
[_univerjs_core.LocaleType.KO_KR, "₩"],
|
|
33
|
-
[_univerjs_core.LocaleType.ES_ES, "€"],
|
|
34
|
-
[_univerjs_core.LocaleType.CA_ES, "€"],
|
|
35
|
-
[_univerjs_core.LocaleType.SK_SK, "€"],
|
|
36
|
-
[_univerjs_core.LocaleType.JA_JP, "¥"],
|
|
37
|
-
[_univerjs_core.LocaleType.PT_BR, "R$"],
|
|
38
|
-
[_univerjs_core.LocaleType.DE_DE, "€"],
|
|
39
|
-
[_univerjs_core.LocaleType.IT_IT, "€"],
|
|
40
|
-
[_univerjs_core.LocaleType.ID_ID, "Rp"],
|
|
41
|
-
[_univerjs_core.LocaleType.PL_PL, "zł"],
|
|
42
|
-
[_univerjs_core.LocaleType.AR_SA, "﷼"]
|
|
43
|
-
]);
|
|
44
23
|
/**
|
|
45
24
|
* Get the currency symbol icon based on the locale.
|
|
46
25
|
*/
|
|
@@ -53,44 +32,44 @@ function getCurrencySymbolIconByLocale(locale) {
|
|
|
53
32
|
case _univerjs_core.LocaleType.IT_IT:
|
|
54
33
|
case _univerjs_core.LocaleType.SK_SK: return {
|
|
55
34
|
icon: "EuroIcon",
|
|
56
|
-
symbol:
|
|
35
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
57
36
|
locale
|
|
58
37
|
};
|
|
59
38
|
case _univerjs_core.LocaleType.RU_RU: return {
|
|
60
39
|
icon: "RoubleIcon",
|
|
61
|
-
symbol:
|
|
40
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
62
41
|
locale
|
|
63
42
|
};
|
|
64
43
|
case _univerjs_core.LocaleType.JA_JP:
|
|
65
44
|
case _univerjs_core.LocaleType.ZH_CN: return {
|
|
66
45
|
icon: "RmbIcon",
|
|
67
|
-
symbol:
|
|
46
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
68
47
|
locale
|
|
69
48
|
};
|
|
70
49
|
case _univerjs_core.LocaleType.VI_VN: return {
|
|
71
50
|
icon: "DongIcon",
|
|
72
|
-
symbol:
|
|
51
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
73
52
|
locale
|
|
74
53
|
};
|
|
75
54
|
case _univerjs_core.LocaleType.AR_SA:
|
|
76
55
|
case _univerjs_core.LocaleType.FA_IR: return {
|
|
77
56
|
icon: "RialIcon",
|
|
78
|
-
symbol:
|
|
57
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
79
58
|
locale
|
|
80
59
|
};
|
|
81
60
|
case _univerjs_core.LocaleType.KO_KR: return {
|
|
82
61
|
icon: "WonIcon",
|
|
83
|
-
symbol:
|
|
62
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
84
63
|
locale
|
|
85
64
|
};
|
|
86
65
|
case _univerjs_core.LocaleType.ID_ID: return {
|
|
87
66
|
icon: "RupiahIcon",
|
|
88
|
-
symbol:
|
|
67
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
89
68
|
locale
|
|
90
69
|
};
|
|
91
70
|
case _univerjs_core.LocaleType.PL_PL: return {
|
|
92
71
|
icon: "ZlotyIcon",
|
|
93
|
-
symbol:
|
|
72
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
94
73
|
locale
|
|
95
74
|
};
|
|
96
75
|
case _univerjs_core.LocaleType.EN_US:
|
|
@@ -99,18 +78,12 @@ function getCurrencySymbolIconByLocale(locale) {
|
|
|
99
78
|
case _univerjs_core.LocaleType.ZH_TW:
|
|
100
79
|
default: return {
|
|
101
80
|
icon: "DollarIcon",
|
|
102
|
-
symbol:
|
|
81
|
+
symbol: (0, _univerjs_core.getCurrencySymbolByLocale)(locale),
|
|
103
82
|
locale
|
|
104
83
|
};
|
|
105
84
|
}
|
|
106
85
|
}
|
|
107
86
|
/**
|
|
108
|
-
* Get the currency symbol by locale.
|
|
109
|
-
*/
|
|
110
|
-
function getCurrencySymbolByLocale(locale) {
|
|
111
|
-
return localeCurrencySymbolMap.get(locale) || "$";
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
87
|
* Get the currency format string based on the locale and number of digits.
|
|
115
88
|
*/
|
|
116
89
|
function getCurrencyFormat(locale, numberDigits = 2) {
|
|
@@ -118,7 +91,7 @@ function getCurrencyFormat(locale, numberDigits = 2) {
|
|
|
118
91
|
if (numberDigits > 127) _numberDigits = 127;
|
|
119
92
|
let decimal = "";
|
|
120
93
|
if (_numberDigits > 0) decimal = `.${"0".repeat(_numberDigits)}`;
|
|
121
|
-
return `"${getCurrencySymbolByLocale(locale)}"#,##0${decimal}_);[Red]("${getCurrencySymbolByLocale(locale)}"#,##0${decimal})`;
|
|
94
|
+
return `"${(0, _univerjs_core.getCurrencySymbolByLocale)(locale)}"#,##0${decimal}_);[Red]("${(0, _univerjs_core.getCurrencySymbolByLocale)(locale)}"#,##0${decimal})`;
|
|
122
95
|
}
|
|
123
96
|
|
|
124
97
|
// src/base/const/formatdetail.ts
|
|
@@ -258,6 +231,95 @@ const CURRENCYFORMAT = [
|
|
|
258
231
|
* See the License for the specific language governing permissions and
|
|
259
232
|
* limitations under the License.
|
|
260
233
|
*/
|
|
234
|
+
const MAX_DECIMALS = 30;
|
|
235
|
+
const MASKED_CHAR = "\0";
|
|
236
|
+
const getDecimalString = (length) => new Array(Math.min(Math.max(0, Number(length)), MAX_DECIMALS)).fill(0).join("");
|
|
237
|
+
/**
|
|
238
|
+
* Replaces quoted literals, escaped characters, `[...]` blocks and the operands of `_` / `*` with a
|
|
239
|
+
* placeholder. Format codes keep their position, so indexes found in the mask apply to the text.
|
|
240
|
+
*/
|
|
241
|
+
function maskSection(section) {
|
|
242
|
+
const chars = section.split("");
|
|
243
|
+
let inQuote = false;
|
|
244
|
+
let inBracket = false;
|
|
245
|
+
for (let i = 0; i < chars.length; i++) {
|
|
246
|
+
const char = chars[i];
|
|
247
|
+
if (inBracket) {
|
|
248
|
+
chars[i] = MASKED_CHAR;
|
|
249
|
+
if (char === "]") inBracket = false;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (char === "\"") {
|
|
253
|
+
chars[i] = MASKED_CHAR;
|
|
254
|
+
inQuote = !inQuote;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (inQuote) {
|
|
258
|
+
chars[i] = MASKED_CHAR;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (char === "\\" || char === "_" || char === "*") {
|
|
262
|
+
chars[i] = MASKED_CHAR;
|
|
263
|
+
if (i + 1 < chars.length) {
|
|
264
|
+
chars[i + 1] = MASKED_CHAR;
|
|
265
|
+
i += 1;
|
|
266
|
+
}
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (char === "[") {
|
|
270
|
+
chars[i] = MASKED_CHAR;
|
|
271
|
+
inBracket = true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return chars.join("");
|
|
275
|
+
}
|
|
276
|
+
function splitSections(pattern) {
|
|
277
|
+
const mask = maskSection(pattern);
|
|
278
|
+
const sections = [];
|
|
279
|
+
let start = 0;
|
|
280
|
+
for (let i = 0; i <= mask.length; i++) if (i === mask.length || mask[i] === ";") {
|
|
281
|
+
sections.push({
|
|
282
|
+
mask: mask.slice(start, i),
|
|
283
|
+
text: pattern.slice(start, i)
|
|
284
|
+
});
|
|
285
|
+
start = i + 1;
|
|
286
|
+
}
|
|
287
|
+
return sections;
|
|
288
|
+
}
|
|
289
|
+
function isDateSection(section) {
|
|
290
|
+
return section.text !== "" && _univerjs_core.numfmt.getFormatInfo(section.text).isDate;
|
|
291
|
+
}
|
|
292
|
+
function isFractionPlaceholder(char) {
|
|
293
|
+
return char === "0" || char === "#" || char === "?";
|
|
294
|
+
}
|
|
295
|
+
function findIntegerZero(mask) {
|
|
296
|
+
for (let i = 0; i < mask.length; i++) if (mask[i] === "0" && mask[i + 1] !== "0") return i;
|
|
297
|
+
return -1;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Grows or shrinks the fractional placeholders of a number section one at a time, the way Excel does:
|
|
301
|
+
* adding copies the last fractional placeholder, removing drops the last one and the decimal point
|
|
302
|
+
* with the last placeholder.
|
|
303
|
+
*/
|
|
304
|
+
function setNumberSectionDecimal(section, decimalLength) {
|
|
305
|
+
const dotIndex = section.mask.indexOf(".");
|
|
306
|
+
if (dotIndex < 0) {
|
|
307
|
+
if (decimalLength === 0) return section.text;
|
|
308
|
+
const zeroIndex = findIntegerZero(section.mask);
|
|
309
|
+
if (zeroIndex < 0) return section.text;
|
|
310
|
+
return `${section.text.slice(0, zeroIndex + 1)}.${getDecimalString(decimalLength)}${section.text.slice(zeroIndex + 1)}`;
|
|
311
|
+
}
|
|
312
|
+
let end = dotIndex + 1;
|
|
313
|
+
while (isFractionPlaceholder(section.mask[end])) end += 1;
|
|
314
|
+
const fraction = section.text.slice(dotIndex + 1, end);
|
|
315
|
+
if (fraction.length === 0 && decimalLength === 0) return `${section.text.slice(0, dotIndex)}${section.text.slice(dotIndex + 1)}`;
|
|
316
|
+
if (decimalLength >= fraction.length) {
|
|
317
|
+
const filler = fraction.length > 0 ? fraction[fraction.length - 1] : "0";
|
|
318
|
+
return `${section.text.slice(0, end)}${filler.repeat(decimalLength - fraction.length)}${section.text.slice(end)}`;
|
|
319
|
+
}
|
|
320
|
+
const nextFraction = fraction.slice(0, decimalLength);
|
|
321
|
+
return nextFraction.length > 0 ? `${section.text.slice(0, dotIndex)}.${nextFraction}${section.text.slice(end)}` : `${section.text.slice(0, dotIndex)}${section.text.slice(end)}`;
|
|
322
|
+
}
|
|
261
323
|
/**
|
|
262
324
|
* the function decimal just use positive,negative configuration ignored
|
|
263
325
|
*/
|
|
@@ -265,13 +327,14 @@ const getDecimalFromPattern = (pattern, defaultValue = 0) => {
|
|
|
265
327
|
if (!pattern) return defaultValue;
|
|
266
328
|
return _univerjs_core.numfmt.getFormatInfo(pattern).maxDecimals ?? defaultValue;
|
|
267
329
|
};
|
|
268
|
-
|
|
330
|
+
/**
|
|
331
|
+
* Changes the decimal places of a number format. Date and time formats are returned unchanged, even
|
|
332
|
+
* when they show fractional seconds, because Excel's increase/decrease decimal commands do not apply
|
|
333
|
+
* to them.
|
|
334
|
+
*/
|
|
269
335
|
const setPatternDecimal = (patterns, decimalLength) => {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (/0([^0]?)|0$/.test(pattern)) return pattern.replace(/0([^0]+)|0$/, `0${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}$1`);
|
|
273
|
-
return pattern;
|
|
274
|
-
}).join(";");
|
|
336
|
+
const length = Math.min(Math.max(0, Math.floor(Number(decimalLength) || 0)), MAX_DECIMALS);
|
|
337
|
+
return splitSections(patterns).map((section) => isDateSection(section) ? section.text : setNumberSectionDecimal(section, length)).join(";");
|
|
275
338
|
};
|
|
276
339
|
const isPatternHasDecimal = (pattern) => /\.0?/.test(pattern) || /0([^0]?)|0$/.test(pattern);
|
|
277
340
|
|
|
@@ -598,15 +661,17 @@ const SubtractDecimalCommand = {
|
|
|
598
661
|
pattern: isScientificNotationNumericCell(cell) ? setPatternDecimal(getScientificNotationFormatFromCell(cell), decimals) : defaultPattern
|
|
599
662
|
});
|
|
600
663
|
} else {
|
|
601
|
-
const
|
|
602
|
-
|
|
664
|
+
const pattern = numfmtValue.pattern;
|
|
665
|
+
const nextPattern = setPatternDecimal(pattern, getDecimalFromPattern(pattern) - 1);
|
|
666
|
+
nextPattern !== pattern && values.push({
|
|
603
667
|
row,
|
|
604
668
|
col,
|
|
605
|
-
pattern:
|
|
669
|
+
pattern: nextPattern
|
|
606
670
|
});
|
|
607
671
|
}
|
|
608
672
|
});
|
|
609
673
|
});
|
|
674
|
+
if (!values.length) return false;
|
|
610
675
|
return await commandService.executeCommand(SetNumfmtCommand.id, { values });
|
|
611
676
|
}
|
|
612
677
|
};
|
|
@@ -631,7 +696,7 @@ const SHEETS_NUMFMT_PLUGIN_CONFIG_KEY = "sheets-numfmt.config";
|
|
|
631
696
|
const configSymbol = Symbol(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY);
|
|
632
697
|
const defaultPluginConfig = {};
|
|
633
698
|
|
|
634
|
-
// \0@oxc-project+runtime@0.
|
|
699
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
|
|
635
700
|
function _typeof(o) {
|
|
636
701
|
"@babel/helpers - typeof";
|
|
637
702
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -641,7 +706,7 @@ function _typeof(o) {
|
|
|
641
706
|
}, _typeof(o);
|
|
642
707
|
}
|
|
643
708
|
|
|
644
|
-
// \0@oxc-project+runtime@0.
|
|
709
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
|
|
645
710
|
function toPrimitive(t, e) {
|
|
646
711
|
if ("object" != _typeof(t) || !t) return t;
|
|
647
712
|
var r;
|
|
@@ -653,13 +718,13 @@ function toPrimitive(t, e) {
|
|
|
653
718
|
return ("string" === e ? String : Number)(t);
|
|
654
719
|
}
|
|
655
720
|
|
|
656
|
-
// \0@oxc-project+runtime@0.
|
|
721
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
|
|
657
722
|
function toPropertyKey(t) {
|
|
658
723
|
var i = toPrimitive(t, "string");
|
|
659
724
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
660
725
|
}
|
|
661
726
|
|
|
662
|
-
// \0@oxc-project+runtime@0.
|
|
727
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
|
|
663
728
|
function _defineProperty(e, r, t) {
|
|
664
729
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
665
730
|
value: t,
|
|
@@ -669,14 +734,14 @@ function _defineProperty(e, r, t) {
|
|
|
669
734
|
}) : e[r] = t, e;
|
|
670
735
|
}
|
|
671
736
|
|
|
672
|
-
// \0@oxc-project+runtime@0.
|
|
737
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
|
|
673
738
|
function __decorateParam(paramIndex, decorator) {
|
|
674
739
|
return function(target, key) {
|
|
675
740
|
decorator(target, key, paramIndex);
|
|
676
741
|
};
|
|
677
742
|
}
|
|
678
743
|
|
|
679
|
-
// \0@oxc-project+runtime@0.
|
|
744
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
|
|
680
745
|
function __decorate(decorators, target, key, desc) {
|
|
681
746
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
682
747
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -817,7 +882,7 @@ SheetsNumfmtCellContentController = __decorate([
|
|
|
817
882
|
|
|
818
883
|
// package.json
|
|
819
884
|
var name = "@univerjs/sheets-numfmt";
|
|
820
|
-
var version = "1.
|
|
885
|
+
var version = "1.6.0";
|
|
821
886
|
|
|
822
887
|
// src/base/const/plugin-name.ts
|
|
823
888
|
/**
|
|
@@ -1072,7 +1137,6 @@ Object.defineProperty(exports, "UniverSheetsNumfmtPlugin", {
|
|
|
1072
1137
|
exports.getCurrencyFormat = getCurrencyFormat;
|
|
1073
1138
|
exports.getCurrencyFormatOptions = getCurrencyFormatOptions;
|
|
1074
1139
|
exports.getCurrencyOptions = getCurrencyOptions;
|
|
1075
|
-
exports.getCurrencySymbolByLocale = getCurrencySymbolByLocale;
|
|
1076
1140
|
exports.getCurrencySymbolIconByLocale = getCurrencySymbolIconByLocale;
|
|
1077
1141
|
exports.getCurrencyType = getCurrencyType;
|
|
1078
1142
|
exports.getDateFormatOptions = getDateFormatOptions;
|
|
@@ -1083,5 +1147,4 @@ exports.getPatternPreview = getPatternPreview;
|
|
|
1083
1147
|
exports.getPatternPreviewIgnoreGeneral = getPatternPreviewIgnoreGeneral;
|
|
1084
1148
|
exports.getPatternType = getPatternType;
|
|
1085
1149
|
exports.isPatternHasDecimal = isPatternHasDecimal;
|
|
1086
|
-
exports.localeCurrencySymbolMap = localeCurrencySymbolMap;
|
|
1087
1150
|
exports.setPatternDecimal = setPatternDecimal;
|
package/lib/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CellValueType, CommandType, DEFAULT_NUMBER_FORMAT, DependentOn, Disposable, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, LocaleService, LocaleType, ObjectMatrix, Plugin, Range, RegionService, ThemeService, Tools, UniverInstanceType, currencySymbols, excelDateTimeSerial, getNumfmtLocaleTag, getNumfmtParseValueFilter, isDefaultFormat, isRealNum, isTextFormat, merge, numfmt, registerDependencies, sequenceExecute, touchDependencies } from "@univerjs/core";
|
|
1
|
+
import { CellValueType, CommandType, DEFAULT_NUMBER_FORMAT, DependentOn, Disposable, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, LocaleService, LocaleType, ObjectMatrix, Plugin, Range, RegionService, ThemeService, Tools, UniverInstanceType, currencySymbols, excelDateTimeSerial, getCurrencySymbolByLocale, getNumfmtLocaleTag, getNumfmtParseValueFilter, isDefaultFormat, isRealNum, isTextFormat, merge, numfmt, registerDependencies, sequenceExecute, touchDependencies } from "@univerjs/core";
|
|
2
2
|
import { INTERCEPTOR_POINT, INumfmtService, InterceptCellContentPriority, RemoveNumfmtMutation, SetNumfmtMutation, SetRangeValuesMutation, SheetInterceptorService, SheetsSelectionsService, UniverSheetsPlugin, checkCellValueType, factoryRemoveNumfmtUndoMutation, factorySetNumfmtUndoMutation, getSheetCommandTarget, rangeMerge, transformCellsToRange } from "@univerjs/sheets";
|
|
3
3
|
import { stripErrorMargin } from "@univerjs/engine-formula";
|
|
4
4
|
import { BehaviorSubject, merge as merge$1, of, skip, switchMap } from "rxjs";
|
|
@@ -19,27 +19,6 @@ import { BehaviorSubject, merge as merge$1, of, skip, switchMap } from "rxjs";
|
|
|
19
19
|
* See the License for the specific language governing permissions and
|
|
20
20
|
* limitations under the License.
|
|
21
21
|
*/
|
|
22
|
-
const localeCurrencySymbolMap = /* @__PURE__ */ new Map([
|
|
23
|
-
[LocaleType.EN_US, "$"],
|
|
24
|
-
[LocaleType.RU_RU, "₽"],
|
|
25
|
-
[LocaleType.VI_VN, "₫"],
|
|
26
|
-
[LocaleType.ZH_CN, "¥"],
|
|
27
|
-
[LocaleType.ZH_TW, "NT$"],
|
|
28
|
-
[LocaleType.ZH_HK, "HK$"],
|
|
29
|
-
[LocaleType.FR_FR, "€"],
|
|
30
|
-
[LocaleType.FA_IR, "﷼"],
|
|
31
|
-
[LocaleType.KO_KR, "₩"],
|
|
32
|
-
[LocaleType.ES_ES, "€"],
|
|
33
|
-
[LocaleType.CA_ES, "€"],
|
|
34
|
-
[LocaleType.SK_SK, "€"],
|
|
35
|
-
[LocaleType.JA_JP, "¥"],
|
|
36
|
-
[LocaleType.PT_BR, "R$"],
|
|
37
|
-
[LocaleType.DE_DE, "€"],
|
|
38
|
-
[LocaleType.IT_IT, "€"],
|
|
39
|
-
[LocaleType.ID_ID, "Rp"],
|
|
40
|
-
[LocaleType.PL_PL, "zł"],
|
|
41
|
-
[LocaleType.AR_SA, "﷼"]
|
|
42
|
-
]);
|
|
43
22
|
/**
|
|
44
23
|
* Get the currency symbol icon based on the locale.
|
|
45
24
|
*/
|
|
@@ -52,44 +31,44 @@ function getCurrencySymbolIconByLocale(locale) {
|
|
|
52
31
|
case LocaleType.IT_IT:
|
|
53
32
|
case LocaleType.SK_SK: return {
|
|
54
33
|
icon: "EuroIcon",
|
|
55
|
-
symbol:
|
|
34
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
56
35
|
locale
|
|
57
36
|
};
|
|
58
37
|
case LocaleType.RU_RU: return {
|
|
59
38
|
icon: "RoubleIcon",
|
|
60
|
-
symbol:
|
|
39
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
61
40
|
locale
|
|
62
41
|
};
|
|
63
42
|
case LocaleType.JA_JP:
|
|
64
43
|
case LocaleType.ZH_CN: return {
|
|
65
44
|
icon: "RmbIcon",
|
|
66
|
-
symbol:
|
|
45
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
67
46
|
locale
|
|
68
47
|
};
|
|
69
48
|
case LocaleType.VI_VN: return {
|
|
70
49
|
icon: "DongIcon",
|
|
71
|
-
symbol:
|
|
50
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
72
51
|
locale
|
|
73
52
|
};
|
|
74
53
|
case LocaleType.AR_SA:
|
|
75
54
|
case LocaleType.FA_IR: return {
|
|
76
55
|
icon: "RialIcon",
|
|
77
|
-
symbol:
|
|
56
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
78
57
|
locale
|
|
79
58
|
};
|
|
80
59
|
case LocaleType.KO_KR: return {
|
|
81
60
|
icon: "WonIcon",
|
|
82
|
-
symbol:
|
|
61
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
83
62
|
locale
|
|
84
63
|
};
|
|
85
64
|
case LocaleType.ID_ID: return {
|
|
86
65
|
icon: "RupiahIcon",
|
|
87
|
-
symbol:
|
|
66
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
88
67
|
locale
|
|
89
68
|
};
|
|
90
69
|
case LocaleType.PL_PL: return {
|
|
91
70
|
icon: "ZlotyIcon",
|
|
92
|
-
symbol:
|
|
71
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
93
72
|
locale
|
|
94
73
|
};
|
|
95
74
|
case LocaleType.EN_US:
|
|
@@ -98,18 +77,12 @@ function getCurrencySymbolIconByLocale(locale) {
|
|
|
98
77
|
case LocaleType.ZH_TW:
|
|
99
78
|
default: return {
|
|
100
79
|
icon: "DollarIcon",
|
|
101
|
-
symbol:
|
|
80
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
102
81
|
locale
|
|
103
82
|
};
|
|
104
83
|
}
|
|
105
84
|
}
|
|
106
85
|
/**
|
|
107
|
-
* Get the currency symbol by locale.
|
|
108
|
-
*/
|
|
109
|
-
function getCurrencySymbolByLocale(locale) {
|
|
110
|
-
return localeCurrencySymbolMap.get(locale) || "$";
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
86
|
* Get the currency format string based on the locale and number of digits.
|
|
114
87
|
*/
|
|
115
88
|
function getCurrencyFormat(locale, numberDigits = 2) {
|
|
@@ -257,6 +230,95 @@ const CURRENCYFORMAT = [
|
|
|
257
230
|
* See the License for the specific language governing permissions and
|
|
258
231
|
* limitations under the License.
|
|
259
232
|
*/
|
|
233
|
+
const MAX_DECIMALS = 30;
|
|
234
|
+
const MASKED_CHAR = "\0";
|
|
235
|
+
const getDecimalString = (length) => new Array(Math.min(Math.max(0, Number(length)), MAX_DECIMALS)).fill(0).join("");
|
|
236
|
+
/**
|
|
237
|
+
* Replaces quoted literals, escaped characters, `[...]` blocks and the operands of `_` / `*` with a
|
|
238
|
+
* placeholder. Format codes keep their position, so indexes found in the mask apply to the text.
|
|
239
|
+
*/
|
|
240
|
+
function maskSection(section) {
|
|
241
|
+
const chars = section.split("");
|
|
242
|
+
let inQuote = false;
|
|
243
|
+
let inBracket = false;
|
|
244
|
+
for (let i = 0; i < chars.length; i++) {
|
|
245
|
+
const char = chars[i];
|
|
246
|
+
if (inBracket) {
|
|
247
|
+
chars[i] = MASKED_CHAR;
|
|
248
|
+
if (char === "]") inBracket = false;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (char === "\"") {
|
|
252
|
+
chars[i] = MASKED_CHAR;
|
|
253
|
+
inQuote = !inQuote;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (inQuote) {
|
|
257
|
+
chars[i] = MASKED_CHAR;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (char === "\\" || char === "_" || char === "*") {
|
|
261
|
+
chars[i] = MASKED_CHAR;
|
|
262
|
+
if (i + 1 < chars.length) {
|
|
263
|
+
chars[i + 1] = MASKED_CHAR;
|
|
264
|
+
i += 1;
|
|
265
|
+
}
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (char === "[") {
|
|
269
|
+
chars[i] = MASKED_CHAR;
|
|
270
|
+
inBracket = true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return chars.join("");
|
|
274
|
+
}
|
|
275
|
+
function splitSections(pattern) {
|
|
276
|
+
const mask = maskSection(pattern);
|
|
277
|
+
const sections = [];
|
|
278
|
+
let start = 0;
|
|
279
|
+
for (let i = 0; i <= mask.length; i++) if (i === mask.length || mask[i] === ";") {
|
|
280
|
+
sections.push({
|
|
281
|
+
mask: mask.slice(start, i),
|
|
282
|
+
text: pattern.slice(start, i)
|
|
283
|
+
});
|
|
284
|
+
start = i + 1;
|
|
285
|
+
}
|
|
286
|
+
return sections;
|
|
287
|
+
}
|
|
288
|
+
function isDateSection(section) {
|
|
289
|
+
return section.text !== "" && numfmt.getFormatInfo(section.text).isDate;
|
|
290
|
+
}
|
|
291
|
+
function isFractionPlaceholder(char) {
|
|
292
|
+
return char === "0" || char === "#" || char === "?";
|
|
293
|
+
}
|
|
294
|
+
function findIntegerZero(mask) {
|
|
295
|
+
for (let i = 0; i < mask.length; i++) if (mask[i] === "0" && mask[i + 1] !== "0") return i;
|
|
296
|
+
return -1;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Grows or shrinks the fractional placeholders of a number section one at a time, the way Excel does:
|
|
300
|
+
* adding copies the last fractional placeholder, removing drops the last one and the decimal point
|
|
301
|
+
* with the last placeholder.
|
|
302
|
+
*/
|
|
303
|
+
function setNumberSectionDecimal(section, decimalLength) {
|
|
304
|
+
const dotIndex = section.mask.indexOf(".");
|
|
305
|
+
if (dotIndex < 0) {
|
|
306
|
+
if (decimalLength === 0) return section.text;
|
|
307
|
+
const zeroIndex = findIntegerZero(section.mask);
|
|
308
|
+
if (zeroIndex < 0) return section.text;
|
|
309
|
+
return `${section.text.slice(0, zeroIndex + 1)}.${getDecimalString(decimalLength)}${section.text.slice(zeroIndex + 1)}`;
|
|
310
|
+
}
|
|
311
|
+
let end = dotIndex + 1;
|
|
312
|
+
while (isFractionPlaceholder(section.mask[end])) end += 1;
|
|
313
|
+
const fraction = section.text.slice(dotIndex + 1, end);
|
|
314
|
+
if (fraction.length === 0 && decimalLength === 0) return `${section.text.slice(0, dotIndex)}${section.text.slice(dotIndex + 1)}`;
|
|
315
|
+
if (decimalLength >= fraction.length) {
|
|
316
|
+
const filler = fraction.length > 0 ? fraction[fraction.length - 1] : "0";
|
|
317
|
+
return `${section.text.slice(0, end)}${filler.repeat(decimalLength - fraction.length)}${section.text.slice(end)}`;
|
|
318
|
+
}
|
|
319
|
+
const nextFraction = fraction.slice(0, decimalLength);
|
|
320
|
+
return nextFraction.length > 0 ? `${section.text.slice(0, dotIndex)}.${nextFraction}${section.text.slice(end)}` : `${section.text.slice(0, dotIndex)}${section.text.slice(end)}`;
|
|
321
|
+
}
|
|
260
322
|
/**
|
|
261
323
|
* the function decimal just use positive,negative configuration ignored
|
|
262
324
|
*/
|
|
@@ -264,13 +326,14 @@ const getDecimalFromPattern = (pattern, defaultValue = 0) => {
|
|
|
264
326
|
if (!pattern) return defaultValue;
|
|
265
327
|
return numfmt.getFormatInfo(pattern).maxDecimals ?? defaultValue;
|
|
266
328
|
};
|
|
267
|
-
|
|
329
|
+
/**
|
|
330
|
+
* Changes the decimal places of a number format. Date and time formats are returned unchanged, even
|
|
331
|
+
* when they show fractional seconds, because Excel's increase/decrease decimal commands do not apply
|
|
332
|
+
* to them.
|
|
333
|
+
*/
|
|
268
334
|
const setPatternDecimal = (patterns, decimalLength) => {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (/0([^0]?)|0$/.test(pattern)) return pattern.replace(/0([^0]+)|0$/, `0${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}$1`);
|
|
272
|
-
return pattern;
|
|
273
|
-
}).join(";");
|
|
335
|
+
const length = Math.min(Math.max(0, Math.floor(Number(decimalLength) || 0)), MAX_DECIMALS);
|
|
336
|
+
return splitSections(patterns).map((section) => isDateSection(section) ? section.text : setNumberSectionDecimal(section, length)).join(";");
|
|
274
337
|
};
|
|
275
338
|
const isPatternHasDecimal = (pattern) => /\.0?/.test(pattern) || /0([^0]?)|0$/.test(pattern);
|
|
276
339
|
|
|
@@ -597,15 +660,17 @@ const SubtractDecimalCommand = {
|
|
|
597
660
|
pattern: isScientificNotationNumericCell(cell) ? setPatternDecimal(getScientificNotationFormatFromCell(cell), decimals) : defaultPattern
|
|
598
661
|
});
|
|
599
662
|
} else {
|
|
600
|
-
const
|
|
601
|
-
|
|
663
|
+
const pattern = numfmtValue.pattern;
|
|
664
|
+
const nextPattern = setPatternDecimal(pattern, getDecimalFromPattern(pattern) - 1);
|
|
665
|
+
nextPattern !== pattern && values.push({
|
|
602
666
|
row,
|
|
603
667
|
col,
|
|
604
|
-
pattern:
|
|
668
|
+
pattern: nextPattern
|
|
605
669
|
});
|
|
606
670
|
}
|
|
607
671
|
});
|
|
608
672
|
});
|
|
673
|
+
if (!values.length) return false;
|
|
609
674
|
return await commandService.executeCommand(SetNumfmtCommand.id, { values });
|
|
610
675
|
}
|
|
611
676
|
};
|
|
@@ -630,7 +695,7 @@ const SHEETS_NUMFMT_PLUGIN_CONFIG_KEY = "sheets-numfmt.config";
|
|
|
630
695
|
const configSymbol = Symbol(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY);
|
|
631
696
|
const defaultPluginConfig = {};
|
|
632
697
|
|
|
633
|
-
// \0@oxc-project+runtime@0.
|
|
698
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
|
|
634
699
|
function _typeof(o) {
|
|
635
700
|
"@babel/helpers - typeof";
|
|
636
701
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -640,7 +705,7 @@ function _typeof(o) {
|
|
|
640
705
|
}, _typeof(o);
|
|
641
706
|
}
|
|
642
707
|
|
|
643
|
-
// \0@oxc-project+runtime@0.
|
|
708
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
|
|
644
709
|
function toPrimitive(t, e) {
|
|
645
710
|
if ("object" != _typeof(t) || !t) return t;
|
|
646
711
|
var r;
|
|
@@ -652,13 +717,13 @@ function toPrimitive(t, e) {
|
|
|
652
717
|
return ("string" === e ? String : Number)(t);
|
|
653
718
|
}
|
|
654
719
|
|
|
655
|
-
// \0@oxc-project+runtime@0.
|
|
720
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
|
|
656
721
|
function toPropertyKey(t) {
|
|
657
722
|
var i = toPrimitive(t, "string");
|
|
658
723
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
659
724
|
}
|
|
660
725
|
|
|
661
|
-
// \0@oxc-project+runtime@0.
|
|
726
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
|
|
662
727
|
function _defineProperty(e, r, t) {
|
|
663
728
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
664
729
|
value: t,
|
|
@@ -668,14 +733,14 @@ function _defineProperty(e, r, t) {
|
|
|
668
733
|
}) : e[r] = t, e;
|
|
669
734
|
}
|
|
670
735
|
|
|
671
|
-
// \0@oxc-project+runtime@0.
|
|
736
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
|
|
672
737
|
function __decorateParam(paramIndex, decorator) {
|
|
673
738
|
return function(target, key) {
|
|
674
739
|
decorator(target, key, paramIndex);
|
|
675
740
|
};
|
|
676
741
|
}
|
|
677
742
|
|
|
678
|
-
// \0@oxc-project+runtime@0.
|
|
743
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
|
|
679
744
|
function __decorate(decorators, target, key, desc) {
|
|
680
745
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
681
746
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -816,7 +881,7 @@ SheetsNumfmtCellContentController = __decorate([
|
|
|
816
881
|
|
|
817
882
|
// package.json
|
|
818
883
|
var name = "@univerjs/sheets-numfmt";
|
|
819
|
-
var version = "1.
|
|
884
|
+
var version = "1.6.0";
|
|
820
885
|
|
|
821
886
|
// src/base/const/plugin-name.ts
|
|
822
887
|
/**
|
|
@@ -1047,4 +1112,4 @@ const getNumberFormatOptions = () => NUMBERFORMAT.map((item) => ({
|
|
|
1047
1112
|
color: item.color
|
|
1048
1113
|
}));
|
|
1049
1114
|
|
|
1050
|
-
export { AddDecimalCommand, CURRENCYFORMAT, DATEFMTLISG, NUMBERFORMAT, SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, SetCurrencyCommand, SetNumfmtCommand, SetPercentCommand, SheetsNumfmtCellContentController, SubtractDecimalCommand, UniverSheetsNumfmtPlugin, getCurrencyFormat, getCurrencyFormatOptions, getCurrencyOptions,
|
|
1115
|
+
export { AddDecimalCommand, CURRENCYFORMAT, DATEFMTLISG, NUMBERFORMAT, SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, SetCurrencyCommand, SetNumfmtCommand, SetPercentCommand, SheetsNumfmtCellContentController, SubtractDecimalCommand, UniverSheetsNumfmtPlugin, getCurrencyFormat, getCurrencyFormatOptions, getCurrencyOptions, getCurrencySymbolIconByLocale, getCurrencyType, getDateFormatOptions, getDecimalFromPattern, getDecimalString, getNumberFormatOptions, getPatternPreview, getPatternPreviewIgnoreGeneral, getPatternType, isPatternHasDecimal, setPatternDecimal };
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CellValueType, CommandType, DEFAULT_NUMBER_FORMAT, DependentOn, Disposable, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, LocaleService, LocaleType, ObjectMatrix, Plugin, Range, RegionService, ThemeService, Tools, UniverInstanceType, currencySymbols, excelDateTimeSerial, getNumfmtLocaleTag, getNumfmtParseValueFilter, isDefaultFormat, isRealNum, isTextFormat, merge, numfmt, registerDependencies, sequenceExecute, touchDependencies } from "@univerjs/core";
|
|
1
|
+
import { CellValueType, CommandType, DEFAULT_NUMBER_FORMAT, DependentOn, Disposable, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, LocaleService, LocaleType, ObjectMatrix, Plugin, Range, RegionService, ThemeService, Tools, UniverInstanceType, currencySymbols, excelDateTimeSerial, getCurrencySymbolByLocale, getNumfmtLocaleTag, getNumfmtParseValueFilter, isDefaultFormat, isRealNum, isTextFormat, merge, numfmt, registerDependencies, sequenceExecute, touchDependencies } from "@univerjs/core";
|
|
2
2
|
import { INTERCEPTOR_POINT, INumfmtService, InterceptCellContentPriority, RemoveNumfmtMutation, SetNumfmtMutation, SetRangeValuesMutation, SheetInterceptorService, SheetsSelectionsService, UniverSheetsPlugin, checkCellValueType, factoryRemoveNumfmtUndoMutation, factorySetNumfmtUndoMutation, getSheetCommandTarget, rangeMerge, transformCellsToRange } from "@univerjs/sheets";
|
|
3
3
|
import { stripErrorMargin } from "@univerjs/engine-formula";
|
|
4
4
|
import { BehaviorSubject, merge as merge$1, of, skip, switchMap } from "rxjs";
|
|
@@ -19,27 +19,6 @@ import { BehaviorSubject, merge as merge$1, of, skip, switchMap } from "rxjs";
|
|
|
19
19
|
* See the License for the specific language governing permissions and
|
|
20
20
|
* limitations under the License.
|
|
21
21
|
*/
|
|
22
|
-
const localeCurrencySymbolMap = /* @__PURE__ */ new Map([
|
|
23
|
-
[LocaleType.EN_US, "$"],
|
|
24
|
-
[LocaleType.RU_RU, "₽"],
|
|
25
|
-
[LocaleType.VI_VN, "₫"],
|
|
26
|
-
[LocaleType.ZH_CN, "¥"],
|
|
27
|
-
[LocaleType.ZH_TW, "NT$"],
|
|
28
|
-
[LocaleType.ZH_HK, "HK$"],
|
|
29
|
-
[LocaleType.FR_FR, "€"],
|
|
30
|
-
[LocaleType.FA_IR, "﷼"],
|
|
31
|
-
[LocaleType.KO_KR, "₩"],
|
|
32
|
-
[LocaleType.ES_ES, "€"],
|
|
33
|
-
[LocaleType.CA_ES, "€"],
|
|
34
|
-
[LocaleType.SK_SK, "€"],
|
|
35
|
-
[LocaleType.JA_JP, "¥"],
|
|
36
|
-
[LocaleType.PT_BR, "R$"],
|
|
37
|
-
[LocaleType.DE_DE, "€"],
|
|
38
|
-
[LocaleType.IT_IT, "€"],
|
|
39
|
-
[LocaleType.ID_ID, "Rp"],
|
|
40
|
-
[LocaleType.PL_PL, "zł"],
|
|
41
|
-
[LocaleType.AR_SA, "﷼"]
|
|
42
|
-
]);
|
|
43
22
|
/**
|
|
44
23
|
* Get the currency symbol icon based on the locale.
|
|
45
24
|
*/
|
|
@@ -52,44 +31,44 @@ function getCurrencySymbolIconByLocale(locale) {
|
|
|
52
31
|
case LocaleType.IT_IT:
|
|
53
32
|
case LocaleType.SK_SK: return {
|
|
54
33
|
icon: "EuroIcon",
|
|
55
|
-
symbol:
|
|
34
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
56
35
|
locale
|
|
57
36
|
};
|
|
58
37
|
case LocaleType.RU_RU: return {
|
|
59
38
|
icon: "RoubleIcon",
|
|
60
|
-
symbol:
|
|
39
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
61
40
|
locale
|
|
62
41
|
};
|
|
63
42
|
case LocaleType.JA_JP:
|
|
64
43
|
case LocaleType.ZH_CN: return {
|
|
65
44
|
icon: "RmbIcon",
|
|
66
|
-
symbol:
|
|
45
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
67
46
|
locale
|
|
68
47
|
};
|
|
69
48
|
case LocaleType.VI_VN: return {
|
|
70
49
|
icon: "DongIcon",
|
|
71
|
-
symbol:
|
|
50
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
72
51
|
locale
|
|
73
52
|
};
|
|
74
53
|
case LocaleType.AR_SA:
|
|
75
54
|
case LocaleType.FA_IR: return {
|
|
76
55
|
icon: "RialIcon",
|
|
77
|
-
symbol:
|
|
56
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
78
57
|
locale
|
|
79
58
|
};
|
|
80
59
|
case LocaleType.KO_KR: return {
|
|
81
60
|
icon: "WonIcon",
|
|
82
|
-
symbol:
|
|
61
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
83
62
|
locale
|
|
84
63
|
};
|
|
85
64
|
case LocaleType.ID_ID: return {
|
|
86
65
|
icon: "RupiahIcon",
|
|
87
|
-
symbol:
|
|
66
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
88
67
|
locale
|
|
89
68
|
};
|
|
90
69
|
case LocaleType.PL_PL: return {
|
|
91
70
|
icon: "ZlotyIcon",
|
|
92
|
-
symbol:
|
|
71
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
93
72
|
locale
|
|
94
73
|
};
|
|
95
74
|
case LocaleType.EN_US:
|
|
@@ -98,18 +77,12 @@ function getCurrencySymbolIconByLocale(locale) {
|
|
|
98
77
|
case LocaleType.ZH_TW:
|
|
99
78
|
default: return {
|
|
100
79
|
icon: "DollarIcon",
|
|
101
|
-
symbol:
|
|
80
|
+
symbol: getCurrencySymbolByLocale(locale),
|
|
102
81
|
locale
|
|
103
82
|
};
|
|
104
83
|
}
|
|
105
84
|
}
|
|
106
85
|
/**
|
|
107
|
-
* Get the currency symbol by locale.
|
|
108
|
-
*/
|
|
109
|
-
function getCurrencySymbolByLocale(locale) {
|
|
110
|
-
return localeCurrencySymbolMap.get(locale) || "$";
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
86
|
* Get the currency format string based on the locale and number of digits.
|
|
114
87
|
*/
|
|
115
88
|
function getCurrencyFormat(locale, numberDigits = 2) {
|
|
@@ -257,6 +230,95 @@ const CURRENCYFORMAT = [
|
|
|
257
230
|
* See the License for the specific language governing permissions and
|
|
258
231
|
* limitations under the License.
|
|
259
232
|
*/
|
|
233
|
+
const MAX_DECIMALS = 30;
|
|
234
|
+
const MASKED_CHAR = "\0";
|
|
235
|
+
const getDecimalString = (length) => new Array(Math.min(Math.max(0, Number(length)), MAX_DECIMALS)).fill(0).join("");
|
|
236
|
+
/**
|
|
237
|
+
* Replaces quoted literals, escaped characters, `[...]` blocks and the operands of `_` / `*` with a
|
|
238
|
+
* placeholder. Format codes keep their position, so indexes found in the mask apply to the text.
|
|
239
|
+
*/
|
|
240
|
+
function maskSection(section) {
|
|
241
|
+
const chars = section.split("");
|
|
242
|
+
let inQuote = false;
|
|
243
|
+
let inBracket = false;
|
|
244
|
+
for (let i = 0; i < chars.length; i++) {
|
|
245
|
+
const char = chars[i];
|
|
246
|
+
if (inBracket) {
|
|
247
|
+
chars[i] = MASKED_CHAR;
|
|
248
|
+
if (char === "]") inBracket = false;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (char === "\"") {
|
|
252
|
+
chars[i] = MASKED_CHAR;
|
|
253
|
+
inQuote = !inQuote;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (inQuote) {
|
|
257
|
+
chars[i] = MASKED_CHAR;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (char === "\\" || char === "_" || char === "*") {
|
|
261
|
+
chars[i] = MASKED_CHAR;
|
|
262
|
+
if (i + 1 < chars.length) {
|
|
263
|
+
chars[i + 1] = MASKED_CHAR;
|
|
264
|
+
i += 1;
|
|
265
|
+
}
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (char === "[") {
|
|
269
|
+
chars[i] = MASKED_CHAR;
|
|
270
|
+
inBracket = true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return chars.join("");
|
|
274
|
+
}
|
|
275
|
+
function splitSections(pattern) {
|
|
276
|
+
const mask = maskSection(pattern);
|
|
277
|
+
const sections = [];
|
|
278
|
+
let start = 0;
|
|
279
|
+
for (let i = 0; i <= mask.length; i++) if (i === mask.length || mask[i] === ";") {
|
|
280
|
+
sections.push({
|
|
281
|
+
mask: mask.slice(start, i),
|
|
282
|
+
text: pattern.slice(start, i)
|
|
283
|
+
});
|
|
284
|
+
start = i + 1;
|
|
285
|
+
}
|
|
286
|
+
return sections;
|
|
287
|
+
}
|
|
288
|
+
function isDateSection(section) {
|
|
289
|
+
return section.text !== "" && numfmt.getFormatInfo(section.text).isDate;
|
|
290
|
+
}
|
|
291
|
+
function isFractionPlaceholder(char) {
|
|
292
|
+
return char === "0" || char === "#" || char === "?";
|
|
293
|
+
}
|
|
294
|
+
function findIntegerZero(mask) {
|
|
295
|
+
for (let i = 0; i < mask.length; i++) if (mask[i] === "0" && mask[i + 1] !== "0") return i;
|
|
296
|
+
return -1;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Grows or shrinks the fractional placeholders of a number section one at a time, the way Excel does:
|
|
300
|
+
* adding copies the last fractional placeholder, removing drops the last one and the decimal point
|
|
301
|
+
* with the last placeholder.
|
|
302
|
+
*/
|
|
303
|
+
function setNumberSectionDecimal(section, decimalLength) {
|
|
304
|
+
const dotIndex = section.mask.indexOf(".");
|
|
305
|
+
if (dotIndex < 0) {
|
|
306
|
+
if (decimalLength === 0) return section.text;
|
|
307
|
+
const zeroIndex = findIntegerZero(section.mask);
|
|
308
|
+
if (zeroIndex < 0) return section.text;
|
|
309
|
+
return `${section.text.slice(0, zeroIndex + 1)}.${getDecimalString(decimalLength)}${section.text.slice(zeroIndex + 1)}`;
|
|
310
|
+
}
|
|
311
|
+
let end = dotIndex + 1;
|
|
312
|
+
while (isFractionPlaceholder(section.mask[end])) end += 1;
|
|
313
|
+
const fraction = section.text.slice(dotIndex + 1, end);
|
|
314
|
+
if (fraction.length === 0 && decimalLength === 0) return `${section.text.slice(0, dotIndex)}${section.text.slice(dotIndex + 1)}`;
|
|
315
|
+
if (decimalLength >= fraction.length) {
|
|
316
|
+
const filler = fraction.length > 0 ? fraction[fraction.length - 1] : "0";
|
|
317
|
+
return `${section.text.slice(0, end)}${filler.repeat(decimalLength - fraction.length)}${section.text.slice(end)}`;
|
|
318
|
+
}
|
|
319
|
+
const nextFraction = fraction.slice(0, decimalLength);
|
|
320
|
+
return nextFraction.length > 0 ? `${section.text.slice(0, dotIndex)}.${nextFraction}${section.text.slice(end)}` : `${section.text.slice(0, dotIndex)}${section.text.slice(end)}`;
|
|
321
|
+
}
|
|
260
322
|
/**
|
|
261
323
|
* the function decimal just use positive,negative configuration ignored
|
|
262
324
|
*/
|
|
@@ -264,13 +326,14 @@ const getDecimalFromPattern = (pattern, defaultValue = 0) => {
|
|
|
264
326
|
if (!pattern) return defaultValue;
|
|
265
327
|
return numfmt.getFormatInfo(pattern).maxDecimals ?? defaultValue;
|
|
266
328
|
};
|
|
267
|
-
|
|
329
|
+
/**
|
|
330
|
+
* Changes the decimal places of a number format. Date and time formats are returned unchanged, even
|
|
331
|
+
* when they show fractional seconds, because Excel's increase/decrease decimal commands do not apply
|
|
332
|
+
* to them.
|
|
333
|
+
*/
|
|
268
334
|
const setPatternDecimal = (patterns, decimalLength) => {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (/0([^0]?)|0$/.test(pattern)) return pattern.replace(/0([^0]+)|0$/, `0${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}$1`);
|
|
272
|
-
return pattern;
|
|
273
|
-
}).join(";");
|
|
335
|
+
const length = Math.min(Math.max(0, Math.floor(Number(decimalLength) || 0)), MAX_DECIMALS);
|
|
336
|
+
return splitSections(patterns).map((section) => isDateSection(section) ? section.text : setNumberSectionDecimal(section, length)).join(";");
|
|
274
337
|
};
|
|
275
338
|
const isPatternHasDecimal = (pattern) => /\.0?/.test(pattern) || /0([^0]?)|0$/.test(pattern);
|
|
276
339
|
|
|
@@ -597,15 +660,17 @@ const SubtractDecimalCommand = {
|
|
|
597
660
|
pattern: isScientificNotationNumericCell(cell) ? setPatternDecimal(getScientificNotationFormatFromCell(cell), decimals) : defaultPattern
|
|
598
661
|
});
|
|
599
662
|
} else {
|
|
600
|
-
const
|
|
601
|
-
|
|
663
|
+
const pattern = numfmtValue.pattern;
|
|
664
|
+
const nextPattern = setPatternDecimal(pattern, getDecimalFromPattern(pattern) - 1);
|
|
665
|
+
nextPattern !== pattern && values.push({
|
|
602
666
|
row,
|
|
603
667
|
col,
|
|
604
|
-
pattern:
|
|
668
|
+
pattern: nextPattern
|
|
605
669
|
});
|
|
606
670
|
}
|
|
607
671
|
});
|
|
608
672
|
});
|
|
673
|
+
if (!values.length) return false;
|
|
609
674
|
return await commandService.executeCommand(SetNumfmtCommand.id, { values });
|
|
610
675
|
}
|
|
611
676
|
};
|
|
@@ -630,7 +695,7 @@ const SHEETS_NUMFMT_PLUGIN_CONFIG_KEY = "sheets-numfmt.config";
|
|
|
630
695
|
const configSymbol = Symbol(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY);
|
|
631
696
|
const defaultPluginConfig = {};
|
|
632
697
|
|
|
633
|
-
// \0@oxc-project+runtime@0.
|
|
698
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
|
|
634
699
|
function _typeof(o) {
|
|
635
700
|
"@babel/helpers - typeof";
|
|
636
701
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -640,7 +705,7 @@ function _typeof(o) {
|
|
|
640
705
|
}, _typeof(o);
|
|
641
706
|
}
|
|
642
707
|
|
|
643
|
-
// \0@oxc-project+runtime@0.
|
|
708
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
|
|
644
709
|
function toPrimitive(t, e) {
|
|
645
710
|
if ("object" != _typeof(t) || !t) return t;
|
|
646
711
|
var r;
|
|
@@ -652,13 +717,13 @@ function toPrimitive(t, e) {
|
|
|
652
717
|
return ("string" === e ? String : Number)(t);
|
|
653
718
|
}
|
|
654
719
|
|
|
655
|
-
// \0@oxc-project+runtime@0.
|
|
720
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
|
|
656
721
|
function toPropertyKey(t) {
|
|
657
722
|
var i = toPrimitive(t, "string");
|
|
658
723
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
659
724
|
}
|
|
660
725
|
|
|
661
|
-
// \0@oxc-project+runtime@0.
|
|
726
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
|
|
662
727
|
function _defineProperty(e, r, t) {
|
|
663
728
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
664
729
|
value: t,
|
|
@@ -668,14 +733,14 @@ function _defineProperty(e, r, t) {
|
|
|
668
733
|
}) : e[r] = t, e;
|
|
669
734
|
}
|
|
670
735
|
|
|
671
|
-
// \0@oxc-project+runtime@0.
|
|
736
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/decorateParam.js
|
|
672
737
|
function __decorateParam(paramIndex, decorator) {
|
|
673
738
|
return function(target, key) {
|
|
674
739
|
decorator(target, key, paramIndex);
|
|
675
740
|
};
|
|
676
741
|
}
|
|
677
742
|
|
|
678
|
-
// \0@oxc-project+runtime@0.
|
|
743
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/decorate.js
|
|
679
744
|
function __decorate(decorators, target, key, desc) {
|
|
680
745
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
681
746
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -816,7 +881,7 @@ SheetsNumfmtCellContentController = __decorate([
|
|
|
816
881
|
|
|
817
882
|
// package.json
|
|
818
883
|
var name = "@univerjs/sheets-numfmt";
|
|
819
|
-
var version = "1.
|
|
884
|
+
var version = "1.6.0";
|
|
820
885
|
|
|
821
886
|
// src/base/const/plugin-name.ts
|
|
822
887
|
/**
|
|
@@ -1047,4 +1112,4 @@ const getNumberFormatOptions = () => NUMBERFORMAT.map((item) => ({
|
|
|
1047
1112
|
color: item.color
|
|
1048
1113
|
}));
|
|
1049
1114
|
|
|
1050
|
-
export { AddDecimalCommand, CURRENCYFORMAT, DATEFMTLISG, NUMBERFORMAT, SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, SetCurrencyCommand, SetNumfmtCommand, SetPercentCommand, SheetsNumfmtCellContentController, SubtractDecimalCommand, UniverSheetsNumfmtPlugin, getCurrencyFormat, getCurrencyFormatOptions, getCurrencyOptions,
|
|
1115
|
+
export { AddDecimalCommand, CURRENCYFORMAT, DATEFMTLISG, NUMBERFORMAT, SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, SetCurrencyCommand, SetNumfmtCommand, SetPercentCommand, SheetsNumfmtCellContentController, SubtractDecimalCommand, UniverSheetsNumfmtPlugin, getCurrencyFormat, getCurrencyFormatOptions, getCurrencyOptions, getCurrencySymbolIconByLocale, getCurrencyType, getDateFormatOptions, getDecimalFromPattern, getDecimalString, getNumberFormatOptions, getPatternPreview, getPatternPreviewIgnoreGeneral, getPatternType, isPatternHasDecimal, setPatternDecimal };
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import { LocaleType } from '@univerjs/core';
|
|
17
|
-
export declare const localeCurrencySymbolMap: Map<LocaleType, string>;
|
|
18
17
|
/**
|
|
19
18
|
* Get the currency symbol icon based on the locale.
|
|
20
19
|
*/
|
|
@@ -55,10 +54,6 @@ export declare function getCurrencySymbolIconByLocale(locale: LocaleType): {
|
|
|
55
54
|
symbol: string;
|
|
56
55
|
locale: LocaleType.EN_US | LocaleType.ZH_TW | LocaleType.ZH_HK | LocaleType.PT_BR;
|
|
57
56
|
};
|
|
58
|
-
/**
|
|
59
|
-
* Get the currency symbol by locale.
|
|
60
|
-
*/
|
|
61
|
-
export declare function getCurrencySymbolByLocale(locale: LocaleType): string;
|
|
62
57
|
/**
|
|
63
58
|
* Get the currency format string based on the locale and number of digits.
|
|
64
59
|
*/
|
package/lib/types/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
export { getCurrencyFormat,
|
|
16
|
+
export { getCurrencyFormat, getCurrencySymbolIconByLocale, } from './base/const/currency-symbols';
|
|
17
17
|
export { CURRENCYFORMAT, DATEFMTLISG, NUMBERFORMAT } from './base/const/formatdetail';
|
|
18
18
|
export { AddDecimalCommand } from './commands/commands/add-decimal.command';
|
|
19
19
|
export { SetCurrencyCommand } from './commands/commands/set-currency.command';
|
|
@@ -13,10 +13,15 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
export declare const getDecimalString: (length: number) => string;
|
|
16
17
|
/**
|
|
17
18
|
* the function decimal just use positive,negative configuration ignored
|
|
18
19
|
*/
|
|
19
20
|
export declare const getDecimalFromPattern: (pattern: string, defaultValue?: number) => number;
|
|
20
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Changes the decimal places of a number format. Date and time formats are returned unchanged, even
|
|
23
|
+
* when they show fractional seconds, because Excel's increase/decrease decimal commands do not apply
|
|
24
|
+
* to them.
|
|
25
|
+
*/
|
|
21
26
|
export declare const setPatternDecimal: (patterns: string, decimalLength: number) => string;
|
|
22
27
|
export declare const isPatternHasDecimal: (pattern: string) => boolean;
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/sheets`,`@univerjs/engine-formula`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsNumfmt={},e.UniverCore,e.UniverSheets,e.UniverEngineFormula,e.rxjs))})(this,function(e,t,n,r,i){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let a=/* @__PURE__ */ new Map([[t.LocaleType.EN_US,`$`],[t.LocaleType.RU_RU,`₽`],[t.LocaleType.VI_VN,`₫`],[t.LocaleType.ZH_CN,`¥`],[t.LocaleType.ZH_TW,`NT$`],[t.LocaleType.ZH_HK,`HK$`],[t.LocaleType.FR_FR,`€`],[t.LocaleType.FA_IR,`﷼`],[t.LocaleType.KO_KR,`₩`],[t.LocaleType.ES_ES,`€`],[t.LocaleType.CA_ES,`€`],[t.LocaleType.SK_SK,`€`],[t.LocaleType.JA_JP,`¥`],[t.LocaleType.PT_BR,`R$`],[t.LocaleType.DE_DE,`€`],[t.LocaleType.IT_IT,`€`],[t.LocaleType.ID_ID,`Rp`],[t.LocaleType.PL_PL,`zł`],[t.LocaleType.AR_SA,`﷼`]]);function o(e){switch(e){case t.LocaleType.CA_ES:case t.LocaleType.DE_DE:case t.LocaleType.ES_ES:case t.LocaleType.FR_FR:case t.LocaleType.IT_IT:case t.LocaleType.SK_SK:return{icon:`EuroIcon`,symbol:a.get(e)||`€`,locale:e};case t.LocaleType.RU_RU:return{icon:`RoubleIcon`,symbol:a.get(e)||`₽`,locale:e};case t.LocaleType.JA_JP:case t.LocaleType.ZH_CN:return{icon:`RmbIcon`,symbol:a.get(e)||`¥`,locale:e};case t.LocaleType.VI_VN:return{icon:`DongIcon`,symbol:a.get(e)||`₫`,locale:e};case t.LocaleType.AR_SA:case t.LocaleType.FA_IR:return{icon:`RialIcon`,symbol:a.get(e)||`﷼`,locale:e};case t.LocaleType.KO_KR:return{icon:`WonIcon`,symbol:a.get(e)||`₩`,locale:e};case t.LocaleType.ID_ID:return{icon:`RupiahIcon`,symbol:a.get(e)||`Rp`,locale:e};case t.LocaleType.PL_PL:return{icon:`ZlotyIcon`,symbol:a.get(e)||`zł`,locale:e};case t.LocaleType.EN_US:case t.LocaleType.PT_BR:case t.LocaleType.ZH_HK:case t.LocaleType.ZH_TW:default:return{icon:`DollarIcon`,symbol:a.get(e)||`$`,locale:e}}}function s(e){return a.get(e)||`$`}function c(e,t=2){let n=t;t>127&&(n=127);let r=``;return n>0&&(r=`.${`0`.repeat(n)}`),`"${s(e)}"#,##0${r}_);[Red]("${s(e)}"#,##0${r})`}let l=[{label:`1930-08-05`,suffix:`yyyy-MM-dd`},{label:`1930/08/05`,suffix:`yyyy/MM/dd`},{label:`1930年08月05日`,suffix:`yyyy"年"MM"月"dd"日"`},{label:`08-05`,suffix:`MM-dd`},{label:`8月5日`,suffix:`M"月"d"日"`},{label:`13:30:30`,suffix:`h:mm:ss`},{label:`13:30`,suffix:`h:mm`},{label:`下午01:30`,suffix:`A/P hh:mm`},{label:`下午1:30`,suffix:`A/P h:mm`},{label:`下午1:30:30`,suffix:`A/P h:mm:ss`},{label:`08-05 下午 01:30`,suffix:`MM-dd A/P hh:mm`}],u=[{label:`(1,235)`,suffix:`#,##0_);(#,##0)`},{label:`(1,235) `,suffix:`#,##0_);[Red](#,##0)`,color:`red`},{label:`1,234.56`,suffix:`#,##0.00_);#,##0.00`},{label:`1,234.56`,suffix:`#,##0.00_);[Red]#,##0.00`,color:`red`},{label:`-1,234.56`,suffix:`#,##0.00_);-#,##0.00`},{label:`-1,234.56`,suffix:`#,##0.00_);[Red]-#,##0.00`,color:`red`}],d=[{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);"${e}"#,##0.00`},{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,color:`red`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);("${e}"#,##0.00)`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,color:`red`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);-"${e}"#,##0.00`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,color:`red`}],f=(e,n=0)=>e?t.numfmt.getFormatInfo(e).maxDecimals??n:n,p=e=>Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(``),m=(e,t)=>e.split(`;`).map(e=>/\.0?/.test(e)?e.replace(/\.0*/g,`${t>0?`.`:``}${p(Number(t||0))}`):/0([^0]?)|0$/.test(e)?e.replace(/0([^0]+)|0$/,`0${t>0?`.`:``}${p(Number(t||0))}$1`):e).join(`;`),h=e=>/\.0?/.test(e)||/0([^0]?)|0$/.test(e),g=e=>{let n=t.numfmt.getFormatInfo(e).type||`unknown`;return n===`general`&&t.numfmt.tokenize(e).some(e=>e.type===t.numfmt.tokenTypes.CONDITION)?`number`:n},_=(e,n,r=`en`,i)=>{try{let a=t.numfmt.formatColor(e,n),o=a?String(a):void 0;return{result:t.numfmt.format(e,n,{locale:r,dateSystem:i,throws:!1}),color:o}}catch(t){console.warn(`getPatternPreview error:`,e,t)}return{result:String(n)}},v=(e,n,i,a)=>e===t.DEFAULT_NUMBER_FORMAT?{result:String((0,r.stripErrorMargin)(n))}:_(e,n,i,a);function y(e){return(e==null?void 0:e.t)===t.CellValueType.NUMBER&&e.v!==void 0&&e.v!==null&&/e/i.test(String(e.v))}function b(e){let t=String(e==null?void 0:e.v),n=t.search(/e/i),r=t.indexOf(`.`),i=n>-1&&r>-1?n-r-1:0;return`0.${`0`.repeat(Math.max(2,i))}E+00`}function x(e){return(0,t.isDefaultFormat)(e)||(0,t.isTextFormat)(e)||g(e)===`scientific`}let S={id:`sheet.command.numfmt.set.numfmt`,type:t.CommandType.COMMAND,handler:(e,r)=>{if(!r)return!1;let i=e.get(t.ICommandService),a=e.get(t.IUniverInstanceService),o=e.get(t.IUndoRedoService),s=(0,n.getSheetCommandTarget)(a,r);if(!s)return!1;let{unitId:c,subUnitId:l,worksheet:u}=s,d=r.values.filter(e=>!y(u.getCellRaw(e.row,e.col))||x(e.pattern)),f=d.filter(e=>!!e.pattern),p=d.filter(e=>!e.pattern);if(!f.length&&!p.length)return!1;let m=(0,n.transformCellsToRange)(c,l,f),h={unitId:c,subUnitId:l,ranges:p.map(e=>({startColumn:e.col,startRow:e.row,endColumn:e.col,endRow:e.row}))},g=[],_=[];if(f.length){let r=f.reduce((e,r)=>{(0,t.isTextFormat)(r.pattern)&&e.setValue(r.row,r.col,{t:t.CellValueType.STRING});let i=u.getCellRaw(r.row,r.col);if(i){let t=(0,n.checkCellValueType)(i.v);t!==i.t&&e.setValue(r.row,r.col,{t})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.ObjectMatrix(r).forValue((e,t)=>{let n=u.getCellRaw(e,t);n?i.setValue(e,t,{t:n.t}):i.setValue(e,t,{t:void 0})}),Object.keys(m.values).forEach(e=>{let t=m.values[e];t.ranges=(0,n.rangeMerge)(t.ranges)}),g.push({id:n.SetNumfmtMutation.id,params:m});let a=(0,n.factorySetNumfmtUndoMutation)(e,m);_.push(...a)}if(p.length){h.ranges=(0,n.rangeMerge)(h.ranges);let r=p.reduce((e,t)=>{let r=u.getCellRaw(t.row,t.col);if(r){let i=(0,n.checkCellValueType)(r.v);i!==r.t&&e.setValue(t.row,t.col,{t:i})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.ObjectMatrix(r).forValue((e,t)=>{let n=u.getCellRaw(e,t);n?i.setValue(e,t,{t:n.t}):i.setValue(e,t,{t:void 0})}),g.push({id:n.RemoveNumfmtMutation.id,params:h},{id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:r}});let a=(0,n.factoryRemoveNumfmtUndoMutation)(e,h);_.push({id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:i.getMatrix()}},...a)}let v=(0,t.sequenceExecute)(g,i).result;return v&&o.pushUndoRedo({unitID:c,undoMutations:_,redoMutations:g}),v}},C={id:`sheet.command.numfmt.add.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(y(r)){d=Math.max(d,f(b(r)));return}if(!d&&r&&r.t===t.CellValueType.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;d=Math.max(d,t)}}return}let i=f(r.pattern);d=i>d?i:d})});let p=d+1,h=m(`0${p>0?`.0`:``}`,p),g=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);g.push({row:e,col:n,pattern:y(t)?m(b(t),p):h})}else{let t=f(r.pattern),i=m(r.pattern,t+1);i!==r.pattern&&g.push({row:e,col:n,pattern:i})}})}),g.length?await r.executeCommand(S.id,{values:g}):!1}},w={id:`sheet.command.numfmt.set.currency`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(t.RegionService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let l=[],u=c(o(a.getCurrentRegion()).locale);return s.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{l.push({row:e,col:t,pattern:u,type:`currency`})})}),await r.executeCommand(S.id,{values:l})}},T={id:`sheet.command.numfmt.set.percent`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService).getCurrentSelections();if(!i||!i.length)return!1;let a=[];return i.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{a.push({row:e,col:t,pattern:`0%`,type:`percent`})})}),await r.executeCommand(S.id,{values:a})}},E={id:`sheet.command.numfmt.subtract.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(y(r)){d=Math.max(d,f(b(r)));return}if(!d&&r&&r.t===t.CellValueType.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;d=Math.max(d,t)}}return}let i=f(r.pattern);d=i>d?i:d})});let p=d-1,h=m(`0${p>0?`.0`:`.`}`,p),g=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);g.push({row:e,col:n,pattern:y(t)?m(b(t),p):h})}else{let t=f(r.pattern);g.push({row:e,col:n,pattern:m(r.pattern,t-1)})}})}),await r.executeCommand(S.id,{values:g})}},D=`sheets-numfmt.config`,O={};function k(e){"@babel/helpers - typeof";return k=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},k(e)}function A(e,t){if(k(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(k(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function j(e){var t=A(e,`string`);return k(t)==`symbol`?t:t+``}function M(e,t,n){return(t=j(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function N(e,t){return function(n,r){t(n,r,e)}}function P(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let F={tl:{size:6,color:`#409f11`}},I=class extends t.Disposable{constructor(e,n,r,a,o,s,c){super(),this._instanceService=e,this._sheetInterceptorService=n,this._themeService=r,this._commandService=a,this._numfmtService=o,this._localeService=s,this._configService=c,M(this,`_renderCache`,new t.ObjectMatrix),M(this,`_locale$`,new i.BehaviorSubject(null)),M(this,`locale$`,this._locale$.asObservable()),this._initInterceptorCellContent()}get locale(){return this.getLocale()}getLocale(e){return this._locale$.getValue()??(0,t.getNumfmtLocaleTag)((e==null?void 0:e.getSnapshot().locale)??this._localeService.getCurrentLocale())}_initInterceptorCellContent(){this.disposeWithMe((0,i.merge)(this._locale$,this._localeService.currentLocale$).subscribe(()=>{this._renderCache.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(n.INTERCEPTOR_POINT.CELL_CONTENT,{effect:t.InterceptorEffectEnum.Value|t.InterceptorEffectEnum.Style,handler:(e,t,n)=>n(this._formatCell(e,t)),priority:n.InterceptCellContentPriority.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===n.SetNumfmtMutation.id){let n=e.params;Object.keys(n.values).forEach(e=>{n.values[e].ranges.forEach(e=>{t.Range.foreach(e,(e,t)=>{this._renderCache.realDeleteValue(e,t)})})})}else if(e.id===n.SetRangeValuesMutation.id){if(t.Tools.isEmptyObject(this._renderCache.getMatrix()))return;let n=e.params;new t.ObjectMatrix(n.cellValue).forValue((e,t)=>{this._renderCache.realDeleteValue(e,t)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,i.switchMap)(e=>(e==null?void 0:e.activeSheet$)??(0,i.of)(null)),(0,i.skip)(1)).subscribe(()=>this._renderCache.reset()))}_getNumfmtValue(e,t){var n,r;let{unitId:i,subUnitId:a,row:o,col:s,workbook:c}=t,l=(n=e.themeStyle)==null?void 0:n.n;if(l===void 0){var u;return((u=c.getStyles().get(e.s))==null?void 0:u.n)??this._numfmtService.getValue(i,a,o,s)}let d=(r=c.getStyles().getStyleByCell(e))==null?void 0:r.n;return d===void 0?this._numfmtService.getValue(i,a,o,s)??l:d}_formatCell(e,n){if(!e||e.v==null||e.t===t.CellValueType.BOOLEAN||e.t===t.CellValueType.FORCE_STRING)return e;let{unitId:r,subUnitId:i,row:a,col:o,workbook:s}=n,c=this.getLocale(s),l=s.getDateSystem(),u=this._getNumfmtValue(e,n),d=u==null?void 0:u.pattern;if((0,t.isDefaultFormat)(d)||!this._isNumericCell(e,d,c,l))return e;let f=e===n.rawData?{...e}:e;if((0,t.isTextFormat)(d))return this._formatTextCell(e,f);let p=`${r}_${i}_${e.v}_${d}_${c}_${l}`,m=this._renderCache.getValue(a,o);if(m&&m.parameters===p)return{...f,...m.result};let h=v(d,Number(e.v),c,l),g={v:h.result,t:t.CellValueType.NUMBER};if(h.result===``&&(g.coverable=!1),h.color){let e=this._themeService.getColorFromTheme(`${h.color}.500`)??h.color;e&&(g.interceptorStyle={cl:{rgb:e}})}return this._renderCache.setValue(a,o,{result:g,parameters:p}),Object.assign(f,g),f}_formatTextCell(e,n){var i;return e.t===t.CellValueType.NUMBER&&(0,t.isRealNum)(n.v)?n.v=(0,r.stripErrorMargin)(Number(n.v)):n.t=t.CellValueType.STRING,(i=this._configService.getConfig(`sheets-numfmt.config`))!=null&&i.disableTextFormatMark||(n.markers={...n.markers,...F}),n}_isNumericCell(e,r,i,a){var o;return e.t===t.CellValueType.NUMBER||(0,n.checkCellValueType)(e.v,e.t)===t.CellValueType.NUMBER||(0,t.isTextFormat)(r)&&typeof e.v==`string`&&typeof((o=(0,t.getNumfmtParseValueFilter)(e.v,{locale:i,dateSystem:a}))==null?void 0:o.v)==`number`}setNumfmtLocal(e){this._locale$.next(e)}};I=P([N(0,t.IUniverInstanceService),N(1,(0,t.Inject)(n.SheetInterceptorService)),N(2,(0,t.Inject)(t.ThemeService)),N(3,(0,t.Inject)(t.ICommandService)),N(4,(0,t.Inject)(n.INumfmtService)),N(5,(0,t.Inject)(t.LocaleService)),N(6,t.IConfigService)],I);var L=`@univerjs/sheets-numfmt`,R=`1.5.2`;let z=class extends t.Plugin{constructor(e=O,n,r,i){super(),this._config=e,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,t.merge)({},O,this._config);this._configService.setConfig(D,a)}onStarting(){(0,t.registerDependencies)(this._injector,[[I]]),(0,t.touchDependencies)(this._injector,[[I]]),[C,E,w,T,S].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}};M(z,`pluginName`,`SHEET_NUMFMT_PLUGIN`),M(z,`packageName`,L),M(z,`version`,R),M(z,`type`,t.UniverInstanceType.UNIVER_SHEET),z=P([(0,t.DependentOn)(n.UniverSheetsPlugin),N(1,(0,t.Inject)(t.Injector)),N(2,t.IConfigService),N(3,t.ICommandService)],z);let B=e=>t.currencySymbols.find(t=>e.includes(t)),V={dates:[`yyyy/MM/dd`,`MM-dd`],shortDate:`MM-dd`},H={dates:[`m/d/yyyy`,`mm/dd/yyyy`,`mmm d, yyyy`,`m/d`,`mmmm d`],shortDate:`m/d`},U={dates:[`dd/mm/yyyy`,`d/m/yyyy`,`d mmm yyyy`,`dd/mm`,`d mmmm`],shortDate:`dd/mm`},W={dates:[`dd.mm.yyyy`,`d.m.yyyy`,`d mmm yyyy`,`dd.mm`,`d mmmm`],shortDate:`dd.mm`},G={dates:[`yyyy/MM/dd`,`yyyy"年"MM"月"dd"日"`,`MM-dd`,`M"月"d"日"`],shortDate:`MM-dd`,periodBeforeTime:!0},K={[t.LocaleType.EN_US]:H,[t.LocaleType.FR_FR]:U,[t.LocaleType.ZH_CN]:G,[t.LocaleType.RU_RU]:W,[t.LocaleType.ZH_TW]:G,[t.LocaleType.ZH_HK]:{...G,dates:[`dd/MM/yyyy`,...G.dates],shortDate:`dd/MM`},[t.LocaleType.VI_VN]:U,[t.LocaleType.FA_IR]:V,[t.LocaleType.CA_ES]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`},[t.LocaleType.JA_JP]:G,[t.LocaleType.KO_KR]:{dates:[`yyyy. mm. dd.`,`yyyy"년" m"월" d"일"`,`mm-dd`,`m"월" d"일"`],shortDate:`mm-dd`,periodBeforeTime:!0},[t.LocaleType.ES_ES]:U,[t.LocaleType.SK_SK]:W,[t.LocaleType.PT_BR]:U,[t.LocaleType.DE_DE]:W,[t.LocaleType.IT_IT]:U,[t.LocaleType.ID_ID]:U,[t.LocaleType.PL_PL]:W,[t.LocaleType.AR_SA]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`}};function q(e){let t=K[e]??V,n=[`hh:mm`,`h:mm`,`h:mm:ss`].map(e=>t.periodBeforeTime?`AM/PM ${e}`:`${e} AM/PM`);return[.../* @__PURE__ */ new Set([`yyyy-MM-dd`,...t.dates,`h:mm:ss`,`h:mm`,...n,`${t.shortDate} ${n[0]}`])]}let J=()=>t.currencySymbols.map(e=>({label:e,value:e})),Y=e=>d.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color})),X=(0,t.excelDateTimeSerial)(new Date(Date.UTC(1930,7,5,13,30,30)));e.AddDecimalCommand=C,e.CURRENCYFORMAT=d,e.DATEFMTLISG=l,e.NUMBERFORMAT=u,e.SHEETS_NUMFMT_PLUGIN_CONFIG_KEY=D,e.SetCurrencyCommand=w,e.SetNumfmtCommand=S,e.SetPercentCommand=T,Object.defineProperty(e,"SheetsNumfmtCellContentController",{enumerable:!0,get:function(){return I}}),e.SubtractDecimalCommand=E,Object.defineProperty(e,"UniverSheetsNumfmtPlugin",{enumerable:!0,get:function(){return z}}),e.getCurrencyFormat=c,e.getCurrencyFormatOptions=Y,e.getCurrencyOptions=J,e.getCurrencySymbolByLocale=s,e.getCurrencySymbolIconByLocale=o,e.getCurrencyType=B,e.getDateFormatOptions=(e,n)=>{if(e===void 0)return l.map(e=>({label:e.label,value:e.suffix}));let r=(0,t.getNumfmtLocaleTag)(e),i=q(e);return n&&!i.includes(n)&&i.unshift(n),i.map(e=>({label:t.numfmt.format(e,X,{locale:r}),value:e}))},e.getDecimalFromPattern=f,e.getDecimalString=p,e.getNumberFormatOptions=()=>u.map(e=>({label:e.label,value:e.suffix,color:e.color})),e.getPatternPreview=_,e.getPatternPreviewIgnoreGeneral=v,e.getPatternType=g,e.isPatternHasDecimal=h,e.localeCurrencySymbolMap=a,e.setPatternDecimal=m});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/sheets`,`@univerjs/engine-formula`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsNumfmt={},e.UniverCore,e.UniverSheets,e.UniverEngineFormula,e.rxjs))})(this,function(e,t,n,r,i){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function a(e){switch(e){case t.LocaleType.CA_ES:case t.LocaleType.DE_DE:case t.LocaleType.ES_ES:case t.LocaleType.FR_FR:case t.LocaleType.IT_IT:case t.LocaleType.SK_SK:return{icon:`EuroIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.RU_RU:return{icon:`RoubleIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.JA_JP:case t.LocaleType.ZH_CN:return{icon:`RmbIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.VI_VN:return{icon:`DongIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.AR_SA:case t.LocaleType.FA_IR:return{icon:`RialIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.KO_KR:return{icon:`WonIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.ID_ID:return{icon:`RupiahIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.PL_PL:return{icon:`ZlotyIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e};case t.LocaleType.EN_US:case t.LocaleType.PT_BR:case t.LocaleType.ZH_HK:case t.LocaleType.ZH_TW:default:return{icon:`DollarIcon`,symbol:(0,t.getCurrencySymbolByLocale)(e),locale:e}}}function o(e,n=2){let r=n;n>127&&(r=127);let i=``;return r>0&&(i=`.${`0`.repeat(r)}`),`"${(0,t.getCurrencySymbolByLocale)(e)}"#,##0${i}_);[Red]("${(0,t.getCurrencySymbolByLocale)(e)}"#,##0${i})`}let s=[{label:`1930-08-05`,suffix:`yyyy-MM-dd`},{label:`1930/08/05`,suffix:`yyyy/MM/dd`},{label:`1930年08月05日`,suffix:`yyyy"年"MM"月"dd"日"`},{label:`08-05`,suffix:`MM-dd`},{label:`8月5日`,suffix:`M"月"d"日"`},{label:`13:30:30`,suffix:`h:mm:ss`},{label:`13:30`,suffix:`h:mm`},{label:`下午01:30`,suffix:`A/P hh:mm`},{label:`下午1:30`,suffix:`A/P h:mm`},{label:`下午1:30:30`,suffix:`A/P h:mm:ss`},{label:`08-05 下午 01:30`,suffix:`MM-dd A/P hh:mm`}],c=[{label:`(1,235)`,suffix:`#,##0_);(#,##0)`},{label:`(1,235) `,suffix:`#,##0_);[Red](#,##0)`,color:`red`},{label:`1,234.56`,suffix:`#,##0.00_);#,##0.00`},{label:`1,234.56`,suffix:`#,##0.00_);[Red]#,##0.00`,color:`red`},{label:`-1,234.56`,suffix:`#,##0.00_);-#,##0.00`},{label:`-1,234.56`,suffix:`#,##0.00_);[Red]-#,##0.00`,color:`red`}],l=[{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);"${e}"#,##0.00`},{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,color:`red`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);("${e}"#,##0.00)`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,color:`red`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);-"${e}"#,##0.00`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,color:`red`}],u=e=>Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(``);function d(e){let t=e.split(``),n=!1,r=!1;for(let e=0;e<t.length;e++){let i=t[e];r?(t[e]=`\0`,i===`]`&&(r=!1)):i===`"`?(t[e]=`\0`,n=!n):n?t[e]=`\0`:i===`\\`||i===`_`||i===`*`?(t[e]=`\0`,e+1<t.length&&(t[e+1]=`\0`,e+=1)):i===`[`&&(t[e]=`\0`,r=!0)}return t.join(``)}function f(e){let t=d(e),n=[],r=0;for(let i=0;i<=t.length;i++)(i===t.length||t[i]===`;`)&&(n.push({mask:t.slice(r,i),text:e.slice(r,i)}),r=i+1);return n}function p(e){return e.text!==``&&t.numfmt.getFormatInfo(e.text).isDate}function m(e){return e===`0`||e===`#`||e===`?`}function h(e){for(let t=0;t<e.length;t++)if(e[t]===`0`&&e[t+1]!==`0`)return t;return-1}function g(e,t){let n=e.mask.indexOf(`.`);if(n<0){if(t===0)return e.text;let n=h(e.mask);return n<0?e.text:`${e.text.slice(0,n+1)}.${u(t)}${e.text.slice(n+1)}`}let r=n+1;for(;m(e.mask[r]);)r+=1;let i=e.text.slice(n+1,r);if(i.length===0&&t===0)return`${e.text.slice(0,n)}${e.text.slice(n+1)}`;if(t>=i.length){let n=i.length>0?i[i.length-1]:`0`;return`${e.text.slice(0,r)}${n.repeat(t-i.length)}${e.text.slice(r)}`}let a=i.slice(0,t);return a.length>0?`${e.text.slice(0,n)}.${a}${e.text.slice(r)}`:`${e.text.slice(0,n)}${e.text.slice(r)}`}let _=(e,n=0)=>e?t.numfmt.getFormatInfo(e).maxDecimals??n:n,v=(e,t)=>{let n=Math.min(Math.max(0,Math.floor(Number(t)||0)),30);return f(e).map(e=>p(e)?e.text:g(e,n)).join(`;`)},y=e=>/\.0?/.test(e)||/0([^0]?)|0$/.test(e),b=e=>{let n=t.numfmt.getFormatInfo(e).type||`unknown`;return n===`general`&&t.numfmt.tokenize(e).some(e=>e.type===t.numfmt.tokenTypes.CONDITION)?`number`:n},x=(e,n,r=`en`,i)=>{try{let a=t.numfmt.formatColor(e,n),o=a?String(a):void 0;return{result:t.numfmt.format(e,n,{locale:r,dateSystem:i,throws:!1}),color:o}}catch(t){console.warn(`getPatternPreview error:`,e,t)}return{result:String(n)}},S=(e,n,i,a)=>e===t.DEFAULT_NUMBER_FORMAT?{result:String((0,r.stripErrorMargin)(n))}:x(e,n,i,a);function C(e){return(e==null?void 0:e.t)===t.CellValueType.NUMBER&&e.v!==void 0&&e.v!==null&&/e/i.test(String(e.v))}function w(e){let t=String(e==null?void 0:e.v),n=t.search(/e/i),r=t.indexOf(`.`),i=n>-1&&r>-1?n-r-1:0;return`0.${`0`.repeat(Math.max(2,i))}E+00`}function T(e){return(0,t.isDefaultFormat)(e)||(0,t.isTextFormat)(e)||b(e)===`scientific`}let E={id:`sheet.command.numfmt.set.numfmt`,type:t.CommandType.COMMAND,handler:(e,r)=>{if(!r)return!1;let i=e.get(t.ICommandService),a=e.get(t.IUniverInstanceService),o=e.get(t.IUndoRedoService),s=(0,n.getSheetCommandTarget)(a,r);if(!s)return!1;let{unitId:c,subUnitId:l,worksheet:u}=s,d=r.values.filter(e=>!C(u.getCellRaw(e.row,e.col))||T(e.pattern)),f=d.filter(e=>!!e.pattern),p=d.filter(e=>!e.pattern);if(!f.length&&!p.length)return!1;let m=(0,n.transformCellsToRange)(c,l,f),h={unitId:c,subUnitId:l,ranges:p.map(e=>({startColumn:e.col,startRow:e.row,endColumn:e.col,endRow:e.row}))},g=[],_=[];if(f.length){let r=f.reduce((e,r)=>{(0,t.isTextFormat)(r.pattern)&&e.setValue(r.row,r.col,{t:t.CellValueType.STRING});let i=u.getCellRaw(r.row,r.col);if(i){let t=(0,n.checkCellValueType)(i.v);t!==i.t&&e.setValue(r.row,r.col,{t})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.ObjectMatrix(r).forValue((e,t)=>{let n=u.getCellRaw(e,t);n?i.setValue(e,t,{t:n.t}):i.setValue(e,t,{t:void 0})}),Object.keys(m.values).forEach(e=>{let t=m.values[e];t.ranges=(0,n.rangeMerge)(t.ranges)}),g.push({id:n.SetNumfmtMutation.id,params:m});let a=(0,n.factorySetNumfmtUndoMutation)(e,m);_.push(...a)}if(p.length){h.ranges=(0,n.rangeMerge)(h.ranges);let r=p.reduce((e,t)=>{let r=u.getCellRaw(t.row,t.col);if(r){let i=(0,n.checkCellValueType)(r.v);i!==r.t&&e.setValue(t.row,t.col,{t:i})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.ObjectMatrix(r).forValue((e,t)=>{let n=u.getCellRaw(e,t);n?i.setValue(e,t,{t:n.t}):i.setValue(e,t,{t:void 0})}),g.push({id:n.RemoveNumfmtMutation.id,params:h},{id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:r}});let a=(0,n.factoryRemoveNumfmtUndoMutation)(e,h);_.push({id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:i.getMatrix()}},...a)}let v=(0,t.sequenceExecute)(g,i).result;return v&&o.pushUndoRedo({unitID:c,undoMutations:_,redoMutations:g}),v}},D={id:`sheet.command.numfmt.add.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(C(r)){d=Math.max(d,_(w(r)));return}if(!d&&r&&r.t===t.CellValueType.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;d=Math.max(d,t)}}return}let i=_(r.pattern);d=i>d?i:d})});let f=d+1,p=v(`0${f>0?`.0`:``}`,f),m=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);m.push({row:e,col:n,pattern:C(t)?v(w(t),f):p})}else{let t=_(r.pattern),i=v(r.pattern,t+1);i!==r.pattern&&m.push({row:e,col:n,pattern:i})}})}),m.length?await r.executeCommand(E.id,{values:m}):!1}},O={id:`sheet.command.numfmt.set.currency`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),s=e.get(t.RegionService),c=i.getCurrentSelections();if(!c||!c.length)return!1;let l=[],u=o(a(s.getCurrentRegion()).locale);return c.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{l.push({row:e,col:t,pattern:u,type:`currency`})})}),await r.executeCommand(E.id,{values:l})}},k={id:`sheet.command.numfmt.set.percent`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService).getCurrentSelections();if(!i||!i.length)return!1;let a=[];return i.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{a.push({row:e,col:t,pattern:`0%`,type:`percent`})})}),await r.executeCommand(E.id,{values:a})}},A={id:`sheet.command.numfmt.subtract.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(C(r)){d=Math.max(d,_(w(r)));return}if(!d&&r&&r.t===t.CellValueType.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;d=Math.max(d,t)}}return}let i=_(r.pattern);d=i>d?i:d})});let f=d-1,p=v(`0${f>0?`.0`:`.`}`,f),m=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);m.push({row:e,col:n,pattern:C(t)?v(w(t),f):p})}else{let t=r.pattern,i=v(t,_(t)-1);i!==t&&m.push({row:e,col:n,pattern:i})}})}),m.length?await r.executeCommand(E.id,{values:m}):!1}},j=`sheets-numfmt.config`,M={};function N(e){"@babel/helpers - typeof";return N=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},N(e)}function P(e,t){if(N(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(N(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function F(e){var t=P(e,`string`);return N(t)==`symbol`?t:t+``}function I(e,t,n){return(t=F(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function L(e,t){return function(n,r){t(n,r,e)}}function R(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let z={tl:{size:6,color:`#409f11`}},B=class extends t.Disposable{constructor(e,n,r,a,o,s,c){super(),this._instanceService=e,this._sheetInterceptorService=n,this._themeService=r,this._commandService=a,this._numfmtService=o,this._localeService=s,this._configService=c,I(this,`_renderCache`,new t.ObjectMatrix),I(this,`_locale$`,new i.BehaviorSubject(null)),I(this,`locale$`,this._locale$.asObservable()),this._initInterceptorCellContent()}get locale(){return this.getLocale()}getLocale(e){return this._locale$.getValue()??(0,t.getNumfmtLocaleTag)((e==null?void 0:e.getSnapshot().locale)??this._localeService.getCurrentLocale())}_initInterceptorCellContent(){this.disposeWithMe((0,i.merge)(this._locale$,this._localeService.currentLocale$).subscribe(()=>{this._renderCache.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(n.INTERCEPTOR_POINT.CELL_CONTENT,{effect:t.InterceptorEffectEnum.Value|t.InterceptorEffectEnum.Style,handler:(e,t,n)=>n(this._formatCell(e,t)),priority:n.InterceptCellContentPriority.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===n.SetNumfmtMutation.id){let n=e.params;Object.keys(n.values).forEach(e=>{n.values[e].ranges.forEach(e=>{t.Range.foreach(e,(e,t)=>{this._renderCache.realDeleteValue(e,t)})})})}else if(e.id===n.SetRangeValuesMutation.id){if(t.Tools.isEmptyObject(this._renderCache.getMatrix()))return;let n=e.params;new t.ObjectMatrix(n.cellValue).forValue((e,t)=>{this._renderCache.realDeleteValue(e,t)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,i.switchMap)(e=>(e==null?void 0:e.activeSheet$)??(0,i.of)(null)),(0,i.skip)(1)).subscribe(()=>this._renderCache.reset()))}_getNumfmtValue(e,t){var n,r;let{unitId:i,subUnitId:a,row:o,col:s,workbook:c}=t,l=(n=e.themeStyle)==null?void 0:n.n;if(l===void 0){var u;return((u=c.getStyles().get(e.s))==null?void 0:u.n)??this._numfmtService.getValue(i,a,o,s)}let d=(r=c.getStyles().getStyleByCell(e))==null?void 0:r.n;return d===void 0?this._numfmtService.getValue(i,a,o,s)??l:d}_formatCell(e,n){if(!e||e.v==null||e.t===t.CellValueType.BOOLEAN||e.t===t.CellValueType.FORCE_STRING)return e;let{unitId:r,subUnitId:i,row:a,col:o,workbook:s}=n,c=this.getLocale(s),l=s.getDateSystem(),u=this._getNumfmtValue(e,n),d=u==null?void 0:u.pattern;if((0,t.isDefaultFormat)(d)||!this._isNumericCell(e,d,c,l))return e;let f=e===n.rawData?{...e}:e;if((0,t.isTextFormat)(d))return this._formatTextCell(e,f);let p=`${r}_${i}_${e.v}_${d}_${c}_${l}`,m=this._renderCache.getValue(a,o);if(m&&m.parameters===p)return{...f,...m.result};let h=S(d,Number(e.v),c,l),g={v:h.result,t:t.CellValueType.NUMBER};if(h.result===``&&(g.coverable=!1),h.color){let e=this._themeService.getColorFromTheme(`${h.color}.500`)??h.color;e&&(g.interceptorStyle={cl:{rgb:e}})}return this._renderCache.setValue(a,o,{result:g,parameters:p}),Object.assign(f,g),f}_formatTextCell(e,n){var i;return e.t===t.CellValueType.NUMBER&&(0,t.isRealNum)(n.v)?n.v=(0,r.stripErrorMargin)(Number(n.v)):n.t=t.CellValueType.STRING,(i=this._configService.getConfig(`sheets-numfmt.config`))!=null&&i.disableTextFormatMark||(n.markers={...n.markers,...z}),n}_isNumericCell(e,r,i,a){var o;return e.t===t.CellValueType.NUMBER||(0,n.checkCellValueType)(e.v,e.t)===t.CellValueType.NUMBER||(0,t.isTextFormat)(r)&&typeof e.v==`string`&&typeof((o=(0,t.getNumfmtParseValueFilter)(e.v,{locale:i,dateSystem:a}))==null?void 0:o.v)==`number`}setNumfmtLocal(e){this._locale$.next(e)}};B=R([L(0,t.IUniverInstanceService),L(1,(0,t.Inject)(n.SheetInterceptorService)),L(2,(0,t.Inject)(t.ThemeService)),L(3,(0,t.Inject)(t.ICommandService)),L(4,(0,t.Inject)(n.INumfmtService)),L(5,(0,t.Inject)(t.LocaleService)),L(6,t.IConfigService)],B);var V=`@univerjs/sheets-numfmt`,H=`1.6.0`;let U=class extends t.Plugin{constructor(e=M,n,r,i){super(),this._config=e,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,t.merge)({},M,this._config);this._configService.setConfig(j,a)}onStarting(){(0,t.registerDependencies)(this._injector,[[B]]),(0,t.touchDependencies)(this._injector,[[B]]),[D,A,O,k,E].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}};I(U,`pluginName`,`SHEET_NUMFMT_PLUGIN`),I(U,`packageName`,V),I(U,`version`,H),I(U,`type`,t.UniverInstanceType.UNIVER_SHEET),U=R([(0,t.DependentOn)(n.UniverSheetsPlugin),L(1,(0,t.Inject)(t.Injector)),L(2,t.IConfigService),L(3,t.ICommandService)],U);let W=e=>t.currencySymbols.find(t=>e.includes(t)),G={dates:[`yyyy/MM/dd`,`MM-dd`],shortDate:`MM-dd`},K={dates:[`m/d/yyyy`,`mm/dd/yyyy`,`mmm d, yyyy`,`m/d`,`mmmm d`],shortDate:`m/d`},q={dates:[`dd/mm/yyyy`,`d/m/yyyy`,`d mmm yyyy`,`dd/mm`,`d mmmm`],shortDate:`dd/mm`},J={dates:[`dd.mm.yyyy`,`d.m.yyyy`,`d mmm yyyy`,`dd.mm`,`d mmmm`],shortDate:`dd.mm`},Y={dates:[`yyyy/MM/dd`,`yyyy"年"MM"月"dd"日"`,`MM-dd`,`M"月"d"日"`],shortDate:`MM-dd`,periodBeforeTime:!0},X={[t.LocaleType.EN_US]:K,[t.LocaleType.FR_FR]:q,[t.LocaleType.ZH_CN]:Y,[t.LocaleType.RU_RU]:J,[t.LocaleType.ZH_TW]:Y,[t.LocaleType.ZH_HK]:{...Y,dates:[`dd/MM/yyyy`,...Y.dates],shortDate:`dd/MM`},[t.LocaleType.VI_VN]:q,[t.LocaleType.FA_IR]:G,[t.LocaleType.CA_ES]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`},[t.LocaleType.JA_JP]:Y,[t.LocaleType.KO_KR]:{dates:[`yyyy. mm. dd.`,`yyyy"년" m"월" d"일"`,`mm-dd`,`m"월" d"일"`],shortDate:`mm-dd`,periodBeforeTime:!0},[t.LocaleType.ES_ES]:q,[t.LocaleType.SK_SK]:J,[t.LocaleType.PT_BR]:q,[t.LocaleType.DE_DE]:J,[t.LocaleType.IT_IT]:q,[t.LocaleType.ID_ID]:q,[t.LocaleType.PL_PL]:J,[t.LocaleType.AR_SA]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`}};function Z(e){let t=X[e]??G,n=[`hh:mm`,`h:mm`,`h:mm:ss`].map(e=>t.periodBeforeTime?`AM/PM ${e}`:`${e} AM/PM`);return[.../* @__PURE__ */ new Set([`yyyy-MM-dd`,...t.dates,`h:mm:ss`,`h:mm`,...n,`${t.shortDate} ${n[0]}`])]}let Q=()=>t.currencySymbols.map(e=>({label:e,value:e})),$=e=>l.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color})),ee=(0,t.excelDateTimeSerial)(new Date(Date.UTC(1930,7,5,13,30,30)));e.AddDecimalCommand=D,e.CURRENCYFORMAT=l,e.DATEFMTLISG=s,e.NUMBERFORMAT=c,e.SHEETS_NUMFMT_PLUGIN_CONFIG_KEY=j,e.SetCurrencyCommand=O,e.SetNumfmtCommand=E,e.SetPercentCommand=k,Object.defineProperty(e,"SheetsNumfmtCellContentController",{enumerable:!0,get:function(){return B}}),e.SubtractDecimalCommand=A,Object.defineProperty(e,"UniverSheetsNumfmtPlugin",{enumerable:!0,get:function(){return U}}),e.getCurrencyFormat=o,e.getCurrencyFormatOptions=$,e.getCurrencyOptions=Q,e.getCurrencySymbolIconByLocale=a,e.getCurrencyType=W,e.getDateFormatOptions=(e,n)=>{if(e===void 0)return s.map(e=>({label:e.label,value:e.suffix}));let r=(0,t.getNumfmtLocaleTag)(e),i=Z(e);return n&&!i.includes(n)&&i.unshift(n),i.map(e=>({label:t.numfmt.format(e,ee,{locale:r}),value:e}))},e.getDecimalFromPattern=_,e.getDecimalString=u,e.getNumberFormatOptions=()=>c.map(e=>({label:e.label,value:e.suffix,color:e.color})),e.getPatternPreview=x,e.getPatternPreviewIgnoreGeneral=S,e.getPatternType=b,e.isPatternHasDecimal=y,e.setPatternDecimal=v});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/sheets-numfmt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Number format services and commands for Univer Sheets.",
|
|
6
6
|
"author": "DreamNum Co., Ltd. <developer@univer.ai>",
|
|
@@ -62,15 +62,15 @@
|
|
|
62
62
|
"rxjs": ">=7.0.0"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@univerjs/core": "1.
|
|
66
|
-
"@univerjs/engine-formula": "1.
|
|
67
|
-
"@univerjs/sheets": "1.
|
|
65
|
+
"@univerjs/core": "1.6.0",
|
|
66
|
+
"@univerjs/engine-formula": "1.6.0",
|
|
67
|
+
"@univerjs/sheets": "1.6.0"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@univerjs-infra/shared": "1.
|
|
70
|
+
"@univerjs-infra/shared": "1.6.0",
|
|
71
71
|
"rxjs": "7.8.2",
|
|
72
|
-
"tsrs": "
|
|
73
|
-
"vitest": "^5.1.
|
|
72
|
+
"tsrs": "7.1.0-beta.tsrs2",
|
|
73
|
+
"vitest": "^5.1.1"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"test": "vitest run",
|