1
Guys, I’m having a question, I’d like to read an Excel spreadsheet, store your content in a variable and then display it on the screen. However the method that reads the file -- parseExcel -- occurs asynchronously, so the program follows the stream and I can’t access the variable with the file text. Below the function reading the file, the function I want to store the information and output on the console.
async parseExcel() {
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
var workbook = XLSX.read(data, {
type: 'binary'
});
var dadosToJson = [];
workbook.SheetNames.forEach(function(sheetName) {
// Here is your object
var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
var json_object = JSON.stringify(XL_row_object);
dadosToJson.push(json_object);
//console.log(json_object);
})
console.log(dadosToJson)
return dadosToJson;
}
reader.onerror = function(ex) {
console.log(ex);
};
reader.readAsBinaryString(this.file)
};
lerAquivoJson(){
var texto = this.parseExcel()
texto.then( (res) => console.log(res))
}
exit lerArquivoJson(): Undefined
async parseExcel(): ["[{"Word id":"3","Words ":"Birthday","Signifi... ,"Insertion date":"1/16/21"},{"Words ":"3647"}]", "[]"]
Note that the Filefilefilefile method does not wait for parseExcel
Edit: I found a solution:
class Leitorarquivoxlsx{
//no momento só aceita um único arquivo
constructor(file){
this.file=file;
this.idTable = "vocabularioView";
this.data;
}
async parseExcel() {
return new Promise((resolve, reject) => {
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
var workbook = XLSX.read(data, {
type: 'binary'
});
var dadosToJson = [];
workbook.SheetNames.forEach(function (sheetName) {
// Here is your object
var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
var json_object = JSON.stringify(XL_row_object);
dadosToJson.push(json_object);
})
resolve(dadosToJson);
}
reader.onerror = function (ex) {
reader.abort();
reject(console.warn(ex));
};
reader.readAsBinaryString(this.file)
});
};
async xlsxToJson(){
try {
const fileContents = await this.parseExcel();
this.data = fileContents;
} catch (e) {
console.warn(e.message)
}finally {
this.imprimeData();
}
}
imprimeData(){
jQuery('#xlx_json').val("fileContents: "+this.data);
}
}
Hi Bob, a strange thing happened, because he gave the following answer: I had these two errors: Readquivo.js:48 Uncaught (in Promise) Rangeerror: Maximum call stack size exceeded at Leitorarquivo.parseExcel (Leitorarquivo.js:12)
 at LeitorArquivo.lerArquivoJson (LeitorArquivo.js:46)
 at LeitorArquivo.parseExcel (LeitorArquivo.js:15)


 
(3950) LeitorArquivo.js:47 Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
 at LeitorArquivo.lerArquivoJson (Reader.js:47)
– Jean Carlos
Hello, sorry for the delay...apparently he waited too long for the answer. send the full condigo for me to see.
– Bruno Bafilli
Hello Bruno, I apologize for the delay, I ran out of internet these days. Anyway I appreciate your help and I say I managed to solve! I followed the proposal of this article: https://blog.shovonhasan.com/using-promises-with-filereader/ and made the parseExcel function return a promisse, and then called it co Try-catch-finnaly
– Jean Carlos