When importing an excel file into Angular using the xlsx
library, the file is converted into an object of arrays. Each array represents a row from the excel file, and each item within the array corresponds to a cell in that row. The process for importing the excel file is as follows:
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});
const workSheetName: string = workBook.SheetNames[0];
const workSheet: XLSX.WorkSheet = workBook.Sheets[workSheetName];
this.data = <Array>(XLSX.utils.sheet_to_json(workSheet,
{header: 1, blankrows: true }));
};
A Regex search is then used to find the column containing a manufacturer description by looping through each array and looking for the term cap
:
getManufacturerDescriptionColumn()
{
console.log(this.data)
for (const row in this.data)
{
var manDescriptIndex = row.search(/cap/i)
console.log(manDescriptIndex)
if (manDescriptIndex > -1)
{
return manDescriptIndex
}
}
}
Despite the clear presence of the phrase "cap" in some arrays, all values returned are -1
, indicating that the phrase is not found. See the example below where CAP appears in several rows but still returns -1 values.
https://i.sstatic.net/I2z2w.png
This issue persists even when attempting to iterate at a deeper level to isolate the individual cells' strings within the rows.