onFileChange(event: any) { debugger; this.spinner.show(); this.toastr.clear(); const…
- User
onFileChange(event: any) { debugger; this.spinner.show(); this.toastr.clear(); const target: DataTransfer = event.target; if (target.files.length !== 1) { throw new Error('Cannot use multiple files'); } const reader: FileReader = new FileReader(); reader.readAsBinaryString(target.files[0]); this.fileName = target.files[0].name; reader.onload = (e: any) => { const binarystr: string = e.target.result; const wb: XLSX.WorkBook = XLSX.read(binarystr, { type: 'binary' }); const wsname: string = wb.SheetNames[0]; const ws: XLSX.WorkSheet = wb.Sheets[wsname]; const data = XLSX.utils.sheet_to_json(ws); this.storeData = data; if(this.storeData.length<=1){ this.toastr.error('There is missing data in the sheet!', 'Failed'); this.storeData = []; this.fileName = ''; } else{ this.storeData.slice(1).map((e: any) => { if (!e.storeId || !e.From) { this.toastr.error('There is missing data in the sheet!', 'Failed'); this.storeData = []; this.fileName = ''; }
}); } this.spinner.hide();}; } now i use this function to upload my excel sheet, the excel sheet act as it has two headers one contains storeId and weekday and each weekday has 2 columns using merge center and the second header contains under each day from and to , now i want to check that each row has store id and weekday with its corresponding from and to have data
- Assistant
To check that each row has storeId and weekday with its corresponding from and to have data, you can modify the existing code as follows:
onFileChange(event: any) { debugger; this.spinner.show(); this.toastr.clear(); const target: DataTransfer = <DataTransfer>event.target; if (target.files.length !== 1) { throw new Error('Cannot use multiple files'); } const reader: FileReader = new FileReader(); reader.readAsBinaryString(target.files[0]); this.fileName = target.files[0].name; reader.onload = (e: any) => { const binarystr: string = e.target.result; const wb: XLSX.WorkBook = XLSX.read(binarystr, { type: 'binary' }); const wsname: string = wb.SheetNames[0]; const ws: XLSX.WorkSheet = wb.Sheets[wsname]; const data = XLSX.utils.sheet_to_json(ws); this.storeData = data; let validData = true; if (this.storeData.length <= 1) { this.toastr.error('There is missing data in the sheet!', 'Failed'); this.storeData = []; this.fileName = ''; validData = false; } else { this.storeData.slice(1).map((e: any, index: number) => { if (!e.storeId || !e.weekday || !e.From || !e.To) { this.toastr.error(`Row ${index + 2} has missing data in the sheet!`, 'Failed'); this.storeData = []; this.fileName = ''; validData = false; } }); } if (validData) { // Process the valid data } this.spinner.hide(); }; } - User
i listed Saturday, Sunday, Monday and so on as header like storeid
- Assistant
If you have the headers listed as days of the week (Saturday, Sunday, Monday, etc.) instead of 'weekday', you can modify the code to check for the corresponding day's 'From' and 'To' values for each row. Here's an updated version of the code:
- User
you can't understand me now i have excel sheet this excel sheet contains of 8 headers one for storeId and 7 others foe week days saturday , sunday , and so on each week day take two columns (example saturday take B,C) and in second row each day has From and to, user will have to fill this excel sheet and when he upload i must check that ally fields are filled how make this
- Assistant
I apologize for the confusion. Based on your description, it seems like you have a setup where each day of the week (Saturday, Sunday, etc.) is represented by two columns (From and To) under the respective day's header.
To check that all fields are filled in this structure, you can modify the code as follows:
- User
didn't work it store data as { "storeId": 125654, "Saturday": "9AM", "__EMPTY": "10AM" } and i want the data read from excel as { "storeId": 125654, "From": "9AM", "To": "10AM" }
- Assistant