UNPKG

@oniryk/xlsx

Version:

A lightweight, efficient TypeScript library for generating single-sheet Excel XLSX files with support for large datasets

104 lines (103 loc) 3.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Package = void 0; const adm_zip_1 = __importDefault(require("adm-zip")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const base_rels_js_1 = __importDefault(require("./templates/base-rels.js")); const content_type_js_1 = __importDefault(require("./templates/content-type.js")); const styles_js_1 = __importDefault(require("./templates/styles.js")); const workbook_js_1 = require("./templates/workbook.js"); /** * Manages the creation of Excel XLSX packages * Handles the assembly of various XML components into a final ZIP archive * following the Office Open XML SpreadsheetML format */ class Package { /** * Creates a new Package instance * @param sheet - Sheet instance containing worksheet data * @param sharedStrings - SharedStrings instance for text content management */ constructor(sheet, sharedStrings) { /** List of temporary files that need cleanup */ this.disposable = []; this.zip = new adm_zip_1.default(); this.sharedStrings = sharedStrings; this.sheet = sheet; } /** * Adds string content to the ZIP archive * @param path - Path within the ZIP archive * @param content - String content to add * @private */ add(path, content) { this.zip.addFile(path, Buffer.from(content, 'utf8')); } /** * Adds a local file to the ZIP archive * Tracks the file for later cleanup * @param file - Target path within the ZIP archive * @param localpath - Local filesystem path of the file to add * @private */ addFile(file, localpath) { const zipName = path_1.default.basename(file); const zipPath = path_1.default.dirname(file); this.zip.addLocalFile(localpath, zipPath, zipName); this.disposable.push(localpath); } /** * Adds required Excel template files to the ZIP archive * Includes relationships, workbook, styles, and content type definitions * @private */ addMockFiles() { this.add('_rels/.rels', (0, base_rels_js_1.default)()); this.add('xl/workbook.xml', (0, workbook_js_1.getWorkbookXML)()); this.add('xl/_rels/workbook.xml.rels', (0, workbook_js_1.getWorkbookRels)()); this.add('xl/styles.xml', (0, styles_js_1.default)()); this.add('[Content_Types].xml', (0, content_type_js_1.default)()); } /** * Builds the final XLSX package * Assembles all components, generates required XML files, * and creates the ZIP archive * @returns Promise that resolves to the XLSX file as a Buffer */ async build() { this.addMockFiles(); const file = await this.sheet.generateSheetXML(); this.addFile('xl/worksheets/sheet1.xml', file); if (this.sharedStrings.size() > 0) { const content = await this.sharedStrings.generateSharedStringsXML(); this.addFile('xl/sharedStrings.xml', content); } const buffer = this.zip.toBuffer(); this.dispose(); return buffer; } /** * @deprecated Use build() instead * Legacy method for package creation * @returns Promise that resolves to the XLSX file as a Buffer */ async pack() { console.warn('deprecated: use build() instead'); return await this.build(); } /** * Cleans up temporary files created during package assembly * @private */ dispose() { for (const file of this.disposable) { fs_1.default.unlink(file, () => { }); } } } exports.Package = Package;