As part of my web app development process, I am utilizing the xlsx
library to import data from an excel file. Each row from the excel sheet is being saved into an object that contains arrays with a length corresponding to the number of cells in each row. The code snippet below demonstrates this process:
onFileChange(event: any) {
const inputFile: DataTransfer = <DataTransfer>(event.target);
const fileReader: FileReader = new FileReader();
fileReader.onload = (event: any) => {
const binaryString: string = event.target.result;
const workBook: XLSX.WorkBook = XLSX.read(binaryString, { type: 'binary', sheetStubs: true});
console.log(typeof binaryString)
const workSheetName: string = workBook.SheetNames[0];
console.log(workSheetName)
const workSheet: XLSX.WorkSheet = workBook.Sheets[workSheetName];
this.data = <Array>(XLSX.utils.sheet_to_json(workSheet, { header: 1, blankrows: true }));
};
fileReader.readAsBinaryString(inputFile.files[0]);
}
In addition to this, I am also working on implementing a function called getFirstMeaningfulRow
. This function aims to return the first row that has a length greater than or equal to 5 elements. To achieve this, I have written the following code using the `find` function:
typescript
getFirstMeaningfulRow() {
console.log(this.data.find(meaningfulRow => this.data.length >= 5
The objective here is to identify the first array within the object that contains more than 5 elements and display it through the console.log
. For example, if the first row imported only has one element like a date (MM/DD/YYYY), the output would be:
["10 SEPTEMBER, 2019"]
0: "10 SEPTEMBER, 2019"
Nevertheless, I am striving to locate the initial array within the object with over 5 elements in it so that the console.log
presents something similar to:
['item0', 'item1', 'item2', 'item3', 'item4',...]
0: 'item0'
1: 'item1'
2: 'item2'
3: 'item3'
4: 'item4'
...