@sme.up/doc-alchemist 1.3.0 → 1.4.0-CommonJS-20251015134706
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/README.md +1 -1
- package/dist/assets/gfx-data.js +4 -1
- package/dist/assets/gfx-data.js.map +1 -1
- package/dist/converters/excel/commons.d.ts +2 -2
- package/dist/converters/excel/commons.js +49 -33
- package/dist/converters/excel/commons.js.map +1 -1
- package/dist/converters/excel/excel-converter.types.js +14 -11
- package/dist/converters/excel/excel-converter.types.js.map +1 -1
- package/dist/converters/excel/matrix-converter.d.ts +3 -3
- package/dist/converters/excel/matrix-converter.js +46 -36
- package/dist/converters/excel/matrix-converter.js.map +1 -1
- package/dist/converters/excel/tree-converter.d.ts +2 -2
- package/dist/converters/excel/tree-converter.js +33 -25
- package/dist/converters/excel/tree-converter.js.map +1 -1
- package/dist/converters/excel-converter.d.ts +3 -3
- package/dist/converters/excel-converter.js +18 -13
- package/dist/converters/excel-converter.js.map +1 -1
- package/dist/converters/images/charts-generator.d.ts +2 -2
- package/dist/converters/images/charts-generator.js +41 -4
- package/dist/converters/images/charts-generator.js.map +1 -1
- package/dist/converters/pdf/autotable-renderer.d.ts +2 -2
- package/dist/converters/pdf/autotable-renderer.js +7 -3
- package/dist/converters/pdf/autotable-renderer.js.map +1 -1
- package/dist/converters/pdf/cover-renderer.js +41 -4
- package/dist/converters/pdf/cover-renderer.js.map +1 -1
- package/dist/converters/pdf/formulas-helper.d.ts +2 -2
- package/dist/converters/pdf/formulas-helper.js +37 -28
- package/dist/converters/pdf/formulas-helper.js.map +1 -1
- package/dist/converters/pdf/matrix-converter.d.ts +2 -2
- package/dist/converters/pdf/matrix-converter.js +52 -12
- package/dist/converters/pdf/matrix-converter.js.map +1 -1
- package/dist/converters/pdf/pdf-converter.types.js +2 -1
- package/dist/converters/pdf/pdf-converter.types.js.map +1 -1
- package/dist/converters/pdf/sch-converter.d.ts +1 -1
- package/dist/converters/pdf/sch-converter.js +43 -6
- package/dist/converters/pdf/sch-converter.js.map +1 -1
- package/dist/converters/pdf-converter.d.ts +3 -3
- package/dist/converters/pdf-converter.js +23 -18
- package/dist/converters/pdf-converter.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +13 -4
- package/dist/index.js.map +1 -1
- package/dist/types/component-props.js +5 -2
- package/dist/types/component-props.js.map +1 -1
- package/dist/types/component.js +7 -4
- package/dist/types/component.js.map +1 -1
- package/dist/types/converter.js +2 -1
- package/dist/types/converter.js.map +1 -1
- package/dist/types/data-structures/smeupDataStructure.d.ts +4 -4
- package/dist/types/data-structures/smeupDataStructure.js +16 -12
- package/dist/types/data-structures/smeupDataStructure.js.map +1 -1
- package/dist/types/data-structures/smeupDataTable.d.ts +1 -1
- package/dist/types/data-structures/smeupDataTable.js +4 -1
- package/dist/types/data-structures/smeupDataTable.js.map +1 -1
- package/dist/types/data-structures/smeupDataTree.d.ts +2 -2
- package/dist/types/data-structures/smeupDataTree.js +2 -1
- package/dist/types/data-structures/smeupDataTree.js.map +1 -1
- package/dist/types/data-structures/smeupSch.d.ts +6 -6
- package/dist/types/data-structures/smeupSch.js +5 -2
- package/dist/types/data-structures/smeupSch.js.map +1 -1
- package/dist/types/dynamism.d.ts +1 -1
- package/dist/types/dynamism.js +7 -4
- package/dist/types/dynamism.js.map +1 -1
- package/dist/types/general.js +7 -2
- package/dist/types/general.js.map +1 -1
- package/dist/types/helpers.js +5 -2
- package/dist/types/helpers.js.map +1 -1
- package/dist/types/index.d.ts +3 -2
- package/dist/types/index.js +7 -4
- package/dist/types/index.js.map +1 -1
- package/dist/utils/commons-utility.d.ts +4 -3
- package/dist/utils/commons-utility.js +136 -43
- package/dist/utils/commons-utility.js.map +1 -1
- package/dist/utils/datastructure-utility.d.ts +1 -1
- package/dist/utils/datastructure-utility.js +5 -1
- package/dist/utils/datastructure-utility.js.map +1 -1
- package/dist/utils/dates-utility.js +37 -27
- package/dist/utils/dates-utility.js.map +1 -1
- package/dist/utils/formatter-utility.d.ts +1 -1
- package/dist/utils/formatter-utility.js +9 -3
- package/dist/utils/formatter-utility.js.map +1 -1
- package/dist/utils/image-utils.js +10 -3
- package/dist/utils/image-utils.js.map +1 -1
- package/dist/utils/math-utility.js +46 -29
- package/dist/utils/math-utility.js.map +1 -1
- package/dist/utils/objects-utility.d.ts +1 -1
- package/dist/utils/objects-utility.js +9 -3
- package/dist/utils/objects-utility.js.map +1 -1
- package/dist/utils/regex-utility.js +4 -1
- package/dist/utils/regex-utility.js.map +1 -1
- package/package.json +1 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"autotable-renderer.js","sourceRoot":"","sources":["../../../src/converters/pdf/autotable-renderer.ts"],"names":[],"mappings":"AASA,
|
|
1
|
+
{"version":3,"file":"autotable-renderer.js","sourceRoot":"","sources":["../../../src/converters/pdf/autotable-renderer.ts"],"names":[],"mappings":";;;AASA,yDAAsE;AAS/D,MAAM,mBAAmB,GAAG,CACjC,cAAwB,EACxB,IAAwB,EACxB,eAAuB,EACvB,UAA6B,EAC7B,OAAyB,EACZ,EAAE;IACf,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,GAAG,cAAc,CACzD,cAAc,EACd,eAAe,CAChB,CAAC;IACF,OAAO;QACL,IAAI,EAAE,CAAC,cAAc,CAAC;QACtB,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACjD,MAAM,EAAE,EAAE;QACV,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;QAClD,MAAM;QACN,YAAY;QACZ,UAAU;QACV,WAAW,EAAE,OAAO,CAAC,EAAE,CAAC,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC;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AC,CAAC","sourcesContent":["import { UserOptions } from \"jspdf-autotable\";\nimport { CellStyleData, CellValueProps } from \"./pdf-converter.types\";\nimport type {\n Styles,\n OverflowType,\n FontStyle,\n CellHookData,\n HookData,\n} from \"jspdf-autotable\";\nimport { getLogoPositionAndDimension } from \"../../utils/image-utils\";\nimport { ConverterContext } from \"../../types/converter\";\n\ntype TableStyles = {\n styles: Partial<Styles>;\n columnStyles: Record<string, Partial<Styles>>;\n headStyles: Partial<Styles>;\n};\n\nexport const getAutoTableOptions = (\n visibleHeaders: string[],\n data: CellValueProps[][],\n optimalFontSize: number,\n cellStyles: CellStyleData[][],\n context: ConverterContext,\n): UserOptions => {\n const { styles, columnStyles, headStyles } = getTableStyles(\n visibleHeaders,\n optimalFontSize,\n );\n return {\n head: [visibleHeaders],\n body: data.map(row => row.map(cell => cell.text)),\n startY: 27,\n margin: { left: 5, right: 5, top: 20, bottom: 10 },\n styles,\n columnStyles,\n headStyles,\n didDrawPage: dataArg => didDrawPageHandler(dataArg, context),\n didParseCell: dataArg => didParseCellHandler(dataArg, cellStyles),\n };\n};\n\nconst getTableStyles = (\n visibleHeaders: string[],\n optimalFontSize: number,\n): TableStyles => {\n const styles: Partial<Styles> = {\n font: \"helvetica\",\n fontSize: optimalFontSize + 1,\n cellPadding: 1,\n overflow: \"linebreak\" as OverflowType,\n cellWidth: \"auto\",\n minCellWidth: 5,\n };\n\n const columnStyles: { [key: string]: Partial<Styles> } = {\n ...Object.fromEntries(\n visibleHeaders.map((_, index) => [\n index,\n {\n cellWidth: \"auto\",\n halign: \"left\",\n overflow: \"linebreak\" as OverflowType,\n cellPadding: { top: 1, right: 1, bottom: 1, left: 1 },\n },\n ]),\n ),\n };\n\n const headStyles: Partial<Styles> = {\n fillColor: [255, 0, 0],\n textColor: [255, 255, 255],\n fontStyle: \"bold\" as FontStyle,\n halign: \"center\",\n };\n\n return { styles, columnStyles, headStyles };\n};\n\nconst didParseCellHandler = (\n dataArg: CellHookData,\n cellStyles: CellStyleData[][],\n) => {\n if (dataArg.section === \"body\") {\n const rowIndex = dataArg.row.index;\n const colIndex = dataArg.column.index;\n const cellStyle = cellStyles[rowIndex]?.[colIndex];\n\n if (cellStyle) {\n // Apply styles from the Excel cell\n Object.assign(dataArg.cell.styles, cellStyle);\n }\n }\n};\n\nconst didDrawPageHandler = (dataArg: HookData, context: ConverterContext) => {\n let rightOffset = 0;\n const pageWidth = dataArg.doc.internal.pageSize.getWidth();\n\n // Add the logo on each page\n if (context?.logoBase64) {\n const { logoWidth, logoHeight, logoX, logoY } = getLogoPositionAndDimension(\n dataArg.doc,\n context.logoBase64,\n );\n\n try {\n dataArg.doc.addImage(\n context.logoBase64,\n \"PNG\",\n logoX,\n logoY,\n logoWidth,\n logoHeight,\n );\n } catch (error) {\n console.warn(\"Error adding logo:\", error);\n }\n\n rightOffset = logoWidth;\n }\n\n let textFontSize = 11;\n const textX = 5;\n let textY = 10;\n\n // split on two lines at most\n // remember to set measure before calling splitTextToSize otherwise it will not work!\n\n if (context?.title) {\n dataArg.doc.setFontSize(textFontSize);\n dataArg.doc.setFont(\"helvetica\", \"normal\");\n const title = dataArg.doc.splitTextToSize(\n context.title,\n pageWidth - rightOffset - textX * 2 - 10,\n );\n dataArg.doc.text(title[0], textX, textY, { align: \"left\" });\n if (title[1]) {\n textY += textFontSize / 2;\n dataArg.doc.text(title[1], textX, textY, { align: \"left\" });\n }\n }\n\n // since the limited vertical space, if subtitle is too long to be fit into one line, it should be trimmed (first element of split is taken into account)`\n\n if (context?.subtitle) {\n textFontSize -= 2;\n textY += textFontSize / 2;\n dataArg.doc.setFontSize(textFontSize);\n dataArg.doc.setFont(\"helvetica\", \"normal\");\n const subtitle = dataArg.doc.splitTextToSize(\n context.subtitle,\n pageWidth - rightOffset - textX * 2 - 10,\n );\n dataArg.doc.text(subtitle[0], textX, textY, { align: \"left\" });\n }\n};\n\n"]}
|
|
@@ -1,10 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.createCoverPdf = void 0;
|
|
37
|
+
const jsPDFModule = __importStar(require("jspdf"));
|
|
38
|
+
const image_utils_1 = require("../../utils/image-utils");
|
|
39
|
+
const createCoverPdf = (image, logo, title, subtitle, subtitle2) => {
|
|
4
40
|
const doc = new jsPDFModule.jsPDF("landscape");
|
|
5
41
|
const pageWidth = doc.internal.pageSize.getWidth();
|
|
6
42
|
const pageHeight = doc.internal.pageSize.getHeight();
|
|
7
|
-
const { logoWidth, logoHeight, logoX, logoY } = getLogoPositionAndDimension(doc, logo);
|
|
43
|
+
const { logoWidth, logoHeight, logoX, logoY } = (0, image_utils_1.getLogoPositionAndDimension)(doc, logo);
|
|
8
44
|
const titleFontSize = 24;
|
|
9
45
|
const subtitleFontSize = 16;
|
|
10
46
|
const subtitle2FontSize = 14;
|
|
@@ -47,4 +83,5 @@ export const createCoverPdf = (image, logo, title, subtitle, subtitle2) => {
|
|
|
47
83
|
}
|
|
48
84
|
return doc.output("arraybuffer");
|
|
49
85
|
};
|
|
86
|
+
exports.createCoverPdf = createCoverPdf;
|
|
50
87
|
//# sourceMappingURL=cover-renderer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cover-renderer.js","sourceRoot":"","sources":["../../../src/converters/pdf/cover-renderer.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"cover-renderer.js","sourceRoot":"","sources":["../../../src/converters/pdf/cover-renderer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAqC;AACrC,yDAAsE;AAE/D,MAAM,cAAc,GAAG,CAC5B,KAAa,EACb,IAAY,EACZ,KAAa,EACb,QAAgB,EAChB,SAAkB,EACL,EAAE;IACf,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAC/C,MAAM,SAAS,GAAG,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;IACnD,MAAM,UAAU,GAAG,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;IACrD,MAAM,EAAE,SA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;IAEpE,IAAI,QAAQ,EAAE,CAAC;QACb,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;QACnC,GAAG,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC;QAClC,cAAc,GAAG,GAAG,CAAC,eAAe,CAAC,QAAQ,EAAE,SAAS,GAAG,EAAE,CAAC,CAAC;QAC/D,QAAQ,IAAI,gBAAgB,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QAC/D,GAAG,CAAC,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,CAAC,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;IACzE,CAAC;IAED,IAAI,SAAS,EAAE,CAAC;QACd,GAAG,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;QACnC,eAAe,GAAG,GAAG,CAAC,eAAe,CAAC,SAAS,EAAE,SAAS,GAAG,EAAE,CAAC,CAAC;QACjE,QAAQ,IAAI,iBAAiB,GAAG,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QACnE,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,SAAS,GAAG,CAAC,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AACnC,CAAC,CAAC;AA5DW,QAAA,cAAc,kBA4DzB","sourcesContent":["import * as jsPDFModule from \"jspdf\";\nimport { getLogoPositionAndDimension } from \"../../utils/image-utils\";\n\nexport const createCoverPdf = (\n image: string,\n logo: string,\n title: string,\n subtitle: string,\n subtitle2?: string,\n): ArrayBuffer => {\n const doc = new jsPDFModule.jsPDF(\"landscape\");\n const pageWidth = doc.internal.pageSize.getWidth();\n const pageHeight = doc.internal.pageSize.getHeight();\n const { logoWidth, logoHeight, logoX, logoY } = getLogoPositionAndDimension(\n doc,\n logo,\n );\n const titleFontSize = 24;\n const subtitleFontSize = 16;\n const subtitle2FontSize = 14;\n let currentY = 45;\n\n // background should be applied before any other element\n if (image) {\n try {\n doc.addImage(image, \"PNG\", 0, 0, pageWidth, pageHeight);\n } catch (error) {\n console.warn(\"Error adding image:\", error);\n }\n }\n\n try {\n doc.addImage(logo, \"PNG\", logoX, logoY, logoWidth, logoHeight);\n } catch (error) {\n console.warn(\"Error adding logo:\", error);\n }\n\n // slices calculations should be made after setting font size!\n let titleSlices: string[] = [];\n let subtitleSlices: string[] = [];\n let subtitle2Slices: string[] = [];\n\n doc.setFont(\"helvetica\", \"bold\");\n doc.setFontSize(titleFontSize);\n titleSlices = doc.splitTextToSize(title, pageWidth - 20);\n doc.text(titleSlices, pageWidth / 2, currentY, { align: \"center\" });\n\n if (subtitle) {\n doc.setFont(\"helvetica\", \"normal\");\n doc.setFontSize(subtitleFontSize);\n subtitleSlices = doc.splitTextToSize(subtitle, pageWidth - 20);\n currentY += subtitleFontSize * Math.max(titleSlices.length, 1);\n doc.text(subtitleSlices, pageWidth / 2, currentY, { align: \"center\" });\n }\n\n if (subtitle2) {\n doc.setFontSize(subtitle2FontSize);\n subtitle2Slices = doc.splitTextToSize(subtitle2, pageWidth - 20);\n currentY += subtitle2FontSize * Math.max(subtitleSlices.length, 1);\n doc.text(subtitle2Slices, pageWidth / 2, currentY, { align: \"center\" });\n }\n\n return doc.output(\"arraybuffer\");\n};\n\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { WebupManagerData } from "../../types/index
|
|
2
|
-
import { CellValueProps } from "./pdf-converter.types
|
|
1
|
+
import { WebupManagerData } from "../../types/index";
|
|
2
|
+
import { CellValueProps } from "./pdf-converter.types";
|
|
3
3
|
import ExcelJS from "exceljs";
|
|
4
4
|
/**
|
|
5
5
|
* Handles the calculation of Excel-like SUBTOTAL formulas for a given cell and range.
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCellValueData = exports.evaluateStandardFormula = exports.handleSubtotalFormula = void 0;
|
|
7
|
+
const commons_1 = require("../excel/commons");
|
|
8
|
+
const dates_utility_1 = require("../../utils/dates-utility");
|
|
9
|
+
const formatter_utility_1 = require("../../utils/formatter-utility");
|
|
10
|
+
const exceljs_1 = __importDefault(require("exceljs"));
|
|
11
|
+
const math_utility_1 = require("../../utils/math-utility");
|
|
6
12
|
/**
|
|
7
13
|
* Handles the calculation of Excel-like SUBTOTAL formulas for a given cell and range.
|
|
8
14
|
*
|
|
@@ -19,13 +25,13 @@ import { calculateExpression } from "../../utils/math-utility.js";
|
|
|
19
25
|
* - [3]: The end cell reference of the range.
|
|
20
26
|
* @returns An object containing the computed value and its type ("number" or "string").
|
|
21
27
|
*/
|
|
22
|
-
|
|
28
|
+
const handleSubtotalFormula = (cell, worksheet, webupManagerData, subtotalMatch) => {
|
|
23
29
|
const subtotalType = parseInt(subtotalMatch[1], 10);
|
|
24
30
|
const startRef = subtotalMatch[2];
|
|
25
31
|
const endRef = subtotalMatch[3];
|
|
26
|
-
const cells = getCellsFromRange(worksheet, startRef, endRef).filter(cell => cell.type !==
|
|
32
|
+
const cells = (0, commons_1.getCellsFromRange)(worksheet, startRef, endRef).filter(cell => cell.type !== exceljs_1.default.ValueType.Formula);
|
|
27
33
|
let result;
|
|
28
|
-
const areAllDates = areAllValuesDates(cells);
|
|
34
|
+
const areAllDates = (0, dates_utility_1.areAllValuesDates)(cells);
|
|
29
35
|
switch (subtotalType) {
|
|
30
36
|
case 3: // COUNTA
|
|
31
37
|
result = cells.length;
|
|
@@ -49,7 +55,7 @@ export const handleSubtotalFormula = (cell, worksheet, webupManagerData, subtota
|
|
|
49
55
|
case 4: // MAX
|
|
50
56
|
result = cells.length
|
|
51
57
|
? Math.max(...cells.map(cell => {
|
|
52
|
-
if (cell.type ===
|
|
58
|
+
if (cell.type === exceljs_1.default.ValueType.Date) {
|
|
53
59
|
return cell.value instanceof Date
|
|
54
60
|
? cell.value.getTime()
|
|
55
61
|
: -Infinity;
|
|
@@ -65,7 +71,7 @@ export const handleSubtotalFormula = (cell, worksheet, webupManagerData, subtota
|
|
|
65
71
|
case 5: // MIN
|
|
66
72
|
result = cells.length
|
|
67
73
|
? Math.min(...cells.map(cell => {
|
|
68
|
-
if (cell.type ===
|
|
74
|
+
if (cell.type === exceljs_1.default.ValueType.Date) {
|
|
69
75
|
return cell.value instanceof Date
|
|
70
76
|
? cell.value.getTime()
|
|
71
77
|
: Infinity;
|
|
@@ -108,14 +114,14 @@ export const handleSubtotalFormula = (cell, worksheet, webupManagerData, subtota
|
|
|
108
114
|
if (areAllDates) {
|
|
109
115
|
return {
|
|
110
116
|
value: result,
|
|
111
|
-
text: formatDate(new Date(result), webupManagerData),
|
|
117
|
+
text: (0, formatter_utility_1.formatDate)(new Date(result), webupManagerData),
|
|
112
118
|
type: "date",
|
|
113
119
|
};
|
|
114
120
|
}
|
|
115
121
|
else if (typeof result === "number" && cell.numFmt) {
|
|
116
122
|
return {
|
|
117
123
|
value: result,
|
|
118
|
-
text: formatNumber(result, cell.numFmt, webupManagerData),
|
|
124
|
+
text: (0, formatter_utility_1.formatNumber)(result, cell.numFmt, webupManagerData),
|
|
119
125
|
type: "number",
|
|
120
126
|
};
|
|
121
127
|
}
|
|
@@ -125,6 +131,7 @@ export const handleSubtotalFormula = (cell, worksheet, webupManagerData, subtota
|
|
|
125
131
|
type: typeof result === "number" ? "number" : "string",
|
|
126
132
|
};
|
|
127
133
|
};
|
|
134
|
+
exports.handleSubtotalFormula = handleSubtotalFormula;
|
|
128
135
|
/**
|
|
129
136
|
* Evaluates a standard Excel formula from a given cell by replacing cell references
|
|
130
137
|
* with their actual values from the worksheet and then computing the result.
|
|
@@ -140,23 +147,23 @@ export const handleSubtotalFormula = (cell, worksheet, webupManagerData, subtota
|
|
|
140
147
|
* @param webupManagerData - Additional data used for number formatting.
|
|
141
148
|
* @returns An object containing the evaluated value and its type.
|
|
142
149
|
*/
|
|
143
|
-
|
|
150
|
+
const evaluateStandardFormula = (cell, worksheet, webupManagerData) => {
|
|
144
151
|
const formula = cell.formula;
|
|
145
152
|
const formulaWithSolvedVariables = formula.replace(/([A-Z]+\d+)/g, ref => {
|
|
146
153
|
try {
|
|
147
154
|
const refCell = worksheet.getCell(ref);
|
|
148
155
|
if (refCell) {
|
|
149
|
-
if (refCell.type ===
|
|
156
|
+
if (refCell.type === exceljs_1.default.ValueType.Number) {
|
|
150
157
|
return refCell.value?.toString() ?? "0";
|
|
151
158
|
}
|
|
152
|
-
else if (refCell.type ===
|
|
159
|
+
else if (refCell.type === exceljs_1.default.ValueType.String) {
|
|
153
160
|
// If the cell contains text, return as is (no quotes) for JS compatibility
|
|
154
161
|
return refCell.value?.toString() ?? "";
|
|
155
162
|
}
|
|
156
|
-
else if (refCell.type ===
|
|
157
|
-
return getCellValueData(refCell, worksheet, webupManagerData).value.toString();
|
|
163
|
+
else if (refCell.type === exceljs_1.default.ValueType.Formula) {
|
|
164
|
+
return (0, exports.getCellValueData)(refCell, worksheet, webupManagerData).value.toString();
|
|
158
165
|
}
|
|
159
|
-
else if (refCell.type ===
|
|
166
|
+
else if (refCell.type === exceljs_1.default.ValueType.Date &&
|
|
160
167
|
refCell.value instanceof Date) {
|
|
161
168
|
return refCell.value.getTime().toString();
|
|
162
169
|
}
|
|
@@ -170,12 +177,12 @@ export const evaluateStandardFormula = (cell, worksheet, webupManagerData) => {
|
|
|
170
177
|
}
|
|
171
178
|
return "0";
|
|
172
179
|
});
|
|
173
|
-
const result = calculateExpression(formulaWithSolvedVariables);
|
|
180
|
+
const result = (0, math_utility_1.calculateExpression)(formulaWithSolvedVariables);
|
|
174
181
|
// If the result is a number and a format is specified, apply it
|
|
175
182
|
if (typeof result === "number" && cell.numFmt) {
|
|
176
183
|
return {
|
|
177
184
|
value: result,
|
|
178
|
-
text: formatNumber(result, cell.numFmt, webupManagerData),
|
|
185
|
+
text: (0, formatter_utility_1.formatNumber)(result, cell.numFmt, webupManagerData),
|
|
179
186
|
type: "number",
|
|
180
187
|
};
|
|
181
188
|
}
|
|
@@ -185,32 +192,33 @@ export const evaluateStandardFormula = (cell, worksheet, webupManagerData) => {
|
|
|
185
192
|
type: typeof result === "number" ? "number" : "string",
|
|
186
193
|
};
|
|
187
194
|
};
|
|
195
|
+
exports.evaluateStandardFormula = evaluateStandardFormula;
|
|
188
196
|
/**
|
|
189
197
|
* Extracts and formats the value of a given ExcelJS cell based on its type.
|
|
190
198
|
*/
|
|
191
|
-
|
|
192
|
-
if (cell.type ===
|
|
199
|
+
const getCellValueData = (cell, worksheet, webupManagerData) => {
|
|
200
|
+
if (cell.type === exceljs_1.default.ValueType.Formula &&
|
|
193
201
|
typeof cell.formula === "string") {
|
|
194
202
|
const subtotalMatch = cell.formula.match(/^SUBTOTAL\((\d+)\s*,\s*([A-Z]+\d+):([A-Z]+\d+)\)/i);
|
|
195
203
|
if (subtotalMatch) {
|
|
196
|
-
return handleSubtotalFormula(cell, worksheet, webupManagerData, subtotalMatch);
|
|
204
|
+
return (0, exports.handleSubtotalFormula)(cell, worksheet, webupManagerData, subtotalMatch);
|
|
197
205
|
}
|
|
198
206
|
else {
|
|
199
|
-
return evaluateStandardFormula(cell, worksheet, webupManagerData);
|
|
207
|
+
return (0, exports.evaluateStandardFormula)(cell, worksheet, webupManagerData);
|
|
200
208
|
}
|
|
201
209
|
}
|
|
202
|
-
else if (cell.type ===
|
|
210
|
+
else if (cell.type === exceljs_1.default.ValueType.Date &&
|
|
203
211
|
cell.value instanceof Date) {
|
|
204
212
|
return {
|
|
205
213
|
value: cell.value,
|
|
206
|
-
text: formatDate(cell.value, webupManagerData),
|
|
214
|
+
text: (0, formatter_utility_1.formatDate)(cell.value, webupManagerData),
|
|
207
215
|
type: "date",
|
|
208
216
|
};
|
|
209
217
|
}
|
|
210
|
-
else if (cell.type ===
|
|
218
|
+
else if (cell.type === exceljs_1.default.ValueType.Number) {
|
|
211
219
|
return {
|
|
212
220
|
value: cell.value,
|
|
213
|
-
text: formatNumber(cell.value, cell.numFmt ?? 0, webupManagerData),
|
|
221
|
+
text: (0, formatter_utility_1.formatNumber)(cell.value, cell.numFmt ?? 0, webupManagerData),
|
|
214
222
|
type: "number",
|
|
215
223
|
};
|
|
216
224
|
}
|
|
@@ -224,4 +232,5 @@ export const getCellValueData = (cell, worksheet, webupManagerData) => {
|
|
|
224
232
|
};
|
|
225
233
|
}
|
|
226
234
|
};
|
|
235
|
+
exports.getCellValueData = getCellValueData;
|
|
227
236
|
//# sourceMappingURL=formulas-helper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formulas-helper.js","sourceRoot":"","sources":["../../../src/converters/pdf/formulas-helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAE5E,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAElE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,IAAkB,EAClB,SAA4B,EAC5B,gBAAkC,EAClC,aAA+B,EACf,EAAE;IAClB,MAAM,YAAY,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACpD,MAAM,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,MAAM,GA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KAAK;YACZ,IAAI,EAAE,KAAK;YACX,IAAI,EAAE,QAAQ;SACf,CAAC;IACJ,CAAC;AACH,CAAC,CAAC","sourcesContent":["import { getCellsFromRange } from \"../excel/commons.js\";\nimport { WebupManagerData } from \"../../types/index.js\";\nimport { areAllValuesDates } from \"../../utils/dates-utility.js\";\nimport { formatDate, formatNumber } from \"../../utils/formatter-utility.js\";\nimport { CellValueProps } from \"./pdf-converter.types.js\";\nimport ExcelJS from \"exceljs\";\nimport { calculateExpression } from \"../../utils/math-utility.js\";\n\n/**\n * Handles the calculation of Excel-like SUBTOTAL formulas for a given cell and range.\n *\n * Supports various SUBTOTAL types such as SUM, AVERAGE, COUNTA, MAX, and MIN.\n * The function extracts the relevant range from the worksheet, computes the result\n * based on the subtotal type, and formats the output as needed (including date and number formatting).\n *\n * @param cell - The ExcelJS cell object where the formula is applied.\n * @param worksheet - The ExcelJS worksheet containing the data range.\n * @param webupManagerData - Additional data/context used for formatting numbers and dates.\n * @param subtotalMatch - The RegExp match array containing the parsed SUBTOTAL formula components:\n * - [1]: The subtotal type (e.g., 1 for AVERAGE, 3 for COUNTA, 4 for MAX, 5 for MIN, 9 for SUM).\n * - [2]: The start cell reference of the range.\n * - [3]: The end cell reference of the range.\n * @returns An object containing the computed value and its type (\"number\" or \"string\").\n */\nexport const handleSubtotalFormula = (\n cell: ExcelJS.Cell,\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n subtotalMatch: RegExpMatchArray,\n): CellValueProps => {\n const subtotalType = parseInt(subtotalMatch[1], 10);\n const startRef = subtotalMatch[2];\n const endRef = subtotalMatch[3];\n const cells = getCellsFromRange(worksheet, startRef, endRef).filter(\n cell => cell.type !== ExcelJS.ValueType.Formula,\n );\n let result: number | string | \"\";\n const areAllDates = areAllValuesDates(cells);\n switch (subtotalType) {\n case 3: // COUNTA\n result = cells.length;\n break;\n case 9: // SUM\n result = cells.reduce((sum, cell) => {\n const n =\n typeof cell.value === \"number\"\n ? cell.value\n : typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : NaN;\n return !isNaN(n)\n ? (typeof sum === \"number\"\n ? sum\n : typeof sum === \"string\"\n ? parseFloat(sum)\n : 0) + n\n : sum;\n }, 0);\n break;\n case 4: // MAX\n result = cells.length\n ? Math.max(\n ...cells.map(cell => {\n if (cell.type === ExcelJS.ValueType.Date) {\n return cell.value instanceof Date\n ? cell.value.getTime()\n : -Infinity;\n }\n return typeof cell.value === \"number\"\n ? cell.value\n : (typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : -Infinity) || -Infinity;\n }),\n )\n : \"\";\n break;\n case 5: // MIN\n result = cells.length\n ? Math.min(\n ...cells.map(cell => {\n if (cell.type === ExcelJS.ValueType.Date) {\n return cell.value instanceof Date\n ? cell.value.getTime()\n : Infinity;\n }\n return typeof cell.value === \"number\"\n ? cell.value\n : (typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : Infinity) || Infinity;\n }),\n )\n : \"\";\n break;\n case 1: {\n // AVERAGE\n const nums = cells\n .map(cell =>\n typeof cell.value === \"number\"\n ? cell.value\n : typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : NaN,\n )\n .filter(v => !isNaN(v));\n result = nums.length ? nums.reduce((a, b) => a + b, 0) / nums.length : \"\";\n break;\n }\n default: {\n if (cell.result != null && typeof cell.result !== \"object\") {\n result = cell.result;\n } else {\n result =\n cell.text ||\n (typeof cell.value === \"string\" || typeof cell.value === \"number\"\n ? cell.value\n : \"\") ||\n \"\";\n }\n break;\n }\n }\n\n if (areAllDates) {\n return {\n value: result,\n text: formatDate(new Date(result), webupManagerData),\n type: \"date\",\n };\n } else if (typeof result === \"number\" && cell.numFmt) {\n return {\n value: result,\n text: formatNumber(result, cell.numFmt, webupManagerData),\n type: \"number\",\n };\n }\n return {\n value: result,\n text: result.toString(),\n type: typeof result === \"number\" ? \"number\" : \"string\",\n };\n};\n\n/**\n * Evaluates a standard Excel formula from a given cell by replacing cell references\n * with their actual values from the worksheet and then computing the result.\n *\n * - Replaces cell references (e.g., \"A1\") in the formula with their corresponding values.\n * - Handles different cell value types: number, string, formula result, and date.\n * - Evaluates the resulting formula string using a safe function context.\n * - Applies number formatting if the result is numeric and a format is specified.\n * - Returns the computed value and its type (\"number\" or \"string\").\n *\n * @param cell - The ExcelJS cell containing the formula to evaluate.\n * @param worksheet - The ExcelJS worksheet object for resolving cell references.\n * @param webupManagerData - Additional data used for number formatting.\n * @returns An object containing the evaluated value and its type.\n */\nexport const evaluateStandardFormula = (\n cell: ExcelJS.Cell,\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n): CellValueProps => {\n const formula = cell.formula;\n const formulaWithSolvedVariables = formula.replace(/([A-Z]+\\d+)/g, ref => {\n try {\n const refCell = worksheet.getCell(ref);\n if (refCell) {\n if (refCell.type === ExcelJS.ValueType.Number) {\n return refCell.value?.toString() ?? \"0\";\n } else if (refCell.type === ExcelJS.ValueType.String) {\n // If the cell contains text, return as is (no quotes) for JS compatibility\n return refCell.value?.toString() ?? \"\";\n } else if (refCell.type === ExcelJS.ValueType.Formula) {\n return getCellValueData(\n refCell,\n worksheet,\n webupManagerData,\n ).value.toString();\n } else if (\n refCell.type === ExcelJS.ValueType.Date &&\n refCell.value instanceof Date\n ) {\n return refCell.value.getTime().toString();\n } else {\n return refCell.value?.toString() ?? \"0\";\n }\n }\n } catch {\n return \"0\";\n }\n return \"0\";\n });\n const result = calculateExpression(formulaWithSolvedVariables);\n // If the result is a number and a format is specified, apply it\n if (typeof result === \"number\" && cell.numFmt) {\n return {\n value: result,\n text: formatNumber(result, cell.numFmt, webupManagerData),\n type: \"number\",\n };\n }\n return {\n value: result,\n text: result != null ? result.toString() : cell.formula,\n type: typeof result === \"number\" ? \"number\" : \"string\",\n };\n};\n\n/**\n * Extracts and formats the value of a given ExcelJS cell based on its type.\n */\nexport const getCellValueData = (\n cell: ExcelJS.Cell,\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n): CellValueProps => {\n if (\n cell.type === ExcelJS.ValueType.Formula &&\n typeof cell.formula === \"string\"\n ) {\n const subtotalMatch = cell.formula.match(\n /^SUBTOTAL\\((\\d+)\\s*,\\s*([A-Z]+\\d+):([A-Z]+\\d+)\\)/i,\n );\n if (subtotalMatch) {\n return handleSubtotalFormula(\n cell,\n worksheet,\n webupManagerData,\n subtotalMatch,\n );\n } else {\n return evaluateStandardFormula(cell, worksheet, webupManagerData);\n }\n } else if (\n cell.type === ExcelJS.ValueType.Date &&\n cell.value instanceof Date\n ) {\n return {\n value: cell.value,\n text: formatDate(cell.value, webupManagerData),\n type: \"date\",\n };\n } else if (cell.type === ExcelJS.ValueType.Number) {\n return {\n value: cell.value as number,\n text: formatNumber(\n cell.value as number,\n cell.numFmt ?? 0,\n webupManagerData,\n ),\n type: \"number\",\n };\n } else {\n // Fallback to text or value if available, otherwise empty string\n const value = cell?.text || cell?.value?.toString() || \"\";\n return {\n value: value,\n text: value,\n type: \"string\",\n };\n }\n};\n"]}
|
|
1
|
+
{"version":3,"file":"formulas-helper.js","sourceRoot":"","sources":["../../../src/converters/pdf/formulas-helper.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAqD;AAErD,6DAA8D;AAC9D,qEAAyE;AAEzE,sDAA8B;AAC9B,2DAA+D;AAE/D;;;;;;;;;;;;;;;GAeG;AACI,MAAM,qBAAqB,GAAG,CACnC,IAAkB,EAClB,SAA4B,EAC5B,gBAAkC,EAClC,aAA+B,EACf,EAAE;IAClB,MAAM,YAAY,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACpD,MAAM,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,IAAA,2BAAiB,EAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CACjE,IAAI,CAAC,EAAE,CA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ACX,IAAI,EAAE,QAAQ;SACf,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAlDW,QAAA,gBAAgB,oBAkD3B","sourcesContent":["import { getCellsFromRange } from \"../excel/commons\";\nimport { WebupManagerData } from \"../../types/index\";\nimport { areAllValuesDates } from \"../../utils/dates-utility\";\nimport { formatDate, formatNumber } from \"../../utils/formatter-utility\";\nimport { CellValueProps } from \"./pdf-converter.types\";\nimport ExcelJS from \"exceljs\";\nimport { calculateExpression } from \"../../utils/math-utility\";\n\n/**\n * Handles the calculation of Excel-like SUBTOTAL formulas for a given cell and range.\n *\n * Supports various SUBTOTAL types such as SUM, AVERAGE, COUNTA, MAX, and MIN.\n * The function extracts the relevant range from the worksheet, computes the result\n * based on the subtotal type, and formats the output as needed (including date and number formatting).\n *\n * @param cell - The ExcelJS cell object where the formula is applied.\n * @param worksheet - The ExcelJS worksheet containing the data range.\n * @param webupManagerData - Additional data/context used for formatting numbers and dates.\n * @param subtotalMatch - The RegExp match array containing the parsed SUBTOTAL formula components:\n * - [1]: The subtotal type (e.g., 1 for AVERAGE, 3 for COUNTA, 4 for MAX, 5 for MIN, 9 for SUM).\n * - [2]: The start cell reference of the range.\n * - [3]: The end cell reference of the range.\n * @returns An object containing the computed value and its type (\"number\" or \"string\").\n */\nexport const handleSubtotalFormula = (\n cell: ExcelJS.Cell,\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n subtotalMatch: RegExpMatchArray,\n): CellValueProps => {\n const subtotalType = parseInt(subtotalMatch[1], 10);\n const startRef = subtotalMatch[2];\n const endRef = subtotalMatch[3];\n const cells = getCellsFromRange(worksheet, startRef, endRef).filter(\n cell => cell.type !== ExcelJS.ValueType.Formula,\n );\n let result: number | string | \"\";\n const areAllDates = areAllValuesDates(cells);\n switch (subtotalType) {\n case 3: // COUNTA\n result = cells.length;\n break;\n case 9: // SUM\n result = cells.reduce((sum, cell) => {\n const n =\n typeof cell.value === \"number\"\n ? cell.value\n : typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : NaN;\n return !isNaN(n)\n ? (typeof sum === \"number\"\n ? sum\n : typeof sum === \"string\"\n ? parseFloat(sum)\n : 0) + n\n : sum;\n }, 0);\n break;\n case 4: // MAX\n result = cells.length\n ? Math.max(\n ...cells.map(cell => {\n if (cell.type === ExcelJS.ValueType.Date) {\n return cell.value instanceof Date\n ? cell.value.getTime()\n : -Infinity;\n }\n return typeof cell.value === \"number\"\n ? cell.value\n : (typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : -Infinity) || -Infinity;\n }),\n )\n : \"\";\n break;\n case 5: // MIN\n result = cells.length\n ? Math.min(\n ...cells.map(cell => {\n if (cell.type === ExcelJS.ValueType.Date) {\n return cell.value instanceof Date\n ? cell.value.getTime()\n : Infinity;\n }\n return typeof cell.value === \"number\"\n ? cell.value\n : (typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : Infinity) || Infinity;\n }),\n )\n : \"\";\n break;\n case 1: {\n // AVERAGE\n const nums = cells\n .map(cell =>\n typeof cell.value === \"number\"\n ? cell.value\n : typeof cell.value === \"string\"\n ? parseFloat(cell.value)\n : NaN,\n )\n .filter(v => !isNaN(v));\n result = nums.length ? nums.reduce((a, b) => a + b, 0) / nums.length : \"\";\n break;\n }\n default: {\n if (cell.result != null && typeof cell.result !== \"object\") {\n result = cell.result;\n } else {\n result =\n cell.text ||\n (typeof cell.value === \"string\" || typeof cell.value === \"number\"\n ? cell.value\n : \"\") ||\n \"\";\n }\n break;\n }\n }\n\n if (areAllDates) {\n return {\n value: result,\n text: formatDate(new Date(result), webupManagerData),\n type: \"date\",\n };\n } else if (typeof result === \"number\" && cell.numFmt) {\n return {\n value: result,\n text: formatNumber(result, cell.numFmt, webupManagerData),\n type: \"number\",\n };\n }\n return {\n value: result,\n text: result.toString(),\n type: typeof result === \"number\" ? \"number\" : \"string\",\n };\n};\n\n/**\n * Evaluates a standard Excel formula from a given cell by replacing cell references\n * with their actual values from the worksheet and then computing the result.\n *\n * - Replaces cell references (e.g., \"A1\") in the formula with their corresponding values.\n * - Handles different cell value types: number, string, formula result, and date.\n * - Evaluates the resulting formula string using a safe function context.\n * - Applies number formatting if the result is numeric and a format is specified.\n * - Returns the computed value and its type (\"number\" or \"string\").\n *\n * @param cell - The ExcelJS cell containing the formula to evaluate.\n * @param worksheet - The ExcelJS worksheet object for resolving cell references.\n * @param webupManagerData - Additional data used for number formatting.\n * @returns An object containing the evaluated value and its type.\n */\nexport const evaluateStandardFormula = (\n cell: ExcelJS.Cell,\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n): CellValueProps => {\n const formula = cell.formula;\n const formulaWithSolvedVariables = formula.replace(/([A-Z]+\\d+)/g, ref => {\n try {\n const refCell = worksheet.getCell(ref);\n if (refCell) {\n if (refCell.type === ExcelJS.ValueType.Number) {\n return refCell.value?.toString() ?? \"0\";\n } else if (refCell.type === ExcelJS.ValueType.String) {\n // If the cell contains text, return as is (no quotes) for JS compatibility\n return refCell.value?.toString() ?? \"\";\n } else if (refCell.type === ExcelJS.ValueType.Formula) {\n return getCellValueData(\n refCell,\n worksheet,\n webupManagerData,\n ).value.toString();\n } else if (\n refCell.type === ExcelJS.ValueType.Date &&\n refCell.value instanceof Date\n ) {\n return refCell.value.getTime().toString();\n } else {\n return refCell.value?.toString() ?? \"0\";\n }\n }\n } catch {\n return \"0\";\n }\n return \"0\";\n });\n const result = calculateExpression(formulaWithSolvedVariables);\n // If the result is a number and a format is specified, apply it\n if (typeof result === \"number\" && cell.numFmt) {\n return {\n value: result,\n text: formatNumber(result, cell.numFmt, webupManagerData),\n type: \"number\",\n };\n }\n return {\n value: result,\n text: result != null ? result.toString() : cell.formula,\n type: typeof result === \"number\" ? \"number\" : \"string\",\n };\n};\n\n/**\n * Extracts and formats the value of a given ExcelJS cell based on its type.\n */\nexport const getCellValueData = (\n cell: ExcelJS.Cell,\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n): CellValueProps => {\n if (\n cell.type === ExcelJS.ValueType.Formula &&\n typeof cell.formula === \"string\"\n ) {\n const subtotalMatch = cell.formula.match(\n /^SUBTOTAL\\((\\d+)\\s*,\\s*([A-Z]+\\d+):([A-Z]+\\d+)\\)/i,\n );\n if (subtotalMatch) {\n return handleSubtotalFormula(\n cell,\n worksheet,\n webupManagerData,\n subtotalMatch,\n );\n } else {\n return evaluateStandardFormula(cell, worksheet, webupManagerData);\n }\n } else if (\n cell.type === ExcelJS.ValueType.Date &&\n cell.value instanceof Date\n ) {\n return {\n value: cell.value,\n text: formatDate(cell.value, webupManagerData),\n type: \"date\",\n };\n } else if (cell.type === ExcelJS.ValueType.Number) {\n return {\n value: cell.value as number,\n text: formatNumber(\n cell.value as number,\n cell.numFmt ?? 0,\n webupManagerData,\n ),\n type: \"number\",\n };\n } else {\n // Fallback to text or value if available, otherwise empty string\n const value = cell?.text || cell?.value?.toString() || \"\";\n return {\n value: value,\n text: value,\n type: \"string\",\n };\n }\n};\n\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as jsPDFModule from "jspdf";
|
|
2
2
|
import ExcelJS from "exceljs";
|
|
3
|
-
import { WebupManagerData } from "../../index
|
|
4
|
-
import { ConverterContext } from "../../types/converter
|
|
3
|
+
import { WebupManagerData } from "../../index";
|
|
4
|
+
import { ConverterContext } from "../../types/converter";
|
|
5
5
|
/**
|
|
6
6
|
* Converts an ExcelJS worksheet into a PDF document using jsPDF and autoTable.
|
|
7
7
|
*
|
|
@@ -1,9 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.dataTableToPdfDoc = void 0;
|
|
40
|
+
const jsPDFModule = __importStar(require("jspdf"));
|
|
41
|
+
const jspdf_autotable_1 = __importDefault(require("jspdf-autotable"));
|
|
42
|
+
const commons_utility_1 = require("../../utils/commons-utility");
|
|
43
|
+
const formulas_helper_1 = require("./formulas-helper");
|
|
44
|
+
const autotable_renderer_1 = require("./autotable-renderer");
|
|
45
|
+
const commons_1 = require("../excel/commons");
|
|
7
46
|
/**
|
|
8
47
|
* Converts an ExcelJS worksheet into a PDF document using jsPDF and autoTable.
|
|
9
48
|
*
|
|
@@ -16,7 +55,7 @@ import { isGroupedCell } from "../excel/commons.js";
|
|
|
16
55
|
* @param webupManagerData - Additional data required for extracting worksheet content.
|
|
17
56
|
* @returns A Promise that resolves to a jsPDF document containing the worksheet data as a table.
|
|
18
57
|
*/
|
|
19
|
-
|
|
58
|
+
const dataTableToPdfDoc = async (worksheet, webupManagerData, context) => {
|
|
20
59
|
const doc = new jsPDFModule.jsPDF({ orientation: "landscape" });
|
|
21
60
|
doc.setFont("helvetica", "normal");
|
|
22
61
|
doc.setFontSize(10);
|
|
@@ -30,9 +69,10 @@ export const dataTableToPdfDoc = async (worksheet, webupManagerData, context) =>
|
|
|
30
69
|
// Calculate optimal font size based on data
|
|
31
70
|
const pageWidth = doc.internal.pageSize.getWidth();
|
|
32
71
|
const optimalFontSize = calculateOptimalFontSize(data, visibleHeaders, pageWidth);
|
|
33
|
-
|
|
72
|
+
(0, jspdf_autotable_1.default)(doc, (0, autotable_renderer_1.getAutoTableOptions)(visibleHeaders, data, optimalFontSize, cellStyles, context));
|
|
34
73
|
return doc;
|
|
35
74
|
};
|
|
75
|
+
exports.dataTableToPdfDoc = dataTableToPdfDoc;
|
|
36
76
|
// Function to calculate the optimal font size
|
|
37
77
|
const calculateOptimalFontSize = (data, visibleHeaders, pageWidth) => {
|
|
38
78
|
const maxSamples = Math.min(100, data.length); // Analyze max 100 rows
|
|
@@ -111,7 +151,7 @@ const extractData = (worksheet, visibleColumnIndexes, webupManagerData) => {
|
|
|
111
151
|
const rowData = [];
|
|
112
152
|
visibleColumnIndexes.forEach(colIdx => {
|
|
113
153
|
const cell = row.getCell(colIdx);
|
|
114
|
-
const cellValue = getCellValueData(cell, worksheet, webupManagerData);
|
|
154
|
+
const cellValue = (0, formulas_helper_1.getCellValueData)(cell, worksheet, webupManagerData);
|
|
115
155
|
rowData.push(cellValue);
|
|
116
156
|
});
|
|
117
157
|
data.push(rowData);
|
|
@@ -146,7 +186,7 @@ const extractStyles = (worksheet, visibleColumnIndexes, rawData) => {
|
|
|
146
186
|
}
|
|
147
187
|
if (cell.font.color?.argb) {
|
|
148
188
|
const color = cell.font.color.argb;
|
|
149
|
-
const [r, g, b] = convertColorToRgb(color);
|
|
189
|
+
const [r, g, b] = (0, commons_utility_1.convertColorToRgb)(color);
|
|
150
190
|
if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {
|
|
151
191
|
cellStyle.textColor = [r, g, b];
|
|
152
192
|
}
|
|
@@ -157,7 +197,7 @@ const extractStyles = (worksheet, visibleColumnIndexes, rawData) => {
|
|
|
157
197
|
cell.fill.type === "pattern" &&
|
|
158
198
|
cell.fill.fgColor?.argb) {
|
|
159
199
|
const color = cell.fill.fgColor.argb;
|
|
160
|
-
const [r, g, b] = convertColorToRgb(color);
|
|
200
|
+
const [r, g, b] = (0, commons_utility_1.convertColorToRgb)(color);
|
|
161
201
|
if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {
|
|
162
202
|
cellStyle.fillColor = [r, g, b];
|
|
163
203
|
}
|
|
@@ -166,7 +206,7 @@ const extractStyles = (worksheet, visibleColumnIndexes, rawData) => {
|
|
|
166
206
|
if (rawData[rowIndex - 2][index].type === "number") {
|
|
167
207
|
cellStyle.halign = "right";
|
|
168
208
|
}
|
|
169
|
-
if (isGroupedCell(cell)) {
|
|
209
|
+
if ((0, commons_1.isGroupedCell)(cell)) {
|
|
170
210
|
cellStyle.cellWidth = "wrap";
|
|
171
211
|
cellStyle.overflow = "visible";
|
|
172
212
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matrix-converter.js","sourceRoot":"","sources":["../../../src/converters/pdf/matrix-converter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,OAAO,CAAC;AAErC,OAAO,SAAS,MAAM,i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H,CAAC;AACH,CAAC,CAAC","sourcesContent":["import * as jsPDFModule from \"jspdf\";\nimport ExcelJS from \"exceljs\";\nimport autoTable from \"jspdf-autotable\";\nimport { WebupManagerData } from \"../../index.js\";\nimport { CellStyleData, CellValueProps } from \"./pdf-converter.types.js\";\nimport { convertColorToRgb } from \"../../utils/commons-utility.js\";\nimport { getCellValueData } from \"./formulas-helper.js\";\nimport { getAutoTableOptions } from \"./autotable-renderer.js\";\nimport { isGroupedCell } from \"../excel/commons.js\";\nimport { ConverterContext } from \"../../types/converter.js\";\n\n/**\n * Converts an ExcelJS worksheet into a PDF document using jsPDF and autoTable.\n *\n * This function extracts visible headers and data from the provided worksheet,\n * applies cell styles, simulates Excel cell overflow, and calculates the optimal\n * font size for the table. It then generates a PDF document with the formatted\n * table and returns the resulting jsPDF instance.\n *\n * @param worksheet - The ExcelJS worksheet to convert to PDF.\n * @param webupManagerData - Additional data required for extracting worksheet content.\n * @returns A Promise that resolves to a jsPDF document containing the worksheet data as a table.\n */\nexport const dataTableToPdfDoc = async (\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n context: ConverterContext,\n): Promise<jsPDFModule.jsPDF> => {\n const doc = new jsPDFModule.jsPDF({ orientation: \"landscape\" });\n doc.setFont(\"helvetica\", \"normal\");\n doc.setFontSize(10);\n\n // Extract headers and column indexes\n const { visibleHeaders, visibleColumnIndexes } =\n extractVisibleHeadersAndIndexes(worksheet);\n\n // Extract data and cell styles separately\n const data = extractData(worksheet, visibleColumnIndexes, webupManagerData);\n const cellStyles = extractStyles(worksheet, visibleColumnIndexes, data);\n\n // Post-process data to simulate Excel overflow\n postProcessExcelOverflow(data, cellStyles);\n\n // Calculate optimal font size based on data\n const pageWidth = doc.internal.pageSize.getWidth();\n const optimalFontSize = calculateOptimalFontSize(\n data,\n visibleHeaders,\n pageWidth,\n );\n\n autoTable(\n doc,\n getAutoTableOptions(\n visibleHeaders,\n data,\n optimalFontSize,\n cellStyles,\n context,\n ),\n );\n\n return doc;\n};\n\n// Function to calculate the optimal font size\nconst calculateOptimalFontSize = (\n data: CellValueProps[][],\n visibleHeaders: string[],\n pageWidth: number,\n): number => {\n const maxSamples = Math.min(100, data.length); // Analyze max 100 rows\n const availableWidth = pageWidth - 20; // Margins\n const numColumns = visibleHeaders.length;\n\n // If there are few columns, we can allow a larger font size\n if (numColumns <= 3) {\n return 10;\n }\n\n // Calculate more accurate statistics per column\n const columnStats = visibleHeaders.map((header, colIndex) => {\n const lengths: number[] = [header.length];\n\n // Collect all cell lengths for this column\n for (let rowIndex = 0; rowIndex < maxSamples; rowIndex++) {\n if (data[rowIndex] && data[rowIndex][colIndex]) {\n const cellLength = data[rowIndex][colIndex].text.replace(\n /\\u00A0/g,\n \" \",\n ).length;\n lengths.push(cellLength);\n }\n }\n\n lengths.sort((a, b) => a - b);\n\n // Use the 90th percentile instead of the maximum to be less conservative\n const percentile90Index = Math.floor(lengths.length * 0.9);\n const effectiveWidth =\n lengths[percentile90Index] || lengths[lengths.length - 1];\n\n // The average length to balance very long columns with short ones\n const avgLength =\n lengths.reduce((sum, len) => sum + len, 0) / lengths.length;\n\n return {\n max: Math.max(...lengths),\n p90: effectiveWidth,\n avg: avgLength,\n min: Math.min(...lengths),\n };\n });\n\n // Use a mixed strategy: combine 90th percentile with weighted average\n const estimatedColumnWidths = columnStats.map(stats => {\n // Weight the average more than the 90th percentile to be less pessimistic\n const weightedWidth = stats.avg * 0.7 + stats.p90 * 0.3;\n return Math.max(weightedWidth, 3); // Minimum width of 3 characters\n });\n\n // Try different font sizes starting from the largest\n for (let testFontSize = 10; testFontSize >= 4; testFontSize -= 0.5) {\n // More optimistic factor based on font size\n const charWidthFactor =\n testFontSize >= 8 ? 3.2 : testFontSize >= 6 ? 2.8 : 2.5;\n\n const estimatedTotalWidth = estimatedColumnWidths.reduce(\n (sum, width) => sum + width * charWidthFactor,\n 0,\n );\n\n // Add a margin for columns (space between columns)\n const totalWidthWithMargins = estimatedTotalWidth + numColumns * 10; // 10px between columns\n\n if (totalWidthWithMargins <= availableWidth) {\n return testFontSize;\n }\n }\n\n return 4; // Minimum font size if nothing fits\n};\n\n/**\n * Extracts visible headers and their column indexes from the worksheet (ignoring hidden columns).\n */\nconst extractVisibleHeadersAndIndexes = (\n worksheet: ExcelJS.Worksheet,\n): { visibleHeaders: string[]; visibleColumnIndexes: number[] } => {\n const headerRow = worksheet.getRow(1);\n const visibleHeaders: string[] = [];\n const visibleColumnIndexes: number[] = [];\n headerRow.eachCell({ includeEmpty: true }, (cell, colNumber) => {\n const column = worksheet.getColumn(colNumber);\n if (!column.hidden) {\n visibleHeaders.push(cell?.toString() ?? \"\");\n visibleColumnIndexes.push(colNumber);\n }\n });\n return { visibleHeaders, visibleColumnIndexes };\n};\n\n/**\n * Extracts the data from the worksheet for the given visible columns, formatting dates and preserving spaces.\n */\nconst extractData = (\n worksheet: ExcelJS.Worksheet,\n visibleColumnIndexes: number[],\n webupManagerData: WebupManagerData,\n): CellValueProps[][] => {\n const data: CellValueProps[][] = [];\n worksheet.eachRow((row, rowIndex) => {\n if (rowIndex === 1) return; // Skip header\n const rowData: CellValueProps[] = [];\n visibleColumnIndexes.forEach(colIdx => {\n const cell = row.getCell(colIdx);\n const cellValue = getCellValueData(cell, worksheet, webupManagerData);\n rowData.push(cellValue);\n });\n data.push(rowData);\n });\n return data;\n};\n\n/**\n * Extracts the cell styles (font, color, fill) for the given visible columns from the worksheet.\n */\nconst extractStyles = (\n worksheet: ExcelJS.Worksheet,\n visibleColumnIndexes: number[],\n rawData: CellValueProps[][],\n): CellStyleData[][] => {\n const cellStyles: CellStyleData[][] = [];\n worksheet.eachRow((row, rowIndex) => {\n if (rowIndex === 1) return; // Skip header\n const rowStyles: CellStyleData[] = [];\n visibleColumnIndexes.forEach((colIdx, index) => {\n const cell = row.getCell(colIdx);\n const cellStyle: CellStyleData = {};\n // Font\n if (cell.font) {\n if (cell.font.bold && cell.font.italic) {\n cellStyle.fontStyle = \"bolditalic\";\n } else if (cell.font.bold) {\n cellStyle.fontStyle = \"bold\";\n } else if (cell.font.italic) {\n cellStyle.fontStyle = \"italic\";\n } else {\n cellStyle.fontStyle = \"normal\";\n }\n if (cell.font.color?.argb) {\n const color = cell.font.color.argb;\n const [r, g, b] = convertColorToRgb(color);\n if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {\n cellStyle.textColor = [r, g, b];\n }\n }\n }\n // Fill (background color)\n if (\n cell.fill &&\n cell.fill.type === \"pattern\" &&\n cell.fill.fgColor?.argb\n ) {\n const color = cell.fill.fgColor.argb;\n const [r, g, b] = convertColorToRgb(color);\n if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {\n cellStyle.fillColor = [r, g, b];\n }\n }\n // Align right if the cell is numeric\n if (rawData[rowIndex - 2][index].type === \"number\") {\n cellStyle.halign = \"right\";\n }\n\n if (isGroupedCell(cell)) {\n cellStyle.cellWidth = \"wrap\";\n cellStyle.overflow = \"visible\";\n }\n\n rowStyles.push(cellStyle);\n });\n cellStyles.push(rowStyles);\n });\n return cellStyles;\n};\n\n/**\n * Post-processes the data and cell styles to simulate Excel's text overflow behavior.\n * If a cell has long text and the next cell is empty, the width is set to auto.\n */\nconst postProcessExcelOverflow = (\n data: CellValueProps[][],\n cellStyles: CellStyleData[][],\n) => {\n for (let rowIndex = 0; rowIndex < data.length; rowIndex++) {\n for (let colIndex = 0; colIndex < data[rowIndex].length - 1; colIndex++) {\n const currentCell = data[rowIndex][colIndex];\n const nextCell = data[rowIndex][colIndex + 1];\n\n // If the current cell has long text and the next one is empty or only spaces\n if (\n currentCell &&\n currentCell.text.length > 15 &&\n (!nextCell || nextCell.text.trim() === \"\")\n ) {\n // Extend the width of the current cell\n if (!cellStyles[rowIndex][colIndex].cellWidth) {\n cellStyles[rowIndex][colIndex].cellWidth = \"auto\";\n }\n }\n }\n }\n};\n"]}
|
|
1
|
+
{"version":3,"file":"matrix-converter.js","sourceRoot":"","sources":["../../../src/converters/pdf/matrix-converter.ts"],"names":[],"mappings":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ent":["import * as jsPDFModule from \"jspdf\";\nimport ExcelJS from \"exceljs\";\nimport autoTable from \"jspdf-autotable\";\nimport { WebupManagerData } from \"../../index\";\nimport { CellStyleData, CellValueProps } from \"./pdf-converter.types\";\nimport { convertColorToRgb } from \"../../utils/commons-utility\";\nimport { getCellValueData } from \"./formulas-helper\";\nimport { getAutoTableOptions } from \"./autotable-renderer\";\nimport { isGroupedCell } from \"../excel/commons\";\nimport { ConverterContext } from \"../../types/converter\";\n\n/**\n * Converts an ExcelJS worksheet into a PDF document using jsPDF and autoTable.\n *\n * This function extracts visible headers and data from the provided worksheet,\n * applies cell styles, simulates Excel cell overflow, and calculates the optimal\n * font size for the table. It then generates a PDF document with the formatted\n * table and returns the resulting jsPDF instance.\n *\n * @param worksheet - The ExcelJS worksheet to convert to PDF.\n * @param webupManagerData - Additional data required for extracting worksheet content.\n * @returns A Promise that resolves to a jsPDF document containing the worksheet data as a table.\n */\nexport const dataTableToPdfDoc = async (\n worksheet: ExcelJS.Worksheet,\n webupManagerData: WebupManagerData,\n context: ConverterContext,\n): Promise<jsPDFModule.jsPDF> => {\n const doc = new jsPDFModule.jsPDF({ orientation: \"landscape\" });\n doc.setFont(\"helvetica\", \"normal\");\n doc.setFontSize(10);\n\n // Extract headers and column indexes\n const { visibleHeaders, visibleColumnIndexes } =\n extractVisibleHeadersAndIndexes(worksheet);\n\n // Extract data and cell styles separately\n const data = extractData(worksheet, visibleColumnIndexes, webupManagerData);\n const cellStyles = extractStyles(worksheet, visibleColumnIndexes, data);\n\n // Post-process data to simulate Excel overflow\n postProcessExcelOverflow(data, cellStyles);\n\n // Calculate optimal font size based on data\n const pageWidth = doc.internal.pageSize.getWidth();\n const optimalFontSize = calculateOptimalFontSize(\n data,\n visibleHeaders,\n pageWidth,\n );\n\n autoTable(\n doc,\n getAutoTableOptions(\n visibleHeaders,\n data,\n optimalFontSize,\n cellStyles,\n context,\n ),\n );\n\n return doc;\n};\n\n// Function to calculate the optimal font size\nconst calculateOptimalFontSize = (\n data: CellValueProps[][],\n visibleHeaders: string[],\n pageWidth: number,\n): number => {\n const maxSamples = Math.min(100, data.length); // Analyze max 100 rows\n const availableWidth = pageWidth - 20; // Margins\n const numColumns = visibleHeaders.length;\n\n // If there are few columns, we can allow a larger font size\n if (numColumns <= 3) {\n return 10;\n }\n\n // Calculate more accurate statistics per column\n const columnStats = visibleHeaders.map((header, colIndex) => {\n const lengths: number[] = [header.length];\n\n // Collect all cell lengths for this column\n for (let rowIndex = 0; rowIndex < maxSamples; rowIndex++) {\n if (data[rowIndex] && data[rowIndex][colIndex]) {\n const cellLength = data[rowIndex][colIndex].text.replace(\n /\\u00A0/g,\n \" \",\n ).length;\n lengths.push(cellLength);\n }\n }\n\n lengths.sort((a, b) => a - b);\n\n // Use the 90th percentile instead of the maximum to be less conservative\n const percentile90Index = Math.floor(lengths.length * 0.9);\n const effectiveWidth =\n lengths[percentile90Index] || lengths[lengths.length - 1];\n\n // The average length to balance very long columns with short ones\n const avgLength =\n lengths.reduce((sum, len) => sum + len, 0) / lengths.length;\n\n return {\n max: Math.max(...lengths),\n p90: effectiveWidth,\n avg: avgLength,\n min: Math.min(...lengths),\n };\n });\n\n // Use a mixed strategy: combine 90th percentile with weighted average\n const estimatedColumnWidths = columnStats.map(stats => {\n // Weight the average more than the 90th percentile to be less pessimistic\n const weightedWidth = stats.avg * 0.7 + stats.p90 * 0.3;\n return Math.max(weightedWidth, 3); // Minimum width of 3 characters\n });\n\n // Try different font sizes starting from the largest\n for (let testFontSize = 10; testFontSize >= 4; testFontSize -= 0.5) {\n // More optimistic factor based on font size\n const charWidthFactor =\n testFontSize >= 8 ? 3.2 : testFontSize >= 6 ? 2.8 : 2.5;\n\n const estimatedTotalWidth = estimatedColumnWidths.reduce(\n (sum, width) => sum + width * charWidthFactor,\n 0,\n );\n\n // Add a margin for columns (space between columns)\n const totalWidthWithMargins = estimatedTotalWidth + numColumns * 10; // 10px between columns\n\n if (totalWidthWithMargins <= availableWidth) {\n return testFontSize;\n }\n }\n\n return 4; // Minimum font size if nothing fits\n};\n\n/**\n * Extracts visible headers and their column indexes from the worksheet (ignoring hidden columns).\n */\nconst extractVisibleHeadersAndIndexes = (\n worksheet: ExcelJS.Worksheet,\n): { visibleHeaders: string[]; visibleColumnIndexes: number[] } => {\n const headerRow = worksheet.getRow(1);\n const visibleHeaders: string[] = [];\n const visibleColumnIndexes: number[] = [];\n headerRow.eachCell({ includeEmpty: true }, (cell, colNumber) => {\n const column = worksheet.getColumn(colNumber);\n if (!column.hidden) {\n visibleHeaders.push(cell?.toString() ?? \"\");\n visibleColumnIndexes.push(colNumber);\n }\n });\n return { visibleHeaders, visibleColumnIndexes };\n};\n\n/**\n * Extracts the data from the worksheet for the given visible columns, formatting dates and preserving spaces.\n */\nconst extractData = (\n worksheet: ExcelJS.Worksheet,\n visibleColumnIndexes: number[],\n webupManagerData: WebupManagerData,\n): CellValueProps[][] => {\n const data: CellValueProps[][] = [];\n worksheet.eachRow((row, rowIndex) => {\n if (rowIndex === 1) return; // Skip header\n const rowData: CellValueProps[] = [];\n visibleColumnIndexes.forEach(colIdx => {\n const cell = row.getCell(colIdx);\n const cellValue = getCellValueData(cell, worksheet, webupManagerData);\n rowData.push(cellValue);\n });\n data.push(rowData);\n });\n return data;\n};\n\n/**\n * Extracts the cell styles (font, color, fill) for the given visible columns from the worksheet.\n */\nconst extractStyles = (\n worksheet: ExcelJS.Worksheet,\n visibleColumnIndexes: number[],\n rawData: CellValueProps[][],\n): CellStyleData[][] => {\n const cellStyles: CellStyleData[][] = [];\n worksheet.eachRow((row, rowIndex) => {\n if (rowIndex === 1) return; // Skip header\n const rowStyles: CellStyleData[] = [];\n visibleColumnIndexes.forEach((colIdx, index) => {\n const cell = row.getCell(colIdx);\n const cellStyle: CellStyleData = {};\n // Font\n if (cell.font) {\n if (cell.font.bold && cell.font.italic) {\n cellStyle.fontStyle = \"bolditalic\";\n } else if (cell.font.bold) {\n cellStyle.fontStyle = \"bold\";\n } else if (cell.font.italic) {\n cellStyle.fontStyle = \"italic\";\n } else {\n cellStyle.fontStyle = \"normal\";\n }\n if (cell.font.color?.argb) {\n const color = cell.font.color.argb;\n const [r, g, b] = convertColorToRgb(color);\n if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {\n cellStyle.textColor = [r, g, b];\n }\n }\n }\n // Fill (background color)\n if (\n cell.fill &&\n cell.fill.type === \"pattern\" &&\n cell.fill.fgColor?.argb\n ) {\n const color = cell.fill.fgColor.argb;\n const [r, g, b] = convertColorToRgb(color);\n if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {\n cellStyle.fillColor = [r, g, b];\n }\n }\n // Align right if the cell is numeric\n if (rawData[rowIndex - 2][index].type === \"number\") {\n cellStyle.halign = \"right\";\n }\n\n if (isGroupedCell(cell)) {\n cellStyle.cellWidth = \"wrap\";\n cellStyle.overflow = \"visible\";\n }\n\n rowStyles.push(cellStyle);\n });\n cellStyles.push(rowStyles);\n });\n return cellStyles;\n};\n\n/**\n * Post-processes the data and cell styles to simulate Excel's text overflow behavior.\n * If a cell has long text and the next cell is empty, the width is set to auto.\n */\nconst postProcessExcelOverflow = (\n data: CellValueProps[][],\n cellStyles: CellStyleData[][],\n) => {\n for (let rowIndex = 0; rowIndex < data.length; rowIndex++) {\n for (let colIndex = 0; colIndex < data[rowIndex].length - 1; colIndex++) {\n const currentCell = data[rowIndex][colIndex];\n const nextCell = data[rowIndex][colIndex + 1];\n\n // If the current cell has long text and the next one is empty or only spaces\n if (\n currentCell &&\n currentCell.text.length > 15 &&\n (!nextCell || nextCell.text.trim() === \"\")\n ) {\n // Extend the width of the current cell\n if (!cellStyles[rowIndex][colIndex].cellWidth) {\n cellStyles[rowIndex][colIndex].cellWidth = \"auto\";\n }\n }\n }\n }\n};\n\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pdf-converter.types.js","sourceRoot":"","sources":["../../../src/converters/pdf/pdf-converter.types.ts"],"names":[],"mappings":"","sourcesContent":["export interface CellStyleData {\n fontStyle?: \"normal\" | \"bold\" | \"italic\" | \"bolditalic\";\n textColor?: [number, number, number];\n fillColor?: [number, number, number];\n cellWidth?: \"auto\" | \"wrap\";\n halign?: \"left\" | \"right\" | \"center\";\n overflow?: \"linebreak\" | \"visible\";\n}\n\nexport interface CellValueProps {\n // The raw value of the cell, can be string, number, Date\n value: string | number | Date;\n // Formatted representation of the value\n text: string;\n type: \"string\" | \"number\" | \"date\";\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pdf-converter.types.js","sourceRoot":"","sources":["../../../src/converters/pdf/pdf-converter.types.ts"],"names":[],"mappings":"","sourcesContent":["export interface CellStyleData {\n fontStyle?: \"normal\" | \"bold\" | \"italic\" | \"bolditalic\";\n textColor?: [number, number, number];\n fillColor?: [number, number, number];\n cellWidth?: \"auto\" | \"wrap\";\n halign?: \"left\" | \"right\" | \"center\";\n overflow?: \"linebreak\" | \"visible\";\n}\n\nexport interface CellValueProps {\n // The raw value of the cell, can be string, number, Date\n value: string | number | Date;\n // Formatted representation of the value\n text: string;\n type: \"string\" | \"number\" | \"date\";\n}\n\n"]}
|