@slickgrid-universal/excel-export 2.5.0 → 2.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.
Files changed (37) hide show
  1. package/dist/commonjs/excelExport.service.js +572 -572
  2. package/dist/commonjs/excelUtils.js +258 -258
  3. package/dist/commonjs/index.js +5 -5
  4. package/dist/commonjs/interfaces/excelCellFormat.interface.js +2 -2
  5. package/dist/commonjs/interfaces/excelMetadata.interface.js +2 -2
  6. package/dist/commonjs/interfaces/excelStylesheet.interface.js +2 -2
  7. package/dist/commonjs/interfaces/index.js +19 -19
  8. package/dist/esm/excelExport.service.js +570 -570
  9. package/dist/esm/excelUtils.js +247 -247
  10. package/dist/esm/index.js +1 -1
  11. package/dist/esm/interfaces/excelCellFormat.interface.js +1 -1
  12. package/dist/esm/interfaces/excelMetadata.interface.js +1 -1
  13. package/dist/esm/interfaces/excelStylesheet.interface.js +1 -1
  14. package/dist/esm/interfaces/index.js +3 -3
  15. package/dist/tsconfig.tsbuildinfo +1 -0
  16. package/dist/{commonjs → types}/excelExport.service.d.ts +137 -136
  17. package/dist/types/excelExport.service.d.ts.map +1 -0
  18. package/dist/{esm → types}/excelUtils.d.ts +32 -31
  19. package/dist/types/excelUtils.d.ts.map +1 -0
  20. package/dist/{esm → types}/index.d.ts +2 -1
  21. package/dist/types/index.d.ts.map +1 -0
  22. package/dist/{esm → types}/interfaces/excelCellFormat.interface.d.ts +6 -5
  23. package/dist/types/interfaces/excelCellFormat.interface.d.ts.map +1 -0
  24. package/dist/{commonjs → types}/interfaces/excelMetadata.interface.d.ts +10 -9
  25. package/dist/types/interfaces/excelMetadata.interface.d.ts.map +1 -0
  26. package/dist/{commonjs → types}/interfaces/excelStylesheet.interface.d.ts +34 -33
  27. package/dist/types/interfaces/excelStylesheet.interface.d.ts.map +1 -0
  28. package/dist/{commonjs → types}/interfaces/index.d.ts +4 -3
  29. package/dist/types/interfaces/index.d.ts.map +1 -0
  30. package/package.json +8 -8
  31. package/dist/commonjs/excelUtils.d.ts +0 -31
  32. package/dist/commonjs/index.d.ts +0 -1
  33. package/dist/commonjs/interfaces/excelCellFormat.interface.d.ts +0 -5
  34. package/dist/esm/excelExport.service.d.ts +0 -136
  35. package/dist/esm/interfaces/excelMetadata.interface.d.ts +0 -9
  36. package/dist/esm/interfaces/excelStylesheet.interface.d.ts +0 -33
  37. package/dist/esm/interfaces/index.d.ts +0 -3
