Skip to content

Commit add4df2

Browse files
authored
Merge pull request #27 from JS-AK/feat/template/updates
feat: updated TemplateMemory
2 parents a57443f + a7c380c commit add4df2

27 files changed

+1829
-352
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Both classes provide the same API for modifying Excel templates.
3030
- `insertRows()` / `insertRowsStream()` — insert rows statically or via stream
3131
- `copySheet()` — duplicate existing sheets
3232
- `validate()` and `save()` / `saveStream()` — output the result
33+
- `set()` — manually modify or inject internal files in the template
3334

3435
```ts
3536
import { TemplateFs } from "@js-ak/excel-toolbox";

docs/template-fs.md

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ new TemplateFs(fileKeys: Set<string>, destination: string)
2020
2121
---
2222

23-
### 📄 Properties
23+
## 📄 Properties
2424

2525
- `fileKeys: Set<string>` — the set of template file paths involved in final assembly.
2626
- `destination: string` — the working directory where files are extracted and edited.
2727
- `destroyed: boolean` — indicates whether the instance has been destroyed (read-only).
2828

2929
---
3030

31-
### 📚 Methods
31+
## 📚 Methods
3232

33-
#### `copySheet(sourceName: string, newName: string): Promise<void>`
33+
### `copySheet(sourceName: string, newName: string): Promise<void>`
3434

3535
Creates a copy of an existing worksheet with a new name.
3636

@@ -42,7 +42,7 @@ Creates a copy of an existing worksheet with a new name.
4242

4343
---
4444

45-
#### `substitute(sheetName: string, replacements: Record<string, unknown>): Promise<void>`
45+
### `substitute(sheetName: string, replacements: Record<string, unknown>): Promise<void>`
4646

4747
Replaces placeholders of the form `${key}` with values from the `replacements` object. For arrays use placeholders with key `${table:key}`
4848

@@ -51,7 +51,7 @@ Replaces placeholders of the form `${key}` with values from the `replacements` o
5151

5252
---
5353

54-
#### `insertRows(data: { sheetName: string; startRowNumber?: number; rows: unknown[][] }): Promise<void>`
54+
### `insertRows(data: { sheetName: string; startRowNumber?: number; rows: unknown[][] }): Promise<void>`
5555

5656
Inserts rows into a specified worksheet.
5757

@@ -65,7 +65,7 @@ Inserts rows into a specified worksheet.
6565

6666
---
6767

68-
#### `insertRowsStream(data: { sheetName: string; startRowNumber?: number; rows: AsyncIterable<unknown[]> }): Promise<void>`
68+
### `insertRowsStream(data: { sheetName: string; startRowNumber?: number; rows: AsyncIterable<unknown[]> }): Promise<void>`
6969

7070
Streams and inserts rows into a worksheet, useful for handling large datasets.
7171

@@ -76,18 +76,7 @@ Streams and inserts rows into a worksheet, useful for handling large datasets.
7676

7777
---
7878

79-
#### `validate(): Promise<void>`
80-
81-
Validates the template by checking all required files exist.
82-
83-
- Returns: `Promise<void>`
84-
- Throws:
85-
- If the template instance has been destroyed.
86-
- If any required files are missing.
87-
88-
---
89-
90-
#### `save(): Promise<Buffer>`
79+
### `save(): Promise<Buffer>`
9180

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

@@ -98,7 +87,7 @@ Generates a new Excel file and returns it as a `Buffer`.
9887

9988
---
10089

101-
#### `saveStream(output: Writable): Promise<void>`
90+
### `saveStream(output: Writable): Promise<void>`
10291

10392
Writes the resulting Excel file to a writable stream.
10493

@@ -109,9 +98,34 @@ Writes the resulting Excel file to a writable stream.
10998

11099
---
111100

112-
### 💡 Usage Examples
101+
### `validate(): Promise<void>`
102+
103+
Validates the template by checking all required files exist.
104+
105+
- Returns: `Promise<void>`
106+
- Throws:
107+
- If the template instance has been destroyed.
108+
- If any required files are missing.
109+
110+
---
111+
112+
### `set(key: string, content: Buffer | string): Promise<void>`
113+
114+
Replaces the contents of a file in the template.
115+
116+
- `key` — the relative path of the file within the Excel package (e.g., `xl/worksheets/sheet1.xml`).
117+
- `content` — the new file content as a `Buffer` or `string`.
118+
119+
- Returns: `Promise<void>`
120+
- Throws:
121+
- If the template instance has been destroyed.
122+
- If the file does not exist in the template.
123+
124+
---
125+
126+
## 💡 Usage Examples
113127

114-
#### Insert Rows from a Stream and Save to File Stream
128+
### Insert Rows from a Stream and Save to File Stream
115129

116130
```ts
117131
async function* asyncRowsGenerator(count: number): AsyncIterable<unknown[]> {
@@ -141,7 +155,7 @@ const outputStream = fs.createWriteStream(
141155
await template.saveStream(outputStream);
142156
```
143157

144-
#### Insert Rows from a Stream and Save to Buffer
158+
### Insert Rows from a Stream and Save to Buffer
145159

146160
```ts
147161
const template = await TemplateFs.from({
@@ -164,7 +178,7 @@ fs.writeFileSync(
164178
);
165179
```
166180

167-
#### Copy Sheet and Apply Substitutions
181+
### Copy Sheet and Apply Substitutions
168182

169183
```ts
170184
const template = await TemplateFs.from({
@@ -196,7 +210,7 @@ await template.saveStream(outputStream);
196210

197211
---
198212

199-
### 🛑 Internal Checks
213+
## 🛑 Internal Checks
200214

201215
Methods perform validation:
202216

docs/template-memory.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ new TemplateMemory(files: Record<string, Buffer>)
1919
2020
---
2121

22-
### 📄 Properties
22+
## 📄 Properties
2323

2424
- `files: Record<string, Buffer>` — in-memory map of file contents.
2525
- `destroyed: boolean` — indicates whether the instance has been destroyed (read-only).
2626

2727
---
2828

29-
### 📚 Methods
29+
## 📚 Methods
3030

31-
#### `copySheet(sourceName: string, newName: string): Promise<void>`
31+
### `copySheet(sourceName: string, newName: string): Promise<void>`
3232

3333
Creates a copy of an existing worksheet with a new name.
3434

@@ -40,7 +40,7 @@ Creates a copy of an existing worksheet with a new name.
4040

4141
---
4242

43-
#### `substitute(sheetName: string, replacements: Record<string, unknown>): Promise<void>`
43+
### `substitute(sheetName: string, replacements: Record<string, unknown>): Promise<void>`
4444

4545
Replaces placeholders of the form `${key}` with values from the `replacements` object. For arrays, use placeholders with key `${table:key}`.
4646

@@ -49,7 +49,7 @@ Replaces placeholders of the form `${key}` with values from the `replacements` o
4949

5050
---
5151

52-
#### `insertRows(data: { sheetName: string; startRowNumber?: number; rows: unknown[][] }): Promise<void>`
52+
### `insertRows(data: { sheetName: string; startRowNumber?: number; rows: unknown[][] }): Promise<void>`
5353

5454
Inserts rows into a specified worksheet.
5555

@@ -63,7 +63,7 @@ Inserts rows into a specified worksheet.
6363

6464
---
6565

66-
#### `insertRowsStream(data: { sheetName: string; startRowNumber?: number; rows: AsyncIterable<unknown[]> }): Promise<void>`
66+
### `insertRowsStream(data: { sheetName: string; startRowNumber?: number; rows: AsyncIterable<unknown[]> }): Promise<void>`
6767

6868
Streams and inserts rows into a worksheet, useful for handling large datasets.
6969

@@ -74,7 +74,7 @@ Streams and inserts rows into a worksheet, useful for handling large datasets.
7474

7575
---
7676

77-
#### `save(): Promise<Buffer>`
77+
### `save(): Promise<Buffer>`
7878

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

@@ -85,7 +85,7 @@ Generates a new Excel file and returns it as a `Buffer`.
8585

8686
---
8787

88-
#### `set(key: string, content: Buffer): Promise<void>`
88+
### `set(key: string, content: Buffer): Promise<void>`
8989

9090
Replaces the content of a specific file in the template.
9191

@@ -97,9 +97,39 @@ Replaces the content of a specific file in the template.
9797

9898
---
9999

100-
### 💡 Usage Examples
100+
### `mergeSheets(data: { additions: { sheetIndexes?: number[]; sheetNames?: string[] }; baseSheetIndex?: number; baseSheetName?: string; gap?: number }): void`
101101

102-
#### Create from File and Modify
102+
Merges multiple worksheets into a single base worksheet.
103+
104+
- `additions` — defines the sheets to merge:
105+
- `sheetIndexes` — array of 1-based sheet indexes to merge.
106+
- `sheetNames` — array of sheet names to merge.
107+
- `baseSheetIndex` — 1-based index of the base sheet to merge into (optional, default is 1).
108+
- `baseSheetName` — name of the base sheet to merge into (optional).
109+
- `gap` — number of empty rows to insert between merged sections (default: `0`).
110+
- Throws if:
111+
- The instance is destroyed.
112+
- Invalid sheet names or indexes are provided.
113+
- Both `baseSheetIndex` and `baseSheetName` are undefined.
114+
115+
---
116+
117+
### `removeSheets(data: { sheetNames?: string[]; sheetIndexes?: number[] }): void`
118+
119+
Removes worksheets from the workbook.
120+
121+
- `sheetNames` — array of sheet names to remove.
122+
- `sheetIndexes` — array of 1-based sheet indexes to remove.
123+
- Throws if:
124+
- The instance is destroyed.
125+
- Sheet names or indexes do not exist.
126+
- Neither `sheetNames` nor `sheetIndexes` are provided.
127+
128+
---
129+
130+
## 💡 Usage Examples
131+
132+
### Create from File and Modify
103133

104134
```ts
105135
const template = await TemplateMemory.from({
@@ -113,7 +143,7 @@ const modifiedExcel = await template.save();
113143
fs.writeFileSync("output.xlsx", modifiedExcel);
114144
```
115145

116-
#### Insert Rows from a Stream
146+
### Insert Rows from a Stream
117147

118148
```ts
119149
async function* generateData() {
@@ -134,7 +164,7 @@ await template.insertRowsStream({
134164
fs.writeFileSync("large-output.xlsx", await template.save());
135165
```
136166

137-
#### Replace Internal File Content
167+
### Replace Internal File Content
138168

139169
```ts
140170
const template = await TemplateMemory.from({
@@ -150,7 +180,7 @@ await template.set(
150180

151181
---
152182

153-
### 🛑 Internal Checks
183+
## 🛑 Internal Checks
154184

155185
Methods perform validation:
156186

0 commit comments

Comments
 (0)