UNPKG

wordxml-builder

Version:

Librería TypeScript para construir documentos XML compatibles con Microsoft Word

151 lines (150 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable no-console */ const table_1 = require("../types/table"); const paragraph_1 = require("../types/paragraph"); const table_2 = require("../builders/table"); // Estilo común para bordes const defaultBorder = { style: 'single', size: 4, color: '000000', space: 1, }; // Estilo para bordes de la tabla const tableBorders = { top: defaultBorder, right: defaultBorder, bottom: defaultBorder, left: defaultBorder, insideH: defaultBorder, insideV: defaultBorder, }; // Estilo para celdas de encabezado superior const headerCellStyle = { alignment: table_1.CellAlignment.Center, borders: tableBorders, shading: { fill: 'EEEEEE', val: 'solid', }, paragraphStyle: { alignment: paragraph_1.ParagraphAlignment.Center, }, }; // Estilo para celdas de encabezado lateral const leftHeaderCellStyle = { alignment: table_1.CellAlignment.Center, borders: tableBorders, shading: { fill: 'DDDDDD', val: 'solid', }, paragraphStyle: { alignment: paragraph_1.ParagraphAlignment.Center, }, }; // Estilo para celdas normales const cellStyle = { alignment: table_1.CellAlignment.Center, borders: tableBorders, paragraphStyle: { alignment: paragraph_1.ParagraphAlignment.Left, }, }; // Estilo común para tablas const tableStyle = { width: 5000, // 5 pulgadas alignment: 'center', borders: tableBorders, cellSpacing: 0, cellPadding: 100, headerCellStyle, leftHeaderCellStyle, cellStyle, }; // Ejemplo 1: Tabla con solo cabecera superior const simpleTable = { headers: [ { content: 'ID', style: headerCellStyle }, { content: 'Nombre', style: headerCellStyle }, { content: 'Edad', style: headerCellStyle }, { content: 'Ciudad', style: headerCellStyle }, ], rows: [ { cells: [ { content: '1', style: cellStyle }, { content: 'Juan Pérez', style: cellStyle }, { content: '25', style: cellStyle }, { content: 'Madrid', style: cellStyle }, ], }, { cells: [ { content: '2', style: cellStyle }, { content: 'María García', style: cellStyle }, { content: '30', style: cellStyle }, { content: 'Barcelona', style: cellStyle }, ], }, { cells: [ { content: '3', style: cellStyle }, { content: 'Carlos López', style: cellStyle }, { content: '22', style: cellStyle }, { content: 'Valencia', style: cellStyle }, ], }, ], style: tableStyle, }; // Ejemplo 2: Tabla con cabeceras superior y lateral const matrixTable = { headers: [ { content: 'Q1', style: headerCellStyle }, { content: 'Q2', style: headerCellStyle }, { content: 'Q3', style: headerCellStyle }, { content: 'Q4', style: headerCellStyle }, ], leftHeaders: [ { content: 'Ventas', style: leftHeaderCellStyle }, { content: 'Gastos', style: leftHeaderCellStyle }, { content: 'Beneficios', style: leftHeaderCellStyle }, ], rows: [ { cells: [ { content: '1000', style: cellStyle }, { content: '1200', style: cellStyle }, { content: '1500', style: cellStyle }, { content: '1800', style: cellStyle }, ], }, { cells: [ { content: '500', style: cellStyle }, { content: '600', style: cellStyle }, { content: '700', style: cellStyle }, { content: '800', style: cellStyle }, ], }, { cells: [ { content: '500', style: cellStyle }, { content: '600', style: cellStyle }, { content: '800', style: cellStyle }, { content: '1000', style: cellStyle }, ], }, ], style: tableStyle, }; // Generar XML para ambas tablas const simpleTableBuilder = new table_2.TableBuilder(simpleTable); const matrixTableBuilder = new table_2.TableBuilder(matrixTable); // Imprimir los XML generados console.log('=== Tabla Simple (solo cabecera superior) ==='); console.log(simpleTableBuilder.toXML()); console.log('\n=== Tabla Matriz (cabeceras superior y lateral) ==='); console.log(matrixTableBuilder.toXML());