-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Excel.js
57 lines (51 loc) · 1.92 KB
/
Excel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const ExcelJS = require('exceljs');
const fs = require('fs')
const workbook = new ExcelJS.Workbook();
const guardaXLSDatos = async (nombre, edad, sexo) => {
// read from a file
await workbook.xlsx.readFile('./bot.xlsx');
// fetch sheet by name
const worksheet = workbook.getWorksheet('Bot');
const rowValues = [];
rowValues[1] = nombre;
rowValues[2] = edad;
rowValues[3] = sexo;
worksheet.addRow(rowValues);
await workbook.xlsx.writeFile('./bot.xlsx');
console.log(rowValues)
console.log("Guardamos XLS")
}
const leeXLSDatos = async (srchStr) => {
// read from a file
await workbook.xlsx.readFile('./bot.xlsx');
// fetch sheet by name
const worksheet = workbook.getWorksheet('Bot');
console.log(worksheet.rowCount)
let colNombre = worksheet.getColumn(1).values
let cont = 0
let encontrado = 0
let row
let res = []
// while (cont <= worksheet.rowCount && encontrado == 0) { // Ocupamos while en lugar de forEach para que deje de buscar en cuanto encuentre el resultado.
// console.log(cont, colNombre[cont], srchStr)
// if(colNombre[cont] === srchStr) {
// row = worksheet.getRow(cont);
// res['nombre'] = row.getCell(1).value
// res['edad'] = row.getCell(2).value
// res['sexo'] = row.getCell(3).value
// encontrado = colNombre[cont]
// }
// cont++;
// }
// console.log("RES=", res)
// for (let index = 0; index < worksheet.rowCount; index++) {
// }
let rows = []
worksheet.eachRow(function(row, rowNumber) {
// console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
rows[rowNumber-1]={'nombre':row.getCell(1).value, 'carnet':row.getCell(4).value, 'factura':row.getCell(5).value, 'prefijo':row.getCell(6).value}
});
// console.log(rows)
return rows
}
module.exports = {guardaXLSDatos, leeXLSDatos};