1

There is a problem with my work. since Firebase's Web/JavaScript API always returns the full tree under the nodes that we request.

So in my case i retrieved all of existing fields from firebase including sensitive fields first and after that I want to export to excel selected fields only, not all of the fields that i got. the problem is, I always succeed exported all existing fields, including the sensitive fields.

Can I export selected field only and exclude the sensitive field? Below is my code:

I retrieve all of my fields include the data from firebase in my .ts file like this:

 getData() {
    this.dataLoading = true;
    this.querySubscription = this._backendService.getDocs('report')
      .subscribe(members => {
        this.members = members;
        this.dataSource = new MatTableDataSource(members);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
      },
        (error) => {
          this.error = true;
          this.errorMessage = error.message;
          this.dataLoading = false;
        },
        () => { this.error = false; this.dataLoading = false; });
  }

    //export func
    exportAsXLSX():void{
      this._backendService.exportAsExcelFile(this.members, 'sample');
    }

My Backend service Code :

    getDocs(coll:string,filters?:any){
    this.itemsCollection=this.afs.collection<any>(this.getCollectionURL(coll));
    return this.itemsCollection.valueChanges();
  }

    getCollectionURL(filter){
        return "ReportApp/fajar/"+filter;
      }

    //export func
    public exportAsExcelFile(json: any[], excelFileName: string): void {
      const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
      const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
      const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
      this.saveAsExcelFile(excelBuffer, excelFileName);
    }
    private saveAsExcelFile(buffer: any, fileName: string): void {
       const data: Blob = new Blob([buffer], {type: EXCEL_TYPE});
       FileSaver.saveAs(data, fileName + '_export_' + new  Date().getTime() + EXCEL_EXTENSION);
    }

as for reference im using code from here to exporting to excel :https://medium.com/@madhavmahesh/exporting-an-excel-file-in-angular-927756ac9857

as u can see I put all of my data into this.member variable and export em, But the result is that I exported all of em, i want to export selected fields only.

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
Stark Tony
  • 11
  • 1

1 Answers1

1

You will need to "trim down" the array of member data before you send it to your exportAsExcelFile() method. Your problem is that you are passing ALL of the member data to that export function. So the solution is to remove any sensitive information before you call the export function.

exportAsXLSX():void {
    // TRIM DOWN ARRAY HERE
    this._backendService.exportAsExcelFile(this.members, 'sample');
}

Since you didn't provide your member database structure, or details of what you consider sensitive information, I'll provide a generic example. You have an array of members... Most likely, you've made each "member" in the array into an object... so we need to loop over that array and delete the "sensitive" property of each member object.

As a precaution, since we don't want to delete the properties from the ACTUAL array, since arrays are reference-types, and since you might need those details elsewhere... let's make a copy of the array - a deep copy to ensure even nested objects are copied.

var newMemberArray = JSON.parse(JSON.stringify(this.members))

Then, we need to loop over that new array and delete our sensitive properties:

newMemberArray.forEach(function(m){
    delete m.sensitivePropertyName1;
    delete m.sensitivePropertyName2;
});

and pass that "sanitized" array to your export function... so putting all this together, something like:

exportAsXLSX():void {
    var newMemberArray = JSON.parse(JSON.stringify(this.members))
    newMemberArray.forEach(function(m){ delete m.sensitivePropertyName });
    this._backendService.exportAsExcelFile(newMemberArray, 'sample');
}

*Disclaimer: untested code, for explanation purposes only

JeremyW
  • 5,157
  • 6
  • 29
  • 30