resolucion de errores
This commit is contained in:
@@ -7,14 +7,14 @@ import { ApiReadEmails } from './excel.decorators';
|
||||
@ApiTags('Excel') // Agrupa los endpoints en Swagger
|
||||
@Controller('excel')
|
||||
export class ExcelController {
|
||||
constructor(private readonly excelService: ExcelService) {}
|
||||
constructor(private readonly excelService: ExcelService) { }
|
||||
|
||||
@Post('read-emails')
|
||||
@UseInterceptors(FileInterceptor('file')) // Permite subir archivos
|
||||
@ApiConsumes('multipart/form-data') // Indica que se usa un formulario con archivos
|
||||
@ApiReadEmails()
|
||||
async getEmails(@UploadedFile() file: Express.Multer.File) {
|
||||
const emails = await this.excelService.readEmailsFromExcel(file);
|
||||
return { emails };
|
||||
}
|
||||
// @Post('read-emails')
|
||||
// @UseInterceptors(FileInterceptor('file')) // Permite subir archivos
|
||||
// @ApiConsumes('multipart/form-data') // Indica que se usa un formulario con archivos
|
||||
// @ApiReadEmails()
|
||||
// async getEmails(@UploadedFile() file: Express.Multer.File) {
|
||||
// const emails = await this.excelService.readEmailsFromExcel(file);
|
||||
// return { emails };
|
||||
// }
|
||||
}
|
||||
|
||||
+27
-27
@@ -4,42 +4,42 @@ import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class ExcelService {
|
||||
async readEmailsFromExcel(file: Express.Multer.File) {
|
||||
|
||||
// async readEmailsFromExcel(file: Express.Multer.File) {
|
||||
|
||||
|
||||
|
||||
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.load(file.buffer); // 🔹 `await` para leer el archivo correctamente
|
||||
|
||||
const worksheet = workbook.getWorksheet(1); // Obtiene la primera hoja
|
||||
if (!worksheet) {
|
||||
throw new Error(' No se encontró ninguna hoja en el archivo.');
|
||||
}
|
||||
|
||||
const jsonData: any[] = [];
|
||||
|
||||
// 🔹 Iterar sobre las filas y extraer datos
|
||||
worksheet.eachRow((row, rowNumber) => {
|
||||
if (rowNumber === 1) return; // Ignorar encabezado
|
||||
// const workbook = new ExcelJS.Workbook();
|
||||
// await workbook.xlsx.load(file.buffer); // 🔹 `await` para leer el archivo correctamente
|
||||
|
||||
const rowData = {
|
||||
Correo: row.getCell(1).value, // Ajusta el índice según tu archivo
|
||||
};
|
||||
// const worksheet = workbook.getWorksheet(1); // Obtiene la primera hoja
|
||||
// if (!worksheet) {
|
||||
// throw new Error(' No se encontró ninguna hoja en el archivo.');
|
||||
// }
|
||||
|
||||
jsonData.push(rowData);
|
||||
});
|
||||
// const jsonData: any[] = [];
|
||||
|
||||
console.log(` Datos extraídos:`, jsonData);
|
||||
// // 🔹 Iterar sobre las filas y extraer datos
|
||||
// worksheet.eachRow((row, rowNumber) => {
|
||||
// if (rowNumber === 1) return; // Ignorar encabezado
|
||||
|
||||
// 🔹 Extraer correos de la columna "Correo"
|
||||
const emails = jsonData
|
||||
.map(row => row.Correo)
|
||||
.filter(email => typeof email === 'string');
|
||||
// const rowData = {
|
||||
// Correo: row.getCell(1).value, // Ajusta el índice según tu archivo
|
||||
// };
|
||||
|
||||
console.log(` Correos extraídos:`, emails);
|
||||
// jsonData.push(rowData);
|
||||
// });
|
||||
|
||||
return emails;
|
||||
}
|
||||
// console.log(` Datos extraídos:`, jsonData);
|
||||
|
||||
// // 🔹 Extraer correos de la columna "Correo"
|
||||
// const emails = jsonData
|
||||
// .map(row => row.Correo)
|
||||
// .filter(email => typeof email === 'string');
|
||||
|
||||
// console.log(` Correos extraídos:`, emails);
|
||||
|
||||
// return emails;
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user