Kumar AU

Kumar AU

  • 1.4k
  • 295
  • 57.5k

How to change 9 digit uploaded data into 10-digit number in Angular

Mar 26 2024 2:54 PM

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

<input #fileUpload type="file" class="file-upload" name="fileUpload" id="fileUpload" accept=".xlsx,.xls,.csv" required (change)="onFileChange($event)">
<button (click)="UploadFile(form.value)" type="button" mat-raised-button color="primary" [disabled]="!fileSelected">Upload</button>
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}`;
    }
  );
}

Answers (2)