|
1 | | -# Merging Sheets into Base File |
| 1 | +# mergeSheetsToBaseFile |
2 | 2 |
|
3 | | -This document explains how to merge sheets from multiple Excel files into a base file using the Excel Toolbox library. |
| 3 | +Merges rows from multiple sheets into a single sheet of a base Excel file while preserving formatting and structure. |
4 | 4 |
|
5 | | -## Overview |
| 5 | +> ⚠️ **Experimental API** |
| 6 | +> Interface may change in future releases. |
6 | 7 |
|
7 | | -The merge functionality allows you to: |
| 8 | +--- |
8 | 9 |
|
9 | | -- Combine rows from multiple sheets into a single base sheet |
10 | | -- Maintain cell formatting and merged cells |
11 | | -- Insert gaps between merged sections |
12 | | -- Remove unwanted sheets from the output file |
| 10 | +## 📦 Functions |
13 | 11 |
|
14 | | -## Basic Usage |
| 12 | +### `mergeSheetsToBaseFile` |
15 | 13 |
|
16 | | -### Asynchronous Merge |
| 14 | +Asynchronously merges rows from specified sheets of one or more Excel files into a sheet of a base Excel file. |
17 | 15 |
|
18 | | -```typescript |
19 | | -import { mergeSheetsToBaseFile } from '@js-ak/excel-toolbox'; |
20 | | - |
21 | | -const result = await mergeSheetsToBaseFile({ |
22 | | - baseFile: baseFileBuffer, |
23 | | - baseSheetIndex: 1, // Optional, defaults to 1 |
24 | | - additions: [ |
25 | | - { |
26 | | - file: sourceFileBuffer, |
27 | | - sheetIndexes: [1, 2], // Sheets to merge from source file |
28 | | - isBaseFile: false // Optional, set to true if source is the base file |
29 | | - } |
30 | | - ], |
31 | | - gap: 1, // Optional, number of empty rows between sections |
32 | | - sheetNamesToRemove: [], // Optional, names of sheets to remove |
33 | | - sheetsToRemove: [] // Optional, indices of sheets to remove |
34 | | -}); |
| 16 | +```ts |
| 17 | +mergeSheetsToBaseFile(options: MergeSheetsOptions): Promise<Buffer> |
35 | 18 | ``` |
36 | 19 |
|
37 | | -### Synchronous Merge |
| 20 | +- Input: `options: MergeSheetsOptions` — configuration for merging |
| 21 | +- Output: `Promise<Buffer>` — the resulting `.xlsx` file as a buffer |
| 22 | +- Throws if: |
| 23 | + - Required files or sheets are missing |
| 24 | + - Sheet indices are invalid |
| 25 | + - XML structure is malformed |
| 26 | + - Merged cells conflict |
38 | 27 |
|
39 | | -```typescript |
40 | | -import { mergeSheetsToBaseFileSync } from '@js-ak/excel-toolbox'; |
| 28 | +--- |
41 | 29 |
|
42 | | -const result = mergeSheetsToBaseFileSync({ |
43 | | - baseFile: baseFileBuffer, |
44 | | - baseSheetIndex: 1, |
45 | | - additions: [ |
46 | | - { |
47 | | - file: sourceFileBuffer, |
48 | | - sheetIndexes: [1, 2] |
49 | | - } |
50 | | - ], |
51 | | - gap: 1 |
52 | | -}); |
| 30 | +### `mergeSheetsToBaseFileSync` |
| 31 | + |
| 32 | +Synchronous version of `mergeSheetsToBaseFile`. |
| 33 | + |
| 34 | +```ts |
| 35 | +mergeSheetsToBaseFileSync(options: MergeSheetsOptions): Buffer |
53 | 36 | ``` |
54 | 37 |
|
55 | | -## Parameters |
| 38 | +- Input: `options: MergeSheetsOptions` |
| 39 | +- Output: `Buffer` — resulting Excel file |
| 40 | +- Same error conditions as async version |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## ⚙️ Parameters |
| 45 | + |
| 46 | +### `MergeSheetsOptions` |
| 47 | + |
| 48 | +```ts |
| 49 | +interface MergeSheetsOptions { |
| 50 | + baseFile: Buffer; |
| 51 | + baseSheetIndex?: number; // default: 1 |
| 52 | + additions: Array<{ |
| 53 | + file: Buffer; |
| 54 | + sheetIndexes: number[]; |
| 55 | + isBaseFile?: boolean; |
| 56 | + }>; |
| 57 | + gap?: number; // default: 0 |
| 58 | + sheetNamesToRemove?: string[]; |
| 59 | + sheetsToRemove?: number[]; |
| 60 | +} |
| 61 | +``` |
56 | 62 |
|
57 | | -### Base File Configuration |
| 63 | +- `baseFile` — buffer of the base Excel file |
| 64 | +- `baseSheetIndex` — 1-based index of the sheet to merge into |
| 65 | +- `additions` — list of files and sheets to merge |
| 66 | +- `gap` — number of empty rows to insert between sections |
| 67 | +- `sheetNamesToRemove` — optional sheet names to remove |
| 68 | +- `sheetsToRemove` — optional sheet indices (1-based) to remove |
58 | 69 |
|
59 | | -- `baseFile`: Buffer containing the base Excel file |
60 | | -- `baseSheetIndex`: 1-based index of the sheet to merge into (default: 1) |
| 70 | +--- |
61 | 71 |
|
62 | | -### Additions Configuration |
| 72 | +## 🧩 Features |
63 | 73 |
|
64 | | -- `additions`: Array of objects specifying files and sheets to merge |
65 | | - - `file`: Buffer containing the source Excel file |
66 | | - - `sheetIndexes`: Array of 1-based sheet indices to merge |
67 | | - - `isBaseFile`: Optional boolean indicating if source is the base file |
| 74 | +### ✅ Merged Cell Support |
68 | 75 |
|
69 | | -### Optional Parameters |
| 76 | +- Preserves existing merged cells |
| 77 | +- Adjusts merge ranges on row shifts |
| 78 | +- Handles overlapping cells gracefully |
70 | 79 |
|
71 | | -- `gap`: Number of empty rows to insert between merged sections (default: 0) |
72 | | -- `sheetNamesToRemove`: Array of sheet names to remove from output |
73 | | -- `sheetsToRemove`: Array of 1-based sheet indices to remove from output |
| 80 | +### 📐 Row Shifting |
74 | 81 |
|
75 | | -## Features |
| 82 | +- Automatically shifts incoming rows |
| 83 | +- Updates row numbers and references |
| 84 | +- Supports configurable row gaps |
76 | 85 |
|
77 | | -### Merged Cells Support |
| 86 | +### 🗂️ Sheet Management |
78 | 87 |
|
79 | | -The merge process automatically handles merged cells by: |
| 88 | +- Removes sheets by name or index |
| 89 | +- Cleans up `workbook.xml`, `workbook.xml.rels`, `[Content_Types].xml` |
| 90 | +- Preserves valid sheet references |
80 | 91 |
|
81 | | -- Preserving existing merged cells in the base sheet |
82 | | -- Adjusting merged cell references when rows are shifted |
83 | | -- Maintaining merge cell count and references |
| 92 | +--- |
84 | 93 |
|
85 | | -### Row Shifting |
| 94 | +## ❌ Errors |
86 | 95 |
|
87 | | -- Rows from source sheets are automatically shifted to maintain proper row numbers |
88 | | -- Cell references are updated to reflect new row positions |
89 | | -- Gaps between sections are properly maintained |
| 96 | +Throws when: |
90 | 97 |
|
91 | | -### Sheet Management |
| 98 | +- `baseFile` or required `sheetIndexes` are missing |
| 99 | +- Sheets in `additions` don’t exist |
| 100 | +- Sheet merges produce invalid or overlapping ranges |
| 101 | +- Sheet names or indices for removal are invalid |
| 102 | +- Input files are corrupt or malformed |
92 | 103 |
|
93 | | -- Can remove specific sheets by name or index |
94 | | -- Updates workbook.xml, workbook.xml.rels, and [Content_Types].xml accordingly |
95 | | -- Maintains proper sheet relationships and references |
| 104 | +--- |
96 | 105 |
|
97 | | -## Error Handling |
| 106 | +## 💡 Best Practices |
98 | 107 |
|
99 | | -The merge process will throw errors for: |
| 108 | +1. Specify `baseSheetIndex` when using multi-sheet base files |
| 109 | +2. Use `gap` for readability in merged output |
| 110 | +3. Clean unused sheets to minimize output size |
| 111 | +4. Validate all inputs prior to merge |
| 112 | +5. Catch and log merge errors during integration |
100 | 113 |
|
101 | | -- Missing base file or sheet |
102 | | -- Invalid sheet indices |
103 | | -- Missing source files or sheets |
104 | | -- Invalid XML structure |
105 | | -- Duplicate merged cells |
106 | | -- Overlapping merge ranges |
| 114 | +--- |
107 | 115 |
|
108 | | -## Best Practices |
| 116 | +## 🧪 Usage Examples |
109 | 117 |
|
110 | | -1. Always specify a valid `baseSheetIndex` if your base file has multiple sheets |
111 | | -2. Use `gap` parameter to improve readability of merged data |
112 | | -3. Remove unnecessary sheets to reduce file size |
113 | | -4. Validate input files before merging |
114 | | -5. Handle errors appropriately in your application |
| 118 | +### Async Merge |
115 | 119 |
|
116 | | -## Example |
| 120 | +```ts |
| 121 | +import { mergeSheetsToBaseFile } from '@js-ak/excel-toolbox'; |
117 | 122 |
|
118 | | -```typescript |
119 | | -// Merge two sheets from source file into base file |
120 | 123 | const result = await mergeSheetsToBaseFile({ |
121 | 124 | baseFile: baseFileBuffer, |
| 125 | + baseSheetIndex: 1, |
122 | 126 | additions: [ |
123 | 127 | { |
124 | 128 | file: sourceFileBuffer, |
125 | | - sheetIndexes: [1, 2] |
| 129 | + sheetIndexes: [1, 2], |
126 | 130 | } |
127 | 131 | ], |
128 | 132 | gap: 1, |
129 | | - sheetsToRemove: [3, 4] // Remove sheets 3 and 4 from output |
| 133 | + sheetsToRemove: [3, 4] |
130 | 134 | }); |
131 | 135 |
|
132 | | -// Save the result |
133 | 136 | await fs.writeFile('merged.xlsx', result); |
134 | 137 | ``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +### Sync Merge |
| 142 | + |
| 143 | +```ts |
| 144 | +import { mergeSheetsToBaseFileSync } from '@js-ak/excel-toolbox'; |
| 145 | + |
| 146 | +const result = mergeSheetsToBaseFileSync({ |
| 147 | + baseFile: baseFileBuffer, |
| 148 | + baseSheetIndex: 1, |
| 149 | + additions: [ |
| 150 | + { |
| 151 | + file: sourceFileBuffer, |
| 152 | + sheetIndexes: [1, 2], |
| 153 | + } |
| 154 | + ], |
| 155 | + gap: 1, |
| 156 | +}); |
| 157 | + |
| 158 | +fs.writeFileSync('merged.xlsx', result); |
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## 🧼 Cleanup & Validation |
| 164 | + |
| 165 | +- Sheet removals affect XML metadata and relationships |
| 166 | +- Output file is fully valid `.xlsx` |
| 167 | +- Sheet relationships are adjusted during merge |
| 168 | + |
| 169 | +--- |
0 commit comments