Skip to content

Commit 6c3692b

Browse files
authored
Merge pull request #33 from JS-AK/feat/template/updates
feat: updated TemplateFs TemplateMemory
2 parents e203b2b + f5ed5cf commit 6c3692b

20 files changed

+923
-130
lines changed

docs/merge-sheets-to-base-file.md

Lines changed: 123 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,169 @@
1-
# Merging Sheets into Base File
1+
# mergeSheetsToBaseFile
22

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.
44

5-
## Overview
5+
> ⚠️ **Experimental API**
6+
> Interface may change in future releases.
67
7-
The merge functionality allows you to:
8+
---
89

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
1311

14-
## Basic Usage
12+
### `mergeSheetsToBaseFile`
1513

16-
### Asynchronous Merge
14+
Asynchronously merges rows from specified sheets of one or more Excel files into a sheet of a base Excel file.
1715

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>
3518
```
3619

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
3827

39-
```typescript
40-
import { mergeSheetsToBaseFileSync } from '@js-ak/excel-toolbox';
28+
---
4129

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
5336
```
5437

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+
```
5662

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
5869

59-
- `baseFile`: Buffer containing the base Excel file
60-
- `baseSheetIndex`: 1-based index of the sheet to merge into (default: 1)
70+
---
6171

62-
### Additions Configuration
72+
## 🧩 Features
6373

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
6875

69-
### Optional Parameters
76+
- Preserves existing merged cells
77+
- Adjusts merge ranges on row shifts
78+
- Handles overlapping cells gracefully
7079

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
7481

75-
## Features
82+
- Automatically shifts incoming rows
83+
- Updates row numbers and references
84+
- Supports configurable row gaps
7685

77-
### Merged Cells Support
86+
### 🗂️ Sheet Management
7887

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
8091

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+
---
8493

85-
### Row Shifting
94+
## ❌ Errors
8695

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:
9097

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
92103

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+
---
96105

97-
## Error Handling
106+
## 💡 Best Practices
98107

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
100113

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+
---
107115

108-
## Best Practices
116+
## 🧪 Usage Examples
109117

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
115119

116-
## Example
120+
```ts
121+
import { mergeSheetsToBaseFile } from '@js-ak/excel-toolbox';
117122

118-
```typescript
119-
// Merge two sheets from source file into base file
120123
const result = await mergeSheetsToBaseFile({
121124
baseFile: baseFileBuffer,
125+
baseSheetIndex: 1,
122126
additions: [
123127
{
124128
file: sourceFileBuffer,
125-
sheetIndexes: [1, 2]
129+
sheetIndexes: [1, 2],
126130
}
127131
],
128132
gap: 1,
129-
sheetsToRemove: [3, 4] // Remove sheets 3 and 4 from output
133+
sheetsToRemove: [3, 4]
130134
});
131135

132-
// Save the result
133136
await fs.writeFile('merged.xlsx', result);
134137
```
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+
---

docs/template-fs.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ Streams and inserts rows into a worksheet, useful for handling large datasets.
115115

116116
---
117117

118+
### `removeSheets`
119+
120+
Removes specified sheets from the workbook by either their names or 1-based indexes.
121+
122+
- Input:
123+
- `data: object` — configuration object
124+
- `sheetNames?: string[]` — array of sheet names to remove
125+
- `sheetIndexes?: number[]` — array of 1-based sheet indexes to remove
126+
- Output: `Promise<void>`
127+
- Preconditions:
128+
- Instance not destroyed
129+
- Sheets exist
130+
- Postconditions:
131+
- Specified sheets removed from workbook
132+
- Workbook relationships updated
133+
- Content types updated
134+
- Throws if:
135+
- The instance has been destroyed
136+
- Any specified sheet does not exist
137+
- Any specified sheet index is invalid
138+
139+
---
140+
118141
### `save`
119142

120143
Generates a new Excel file and returns it as a `Buffer`.

docs/template-memory.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Merges multiple worksheets into a single base worksheet.
162162
- `baseSheetIndex?: number` — 1-based index of the base sheet to merge into (optional, default is 1).
163163
- `baseSheetName?: string` — name of the base sheet to merge into (optional).
164164
- `gap?: number` - number of empty rows to insert between merged sections (default: `0`).
165-
- Output: `void`
165+
- Output: `Promise<void>`
166166
- Preconditions:
167167
- Instance not destroyed
168168
- Valid sheet names/indexes
@@ -185,7 +185,7 @@ Removes worksheets from the workbook.
185185
- Input:
186186
- `sheetNames?: string[]` - names of sheets to remove
187187
- `sheetIndexes?: number[]` - 1-based indexes of sheets to remove
188-
- Output: `void`
188+
- Output: `Promise<void>`
189189
- Preconditions:
190190
- Instance not destroyed
191191
- Sheets exist

0 commit comments

Comments
 (0)