UNPKG

@oniryk/xlsx

Version:

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

74 lines (73 loc) 2.62 kB
import type SharedStrings from './shared-strings.js'; /** Represents a cell value in an Excel sheet */ export type Cell = string | number | Date | null; /** Represents a row of cells in an Excel sheet */ export type Row = Cell[]; /** Function type for mapping column properties to XML string */ export type ColMapper = ([k, w]: [number, number]) => string; /** * Represents an Excel worksheet * Handles the creation and management of worksheet data including rows, columns, and cell formatting */ export default class Sheet { /** Map of pre-calculated column names (A-ZZ) */ private static readonly COLUMNS; /** Number of rows to process in each chunk when generating XML */ private static readonly CHUNK_SIZE; /** Storage for worksheet rows */ private rows; /** Reference to shared strings table */ private sharedStrings; /** Temporary file path for the worksheet XML */ private file; /** Map of column indices to their widths */ private columnWidths; /** * Creates a new Sheet instance * @param sharedStrings - SharedStrings instance for managing string deduplication */ constructor(sharedStrings: SharedStrings); /** * Adds a single row to the worksheet * @param row - Array of cell values */ addRow(row: Row): void; /** * Adds multiple rows to the worksheet * @param rows - Array of rows to add */ addRows(rows: Row[]): void; /** * Gets the total number of rows in the worksheet * @returns Number of rows */ rowsCount(): number; /** * Sets the width for a single column or multiple columns * @param index - Column index or array of [index, width] pairs * @param width - Column width (when setting single column) */ setColumWidth(index: number, width: number): void; setColumWidth(sizes: [number, number][]): void; /** * Gets the Excel column name for a given index * @param index - Zero-based column index * @returns Excel column name (e.g., 'A', 'B', 'AA') * @private */ private getExcelColumn; /** * Writes a chunk of rows to the worksheet XML * @param write - Promise-based writer function * @param rows - Array of rows to write * @param startIndex - Starting row index for this chunk * @returns Promise that resolves when chunk is written * @private */ private writeChunk; /** * Generates the complete worksheet XML file * @returns Promise that resolves to the path of the generated XML file */ generateSheetXML(): Promise<string>; }