@js-ak/excel-toolbox 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +41 -62
  2. package/build/cjs/lib/merge-sheets-to-base-file-process-sync.js +105 -0
  3. package/build/cjs/lib/merge-sheets-to-base-file-process.js +3 -3
  4. package/build/cjs/lib/merge-sheets-to-base-file-sync.js +2 -2
  5. package/build/cjs/lib/merge-sheets-to-base-file.js +1 -1
  6. package/build/cjs/lib/template/template-fs.js +143 -63
  7. package/build/cjs/lib/template/template-memory.js +281 -59
  8. package/build/cjs/lib/template/utils/index.js +25 -0
  9. package/build/cjs/lib/template/utils/prepare-row-to-cells.js +5 -1
  10. package/build/cjs/lib/template/utils/regexp.js +32 -0
  11. package/build/cjs/lib/template/utils/update-dimension.js +15 -0
  12. package/build/cjs/lib/template/utils/validate-worksheet-xml.js +74 -74
  13. package/build/cjs/lib/template/utils/write-rows-to-stream.js +57 -17
  14. package/build/cjs/lib/xml/extract-rows-from-sheet-sync.js +67 -0
  15. package/build/cjs/lib/xml/extract-rows-from-sheet.js +4 -2
  16. package/build/cjs/lib/xml/extract-xml-from-sheet-sync.js +43 -0
  17. package/build/cjs/lib/xml/extract-xml-from-sheet.js +15 -15
  18. package/build/cjs/lib/xml/index.js +2 -1
  19. package/build/esm/lib/merge-sheets-to-base-file-process-sync.js +69 -0
  20. package/build/esm/lib/merge-sheets-to-base-file-process.js +3 -3
  21. package/build/esm/lib/merge-sheets-to-base-file-sync.js +2 -2
  22. package/build/esm/lib/merge-sheets-to-base-file.js +1 -1
  23. package/build/esm/lib/template/template-fs.js +140 -63
  24. package/build/esm/lib/template/template-memory.js +281 -59
  25. package/build/esm/lib/template/utils/index.js +2 -0
  26. package/build/esm/lib/template/utils/prepare-row-to-cells.js +5 -1
  27. package/build/esm/lib/template/utils/regexp.js +28 -0
  28. package/build/esm/lib/template/utils/update-dimension.js +15 -0
  29. package/build/esm/lib/template/utils/validate-worksheet-xml.js +74 -74
  30. package/build/esm/lib/template/utils/write-rows-to-stream.js +57 -17
  31. package/build/esm/lib/xml/extract-rows-from-sheet-sync.js +64 -0
  32. package/build/esm/lib/xml/extract-rows-from-sheet.js +4 -2
  33. package/build/esm/lib/xml/extract-xml-from-sheet-sync.js +40 -0
  34. package/build/esm/lib/xml/extract-xml-from-sheet.js +12 -15
  35. package/build/esm/lib/xml/index.js +2 -1
  36. package/build/types/lib/merge-sheets-to-base-file-process-sync.d.ts +27 -0
  37. package/build/types/lib/merge-sheets-to-base-file-process.d.ts +1 -1
  38. package/build/types/lib/template/template-fs.d.ts +2 -0
  39. package/build/types/lib/template/template-memory.d.ts +61 -0
  40. package/build/types/lib/template/utils/index.d.ts +2 -0
  41. package/build/types/lib/template/utils/prepare-row-to-cells.d.ts +5 -1
  42. package/build/types/lib/template/utils/regexp.d.ts +24 -0
  43. package/build/types/lib/template/utils/update-dimension.d.ts +15 -0
  44. package/build/types/lib/template/utils/write-rows-to-stream.d.ts +22 -9
  45. package/build/types/lib/xml/extract-rows-from-sheet-sync.d.ts +28 -0
  46. package/build/types/lib/xml/extract-rows-from-sheet.d.ts +2 -2
  47. package/build/types/lib/xml/extract-xml-from-sheet-sync.d.ts +14 -0
  48. package/build/types/lib/xml/extract-xml-from-sheet.d.ts +2 -2
  49. package/build/types/lib/xml/index.d.ts +2 -1
  50. package/package.json +1 -5
  51. package/build/cjs/lib/xml/extract-xml-from-system-content.js +0 -53
  52. package/build/esm/lib/xml/extract-xml-from-system-content.js +0 -49
  53. package/build/types/lib/xml/extract-xml-from-system-content.d.ts +0 -15
