Hi Team,
I am totally new to angular. I have angular UI where we upload a .CSV file with 6 columns and their respective data, so before I invoke .net API, I need to change. If the 1st column has data with 9 digits, then we need to change it to 10 digits just by prefixing ‘0’; for example, If the Excel 1st (AccountNumber)column data is ‘217614234’, this number has to be changed to ‘0217614234’, after the changed entire file content I need pass to API.
Here is my working Code for uploaded data that is being passed to API. Now, I need to add a condition to change the number to 10 digits. If I found a 9-digit number, this is applicable only to the 1st (AccountNumber) column in the .csv file that was uploaded.
| UserNumber | 876876876 | ||||
| DESCRIPTION | Hello | ||||
| AccountNumber | State | Current | Premium | Interest | Total |
| 1207614641 | FL | GG | 200.51 | 24.58 | 225.09 |
| 217614234 | FL | GG | 474.65 | 51.77 | 526.42 |
Here is a sample exelsheet - https://i.stack.imgur.com/mynhP.png
onFileChange(event: any): void {
this.file = event.target.files[0];
if (this.file) {
console.log("this.file.type: " + this.file.type);
const fileName = this.file.name;
this.fileSelected = true;
this.invalidFileType = false;
this.invalidFileSize = false;
this.invalidFileRecords = false;
let file: File = this.file;
var value;
const fileReader = new FileReader();
fileReader.onload = (e: any) => {
value = e.target.result;
this.data = value;
var rows = e.target.result.split("\n");
if (rows.length > 6003) {
LoggerService.error(`upload records less than 6K.`, true, true);
}
};
fileReader.readAsBinaryString(this.file);
}
}
UploadFile(filevalue) {
const reqbody = {
filename: this.file?.name,
destinationPath: '',
lines: this.data.replace(/[^a-zA-Z0-9,\n\r.'" ]/g, "").split('\r\n')
};
var route = "CallNETAPI";
this.dataService.post(reqbody, route).subscribe(
(data: any[]) => {
console.log("Response data: " + data);
LoggerService.success(`File successfully uploaded to server`, true, false);
this.responseData = data;
},
(error: Response) => {
this.displayError = true;
this.errorText = `Error from CallNETAPI API service ${error.status}`;
}
);
}
Sharan KiranPosted Mar 31, 2024, 1:10 AM
Thank you Naimish Makwana
Naimish MakwanaPosted Mar 27, 2024, 4:32 AM
You can modify your
onFileChangefunction to check if the first column of each row has 9 digits and if so, prefix it with ‘0’. Here’s how you can do it:This code will check each row of your CSV file, and if the first column (AccountNumber) has 9 digits, it will prefix it with ‘0’. The modified data is then stored back in
this.data, ready to be passed to your API. Please note that this code assumes that your CSV data is comma-separated. If it’s not, you’ll need to adjust thesplitandjoinmethods accordingly. Also, this code doesn’t handle cases where the AccountNumber column might not be a number or might have leading or trailing spaces. You might want to add some error checking to handle these cases.Thanks