convertir-formato-asistencia 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/index.js +175 -0
- package/package.json +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 José Aljuria
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Convertir Formato de Asistencia
|
|
2
|
+
|
|
3
|
+
Herramienta simple en Node.js para convertir registros de asistencia (XLS/XLSX) a un reporte de asistencia en formato Excel más legible.
|
|
4
|
+
|
|
5
|
+
Características principales
|
|
6
|
+
- Lee un archivo de entrada con registros de asistencia (columna `Fecha/Hora`).
|
|
7
|
+
- Agrupa por usuario y día, determina hora de entrada y salida.
|
|
8
|
+
- Marca días de fin de semana y faltas.
|
|
9
|
+
- Genera un archivo Excel `.xlsx` con columnas claras: ID, Nombre, Fecha, Entrada, Salida, Departamento, Estado.
|
|
10
|
+
|
|
11
|
+
Requisitos
|
|
12
|
+
- Node.js 16+ instalado.
|
|
13
|
+
- Recomendado: `pnpm` (se incluye `packageManager`), también funciona con `npm`.
|
|
14
|
+
|
|
15
|
+
Instalación
|
|
16
|
+
1. Clona o descarga el repositorio.
|
|
17
|
+
2. Desde la carpeta del proyecto instala dependencias:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm install
|
|
21
|
+
# o, si usas npm:
|
|
22
|
+
npm install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Uso
|
|
26
|
+
1. Formato de entrada esperado:
|
|
27
|
+
- Archivo Excel (`.xls` / `.xlsx`) cuya primera hoja contiene registros con, como mínimo, las columnas:
|
|
28
|
+
- `ID de usuario`
|
|
29
|
+
- `Nombre`
|
|
30
|
+
- `Departamento`
|
|
31
|
+
- `Fecha/Hora` (valor de fecha/hora reconocible por Excel)
|
|
32
|
+
|
|
33
|
+
2. Ejecuta el script indicando ruta de entrada y ruta de salida:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
node index.js <ruta_entrada> <ruta_salida>
|
|
37
|
+
# Ejemplo:
|
|
38
|
+
node index.js ./asistencia.xls ./reporte_abril.xlsx
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Salida
|
|
42
|
+
- Se genera un archivo `.xlsx` con una hoja llamada `Asistencia` y las columnas:
|
|
43
|
+
`ID`, `Nombre`, `Fecha`, `Entrada`, `Salida`, `Departamento`, `Estado`.
|
|
44
|
+
- El estado puede ser `PRESENTE`, `FALTÓ`, `FIN DE SEMANA` o `TRABAJÓ FIN DE SEMANA`.
|
|
45
|
+
|
|
46
|
+
Notas y consejos
|
|
47
|
+
- Si no pasas argumentos, el script imprimirá el uso correcto.
|
|
48
|
+
- El script determina la hora de entrada como la primera marca del día y la hora de salida como la última.
|
|
49
|
+
- Asegúrate de que la columna `Fecha/Hora` esté en un formato de fecha válido en el archivo de Excel para que la librería pueda parsearla correctamente.
|
|
50
|
+
|
|
51
|
+
Contribuciones
|
|
52
|
+
- Si quieres mejorar la herramienta, abre un issue o un pull request con cambios concretos.
|
|
53
|
+
|
|
54
|
+
Licencia
|
|
55
|
+
- Revisa el archivo `LICENSE` incluido en el repositorio.
|
package/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
const ExcelJS = require('exceljs');
|
|
2
|
+
const path = require('node:path');
|
|
3
|
+
const xlsx = require('xlsx');
|
|
4
|
+
const fs = require('node:fs')
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
// --- FUNCIONES DE AYUDA ---
|
|
8
|
+
const formatearFecha = (f) => {
|
|
9
|
+
const dd = String(f.getDate()).padStart(2, '0');
|
|
10
|
+
const mm = String(f.getMonth() + 1).padStart(2, '0');
|
|
11
|
+
const yy = String(f.getFullYear()).slice(-2);
|
|
12
|
+
return `${dd}/${mm}/${yy}`;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const generarCalendario = (data) =>{
|
|
16
|
+
// --- PROCESAMIENTO DE FECHAS ---
|
|
17
|
+
const fechas = data.map(reg => new Date(reg['Fecha/Hora']));
|
|
18
|
+
|
|
19
|
+
const minFecha = new Date(Math.min(...fechas));
|
|
20
|
+
const maxFecha = new Date(Math.max(...fechas));
|
|
21
|
+
minFecha.setHours(0,0,0,0);
|
|
22
|
+
maxFecha.setHours(0,0,0,0);
|
|
23
|
+
|
|
24
|
+
let fechaAux = new Date(minFecha);
|
|
25
|
+
const calendario = [];
|
|
26
|
+
|
|
27
|
+
while (fechaAux <= maxFecha) {
|
|
28
|
+
calendario.push(formatearFecha(fechaAux));
|
|
29
|
+
fechaAux.setDate(fechaAux.getDate() + 1);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return calendario;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// --- AGRUPAR TRABAJADORES ---
|
|
36
|
+
const obtenerTrabajadores = (data) =>{
|
|
37
|
+
return data.reduce((acc, reg) => {
|
|
38
|
+
if (!acc.find(t => t.id === reg["ID de usuario"])) {
|
|
39
|
+
acc.push({ id: reg["ID de usuario"], nombre: reg["Nombre"], depto: reg["Departamento"] });
|
|
40
|
+
}
|
|
41
|
+
return acc;
|
|
42
|
+
}, []);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// -- MAPEA LOS REGISTROS PARA SU FACIL ACCESO
|
|
46
|
+
const generarMapaAsistencia = (data) =>{
|
|
47
|
+
const mapaAsistencia = {};
|
|
48
|
+
data.forEach(reg => {
|
|
49
|
+
const fLimpia = formatearFecha(new Date(reg["Fecha/Hora"]));
|
|
50
|
+
const llave = `${reg["ID de usuario"]}-${fLimpia}`;
|
|
51
|
+
if (!mapaAsistencia[llave]) mapaAsistencia[llave] = [];
|
|
52
|
+
mapaAsistencia[llave].push(reg);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return mapaAsistencia
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function generarReporte(rutaEntrada, rutaSalida) {
|
|
59
|
+
// Validar que se pasaron los argumentos necesarios
|
|
60
|
+
if (!rutaEntrada || !rutaSalida) {
|
|
61
|
+
console.error("❌ ERROR: Faltan argumentos.");
|
|
62
|
+
console.log("Uso correcto: node index.js <ruta_entrada> <ruta_salida>");
|
|
63
|
+
console.log('Ejemplo: node index.js ./asistencia.xls ./reporte_abril.xlsx');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Validar si el archivo de entrada realmente existe
|
|
68
|
+
if (!fs.existsSync(rutaEntrada)) {
|
|
69
|
+
console.error(`❌ ERROR: El archivo de entrada no existe en: ${rutaEntrada}`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
console.log(`🚀 Procesando: ${path.basename(rutaEntrada)}...`);
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
// 1. LEER DATOS
|
|
78
|
+
const book = xlsx.readFile(rutaEntrada, { cellDates: true });
|
|
79
|
+
const data = xlsx.utils.sheet_to_json(book.Sheets[book.SheetNames[0]]);
|
|
80
|
+
|
|
81
|
+
if (data.length === 0) {
|
|
82
|
+
throw new Error("El archivo de entrada está vacío o no se leyó correctamente.");
|
|
83
|
+
}
|
|
84
|
+
console.log(`✅ Leídos ${data.length} registros del archivo original.`);
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
const calendario = generarCalendario(data)
|
|
88
|
+
const trabajadores = obtenerTrabajadores(data)
|
|
89
|
+
const mapaAsistencia = generarMapaAsistencia(data)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
// 2. CONFIGURAR EXCELJS
|
|
93
|
+
const workbook = new ExcelJS.Workbook();
|
|
94
|
+
const worksheet = workbook.addWorksheet('Asistencia');
|
|
95
|
+
|
|
96
|
+
worksheet.columns = [
|
|
97
|
+
{ header: 'ID', key: 'id', width: 10 },
|
|
98
|
+
{ header: 'Nombre', key: 'nombre', width: 30 },
|
|
99
|
+
{ header: 'Fecha', key: 'fecha', width: 15 },
|
|
100
|
+
{ header: 'Entrada', key: 'entrada', width: 12 },
|
|
101
|
+
{ header: 'Salida', key: 'salida', width: 12 },
|
|
102
|
+
{ header: 'Departamento', key: 'depto', width: 25 },
|
|
103
|
+
{ header: 'Estado', key: 'estado', width: 25 }
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
// 3. GENERAR FILAS
|
|
107
|
+
console.log("⏳ Generando filas y aplicando lógica de entrada/salida...");
|
|
108
|
+
trabajadores.forEach(trabajador => {
|
|
109
|
+
calendario.forEach(dia => {
|
|
110
|
+
const llave = `${trabajador.id}-${dia}`;
|
|
111
|
+
const registros = mapaAsistencia[llave];
|
|
112
|
+
|
|
113
|
+
const [d, m, y] = dia.split('/');
|
|
114
|
+
const fEval = new Date(2000 + parseInt(y), parseInt(m)-1, parseInt(d));
|
|
115
|
+
const esFinde = (fEval.getDay() === 0 || fEval.getDay() === 6);
|
|
116
|
+
|
|
117
|
+
let rowData;
|
|
118
|
+
if (registros && registros.length > 0) {
|
|
119
|
+
registros.sort((a, b) => new Date(a["Fecha/Hora"]) - new Date(b["Fecha/Hora"]));
|
|
120
|
+
const hEntrada = new Date(registros[0]["Fecha/Hora"]).toLocaleTimeString('es-VE', {hour:'2-digit', minute:'2-digit', hour12:false});
|
|
121
|
+
const hSalida = registros.length > 1
|
|
122
|
+
? new Date(registros[registros.length-1]["Fecha/Hora"]).toLocaleTimeString('es-VE', {hour:'2-digit', minute:'2-digit', hour12:false})
|
|
123
|
+
: "--:--";
|
|
124
|
+
|
|
125
|
+
rowData = {
|
|
126
|
+
id: trabajador.id, nombre: trabajador.nombre, fecha: dia,
|
|
127
|
+
entrada: hEntrada, salida: hSalida, depto: trabajador.depto,
|
|
128
|
+
estado: esFinde ? 'TRABAJÓ FIN DE SEMANA' : 'PRESENTE'
|
|
129
|
+
};
|
|
130
|
+
} else {
|
|
131
|
+
rowData = {
|
|
132
|
+
id: trabajador.id, nombre: trabajador.nombre, fecha: dia,
|
|
133
|
+
entrada: '--:--', salida: '--:--', depto: trabajador.depto,
|
|
134
|
+
estado: esFinde ? 'FIN DE SEMANA' : 'FALTÓ'
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const row = worksheet.addRow(rowData);
|
|
139
|
+
// Color rojo si faltó
|
|
140
|
+
if (rowData.estado === 'FALTÓ') {
|
|
141
|
+
row.getCell('estado').font = { color: { argb: 'FFFF0000' }, bold: true };
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// 4. FORMATO DE ENCABEZADO
|
|
147
|
+
worksheet.getRow(1).font = { bold: true, color: { argb: 'FFFFFFFF' } };
|
|
148
|
+
worksheet.getRow(1).fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF4472C4' } };
|
|
149
|
+
|
|
150
|
+
// 5. GUARDAR (CON AWAIT)
|
|
151
|
+
console.log(`💾 Intentando guardar archivo en: ${rutaSalida}`);
|
|
152
|
+
await workbook.xlsx.writeFile(rutaSalida);
|
|
153
|
+
|
|
154
|
+
console.log("✨ ¡PROCESO COMPLETADO! El archivo se generó correctamente.");
|
|
155
|
+
|
|
156
|
+
} catch (error) {
|
|
157
|
+
console.error("❌ ERROR CRÍTICO:", error.message);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (require.main === module) {
|
|
162
|
+
// 1. Capturar argumentos de la terminal
|
|
163
|
+
// process.argv[0] es 'node', process.argv[1] es el script.
|
|
164
|
+
// El [2] es el input y el [3] es el output.
|
|
165
|
+
const rutaEntrada = process.argv[2];
|
|
166
|
+
const rutaSalida = process.argv[3];
|
|
167
|
+
|
|
168
|
+
(async () => {
|
|
169
|
+
await generarReporte(rutaEntrada, rutaSalida);
|
|
170
|
+
})();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
module.exports = {
|
|
174
|
+
generarReporte
|
|
175
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "convertir-formato-asistencia",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"packageManager": "pnpm@10.26.2",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"exceljs": "^4.4.0",
|
|
15
|
+
"xlsx": "^0.18.5"
|
|
16
|
+
}
|
|
17
|
+
}
|