@@ -13,17 +13,30 @@ interface WritableLike {
13
13
  * for the first row written to the file. Subsequent rows are written
14
14
  * with incrementing row numbers.
15
15
  *
16
- * @param output - A file write stream to write the Excel XML to.
17
- * @param rows - An async iterable of rows, where each row is an array
18
- * of values.
19
- * @param startRowNumber - The starting row number to use for the first
20
- * row written to the file.
21
- *
22
- * @returns An object with a single property `rowNumber`, which is the
23
- * last row number written to the file (i.e., the `startRowNumber`
24
- * plus the number of rows written).
16
+ * @param {WritableLike} output - A file write stream to write the Excel XML to.
17
+ * @param {AsyncIterable<unknown[] | unknown[][]>} rows - An async iterable of rows, where each row is an array
18
+ * of values or an array of arrays of values.
19
+ * @param {number} startRowNumber - The starting row number to use for the first
20
+ * row written to the file.
21
+ * @returns {Promise<{
22
+ * dimension: {
23
+ * maxColumn: string;
24
+ * maxRow: number;
25
+ * minColumn: string;
26
+ * minRow: number;
27
+ * };
28
+ * rowNumber: number;
29
+ * }>} An object containing:
30
+ * - dimension: The boundaries of the written data (min/max columns and rows)
31
+ * - rowNumber: The last row number written to the file
25
32
  */
26
33
  export declare function writeRowsToStream(output: WritableLike, rows: AsyncIterable<unknown[] | unknown[][]>, startRowNumber: number): Promise<{
34
+ dimension: {
35
+ maxColumn: string;
36
+ maxRow: number;
37
+ minColumn: string;
38
+ minRow: number;
39
+ };
27
40
  rowNumber: number;
28
41
  }>;
29
42
  export {};
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Parses a worksheet (either as Buffer or string) to extract row data,
3
+ * last row number, and merge cell information from Excel XML format.
4
+ *
5
+ * This function is particularly useful for processing Excel files in
6
+ * Open XML Spreadsheet format (.xlsx).
7
+ *
8
+ * @param {Buffer|string} sheet - The worksheet content to parse, either as:
9
+ * - Buffer (binary Excel sheet)
10
+ * - string (raw XML content)
11
+ * @returns {{
12
+ * rows: string[],
13
+ * lastRowNumber: number,
14
+ * mergeCells: {ref: string}[]
15
+ * }} An object containing:
16
+ * - rows: Array of raw XML strings for each <row> element
17
+ * - lastRowNumber: Highest row number found in the sheet (1-based)
18
+ * - mergeCells: Array of merged cell ranges (e.g., [{ref: "A1:B2"}])
19
+ * @throws {Error} If the sheetData section is not found in the XML
20
+ */
21
+ export declare function extractRowsFromSheetSync(sheet: Buffer | string): {
22
+ rows: string[];
23
+ lastRowNumber: number;
24
+ mergeCells: {
25
+ ref: string;
26
+ }[];
27
+ xml: string;
28
+ };
@@ -18,11 +18,11 @@
18
18
  * - mergeCells: Array of merged cell ranges (e.g., [{ref: "A1:B2"}])
19
19
  * @throws {Error} If the sheetData section is not found in the XML
20
20
  */
21
- export declare function extractRowsFromSheet(sheet: Buffer | string): {
21
+ export declare function extractRowsFromSheet(sheet: Buffer | string): Promise<{
22
22
  rows: string[];
23
23
  lastRowNumber: number;
24
24
  mergeCells: {
25
25
  ref: string;
26
26
  }[];
27
27
  xml: string;
28
- };
28
+ }>;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Extracts and parses XML content from an Excel worksheet file (e.g., xl/worksheets/sheet1.xml).
3
+ * Handles both compressed (raw deflate) and uncompressed (plain XML) formats.
4
+ *
5
+ * This function is designed to work with Excel Open XML (.xlsx) worksheet files,
6
+ * which may be stored in either compressed or uncompressed format within the ZIP container.
7
+ *
8
+ * @param {Buffer} buffer - The file content to process, which may be:
9
+ * - Raw XML text
10
+ * - Deflate-compressed XML data (without zlib headers)
11
+ * @returns {string} - The extracted XML content as a UTF-8 string
12
+ * @throws {Error} - If the buffer is empty or cannot be processed
13
+ */
14
+ export declare function extractXmlFromSheetSync(buffer: Buffer): string;
@@ -8,7 +8,7 @@
8
8
  * @param {Buffer} buffer - The file content to process, which may be:
9
9
  * - Raw XML text
10
10
  * - Deflate-compressed XML data (without zlib headers)
11
- * @returns {string} - The extracted XML content as a UTF-8 string
11
+ * @returns {Promise<string>} - The extracted XML content as a UTF-8 string
12
12
  * @throws {Error} - If the buffer is empty or cannot be processed
13
13
  */
14
- export declare function extractXmlFromSheet(buffer: Buffer): string;
14
+ export declare function extractXmlFromSheet(buffer: Buffer): Promise<string>;
@@ -1,5 +1,6 @@
1
1
  export * from "./build-merged-sheet.js";
2
+ export * from "./extract-rows-from-sheet-sync.js";
2
3
  export * from "./extract-rows-from-sheet.js";
4
+ export * from "./extract-xml-from-sheet-sync.js";
3
5
  export * from "./extract-xml-from-sheet.js";
4
- export * from "./extract-xml-from-system-content.js";
5
6
  export * from "./shift-row-indices.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-ak/excel-toolbox",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "excel-toolbox",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -70,7 +70,6 @@
70
70
  "@semantic-release/release-notes-generator": "14.0.0",
71
71
  "@stylistic/eslint-plugin-ts": "4.2.0",
72
72
  "@types/node": "22.14.0",
73
- "@types/pako": "2.0.3",
74
73
  "@vitest/coverage-v8": "3.1.2",
75
74
  "eslint": "9.24.0",
76
75
  "eslint-plugin-sort-destructure-keys": "2.0.0",
@@ -80,8 +79,5 @@
80
79
  "typescript": "5.8.3",
81
80
  "typescript-eslint": "8.29.0",
82
81
  "vitest": "3.1.2"
83
- },
84
- "dependencies": {
85
- "pako": "2.1.0"
86
82
  }
