11# TemplateFs
22
3- The ` TemplateFs ` class is designed for working with Excel (` .xlsx ` ) templates. It supports extracting, modifying, and rebuilding Excel files. Typical use cases include placeholder substitution, sheet duplication, and row insertion .
3+ The ` TemplateFs ` class is designed for working with Excel (` .xlsx ` ) templates extracted to the filesystem . It enables modifying templates, inserting rows, substituting placeholders, and saving either as a ` Buffer ` or directly into a writable stream .
44
55> ⚠️ ** Experimental API**
66> Interface is subject to change in future versions.
@@ -13,113 +13,171 @@ The `TemplateFs` class is designed for working with Excel (`.xlsx`) templates. I
1313new TemplateFs (fileKeys : Set < string > , destination : string )
1414```
1515
16- - ` fileKeys ` — a set of relative file paths that make up the Excel template.
17- - ` destination ` — a path to a directory where the template is extracted and edited.
16+ - Input:
17+ - ` fileKeys ` — a set of relative file paths representing the ` .xlsx ` file structure.
18+ - ` destination ` — path to a directory where the template is extracted and modified.
19+ - Output: ` TemplateFs ` instance
20+ - Preconditions: None
21+ - Postconditions: Instance is ready for use with provided files.
1822
1923> Prefer using the static method ` TemplateFs.from() ` to create instances.
2024
2125---
2226
2327## 📄 Properties
2428
25- - ` fileKeys: Set<string> ` — the set of template file paths involved in final assembly .
26- - ` destination: string ` — the working directory where files are extracted and edited .
29+ - ` fileKeys: Set<string> ` — set of template file paths used for rebuilding the ` .xlsx ` .
30+ - ` destination: string ` — working directory for extracted and modified files .
2731- ` destroyed: boolean ` — indicates whether the instance has been destroyed (read-only).
2832
2933---
3034
3135## 📚 Methods
3236
33- ### ` copySheet(sourceName: string, newName: string): Promise<void> `
37+ ### ` copySheet `
3438
3539Creates a copy of an existing worksheet with a new name.
3640
37- - ` sourceName ` — the name of the existing sheet.
38- - ` newName ` — the name for the new sheet.
41+ - Input:
42+ - ` sourceName: string ` — name of the existing sheet.
43+ - ` newName: string ` — name for the new sheet.
44+ - Output: ` Promise<void> `
45+ - Preconditions:
46+ - Instance not destroyed
47+ - ` sourceName ` exists
48+ - ` newName ` does not exist
49+ - Postconditions:
50+ - New sheet created with content from source
51+ - Sheet relationships updated
3952- Throws if:
4053 - ` sourceName ` does not exist.
4154 - ` newName ` already exists.
4255
4356---
4457
45- ### ` substitute(sheetName: string, replacements: Record<string, unknown>): Promise<void> `
58+ ### ` substitute `
4659
47- Replaces placeholders of the form ` ${key} ` with values from the ` replacements ` object. For arrays use placeholders with key ` ${table:key} `
60+ Replaces placeholders of the form ` ${key} ` with values from the ` replacements ` object. For arrays, use placeholders with key ` ${table:key} ` .
4861
49- - ` sheetName ` — the name of the worksheet.
50- - ` replacements ` — key-value map for substitution.
62+ - Input:
63+ - ` sheetName: string ` — name of worksheet
64+ - ` replacements: Record<string, unknown> ` — key-value map for substitution
65+ - Output: ` Promise<void> `
66+ - Preconditions:
67+ - Instance not destroyed
68+ - Sheet exists
69+ - Postconditions:
70+ - Placeholders replaced with values
71+ - Shared strings updated if needed
5172
52- ---
53-
54- ### ` insertRows(data: { sheetName: string; startRowNumber?: number; rows: unknown[][] }): Promise<void> `
73+ ### ` insertRows `
5574
5675Inserts rows into a specified worksheet.
5776
58- - ` sheetName ` — name of the worksheet.
59- - ` startRowNumber ` — starting row index (default: append to the end).
60- - ` rows ` — array of arrays, each representing a row of values.
77+ - Input:
78+ - ` data: { sheetName: string; startRowNumber?: number; rows: unknown[][] } `
79+ - Output: ` Promise<void> `
80+ - Preconditions:
81+ - Instance not destroyed
82+ - Sheet exists
83+ - Row number valid if specified
84+ - Cells within bounds
85+ - Postconditions:
86+ - Rows inserted at specified position
87+ - Sheet data updated
6188- Throws if:
6289 - The sheet does not exist.
6390 - The row number is invalid.
6491 - A cell is out of bounds.
6592
6693---
6794
68- ### ` insertRowsStream(data: { sheetName: string; startRowNumber?: number; rows: AsyncIterable<unknown[]> }): Promise<void> `
95+ ### ` insertRowsStream `
6996
7097Streams and inserts rows into a worksheet, useful for handling large datasets.
7198
72- - ` sheetName ` — name of the worksheet.
73- - ` startRowNumber ` — starting row index (default: append to the end).
74- - ` rows ` — an async iterable where each item is an array of cell values.
99+ - Input:
100+ - ` data: { sheetName: string; startRowNumber?: number; rows: AsyncIterable<unknown[]> } `
101+ - Output: ` Promise<void> `
102+ - Preconditions:
103+ - Instance not destroyed
104+ - Sheet exists
105+ - Row number valid if specified
106+ - Cells within bounds
107+ - Postconditions:
108+ - Rows streamed and inserted
109+ - Sheet data updated
75110- Same error conditions as ` insertRows ` .
76111
77112---
78113
79- ### ` save(): Promise<Buffer> `
114+ ### ` save `
80115
81116Generates a new Excel file and returns it as a ` Buffer ` .
82117
83- - Returns: ` Promise<Buffer> ` — the full ` .xlsx ` file contents in memory.
118+ - Input: None
119+ - Output: ` Promise<Buffer> ` — the full ` .xlsx ` file contents in memory.
120+ - Preconditions:
121+ - Instance not destroyed
122+ - Postconditions:
123+ - Instance marked as destroyed
124+ - Temporary files removed if necessary
125+ - ZIP archive created
84126- Throws if:
85127 - The instance has been destroyed.
86- - There was a failure while rebuilding the ZIP archive.
128+ - Failure occurs while rebuilding the ZIP archive.
87129
88130---
89131
90- ### ` saveStream(output: Writable): Promise<void> `
132+ ### ` saveStream `
91133
92- Writes the resulting Excel file to a writable stream.
134+ Writes the resulting Excel file directly to a writable stream.
93135
94- - ` output ` — any writable stream, e.g. a file or HTTP response.
136+ - Input:
137+ - ` output: Writable ` — target writable stream (e.g., file, HTTP response).
138+ - Output: ` Promise<void> `
139+ - Preconditions:
140+ - Instance not destroyed
141+ - Postconditions:
142+ - Excel file streamed to output
95143- Throws if:
96144 - The instance has been destroyed.
97- - There was a failure during streaming or rebuilding the ZIP archive .
145+ - Streaming or rebuilding fails .
98146
99147---
100148
101- ### ` validate(): Promise<void> `
149+ ### ` validate `
102150
103- Validates the template by checking all required files exist.
151+ Validates the internal state by checking if all required files exist.
104152
105- - Returns: ` Promise<void> `
106- - Throws:
107- - If the template instance has been destroyed.
108- - If any required files are missing.
153+ - Input: None
154+ - Output: ` Promise<void> `
155+ - Preconditions:
156+ - Instance not destroyed
157+ - Postconditions:
158+ - Missing files detected (if any)
159+ - Throws if:
160+ - The instance has been destroyed.
161+ - Any required file is missing.
109162
110163---
111164
112- ### ` set(key: string, content: Buffer | string): Promise<void> `
165+ ### ` set `
113166
114- Replaces the contents of a file in the template.
167+ Replaces the content of a specific file in the template.
115168
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.
169+ - Input:
170+ - ` key: string ` — relative Excel path (e.g., ` xl/worksheets/sheet1.xml ` )
171+ - ` content: Buffer | string ` — new file content
172+ - Output: ` Promise<void> `
173+ - Preconditions:
174+ - Instance not destroyed
175+ - File exists
176+ - Postconditions:
177+ - File content updated
178+ - Throws if:
179+ - The instance has been destroyed.
180+ - The file does not exist.
123181
124182---
125183
@@ -214,5 +272,6 @@ await template.saveStream(outputStream);
214272
215273Methods perform validation:
216274
217- - Ensures the instance hasn't been destroyed.
218- - Prevents concurrent modifications.
275+ - Ensure the instance hasn't been destroyed.
276+ - Prevent concurrent modifications.
277+ - Ensure required files are present before saving.
0 commit comments