@js-ak/excel-toolbox 1.3.2 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/lib/template/template-fs.js +458 -196
- package/build/cjs/lib/template/utils/apply-replacements.js +26 -0
- package/build/cjs/lib/template/utils/check-row.js +6 -11
- package/build/cjs/lib/template/utils/column-index-to-letter.js +14 -3
- package/build/cjs/lib/template/utils/extract-xml-declaration.js +22 -0
- package/build/cjs/lib/template/utils/get-by-path.js +18 -0
- package/build/cjs/lib/template/utils/get-max-row-number.js +1 -1
- package/build/cjs/lib/template/utils/index.js +9 -0
- package/build/cjs/lib/template/utils/process-merge-cells.js +40 -0
- package/build/cjs/lib/template/utils/process-merge-finalize.js +51 -0
- package/build/cjs/lib/template/utils/process-rows.js +160 -0
- package/build/cjs/lib/template/utils/process-shared-strings.js +45 -0
- package/build/cjs/lib/template/utils/to-excel-column-object.js +2 -10
- package/build/cjs/lib/template/utils/update-dimension.js +40 -0
- package/build/cjs/lib/template/utils/validate-worksheet-xml.js +231 -0
- package/build/cjs/lib/template/utils/write-rows-to-stream.js +2 -1
- package/build/cjs/lib/zip/create-with-stream.js +2 -2
- package/build/esm/lib/template/template-fs.js +458 -196
- package/build/esm/lib/template/utils/apply-replacements.js +22 -0
- package/build/esm/lib/template/utils/check-row.js +6 -11
- package/build/esm/lib/template/utils/column-index-to-letter.js +14 -3
- package/build/esm/lib/template/utils/extract-xml-declaration.js +19 -0
- package/build/esm/lib/template/utils/get-by-path.js +15 -0
- package/build/esm/lib/template/utils/get-max-row-number.js +1 -1
- package/build/esm/lib/template/utils/index.js +9 -0
- package/build/esm/lib/template/utils/process-merge-cells.js +37 -0
- package/build/esm/lib/template/utils/process-merge-finalize.js +48 -0
- package/build/esm/lib/template/utils/process-rows.js +157 -0
- package/build/esm/lib/template/utils/process-shared-strings.js +42 -0
- package/build/esm/lib/template/utils/to-excel-column-object.js +2 -10
- package/build/esm/lib/template/utils/update-dimension.js +37 -0
- package/build/esm/lib/template/utils/validate-worksheet-xml.js +228 -0
- package/build/esm/lib/template/utils/write-rows-to-stream.js +2 -1
- package/build/esm/lib/zip/create-with-stream.js +2 -2
- package/build/types/lib/template/template-fs.d.ts +24 -0
- package/build/types/lib/template/utils/apply-replacements.d.ts +13 -0
- package/build/types/lib/template/utils/check-row.d.ts +5 -10
- package/build/types/lib/template/utils/column-index-to-letter.d.ts +11 -3
- package/build/types/lib/template/utils/extract-xml-declaration.d.ts +14 -0
- package/build/types/lib/template/utils/get-by-path.d.ts +8 -0
- package/build/types/lib/template/utils/index.d.ts +9 -0
- package/build/types/lib/template/utils/process-merge-cells.d.ts +20 -0
- package/build/types/lib/template/utils/process-merge-finalize.d.ts +38 -0
- package/build/types/lib/template/utils/process-rows.d.ts +31 -0
- package/build/types/lib/template/utils/process-shared-strings.d.ts +20 -0
- package/build/types/lib/template/utils/update-dimension.d.ts +1 -0
- package/build/types/lib/template/utils/validate-worksheet-xml.d.ts +25 -0
- package/package.json +6 -4
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.applyReplacements = void 0;
|
4
|
+
const get_by_path_js_1 = require("./get-by-path.js");
|
5
|
+
/**
|
6
|
+
* Replaces placeholders in the given content string with values from the replacements map.
|
7
|
+
*
|
8
|
+
* The function searches for placeholders in the format `${key}` within the content
|
9
|
+
* string, where `key` corresponds to a path in the replacements object.
|
10
|
+
* If a value is found for the key, it replaces the placeholder with the value.
|
11
|
+
* If no value is found, the original placeholder remains unchanged.
|
12
|
+
*
|
13
|
+
* @param content - The string containing placeholders to be replaced.
|
14
|
+
* @param replacements - An object where keys represent placeholder paths and values are the replacements.
|
15
|
+
* @returns A new string with placeholders replaced by corresponding values from the replacements object.
|
16
|
+
*/
|
17
|
+
const applyReplacements = (content, replacements) => {
|
18
|
+
if (!content) {
|
19
|
+
return "";
|
20
|
+
}
|
21
|
+
return content.replace(/\$\{([^}]+)\}/g, (match, path) => {
|
22
|
+
const value = (0, get_by_path_js_1.getByPath)(replacements, path);
|
23
|
+
return value !== undefined ? String(value) : match;
|
24
|
+
});
|
25
|
+
};
|
26
|
+
exports.applyReplacements = applyReplacements;
|
@@ -2,21 +2,16 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.checkRow = checkRow;
|
4
4
|
/**
|
5
|
-
* Validates
|
5
|
+
* Validates an object representing a single row of data to ensure that its keys
|
6
|
+
* are valid Excel column references. Throws an error if any of the keys are
|
7
|
+
* invalid.
|
6
8
|
*
|
7
|
-
*
|
8
|
-
*
|
9
|
-
* not match this pattern, an error is thrown with a message indicating the
|
10
|
-
* invalid cell reference.
|
11
|
-
*
|
12
|
-
* @param row - An object representing a row of data, where keys are cell
|
13
|
-
* references and values are strings.
|
14
|
-
*
|
15
|
-
* @throws {Error} If any key in the row is not a valid column letter.
|
9
|
+
* @param row An object with string keys that represent the cell references and
|
10
|
+
* string values that represent the values of those cells.
|
16
11
|
*/
|
17
12
|
function checkRow(row) {
|
18
13
|
for (const key of Object.keys(row)) {
|
19
|
-
if (!/^[A-Z]+$/i.test(key)) {
|
14
|
+
if (!/^[A-Z]+$/i.test(key) || !/^[A-Z]$|^[A-Z][A-Z]$|^[A-Z][A-Z][A-Z]$/i.test(key)) {
|
20
15
|
throw new Error(`Invalid cell reference "${key}" in row. Only column letters (like "A", "B", "C") are allowed.`);
|
21
16
|
}
|
22
17
|
}
|
@@ -2,12 +2,23 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.columnIndexToLetter = columnIndexToLetter;
|
4
4
|
/**
|
5
|
-
* Converts a
|
5
|
+
* Converts a zero-based column index to its corresponding Excel column letter.
|
6
6
|
*
|
7
|
-
* @
|
8
|
-
* @
|
7
|
+
* @throws Will throw an error if the input is not a positive integer.
|
8
|
+
* @param {number} index - The zero-based index of the column to convert.
|
9
|
+
* @returns {string} The corresponding Excel column letter.
|
10
|
+
*
|
11
|
+
* @example
|
12
|
+
* columnIndexToLetter(0); // returns "A"
|
13
|
+
* columnIndexToLetter(25); // returns "Z"
|
14
|
+
* columnIndexToLetter(26); // returns "AA"
|
15
|
+
* columnIndexToLetter(51); // returns "AZ"
|
16
|
+
* columnIndexToLetter(52); // returns "BA"
|
9
17
|
*/
|
10
18
|
function columnIndexToLetter(index) {
|
19
|
+
if (!Number.isInteger(index) || index < 0) {
|
20
|
+
throw new Error(`Invalid column index: ${index}. Must be a positive integer.`);
|
21
|
+
}
|
11
22
|
let letters = "";
|
12
23
|
while (index >= 0) {
|
13
24
|
letters = String.fromCharCode((index % 26) + 65) + letters;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.extractXmlDeclaration = extractXmlDeclaration;
|
4
|
+
/**
|
5
|
+
* Extracts the XML declaration from a given XML string.
|
6
|
+
*
|
7
|
+
* The XML declaration is a string that looks like `<?xml ...?>` and is usually
|
8
|
+
* present at the beginning of an XML file. It contains information about the
|
9
|
+
* XML version, encoding, and standalone status.
|
10
|
+
*
|
11
|
+
* This function returns `null` if the input string does not have a valid XML
|
12
|
+
* declaration.
|
13
|
+
*
|
14
|
+
* @param xmlString - The XML string to extract the declaration from.
|
15
|
+
* @returns The extracted XML declaration string, or `null`.
|
16
|
+
*/
|
17
|
+
function extractXmlDeclaration(xmlString) {
|
18
|
+
// const declarationRegex = /^<\?xml\s+[^?]+\?>/;
|
19
|
+
const declarationRegex = /^<\?xml\s+version\s*=\s*["'][^"']+["'](\s+(encoding|standalone)\s*=\s*["'][^"']+["'])*\s*\?>/;
|
20
|
+
const match = xmlString.trim().match(declarationRegex);
|
21
|
+
return match ? match[0] : null;
|
22
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getByPath = getByPath;
|
4
|
+
/**
|
5
|
+
* Gets a value from an object by a given path.
|
6
|
+
*
|
7
|
+
* @param obj - The object to search.
|
8
|
+
* @param path - The path to the value, separated by dots.
|
9
|
+
* @returns The value at the given path, or undefined if not found.
|
10
|
+
*/
|
11
|
+
function getByPath(obj, path) {
|
12
|
+
return path.split(".").reduce((acc, key) => {
|
13
|
+
if (acc && typeof acc === "object" && key in acc) {
|
14
|
+
return acc[key];
|
15
|
+
}
|
16
|
+
return undefined;
|
17
|
+
}, obj);
|
18
|
+
}
|
@@ -9,7 +9,7 @@ exports.getMaxRowNumber = getMaxRowNumber;
|
|
9
9
|
*/
|
10
10
|
function getMaxRowNumber(line) {
|
11
11
|
let result = 1;
|
12
|
-
const rowMatches = [...line.matchAll(/<row[^>]+r="(\d+)"[^>]*>/
|
12
|
+
const rowMatches = [...line.matchAll(/<row[^>]+r="(\d+)"[^>]*>/gi)];
|
13
13
|
for (const match of rowMatches) {
|
14
14
|
const rowNum = parseInt(match[1], 10);
|
15
15
|
if (rowNum >= result) {
|
@@ -14,14 +14,23 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./apply-replacements.js"), exports);
|
17
18
|
__exportStar(require("./check-row.js"), exports);
|
18
19
|
__exportStar(require("./check-rows.js"), exports);
|
19
20
|
__exportStar(require("./check-start-row.js"), exports);
|
20
21
|
__exportStar(require("./column-index-to-letter.js"), exports);
|
21
22
|
__exportStar(require("./escape-xml.js"), exports);
|
23
|
+
__exportStar(require("./extract-xml-declaration.js"), exports);
|
24
|
+
__exportStar(require("./get-by-path.js"), exports);
|
22
25
|
__exportStar(require("./get-max-row-number.js"), exports);
|
23
26
|
__exportStar(require("./get-rows-above.js"), exports);
|
24
27
|
__exportStar(require("./get-rows-below.js"), exports);
|
25
28
|
__exportStar(require("./parse-rows.js"), exports);
|
29
|
+
__exportStar(require("./process-merge-cells.js"), exports);
|
30
|
+
__exportStar(require("./process-merge-finalize.js"), exports);
|
31
|
+
__exportStar(require("./process-rows.js"), exports);
|
32
|
+
__exportStar(require("./process-shared-strings.js"), exports);
|
26
33
|
__exportStar(require("./to-excel-column-object.js"), exports);
|
34
|
+
__exportStar(require("./update-dimension.js"), exports);
|
35
|
+
__exportStar(require("./validate-worksheet-xml.js"), exports);
|
27
36
|
__exportStar(require("./write-rows-to-stream.js"), exports);
|
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.processMergeCells = processMergeCells;
|
4
|
+
/**
|
5
|
+
* Processes the sheet XML by extracting the initial <mergeCells> block and
|
6
|
+
* extracting all merge cell references. The function returns an object with
|
7
|
+
* three properties:
|
8
|
+
* - `initialMergeCells`: The initial <mergeCells> block as a string array.
|
9
|
+
* - `mergeCellMatches`: An array of objects with `from` and `to` properties,
|
10
|
+
* representing the merge cell references.
|
11
|
+
* - `modifiedXml`: The modified sheet XML with the <mergeCells> block removed.
|
12
|
+
*
|
13
|
+
* @param sheetXml - The sheet XML string.
|
14
|
+
* @returns An object with the above three properties.
|
15
|
+
*/
|
16
|
+
function processMergeCells(sheetXml) {
|
17
|
+
// Regular expression for finding <mergeCells> block
|
18
|
+
const mergeCellsBlockRegex = /<mergeCells[^>]*>[\s\S]*?<\/mergeCells>/;
|
19
|
+
// Find the first <mergeCells> block (if there are multiple, in xlsx usually there is only one)
|
20
|
+
const mergeCellsBlockMatch = sheetXml.match(mergeCellsBlockRegex);
|
21
|
+
const initialMergeCells = [];
|
22
|
+
const mergeCellMatches = [];
|
23
|
+
if (mergeCellsBlockMatch) {
|
24
|
+
const mergeCellsBlock = mergeCellsBlockMatch[0];
|
25
|
+
initialMergeCells.push(mergeCellsBlock);
|
26
|
+
// Extract <mergeCell ref="A1:B2"/> from this block
|
27
|
+
const mergeCellRegex = /<mergeCell ref="([A-Z]+\d+):([A-Z]+\d+)"\/>/g;
|
28
|
+
for (const match of mergeCellsBlock.matchAll(mergeCellRegex)) {
|
29
|
+
mergeCellMatches.push({ from: match[1], to: match[2] });
|
30
|
+
}
|
31
|
+
}
|
32
|
+
// Remove the <mergeCells> block from the XML
|
33
|
+
const modifiedXml = sheetXml.replace(mergeCellsBlockRegex, "");
|
34
|
+
return {
|
35
|
+
initialMergeCells,
|
36
|
+
mergeCellMatches,
|
37
|
+
modifiedXml,
|
38
|
+
};
|
39
|
+
}
|
40
|
+
;
|
@@ -0,0 +1,51 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.processMergeFinalize = processMergeFinalize;
|
4
|
+
const update_dimension_js_1 = require("./update-dimension.js");
|
5
|
+
/**
|
6
|
+
* Finalizes the processing of the merged sheet by updating the merge cells and
|
7
|
+
* inserting them into the sheet XML. It also returns the modified sheet XML and
|
8
|
+
* shared strings.
|
9
|
+
*
|
10
|
+
* @param {object} data - An object containing the following properties:
|
11
|
+
* - `initialMergeCells`: The initial merge cells from the original sheet.
|
12
|
+
* - `lastIndex`: The last processed position in the sheet XML.
|
13
|
+
* - `mergeCellMatches`: An array of objects with `from` and `to` properties,
|
14
|
+
* describing the merge cells.
|
15
|
+
* - `resultRows`: An array of processed XML rows.
|
16
|
+
* - `rowShift`: The total row shift.
|
17
|
+
* - `sharedStrings`: An array of shared strings.
|
18
|
+
* - `sharedStringsHeader`: The XML declaration of the shared strings.
|
19
|
+
* - `sheetMergeCells`: An array of merge cell XML strings.
|
20
|
+
* - `sheetXml`: The original sheet XML string.
|
21
|
+
*
|
22
|
+
* @returns An object with two properties:
|
23
|
+
* - `shared`: The modified shared strings XML string.
|
24
|
+
* - `sheet`: The modified sheet XML string with updated merge cells.
|
25
|
+
*/
|
26
|
+
function processMergeFinalize(data) {
|
27
|
+
const { initialMergeCells, lastIndex, mergeCellMatches, resultRows, rowShift, sharedStrings, sharedStringsHeader, sheetMergeCells, sheetXml, } = data;
|
28
|
+
for (const { from, to } of mergeCellMatches) {
|
29
|
+
const [, fromCol, fromRow] = from.match(/^([A-Z]+)(\d+)$/);
|
30
|
+
const [, toCol, toRow] = to.match(/^([A-Z]+)(\d+)$/);
|
31
|
+
const fromRowNum = Number(fromRow);
|
32
|
+
// These rows have already been processed, don't add duplicates
|
33
|
+
if (fromRowNum <= lastIndex)
|
34
|
+
continue;
|
35
|
+
const newFrom = `${fromCol}${fromRowNum + rowShift}`;
|
36
|
+
const newTo = `${toCol}${Number(toRow) + rowShift}`;
|
37
|
+
sheetMergeCells.push(`<mergeCell ref="${newFrom}:${newTo}"/>`);
|
38
|
+
}
|
39
|
+
resultRows.push(sheetXml.slice(lastIndex));
|
40
|
+
// Form XML for mergeCells if there are any
|
41
|
+
const mergeXml = sheetMergeCells.length
|
42
|
+
? `<mergeCells count="${sheetMergeCells.length}">${sheetMergeCells.join("")}</mergeCells>`
|
43
|
+
: initialMergeCells;
|
44
|
+
// Insert mergeCells before the closing sheetData tag
|
45
|
+
const sheetWithMerge = resultRows.join("").replace(/<\/sheetData>/, `</sheetData>${mergeXml}`);
|
46
|
+
// Return modified sheet XML and shared strings
|
47
|
+
return {
|
48
|
+
shared: `${sharedStringsHeader}\n<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="${sharedStrings.length}" uniqueCount="${sharedStrings.length}">${sharedStrings.join("")}</sst>`,
|
49
|
+
sheet: (0, update_dimension_js_1.updateDimension)(sheetWithMerge),
|
50
|
+
};
|
51
|
+
}
|
@@ -0,0 +1,160 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.processRows = processRows;
|
4
|
+
/**
|
5
|
+
* Processes a sheet XML by replacing table placeholders with real data and adjusting row numbers accordingly.
|
6
|
+
*
|
7
|
+
* @param data - An object containing the following properties:
|
8
|
+
* - `replacements`: An object where keys are table names and values are arrays of objects with table data.
|
9
|
+
* - `sharedIndexMap`: A Map of shared string indexes by their text content.
|
10
|
+
* - `mergeCellMatches`: An array of objects with `from` and `to` properties, describing the merge cells.
|
11
|
+
* - `sharedStrings`: An array of shared strings.
|
12
|
+
* - `sheetMergeCells`: An array of merge cell XML strings.
|
13
|
+
* - `sheetXml`: The sheet XML string.
|
14
|
+
*
|
15
|
+
* @returns An object with the following properties:
|
16
|
+
* - `lastIndex`: The last processed position in the sheet XML.
|
17
|
+
* - `resultRows`: An array of processed XML rows.
|
18
|
+
* - `rowShift`: The total row shift.
|
19
|
+
*/
|
20
|
+
function processRows(data) {
|
21
|
+
const { mergeCellMatches, replacements, sharedIndexMap, sharedStrings, sheetMergeCells, sheetXml, } = data;
|
22
|
+
const TABLE_REGEX = /\$\{table:([a-zA-Z0-9_]+)\.([a-zA-Z0-9_]+)\}/g;
|
23
|
+
// Array for storing resulting XML rows
|
24
|
+
const resultRows = [];
|
25
|
+
// Previous position of processed part of XML
|
26
|
+
let lastIndex = 0;
|
27
|
+
// Shift for row numbers
|
28
|
+
let rowShift = 0;
|
29
|
+
// Regular expression for finding <row> elements
|
30
|
+
const rowRegex = /<row[^>]*?>[\s\S]*?<\/row>/g;
|
31
|
+
// Process each <row> element
|
32
|
+
for (const match of sheetXml.matchAll(rowRegex)) {
|
33
|
+
// Full XML row
|
34
|
+
const fullRow = match[0];
|
35
|
+
// Start position of the row in XML
|
36
|
+
const matchStart = match.index;
|
37
|
+
// End position of the row in XML
|
38
|
+
const matchEnd = matchStart + fullRow.length;
|
39
|
+
// Add the intermediate XML chunk (if any) between the previous and the current row
|
40
|
+
if (lastIndex !== matchStart) {
|
41
|
+
resultRows.push(sheetXml.slice(lastIndex, matchStart));
|
42
|
+
}
|
43
|
+
lastIndex = matchEnd;
|
44
|
+
// Get row number from r attribute
|
45
|
+
const originalRowNumber = parseInt(fullRow.match(/<row[^>]* r="(\d+)"/)?.[1] ?? "1", 10);
|
46
|
+
// Update row number based on rowShift
|
47
|
+
const shiftedRowNumber = originalRowNumber + rowShift;
|
48
|
+
// Find shared string indexes in cells of the current row
|
49
|
+
const sharedValueIndexes = [];
|
50
|
+
// Regular expression for finding a cell
|
51
|
+
const cellRegex = /<c[^>]*?r="([A-Z]+\d+)"[^>]*?>([\s\S]*?)<\/c>/g;
|
52
|
+
for (const cell of fullRow.matchAll(cellRegex)) {
|
53
|
+
const cellTag = cell[0];
|
54
|
+
// Check if the cell is a shared string
|
55
|
+
const isShared = /t="s"/.test(cellTag);
|
56
|
+
const valueMatch = cellTag.match(/<v>(\d+)<\/v>/);
|
57
|
+
if (isShared && valueMatch) {
|
58
|
+
sharedValueIndexes.push(parseInt(valueMatch[1], 10));
|
59
|
+
}
|
60
|
+
}
|
61
|
+
// Get the text content of shared strings by their indexes
|
62
|
+
const sharedTexts = sharedValueIndexes.map(i => sharedStrings[i]?.replace(/<\/?si>/g, "") ?? "");
|
63
|
+
// Find table placeholders in shared strings
|
64
|
+
const tablePlaceholders = sharedTexts.flatMap(e => [...e.matchAll(TABLE_REGEX)]);
|
65
|
+
// If there are no table placeholders, just shift the row
|
66
|
+
if (tablePlaceholders.length === 0) {
|
67
|
+
const updatedRow = fullRow
|
68
|
+
.replace(/(<row[^>]* r=")(\d+)(")/, `$1${shiftedRowNumber}$3`)
|
69
|
+
.replace(/<c r="([A-Z]+)(\d+)"/g, (_, col) => `<c r="${col}${shiftedRowNumber}"`);
|
70
|
+
resultRows.push(updatedRow);
|
71
|
+
// Update mergeCells for regular row with rowShift
|
72
|
+
const calculatedRowNumber = originalRowNumber + rowShift;
|
73
|
+
for (const { from, to } of mergeCellMatches) {
|
74
|
+
const [, fromCol, fromRow] = from.match(/^([A-Z]+)(\d+)$/);
|
75
|
+
const [, toCol] = to.match(/^([A-Z]+)(\d+)$/);
|
76
|
+
if (Number(fromRow) === calculatedRowNumber) {
|
77
|
+
const newFrom = `${fromCol}${shiftedRowNumber}`;
|
78
|
+
const newTo = `${toCol}${shiftedRowNumber}`;
|
79
|
+
sheetMergeCells.push(`<mergeCell ref="${newFrom}:${newTo}"/>`);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
continue;
|
83
|
+
}
|
84
|
+
// Get the table name from the first placeholder
|
85
|
+
const firstMatch = tablePlaceholders[0];
|
86
|
+
const tableName = firstMatch?.[1];
|
87
|
+
if (!tableName)
|
88
|
+
throw new Error("Table name not found");
|
89
|
+
// Get data for replacement from replacements
|
90
|
+
const array = replacements[tableName];
|
91
|
+
if (!array)
|
92
|
+
continue;
|
93
|
+
if (!Array.isArray(array))
|
94
|
+
throw new Error("Table data is not an array");
|
95
|
+
const tableRowStart = shiftedRowNumber;
|
96
|
+
// Find mergeCells to duplicate (mergeCells that start with the current row)
|
97
|
+
const mergeCellsToDuplicate = mergeCellMatches.filter(({ from }) => {
|
98
|
+
const match = from.match(/^([A-Z]+)(\d+)$/);
|
99
|
+
if (!match)
|
100
|
+
return false;
|
101
|
+
// Row number of the merge cell start position is in the second group
|
102
|
+
const rowNumber = match[2];
|
103
|
+
return Number(rowNumber) === tableRowStart;
|
104
|
+
});
|
105
|
+
// Change the current row to multiple rows from the data array
|
106
|
+
for (let i = 0; i < array.length; i++) {
|
107
|
+
const rowData = array[i];
|
108
|
+
let newRow = fullRow;
|
109
|
+
// Replace placeholders in shared strings with real data
|
110
|
+
sharedValueIndexes.forEach((originalIdx, idx) => {
|
111
|
+
const originalText = sharedTexts[idx];
|
112
|
+
if (!originalText)
|
113
|
+
throw new Error("Shared value not found");
|
114
|
+
// Replace placeholders ${tableName.field} with real data from array data
|
115
|
+
const replacedText = originalText.replace(TABLE_REGEX, (_, tbl, field) => tbl === tableName ? String(rowData?.[field] ?? "") : "");
|
116
|
+
// Add new text to shared strings if it doesn't exist
|
117
|
+
let newIndex;
|
118
|
+
if (sharedIndexMap.has(replacedText)) {
|
119
|
+
newIndex = sharedIndexMap.get(replacedText);
|
120
|
+
}
|
121
|
+
else {
|
122
|
+
newIndex = sharedStrings.length;
|
123
|
+
sharedIndexMap.set(replacedText, newIndex);
|
124
|
+
sharedStrings.push(`<si>${replacedText}</si>`);
|
125
|
+
}
|
126
|
+
// Replace the shared string index in the cell
|
127
|
+
newRow = newRow.replace(`<v>${originalIdx}</v>`, `<v>${newIndex}</v>`);
|
128
|
+
});
|
129
|
+
// Update row number and cell references
|
130
|
+
const newRowNum = shiftedRowNumber + i;
|
131
|
+
newRow = newRow
|
132
|
+
.replace(/<row[^>]* r="\d+"/, rowTag => rowTag.replace(/r="\d+"/, `r="${newRowNum}"`))
|
133
|
+
.replace(/<c r="([A-Z]+)\d+"/g, (_, col) => `<c r="${col}${newRowNum}"`);
|
134
|
+
resultRows.push(newRow);
|
135
|
+
// Add duplicate mergeCells for new rows
|
136
|
+
for (const { from, to } of mergeCellsToDuplicate) {
|
137
|
+
const [, colFrom, rowFrom] = from.match(/^([A-Z]+)(\d+)$/);
|
138
|
+
const [, colTo, rowTo] = to.match(/^([A-Z]+)(\d+)$/);
|
139
|
+
const newFrom = `${colFrom}${Number(rowFrom) + i}`;
|
140
|
+
const newTo = `${colTo}${Number(rowTo) + i}`;
|
141
|
+
sheetMergeCells.push(`<mergeCell ref="${newFrom}:${newTo}"/>`);
|
142
|
+
}
|
143
|
+
}
|
144
|
+
// It increases the row shift by the number of added rows minus one replaced
|
145
|
+
rowShift += array.length - 1;
|
146
|
+
const delta = array.length - 1;
|
147
|
+
const calculatedRowNumber = originalRowNumber + rowShift - array.length + 1;
|
148
|
+
if (delta > 0) {
|
149
|
+
for (const merge of mergeCellMatches) {
|
150
|
+
const fromRow = parseInt(merge.from.match(/\d+$/)[0], 10);
|
151
|
+
if (fromRow > calculatedRowNumber) {
|
152
|
+
merge.from = merge.from.replace(/\d+$/, r => `${parseInt(r) + delta}`);
|
153
|
+
merge.to = merge.to.replace(/\d+$/, r => `${parseInt(r) + delta}`);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
return { lastIndex, resultRows, rowShift };
|
159
|
+
}
|
160
|
+
;
|
@@ -0,0 +1,45 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.processSharedStrings = processSharedStrings;
|
4
|
+
const extract_xml_declaration_js_1 = require("./extract-xml-declaration.js");
|
5
|
+
/**
|
6
|
+
* Processes the shared strings XML by extracting the XML declaration,
|
7
|
+
* extracting individual <si> elements, and storing them in an array.
|
8
|
+
*
|
9
|
+
* The function returns an object with four properties:
|
10
|
+
* - sharedIndexMap: A map of shared string content to their corresponding index
|
11
|
+
* - sharedStrings: An array of shared strings
|
12
|
+
* - sharedStringsHeader: The XML declaration of the shared strings
|
13
|
+
* - sheetMergeCells: An empty array, which is only used for type compatibility
|
14
|
+
* with the return type of processBuild.
|
15
|
+
*
|
16
|
+
* @param sharedStringsXml - The XML string of the shared strings
|
17
|
+
* @returns An object with the four properties above
|
18
|
+
*/
|
19
|
+
function processSharedStrings(sharedStringsXml) {
|
20
|
+
// Final list of merged cells with all changes
|
21
|
+
const sheetMergeCells = [];
|
22
|
+
// Array for storing shared strings
|
23
|
+
const sharedStrings = [];
|
24
|
+
const sharedStringsHeader = (0, extract_xml_declaration_js_1.extractXmlDeclaration)(sharedStringsXml);
|
25
|
+
// Map for fast lookup of shared string index by content
|
26
|
+
const sharedIndexMap = new Map();
|
27
|
+
// Regular expression for finding <si> elements (shared string items)
|
28
|
+
const siRegex = /<si>([\s\S]*?)<\/si>/g;
|
29
|
+
// Parse sharedStringsXml and fill sharedStrings and sharedIndexMap
|
30
|
+
for (const match of sharedStringsXml.matchAll(siRegex)) {
|
31
|
+
const content = match[1];
|
32
|
+
if (!content)
|
33
|
+
continue;
|
34
|
+
const fullSi = `<si>${content}</si>`;
|
35
|
+
sharedIndexMap.set(content, sharedStrings.length);
|
36
|
+
sharedStrings.push(fullSi);
|
37
|
+
}
|
38
|
+
return {
|
39
|
+
sharedIndexMap,
|
40
|
+
sharedStrings,
|
41
|
+
sharedStringsHeader,
|
42
|
+
sheetMergeCells,
|
43
|
+
};
|
44
|
+
}
|
45
|
+
;
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.toExcelColumnObject = toExcelColumnObject;
|
4
|
+
const column_index_to_letter_js_1 = require("./column-index-to-letter.js");
|
4
5
|
/**
|
5
6
|
* Converts an array of values into a Record<string, string> with Excel column names as keys.
|
6
7
|
*
|
@@ -11,18 +12,9 @@ exports.toExcelColumnObject = toExcelColumnObject;
|
|
11
12
|
* @returns The resulting Record<string, string>
|
12
13
|
*/
|
13
14
|
function toExcelColumnObject(values) {
|
14
|
-
const toExcelColumn = (index) => {
|
15
|
-
let column = "";
|
16
|
-
let i = index;
|
17
|
-
while (i >= 0) {
|
18
|
-
column = String.fromCharCode((i % 26) + 65) + column;
|
19
|
-
i = Math.floor(i / 26) - 1;
|
20
|
-
}
|
21
|
-
return column;
|
22
|
-
};
|
23
15
|
const result = {};
|
24
16
|
for (let i = 0; i < values.length; i++) {
|
25
|
-
const key =
|
17
|
+
const key = (0, column_index_to_letter_js_1.columnIndexToLetter)(i);
|
26
18
|
result[key] = String(values[i]);
|
27
19
|
}
|
28
20
|
return result;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.updateDimension = updateDimension;
|
4
|
+
function updateDimension(xml) {
|
5
|
+
const cellRefs = [...xml.matchAll(/<c r="([A-Z]+)(\d+)"/g)];
|
6
|
+
if (cellRefs.length === 0)
|
7
|
+
return xml;
|
8
|
+
let minCol = Infinity, maxCol = -Infinity;
|
9
|
+
let minRow = Infinity, maxRow = -Infinity;
|
10
|
+
for (const [, colStr, rowStr] of cellRefs) {
|
11
|
+
const col = columnLetterToNumber(colStr);
|
12
|
+
const row = parseInt(rowStr, 10);
|
13
|
+
if (col < minCol)
|
14
|
+
minCol = col;
|
15
|
+
if (col > maxCol)
|
16
|
+
maxCol = col;
|
17
|
+
if (row < minRow)
|
18
|
+
minRow = row;
|
19
|
+
if (row > maxRow)
|
20
|
+
maxRow = row;
|
21
|
+
}
|
22
|
+
const newRef = `${columnNumberToLetter(minCol)}${minRow}:${columnNumberToLetter(maxCol)}${maxRow}`;
|
23
|
+
return xml.replace(/<dimension ref="[^"]*"/, `<dimension ref="${newRef}"`);
|
24
|
+
}
|
25
|
+
function columnLetterToNumber(letters) {
|
26
|
+
let num = 0;
|
27
|
+
for (let i = 0; i < letters.length; i++) {
|
28
|
+
num = num * 26 + (letters.charCodeAt(i) - 64);
|
29
|
+
}
|
30
|
+
return num;
|
31
|
+
}
|
32
|
+
function columnNumberToLetter(num) {
|
33
|
+
let letters = "";
|
34
|
+
while (num > 0) {
|
35
|
+
const rem = (num - 1) % 26;
|
36
|
+
letters = String.fromCharCode(65 + rem) + letters;
|
37
|
+
num = Math.floor((num - 1) / 26);
|
38
|
+
}
|
39
|
+
return letters;
|
40
|
+
}
|