87
83
  }
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extractXmlFromSystemContent = void 0;
4
- const pako_1 = require("pako");
5
- /**
6
- * Extracts and decompresses XML content from Excel system files (e.g., workbook.xml, [Content_Types].xml).
7
- * Handles both compressed (raw DEFLATE) and uncompressed (plain XML) formats with comprehensive error handling.
8
- *
9
- * @param {Buffer} buffer - The file content to process, which may be:
10
- * - Raw XML text
11
- * - DEFLATE-compressed XML data (without zlib headers)
12
- * @param {string} name - The filename being processed (for error reporting)
13
- * @returns {string} - The extracted XML content as a sanitized UTF-8 string
14
- * @throws {Error} - With descriptive messages for various failure scenarios:
15
- * - Empty buffer
16
- * - Decompression failures
17
- * - Invalid XML content
18
- */
19
- const extractXmlFromSystemContent = (buffer, name) => {
20
- // Validate input buffer
21
- if (!buffer || buffer.length === 0) {
22
- throw new Error(`Empty data buffer provided for file ${name}`);
23
- }
24
- let xml;
25
- // Check for XML declaration in first 5 bytes (<?xml)
26
- const startsWithXml = buffer.subarray(0, 5).toString("utf8").trim().startsWith("<?xml");
27
- if (startsWithXml) {
28
- // Case 1: Already uncompressed XML - convert directly to string
29
- xml = buffer.toString("utf8");
30
- }
31
- else {
32
- // Case 2: Attempt DEFLATE decompression
33
- try {
34
- const inflated = (0, pako_1.inflateRaw)(buffer, { to: "string" });
35
- // Validate decompressed content contains XML declaration
36
- if (inflated && inflated.includes("<?xml")) {
37
- xml = inflated;
38
- }
39
- else {
40
- throw new Error(`Decompressed data doesn't contain valid XML in ${name}`);
41
- }
42
- }
43
- catch (error) {
44
- const message = error instanceof Error ? error.message : "Unknown error";
45
- throw new Error(`Failed to decompress ${name}: ${message}`);
46
- }
47
- }
48
- // Sanitize XML by removing illegal control characters (per XML 1.0 spec)
49
- // Preserves tabs (0x09), newlines (0x0A), and carriage returns (0x0D)
50
- xml = xml.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, "");
51
- return xml;
52
- };
53
- exports.extractXmlFromSystemContent = extractXmlFromSystemContent;
@@ -1,49 +0,0 @@
1
- import { inflateRaw } from "pako";
2
- /**
3
- * Extracts and decompresses XML content from Excel system files (e.g., workbook.xml, [Content_Types].xml).
4
- * Handles both compressed (raw DEFLATE) and uncompressed (plain XML) formats with comprehensive error handling.
5
- *
6
- * @param {Buffer} buffer - The file content to process, which may be:
7
- * - Raw XML text
8
- * - DEFLATE-compressed XML data (without zlib headers)
9
- * @param {string} name - The filename being processed (for error reporting)
10
- * @returns {string} - The extracted XML content as a sanitized UTF-8 string
11
- * @throws {Error} - With descriptive messages for various failure scenarios:
12
- * - Empty buffer
13
- * - Decompression failures
14
- * - Invalid XML content
15
- */
16
- export const extractXmlFromSystemContent = (buffer, name) => {
17
- // Validate input buffer
18
- if (!buffer || buffer.length === 0) {
19
- throw new Error(`Empty data buffer provided for file ${name}`);
20
- }
21
- let xml;
22
- // Check for XML declaration in first 5 bytes (<?xml)
23
- const startsWithXml = buffer.subarray(0, 5).toString("utf8").trim().startsWith("<?xml");
24
- if (startsWithXml) {
25
- // Case 1: Already uncompressed XML - convert directly to string
26
- xml = buffer.toString("utf8");
27
- }
28
- else {
29
- // Case 2: Attempt DEFLATE decompression
30
- try {
31
- const inflated = inflateRaw(buffer, { to: "string" });
32
- // Validate decompressed content contains XML declaration
33
- if (inflated && inflated.includes("<?xml")) {
34
- xml = inflated;
35
- }
36
- else {
37
- throw new Error(`Decompressed data doesn't contain valid XML in ${name}`);
38
- }
39
- }
40
- catch (error) {
41
- const message = error instanceof Error ? error.message : "Unknown error";
42
- throw new Error(`Failed to decompress ${name}: ${message}`);
43
- }
44
- }
45
- // Sanitize XML by removing illegal control characters (per XML 1.0 spec)
46
- // Preserves tabs (0x09), newlines (0x0A), and carriage returns (0x0D)
47
- xml = xml.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, "");
48
- return xml;
49
- };
@@ -1,15 +0,0 @@
1
- /**
2
- * Extracts and decompresses XML content from Excel system files (e.g., workbook.xml, [Content_Types].xml).
3
- * Handles both compressed (raw DEFLATE) and uncompressed (plain XML) formats with comprehensive error handling.
4
- *
5
- * @param {Buffer} buffer - The file content to process, which may be:
6
- * - Raw XML text
7
- * - DEFLATE-compressed XML data (without zlib headers)
8
- * @param {string} name - The filename being processed (for error reporting)
9
- * @returns {string} - The extracted XML content as a sanitized UTF-8 string
10
- * @throws {Error} - With descriptive messages for various failure scenarios:
11
- * - Empty buffer
12
- * - Decompression failures
13
- * - Invalid XML content
14
- */
15
- export declare const extractXmlFromSystemContent: (buffer: Buffer, name: string) => string;