@@ -1,248 +1,248 @@
1
- import { Constants, FieldType, Formatters, getColumnFieldType, getValueFromParamsOrFormatterOptions, GroupTotalFormatters, retrieveFormatterOptions, sanitizeHtmlToText, } from '@slickgrid-universal/common';
2
- // define all type of potential excel data function callbacks
3
- export const getExcelSameInputDataCallback = (data) => data;
4
- export const getExcelNumberCallback = (data, column, excelFormatterId, _excelSheet, gridOptions) => ({
5
- value: typeof data === 'string' && /\d/g.test(data) ? parseNumberWithFormatterOptions(data, column, gridOptions) : data,
6
- metadata: { style: excelFormatterId }
7
- });
8
- /** Parse a number which the user might have provided formatter options (for example a user might have provided { decimalSeparator: ',', thousandSeparator: ' '}) */
9
- export function parseNumberWithFormatterOptions(value, column, gridOptions) {
10
- let outValue = value;
11
- if (typeof value === 'string' && value) {
12
- const decimalSeparator = getValueFromParamsOrFormatterOptions('decimalSeparator', column, gridOptions, Constants.DEFAULT_NUMBER_DECIMAL_SEPARATOR);
13
- const val = (decimalSeparator === ',')
14
- ? parseFloat(value.replace(/[^0-9\,]+/g, '').replace(',', '.'))
15
- : parseFloat(value.replace(/[^\d\.]/g, ''));
16
- outValue = isNaN(val) ? value : val;
17
- }
18
- return outValue;
19
- }
20
- /** use different Excel Stylesheet Format as per the Field Type */
21
- export function useCellFormatByFieldType(stylesheet, stylesheetFormatters, columnDef, grid) {
22
- const fieldType = getColumnFieldType(columnDef);
23
- let stylesheetFormatterId;
24
- let callback = getExcelSameInputDataCallback;
25
- if (fieldType === FieldType.number) {
26
- stylesheetFormatterId = getExcelFormatFromGridFormatter(stylesheet, stylesheetFormatters, columnDef, grid, 'cell').stylesheetFormatter.id;
27
- callback = getExcelNumberCallback;
28
- }
29
- return { stylesheetFormatterId, getDataValueParser: callback };
30
- }
31
- export function getGroupTotalValue(totals, columnDef, groupType) {
32
- var _a, _b;
33
- return (_b = (_a = totals === null || totals === void 0 ? void 0 : totals[groupType]) === null || _a === void 0 ? void 0 : _a[columnDef.field]) !== null && _b !== void 0 ? _b : 0;
34
- }
35
- /** Get numeric formatter options when defined or use default values (minDecimal, maxDecimal, thousandSeparator, decimalSeparator, wrapNegativeNumber) */
36
- export function getNumericFormatterOptions(columnDef, grid, formatterType) {
37
- var _a;
38
- let dataType;
39
- if (formatterType === 'group') {
40
- switch (columnDef.groupTotalsFormatter) {
41
- case GroupTotalFormatters.avgTotalsCurrency:
42
- case GroupTotalFormatters.avgTotalsDollar:
43
- case GroupTotalFormatters.sumTotalsCurrency:
44
- case GroupTotalFormatters.sumTotalsCurrencyColored:
45
- case GroupTotalFormatters.sumTotalsDollar:
46
- case GroupTotalFormatters.sumTotalsDollarBold:
47
- case GroupTotalFormatters.sumTotalsDollarColored:
48
- case GroupTotalFormatters.sumTotalsDollarColoredBold:
49
- dataType = 'currency';
50
- break;
51
- case GroupTotalFormatters.avgTotalsPercentage:
52
- dataType = 'percent';
53
- break;
54
- case GroupTotalFormatters.avgTotals:
55
- case GroupTotalFormatters.minTotals:
56
- case GroupTotalFormatters.maxTotals:
57
- case GroupTotalFormatters.sumTotals:
58
- case GroupTotalFormatters.sumTotalsColored:
59
- case GroupTotalFormatters.sumTotalsBold:
60
- default:
61
- // side note, formatters are using "regular" without any decimal limits (min, max),
62
- // however in Excel export with custom format that doesn't work so well, we should use "decimal" to at least show optional decimals with "##"
63
- dataType = 'decimal';
64
- break;
65
- }
66
- }
67
- else {
68
- // when formatter is a Formatter.multiple, we need to loop through each of its formatter to find the best numeric data type
69
- if (columnDef.formatter === Formatters.multiple && Array.isArray((_a = columnDef.params) === null || _a === void 0 ? void 0 : _a.formatters)) {
70
- dataType = 'decimal';
71
- for (const formatter of columnDef.params.formatters) {
72
- dataType = getFormatterNumericDataType(formatter);
73
- if (dataType !== 'decimal') {
74
- break; // if we found something different than the default (decimal) then we can assume that we found our type so we can stop & return
75
- }
76
- }
77
- }
78
- else {
79
- dataType = getFormatterNumericDataType(columnDef.formatter);
80
- }
81
- }
82
- return retrieveFormatterOptions(columnDef, grid, dataType, formatterType);
83
- }
84
- export function getFormatterNumericDataType(formatter) {
85
- let dataType;
86
- switch (formatter) {
87
- case Formatters.currency:
88
- case Formatters.dollar:
89
- case Formatters.dollarColored:
90
- case Formatters.dollarColoredBold:
91
- dataType = 'currency';
92
- break;
93
- case Formatters.percent:
94
- case Formatters.percentComplete:
95
- case Formatters.percentCompleteBar:
96
- case Formatters.percentCompleteBarWithText:
97
- case Formatters.percentSymbol:
98
- dataType = 'percent';
99
- break;
100
- case Formatters.decimal:
101
- default:
102
- // use "decimal" instead of "regular" to show optional decimals "##" in Excel
103
- dataType = 'decimal';
104
- break;
105
- }
106
- return dataType;
107
- }
108
- export function getExcelFormatFromGridFormatter(stylesheet, stylesheetFormatters, columnDef, grid, formatterType) {
109
- var _a;
110
- let format = '';
111
- let groupType = '';
112
- let stylesheetFormatter;
113
- const fieldType = getColumnFieldType(columnDef);
114
- if (formatterType === 'group') {
115
- switch (columnDef.groupTotalsFormatter) {
116
- case GroupTotalFormatters.avgTotals:
117
- case GroupTotalFormatters.avgTotalsCurrency:
118
- case GroupTotalFormatters.avgTotalsDollar:
119
- case GroupTotalFormatters.avgTotalsPercentage:
120
- groupType = 'avg';
121
- break;
122
- case GroupTotalFormatters.minTotals:
123
- groupType = 'min';
124
- break;
125
- case GroupTotalFormatters.maxTotals:
126
- groupType = 'max';
127
- break;
128
- case GroupTotalFormatters.sumTotals:
129
- case GroupTotalFormatters.sumTotalsBold:
130
- case GroupTotalFormatters.sumTotalsColored:
131
- case GroupTotalFormatters.sumTotalsCurrency:
132
- case GroupTotalFormatters.sumTotalsCurrencyColored:
133
- case GroupTotalFormatters.sumTotalsDollar:
134
- case GroupTotalFormatters.sumTotalsDollarColoredBold:
135
- case GroupTotalFormatters.sumTotalsDollarColored:
136
- case GroupTotalFormatters.sumTotalsDollarBold:
137
- groupType = 'sum';
138
- break;
139
- default:
140
- stylesheetFormatter = stylesheetFormatters.numberFormatter;
141
- break;
142
- }
143
- }
144
- else {
145
- switch (fieldType) {
146
- case FieldType.number:
147
- switch (columnDef.formatter) {
148
- case Formatters.multiple:
149
- // when formatter is a Formatter.multiple, we need to loop through each of its formatter to find the best possible Excel format
150
- if (Array.isArray((_a = columnDef.params) === null || _a === void 0 ? void 0 : _a.formatters)) {
151
- for (const formatter of columnDef.params.formatters) {
152
- const { stylesheetFormatter: stylesheetFormatterResult } = getExcelFormatFromGridFormatter(stylesheet, stylesheetFormatters, { ...columnDef, formatter }, grid, formatterType);
153
- if (stylesheetFormatterResult !== stylesheetFormatters.numberFormatter) {
154
- stylesheetFormatter = stylesheetFormatterResult;
155
- break;
156
- }
157
- }
158
- }
159
- if (!stylesheetFormatter) {
160
- stylesheetFormatter = stylesheetFormatters.numberFormatter;
161
- }
162
- break;
163
- case Formatters.currency:
164
- case Formatters.decimal:
165
- case Formatters.dollar:
166
- case Formatters.dollarColored:
167
- case Formatters.dollarColoredBold:
168
- case Formatters.percent:
169
- case Formatters.percentComplete:
170
- case Formatters.percentCompleteBar:
171
- case Formatters.percentCompleteBarWithText:
172
- case Formatters.percentSymbol:
173
- format = createExcelFormatFromGridFormatter(columnDef, grid, 'cell');
174
- break;
175
- default:
176
- stylesheetFormatter = stylesheetFormatters.numberFormatter;
177
- break;
178
- }
179
- break;
180
- }
181
- }
182
- if (!stylesheetFormatter && (columnDef.formatter || columnDef.groupTotalsFormatter)) {
183
- format = createExcelFormatFromGridFormatter(columnDef, grid, formatterType, groupType);
184
- if (!stylesheetFormatters.hasOwnProperty(format)) {
185
- stylesheetFormatters[format] = stylesheet.createFormat({ format }); // save new formatter with its format as a prop key
186
- }
187
- stylesheetFormatter = stylesheetFormatters[format];
188
- }
189
- return { stylesheetFormatter: stylesheetFormatter, groupType };
190
- }
191
- // --
192
- // private functions
193
- // ------------------
194
- function createFormatFromNumber(formattedVal) {
195
- // full number syntax can have up to 7 sections, for example::
196
- // Total: ($10,420.55 USD) Expensed
197
- const [
198
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
199
- _, prefix, openBraquet, symbolPrefix, number, symbolSuffix, closingBraquet, suffix] = (formattedVal === null || formattedVal === void 0 ? void 0 : formattedVal.match(/^([^\d\(\-]*)([\(]?)([^\d]*)([\-]?[\w]]?[\d\s]*[.,\d]*[\d]*[^)\s\%]?)([^\d.,)]*)([\)]?)([^\d]*)$/i)) || [];
200
- // we use 1 so that they won't be removed when rounding numbers, however Excel uses 0 and # symbol
201
- // replace 1's by 0's (required numbers) and replace 2's by "#" (optional numbers)
202
- const replacedNumber = (number || '').replace(/1/g, '0').replace(/[2]/g, '#');
203
- const format = [
204
- escapeQuotes(prefix !== null && prefix !== void 0 ? prefix : ''),
205
- openBraquet !== null && openBraquet !== void 0 ? openBraquet : '',
206
- escapeQuotes(symbolPrefix !== null && symbolPrefix !== void 0 ? symbolPrefix : ''),
207
- replacedNumber,
208
- escapeQuotes(symbolSuffix !== null && symbolSuffix !== void 0 ? symbolSuffix : ''),
209
- closingBraquet !== null && closingBraquet !== void 0 ? closingBraquet : '',
210
- escapeQuotes(suffix !== null && suffix !== void 0 ? suffix : '')
211
- ].join('');
212
- return format.replace(',', '\,');
213
- }
214
- function createExcelFormatFromGridFormatter(columnDef, grid, formatterType, groupType = '') {
215
- let outputFormat = '';
216
- let positiveFormat = '';
217
- let negativeFormat = '';
218
- const { minDecimal, maxDecimal, thousandSeparator } = getNumericFormatterOptions(columnDef, grid, formatterType);
219
- const leftInteger = thousandSeparator ? '2220' : '0';
220
- const testingNo = parseFloat(`${leftInteger}.${excelTestingDecimalNumberPadding(minDecimal, maxDecimal)}`);
221
- if (formatterType === 'group' && columnDef.groupTotalsFormatter) {
222
- positiveFormat = sanitizeHtmlToText(columnDef.groupTotalsFormatter({ [groupType]: { [columnDef.field]: testingNo } }, columnDef, grid));
223
- negativeFormat = sanitizeHtmlToText(columnDef.groupTotalsFormatter({ [groupType]: { [columnDef.field]: -testingNo } }, columnDef, grid));
224
- }
225
- else if (columnDef.formatter) {
226
- positiveFormat = sanitizeHtmlToText(columnDef.formatter(0, 0, testingNo, columnDef, {}, grid));
227
- negativeFormat = sanitizeHtmlToText(columnDef.formatter(0, 0, -testingNo, columnDef, {}, grid));
228
- }
229
- if (positiveFormat && negativeFormat) {
230
- outputFormat = createFormatFromNumber(positiveFormat) + ';' + createFormatFromNumber(negativeFormat);
231
- }
232
- return outputFormat;
233
- }
234
- function escapeQuotes(val) {
235
- return val ? `"${val}"` : val;
236
- }
237
- /** Get number format for a number cell, for example { minDecimal: 2, maxDecimal: 5 } will return "00###" */
238
- function excelTestingDecimalNumberPadding(minDecimal, maxDecimal) {
239
- return textPadding('1', minDecimal) + textPadding('2', maxDecimal - minDecimal);
240
- }
241
- function textPadding(numberStr, count) {
242
- let output = '';
243
- for (let i = 0; i < count; i++) {
244
- output += numberStr;
245
- }
246
- return output;
247
- }
1
+ import { Constants, FieldType, Formatters, getColumnFieldType, getValueFromParamsOrFormatterOptions, GroupTotalFormatters, retrieveFormatterOptions, sanitizeHtmlToText, } from '@slickgrid-universal/common';
2
+ // define all type of potential excel data function callbacks
3
+ export const getExcelSameInputDataCallback = (data) => data;
4
+ export const getExcelNumberCallback = (data, column, excelFormatterId, _excelSheet, gridOptions) => ({
5
+ value: typeof data === 'string' && /\d/g.test(data) ? parseNumberWithFormatterOptions(data, column, gridOptions) : data,
6
+ metadata: { style: excelFormatterId }
7
+ });
8
+ /** Parse a number which the user might have provided formatter options (for example a user might have provided { decimalSeparator: ',', thousandSeparator: ' '}) */
9
+ export function parseNumberWithFormatterOptions(value, column, gridOptions) {
10
+ let outValue = value;
11
+ if (typeof value === 'string' && value) {
12
+ const decimalSeparator = getValueFromParamsOrFormatterOptions('decimalSeparator', column, gridOptions, Constants.DEFAULT_NUMBER_DECIMAL_SEPARATOR);
13
+ const val = (decimalSeparator === ',')
14
+ ? parseFloat(value.replace(/[^0-9\,]+/g, '').replace(',', '.'))
15
+ : parseFloat(value.replace(/[^\d\.]/g, ''));
16
+ outValue = isNaN(val) ? value : val;
17
+ }
18
+ return outValue;
19
+ }
20
+ /** use different Excel Stylesheet Format as per the Field Type */
21
+ export function useCellFormatByFieldType(stylesheet, stylesheetFormatters, columnDef, grid) {
22
+ const fieldType = getColumnFieldType(columnDef);
23
+ let stylesheetFormatterId;
24
+ let callback = getExcelSameInputDataCallback;
25
+ if (fieldType === FieldType.number) {
26
+ stylesheetFormatterId = getExcelFormatFromGridFormatter(stylesheet, stylesheetFormatters, columnDef, grid, 'cell').stylesheetFormatter.id;
27
+ callback = getExcelNumberCallback;
28
+ }
29
+ return { stylesheetFormatterId, getDataValueParser: callback };
30
+ }
31
+ export function getGroupTotalValue(totals, columnDef, groupType) {
32
+ var _a, _b;
33
+ return (_b = (_a = totals === null || totals === void 0 ? void 0 : totals[groupType]) === null || _a === void 0 ? void 0 : _a[columnDef.field]) !== null && _b !== void 0 ? _b : 0;
34
+ }
35
+ /** Get numeric formatter options when defined or use default values (minDecimal, maxDecimal, thousandSeparator, decimalSeparator, wrapNegativeNumber) */
36
+ export function getNumericFormatterOptions(columnDef, grid, formatterType) {
37
+ var _a;
38
+ let dataType;
39
+ if (formatterType === 'group') {
40
+ switch (columnDef.groupTotalsFormatter) {
41
+ case GroupTotalFormatters.avgTotalsCurrency:
42
+ case GroupTotalFormatters.avgTotalsDollar:
43
+ case GroupTotalFormatters.sumTotalsCurrency:
44
+ case GroupTotalFormatters.sumTotalsCurrencyColored:
45
+ case GroupTotalFormatters.sumTotalsDollar:
46
+ case GroupTotalFormatters.sumTotalsDollarBold:
47
+ case GroupTotalFormatters.sumTotalsDollarColored:
48
+ case GroupTotalFormatters.sumTotalsDollarColoredBold:
49
+ dataType = 'currency';
50
+ break;
51
+ case GroupTotalFormatters.avgTotalsPercentage:
52
+ dataType = 'percent';
53
+ break;
54
+ case GroupTotalFormatters.avgTotals:
55
+ case GroupTotalFormatters.minTotals:
56
+ case GroupTotalFormatters.maxTotals:
57
+ case GroupTotalFormatters.sumTotals:
58
+ case GroupTotalFormatters.sumTotalsColored:
59
+ case GroupTotalFormatters.sumTotalsBold:
60
+ default:
61
+ // side note, formatters are using "regular" without any decimal limits (min, max),
62
+ // however in Excel export with custom format that doesn't work so well, we should use "decimal" to at least show optional decimals with "##"
63
+ dataType = 'decimal';
64
+ break;
65
+ }
66
+ }
67
+ else {
68
+ // when formatter is a Formatter.multiple, we need to loop through each of its formatter to find the best numeric data type
69
+ if (columnDef.formatter === Formatters.multiple && Array.isArray((_a = columnDef.params) === null || _a === void 0 ? void 0 : _a.formatters)) {
70
+ dataType = 'decimal';
71
+ for (const formatter of columnDef.params.formatters) {
72
+ dataType = getFormatterNumericDataType(formatter);
73
+ if (dataType !== 'decimal') {
74
+ break; // if we found something different than the default (decimal) then we can assume that we found our type so we can stop & return
75
+ }
76
+ }
77
+ }
78
+ else {
79
+ dataType = getFormatterNumericDataType(columnDef.formatter);
80
+ }
81
+ }
82
+ return retrieveFormatterOptions(columnDef, grid, dataType, formatterType);
83
+ }
84
+ export function getFormatterNumericDataType(formatter) {
85
+ let dataType;
86
+ switch (formatter) {
87
+ case Formatters.currency:
88
+ case Formatters.dollar:
89
+ case Formatters.dollarColored:
90
+ case Formatters.dollarColoredBold:
91
+ dataType = 'currency';
92
+ break;
93
+ case Formatters.percent:
94
+ case Formatters.percentComplete:
95
+ case Formatters.percentCompleteBar:
96
+ case Formatters.percentCompleteBarWithText:
97
+ case Formatters.percentSymbol:
98
+ dataType = 'percent';
99
+ break;
100
+ case Formatters.decimal:
101
+ default:
102
+ // use "decimal" instead of "regular" to show optional decimals "##" in Excel
103
+ dataType = 'decimal';
104
+ break;
105
+ }
106
+ return dataType;
107
+ }
108
+ export function getExcelFormatFromGridFormatter(stylesheet, stylesheetFormatters, columnDef, grid, formatterType) {
109
+ var _a;
110
+ let format = '';
111
+ let groupType = '';
112
+ let stylesheetFormatter;
113
+ const fieldType = getColumnFieldType(columnDef);
114
+ if (formatterType === 'group') {
115
+ switch (columnDef.groupTotalsFormatter) {
116
+ case GroupTotalFormatters.avgTotals:
117
+ case GroupTotalFormatters.avgTotalsCurrency:
118
+ case GroupTotalFormatters.avgTotalsDollar:
119
+ case GroupTotalFormatters.avgTotalsPercentage:
120
+ groupType = 'avg';
121
+ break;
122
+ case GroupTotalFormatters.minTotals:
123
+ groupType = 'min';
124
+ break;
125
+ case GroupTotalFormatters.maxTotals:
126
+ groupType = 'max';
127
+ break;
128
+ case GroupTotalFormatters.sumTotals:
129
+ case GroupTotalFormatters.sumTotalsBold:
130
+ case GroupTotalFormatters.sumTotalsColored:
131
+ case GroupTotalFormatters.sumTotalsCurrency:
132
+ case GroupTotalFormatters.sumTotalsCurrencyColored:
133
+ case GroupTotalFormatters.sumTotalsDollar:
134
+ case GroupTotalFormatters.sumTotalsDollarColoredBold:
135
+ case GroupTotalFormatters.sumTotalsDollarColored:
136
+ case GroupTotalFormatters.sumTotalsDollarBold:
137
+ groupType = 'sum';
138
+ break;
139
+ default:
140
+ stylesheetFormatter = stylesheetFormatters.numberFormatter;
141
+ break;
142
+ }
143
+ }
144
+ else {
145
+ switch (fieldType) {
146
+ case FieldType.number:
147
+ switch (columnDef.formatter) {
148
+ case Formatters.multiple:
149
+ // when formatter is a Formatter.multiple, we need to loop through each of its formatter to find the best possible Excel format
150
+ if (Array.isArray((_a = columnDef.params) === null || _a === void 0 ? void 0 : _a.formatters)) {
151
+ for (const formatter of columnDef.params.formatters) {
152
+ const { stylesheetFormatter: stylesheetFormatterResult } = getExcelFormatFromGridFormatter(stylesheet, stylesheetFormatters, { ...columnDef, formatter }, grid, formatterType);
153
+ if (stylesheetFormatterResult !== stylesheetFormatters.numberFormatter) {
154
+ stylesheetFormatter = stylesheetFormatterResult;
155
+ break;
156
+ }
157
+ }
158
+ }
159
+ if (!stylesheetFormatter) {
160
+ stylesheetFormatter = stylesheetFormatters.numberFormatter;
161
+ }
162
+ break;
163
+ case Formatters.currency:
164
+ case Formatters.decimal:
165
+ case Formatters.dollar:
166
+ case Formatters.dollarColored:
167
+ case Formatters.dollarColoredBold:
168
+ case Formatters.percent:
169
+ case Formatters.percentComplete:
170
+ case Formatters.percentCompleteBar:
171
+ case Formatters.percentCompleteBarWithText:
172
+ case Formatters.percentSymbol:
173
+ format = createExcelFormatFromGridFormatter(columnDef, grid, 'cell');
174
+ break;
175
+ default:
176
+ stylesheetFormatter = stylesheetFormatters.numberFormatter;
177
+ break;
178
+ }
179
+ break;
180
+ }
181
+ }
182
+ if (!stylesheetFormatter && (columnDef.formatter || columnDef.groupTotalsFormatter)) {
183
+ format = createExcelFormatFromGridFormatter(columnDef, grid, formatterType, groupType);
184
+ if (!stylesheetFormatters.hasOwnProperty(format)) {
185
+ stylesheetFormatters[format] = stylesheet.createFormat({ format }); // save new formatter with its format as a prop key
186
+ }
187
+ stylesheetFormatter = stylesheetFormatters[format];
188
+ }
189
+ return { stylesheetFormatter: stylesheetFormatter, groupType };
190
+ }
191
+ // --
192
+ // private functions
193
+ // ------------------
194
+ function createFormatFromNumber(formattedVal) {
195
+ // full number syntax can have up to 7 sections, for example::
196
+ // Total: ($10,420.55 USD) Expensed
197
+ const [
198
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
199
+ _, prefix, openBraquet, symbolPrefix, number, symbolSuffix, closingBraquet, suffix] = (formattedVal === null || formattedVal === void 0 ? void 0 : formattedVal.match(/^([^\d\(\-]*)([\(]?)([^\d]*)([\-]?[\w]]?[\d\s]*[.,\d]*[\d]*[^)\s\%]?)([^\d.,)]*)([\)]?)([^\d]*)$/i)) || [];
200
+ // we use 1 so that they won't be removed when rounding numbers, however Excel uses 0 and # symbol
201
+ // replace 1's by 0's (required numbers) and replace 2's by "#" (optional numbers)
202
+ const replacedNumber = (number || '').replace(/1/g, '0').replace(/[2]/g, '#');
203
+ const format = [
204
+ escapeQuotes(prefix !== null && prefix !== void 0 ? prefix : ''),
205
+ openBraquet !== null && openBraquet !== void 0 ? openBraquet : '',
206
+ escapeQuotes(symbolPrefix !== null && symbolPrefix !== void 0 ? symbolPrefix : ''),
207
+ replacedNumber,
208
+ escapeQuotes(symbolSuffix !== null && symbolSuffix !== void 0 ? symbolSuffix : ''),
209
+ closingBraquet !== null && closingBraquet !== void 0 ? closingBraquet : '',
210
+ escapeQuotes(suffix !== null && suffix !== void 0 ? suffix : '')
211
+ ].join('');
212
+ return format.replace(',', '\,');
213
+ }
214
+ function createExcelFormatFromGridFormatter(columnDef, grid, formatterType, groupType = '') {
215
+ let outputFormat = '';
216
+ let positiveFormat = '';
217
+ let negativeFormat = '';
218
+ const { minDecimal, maxDecimal, thousandSeparator } = getNumericFormatterOptions(columnDef, grid, formatterType);
219
+ const leftInteger = thousandSeparator ? '2220' : '0';
220
+ const testingNo = parseFloat(`${leftInteger}.${excelTestingDecimalNumberPadding(minDecimal, maxDecimal)}`);
221
+ if (formatterType === 'group' && columnDef.groupTotalsFormatter) {
222
+ positiveFormat = sanitizeHtmlToText(columnDef.groupTotalsFormatter({ [groupType]: { [columnDef.field]: testingNo } }, columnDef, grid));
223
+ negativeFormat = sanitizeHtmlToText(columnDef.groupTotalsFormatter({ [groupType]: { [columnDef.field]: -testingNo } }, columnDef, grid));
224
+ }
225
+ else if (columnDef.formatter) {
226
+ positiveFormat = sanitizeHtmlToText(columnDef.formatter(0, 0, testingNo, columnDef, {}, grid));
227
+ negativeFormat = sanitizeHtmlToText(columnDef.formatter(0, 0, -testingNo, columnDef, {}, grid));
228
+ }
229
+ if (positiveFormat && negativeFormat) {
230
+ outputFormat = createFormatFromNumber(positiveFormat) + ';' + createFormatFromNumber(negativeFormat);
231
+ }
232
+ return outputFormat;
233
+ }
234
+ function escapeQuotes(val) {
235
+ return val ? `"${val}"` : val;
236
+ }
237
+ /** Get number format for a number cell, for example { minDecimal: 2, maxDecimal: 5 } will return "00###" */
238
+ function excelTestingDecimalNumberPadding(minDecimal, maxDecimal) {
239
+ return textPadding('1', minDecimal) + textPadding('2', maxDecimal - minDecimal);
240
+ }
241
+ function textPadding(numberStr, count) {
242
+ let output = '';
243
+ for (let i = 0; i < count; i++) {
244
+ output += numberStr;
245
+ }
246
+ return output;
247
+ }
248
248
  //# sourceMappingURL=excelUtils.js.map
package/dist/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { ExcelExportService } from './excelExport.service';
1
+ export { ExcelExportService } from './excelExport.service';
2
2
  //# sourceMappingURL=index.js.map
@@ -1,2 +1,2 @@
1
- export {};
1
+ export {};
2
2
  //# sourceMappingURL=excelCellFormat.interface.js.map
@@ -1,2 +1,2 @@
1
- export {};
1
+ export {};
2
2
  //# sourceMappingURL=excelMetadata.interface.js.map
@@ -1,2 +1,2 @@
1
- export {};
1
+ export {};
2
2
  //# sourceMappingURL=excelStylesheet.interface.js.map
@@ -1,4 +1,4 @@
1
- export * from './excelCellFormat.interface';
2
- export * from './excelMetadata.interface';
3
- export * from './excelStylesheet.interface';
1
+ export * from './excelCellFormat.interface';
2
+ export * from './excelMetadata.interface';
3
+ export * from './excelStylesheet.interface';
4
4
  //# sourceMappingURL=index.js.map