Skip to main content
First pass at code cleanup, spelling and other readability improvements
Source Link
Moonpie
  • 2.7k
  • 6
  • 15
  • 29
  • 1.First row (A1) name needs to be just Name . i have passed just 'Name',You can see in below code s=[] ( Undefined is added automatically ).Due to the lwc example i'm using . I need some help in removing unwanted code.
  • 2.From the second row , Unwanted column is being added, Whenever i used '\n' to change row . The following is the lwc code**

.

/ Row 1 : Static Data (so adding directly in array S[]) /

  1. First row (A1) name needs to be just Name . i have passed just 'Name',You can see in below code s=[] ( Undefined is added automatically ).Due to the lwc example i'm using . I need some help in removing unwanted code.
  2. From the second row , Unwanted column is being added, Whenever i used '\n' to change row . The following is the lwc code**
` exportToCSV() {  

let s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; 
                            ==>  [this.columnHeaderLists1] ==> Data from Apex 
          for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 :Dynaimic data fetching from Apex using Lwc Wire */****
      for(let key in this.columnHeaderList){
        let t;              ==> [this.columnHeaderLists] ==> Data from Apex
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
  

    //Passing the s[] to column headers 
          let columnHeader = s;
 **[ I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code ]**
        let jsonKeys = [];
          var jsonRecordsData = []; 
          console.log('length ',jsonRecordsData.length);
          let csvIterativeData;  
          let csvSeperator  
          let newLineCharacter;  
          csvSeperator = ",";  
          newLineCharacter = "\n";  
         // csvIterativeData = "";  
          csvIterativeData += columnHeader.join(csvSeperator);  
          csvIterativeData += newLineCharacter;  
          for (let i = 0; i < jsonRecordsData.length; i++) {  
            let counter = 0;  
            for (let iteratorObj in jsonKeys) {  
              let dataKey = jsonKeys[iteratorObj];  
              if (counter > 0) {  csvIterativeData += csvSeperator;  }  
              if (  jsonRecordsData[i][dataKey] !== null &&  
                jsonRecordsData[i][dataKey] !== undefined  
              ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
              } else {  csvIterativeData += '""';  
              }  
              counter++;  
            }  
            csvIterativeData += newLineCharacter;  
          }  
          console.log("csvIterativeData", csvIterativeData);  
          this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
        }     }`
****/*  Row 1 : Static Data (so adding directly in array S[]) */****
exportToCSV() {  
    let s =["Name", "Example Data", "Field Type", "Data Category", "Data Category Definition", "Data Classification"]; 
              ==>  [this.columnHeaderLists1] ==> Data from Apex 
    for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
    }
    s.push("\n");
    
****/*  Row 2 :Dynaimic data fetching from Apex using Lwc Wire */****
    for(let key in this.columnHeaderList){
        let t;
              ==> [this.columnHeaderLists] ==> Data from Apex
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
            s.push("\"" + t[key] + "\"");
        }
    }
    s.push("\n");
}
      
//Passing the s[] to column headers 
let columnHeader = s;

**[ I'm not using the below code , which requires a pojo*/
   /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code ]**
let jsonKeys = [];
var jsonRecordsData = []; 
console.log('length ',jsonRecordsData.length);
let csvIterativeData;  
let csvSeperator  
let newLineCharacter;  
csvSeperator = ",";  
newLineCharacter = "\n";  
// csvIterativeData = "";  
csvIterativeData += columnHeader.join(csvSeperator);  
csvIterativeData += newLineCharacter;  
for (let i = 0; i < jsonRecordsData.length; i++) {  
    let counter = 0;  
    for (let iteratorObj in jsonKeys) {  
        let dataKey = jsonKeys[iteratorObj];  
        if (counter > 0) {  csvIterativeData += csvSeperator;  }  
        if (  jsonRecordsData[i][dataKey] !== null && jsonRecordsData[i][dataKey] !== undefined ) { 
            csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';
        } else {  
            csvIterativeData += '""';  
        }  
        counter++;  
    }  
    csvIterativeData += newLineCharacter;  
}  
console.log("csvIterativeData", csvIterativeData);  
this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
}
}

We are basically passing the data to array S[]S[] (using s.pushs.push) . We have pushed data to row . After 1 row of data we are using '\n''\n' to go new line . . Right after using '\n''\n' cursor is movngmoving to next row and also an empty column is being created in the next row. (Can be observed from row:2) 1.In row2 we need to pass data from A2 . But Due to empty column . Data is populating from B2. I need help in resolving this . Is that a right approach of passing values to the csv Desired Out-put : enter image description here Any suggestion is appreciated , Thankyou .

  1. In row2 we need to pass data from A2 . But Due to empty column . Data is populating from B2. I need help in resolving this . Is that a right approach of passing values to the csv Desired Out-put : enter image description here Any suggestion is appreciated , Thankyou .
  • 1.First row (A1) name needs to be just Name . i have passed just 'Name',You can see in below code s=[] ( Undefined is added automatically ).Due to the lwc example i'm using . I need some help in removing unwanted code.
  • 2.From the second row , Unwanted column is being added, Whenever i used '\n' to change row . The following is the lwc code**

.

/ Row 1 : Static Data (so adding directly in array S[]) /

` exportToCSV() {  

let s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; 
                            ==>  [this.columnHeaderLists1] ==> Data from Apex 
          for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 :Dynaimic data fetching from Apex using Lwc Wire */****
      for(let key in this.columnHeaderList){
        let t;              ==> [this.columnHeaderLists] ==> Data from Apex
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
  

    //Passing the s[] to column headers 
          let columnHeader = s;
 **[ I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code ]**
        let jsonKeys = [];
          var jsonRecordsData = []; 
          console.log('length ',jsonRecordsData.length);
          let csvIterativeData;  
          let csvSeperator  
          let newLineCharacter;  
          csvSeperator = ",";  
          newLineCharacter = "\n";  
         // csvIterativeData = "";  
          csvIterativeData += columnHeader.join(csvSeperator);  
          csvIterativeData += newLineCharacter;  
          for (let i = 0; i < jsonRecordsData.length; i++) {  
            let counter = 0;  
            for (let iteratorObj in jsonKeys) {  
              let dataKey = jsonKeys[iteratorObj];  
              if (counter > 0) {  csvIterativeData += csvSeperator;  }  
              if (  jsonRecordsData[i][dataKey] !== null &&  
                jsonRecordsData[i][dataKey] !== undefined  
              ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
              } else {  csvIterativeData += '""';  
              }  
              counter++;  
            }  
            csvIterativeData += newLineCharacter;  
          }  
          console.log("csvIterativeData", csvIterativeData);  
          this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
        }     }`

We are basically passing the data to array S[] (using s.push) . We have pushed data to row . After 1 row of data we are using '\n' to go new line . . Right after using '\n' cursor is movng to next row and also an empty column is being created in the next row. (Can be observed from row:2) 1.In row2 we need to pass data from A2 . But Due to empty column . Data is populating from B2. I need help in resolving this . Is that a right approach of passing values to the csv Desired Out-put : enter image description here Any suggestion is appreciated , Thankyou .

  1. First row (A1) name needs to be just Name . i have passed just 'Name',You can see in below code s=[] ( Undefined is added automatically ).Due to the lwc example i'm using . I need some help in removing unwanted code.
  2. From the second row , Unwanted column is being added, Whenever i used '\n' to change row . The following is the lwc code**
****/*  Row 1 : Static Data (so adding directly in array S[]) */****
exportToCSV() {  
    let s =["Name", "Example Data", "Field Type", "Data Category", "Data Category Definition", "Data Classification"]; 
              ==>  [this.columnHeaderLists1] ==> Data from Apex 
    for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
    }
    s.push("\n");
    
****/*  Row 2 :Dynaimic data fetching from Apex using Lwc Wire */****
    for(let key in this.columnHeaderList){
        let t;
              ==> [this.columnHeaderLists] ==> Data from Apex
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
            s.push("\"" + t[key] + "\"");
        }
    }
    s.push("\n");
}
      
//Passing the s[] to column headers 
let columnHeader = s;

**[ I'm not using the below code , which requires a pojo*/
   /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code ]**
let jsonKeys = [];
var jsonRecordsData = []; 
console.log('length ',jsonRecordsData.length);
let csvIterativeData;  
let csvSeperator  
let newLineCharacter;  
csvSeperator = ",";  
newLineCharacter = "\n";  
// csvIterativeData = "";  
csvIterativeData += columnHeader.join(csvSeperator);  
csvIterativeData += newLineCharacter;  
for (let i = 0; i < jsonRecordsData.length; i++) {  
    let counter = 0;  
    for (let iteratorObj in jsonKeys) {  
        let dataKey = jsonKeys[iteratorObj];  
        if (counter > 0) {  csvIterativeData += csvSeperator;  }  
        if (  jsonRecordsData[i][dataKey] !== null && jsonRecordsData[i][dataKey] !== undefined ) { 
            csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';
        } else {  
            csvIterativeData += '""';  
        }  
        counter++;  
    }  
    csvIterativeData += newLineCharacter;  
}  
console.log("csvIterativeData", csvIterativeData);  
this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
}
}

We are basically passing the data to array S[] (using s.push) . We have pushed data to row . After 1 row of data we are using '\n' to go new line . . Right after using '\n' cursor is moving to next row and also an empty column is being created in the next row. (Can be observed from row:2)

  1. In row2 we need to pass data from A2 . But Due to empty column . Data is populating from B2. I need help in resolving this . Is that a right approach of passing values to the csv Desired Out-put : enter image description here Any suggestion is appreciated , Thankyou .
added 1120 characters in body
Source Link

i amI'm very new to salesforce and lwc , i'm trying to create aExport some static and some dynamic data in a csv using LWC Salesforce and succeeded in that( except with a new column has been added from the second line , enter image description here.) The following is![enter image description here

In the lwc codeabove image : There are two issues i'm facing :

  • 1.First row (A1) name needs to be just Name . i have passed just 'Name',You can see in below code s=[] ( Undefined is added automatically ).Due to the lwc example i'm using . I need some help in removing unwanted code.
  • 2.From the second row , Unwanted column is being added, Whenever i used '\n' to change row . The following is the lwc code**

.` exportToCSV() {

/ Row 1 :Staic HEADER Static Data (so adding directly in array S[]) /

let s;
 ` exportToCSV() {  

let s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; /* 
  Row 1 : Dynamic Headers Continuation to Static Headers */                 ==>  [this.columnHeaderLists1] ==> Data from Apex 
          for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 :Dynaimic Dynamicdata secondfetching from HeadersApex using Lwc Wire */****
 
**Here I need 6 spaces but pushingfor(let 5key emptyin stringsthis.columnHeaderList){
 (Due to empty Column created)**
   let t;  s.push("","","","","");
      for(let key in this.columnHeaderList){
    ==> [this.columnHeaderLists] ==> Data letfrom t;Apex
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
   

    
 //Passing the s[] to column headers console.log('s:',s);
          let columnHeader = s;
**/* **[ I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code */**
]**
        let jsonKeys = [];
      //"name","exampleData","fieldType","category","categoryDefinition","classification"];
      var jsonRecordsData = []; 
          console.log('length ',jsonRecordsData.length);
          let csvIterativeData;  
          let csvSeperator  
          let newLineCharacter;  
          csvSeperator = ",";  
          newLineCharacter = "\n";  
         // csvIterativeData = "";  
          csvIterativeData += columnHeader.join(csvSeperator);  
          csvIterativeData += newLineCharacter;  
          for (let i = 0; i < jsonRecordsData.length; i++) {  
            let counter = 0;  
            for (let iteratorObj in jsonKeys) {  
              let dataKey = jsonKeys[iteratorObj];  
              if (counter > 0) {  csvIterativeData += csvSeperator;  }  
              if (  jsonRecordsData[i][dataKey] !== null &&  
                jsonRecordsData[i][dataKey] !== undefined  
              ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
              } else {  csvIterativeData += '""';  
              }  
              counter++;  
            }  
            csvIterativeData += newLineCharacter;  
          }  
          console.log("csvIterativeData", csvIterativeData);  
          this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
        }     }`

IsExaplanation:

We are basically passing the data to array S[] (using s.push) . We have pushed data to row . After 1 row of data we are using '\n' to go new line . . Right after using '\n' cursor is movng to next row and also an empty column is being created in the next row. (Can be observed from row:2) 1.In row2 we need to pass data from A2 . But Due to empty column . Data is populating from B2. I need help in resolving this . Is that a right approach using nested if'sof passing values to handle differentthe csv header Desired Out-put : enter image description here Any suggestion is appreciated , Thankyou .

i am trying to create a csv and succeeded in that except with a new column has been added from the second line , enter image description here The following is the lwc code .` exportToCSV() {

/ Row 1 :Staic HEADER /

let s;
      s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; /*  Row 1 : Dynamic Headers Continuation to Static Headers */
      for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 : Dynamic second  Headers */****
 
**Here I need 6 spaces but pushing 5 empty strings (Due to empty Column created)**
      s.push("","","","","");
      for(let key in this.columnHeaderList){
        let t;
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
      
       console.log('s:',s);
      let columnHeader = s;
**/* I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code */**

    let jsonKeys = [];
      //"name","exampleData","fieldType","category","categoryDefinition","classification"];
      var jsonRecordsData = []; 
      console.log('length ',jsonRecordsData.length);
      let csvIterativeData;  
      let csvSeperator  
      let newLineCharacter;  
      csvSeperator = ",";  
      newLineCharacter = "\n";  
     // csvIterativeData = "";  
      csvIterativeData += columnHeader.join(csvSeperator);  
      csvIterativeData += newLineCharacter;  
      for (let i = 0; i < jsonRecordsData.length; i++) {  
        let counter = 0;  
        for (let iteratorObj in jsonKeys) {  
          let dataKey = jsonKeys[iteratorObj];  
          if (counter > 0) {  csvIterativeData += csvSeperator;  }  
          if (  jsonRecordsData[i][dataKey] !== null &&  
            jsonRecordsData[i][dataKey] !== undefined  
          ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
          } else {  csvIterativeData += '""';  
          }  
          counter++;  
        }  
        csvIterativeData += newLineCharacter;  
      }  
      console.log("csvIterativeData", csvIterativeData);  
      this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
    }     }`

Is that a right approach using nested if's to handle different csv header .

I'm very new to salesforce and lwc , i'm trying to Export some static and some dynamic data in a csv using LWC Salesforce and succeeded in that( except with a new column has been added from the second line .) ![enter image description here

In the above image : There are two issues i'm facing :

  • 1.First row (A1) name needs to be just Name . i have passed just 'Name',You can see in below code s=[] ( Undefined is added automatically ).Due to the lwc example i'm using . I need some help in removing unwanted code.
  • 2.From the second row , Unwanted column is being added, Whenever i used '\n' to change row . The following is the lwc code**

.

/ Row 1 : Static Data (so adding directly in array S[]) /

` exportToCSV() {  

let s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"];  
                            ==>  [this.columnHeaderLists1] ==> Data from Apex 
          for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 :Dynaimic data fetching from Apex using Lwc Wire */****
      for(let key in this.columnHeaderList){
        let t;              ==> [this.columnHeaderLists] ==> Data from Apex
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
   

    //Passing the s[] to column headers 
          let columnHeader = s;
 **[ I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code ]**
        let jsonKeys = [];
          var jsonRecordsData = []; 
          console.log('length ',jsonRecordsData.length);
          let csvIterativeData;  
          let csvSeperator  
          let newLineCharacter;  
          csvSeperator = ",";  
          newLineCharacter = "\n";  
         // csvIterativeData = "";  
          csvIterativeData += columnHeader.join(csvSeperator);  
          csvIterativeData += newLineCharacter;  
          for (let i = 0; i < jsonRecordsData.length; i++) {  
            let counter = 0;  
            for (let iteratorObj in jsonKeys) {  
              let dataKey = jsonKeys[iteratorObj];  
              if (counter > 0) {  csvIterativeData += csvSeperator;  }  
              if (  jsonRecordsData[i][dataKey] !== null &&  
                jsonRecordsData[i][dataKey] !== undefined  
              ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
              } else {  csvIterativeData += '""';  
              }  
              counter++;  
            }  
            csvIterativeData += newLineCharacter;  
          }  
          console.log("csvIterativeData", csvIterativeData);  
          this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
        }     }`

Exaplanation:

We are basically passing the data to array S[] (using s.push) . We have pushed data to row . After 1 row of data we are using '\n' to go new line . . Right after using '\n' cursor is movng to next row and also an empty column is being created in the next row. (Can be observed from row:2) 1.In row2 we need to pass data from A2 . But Due to empty column . Data is populating from B2. I need help in resolving this . Is that a right approach of passing values to the csv Desired Out-put : enter image description here Any suggestion is appreciated , Thankyou .

added 221 characters in body
Source Link

let s; s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; /* Row 1 : Dynamic Headers Continuation to Static Headers */ for(let d in this.columnHeaderLists1){ s.push(""" + this.columnHeaderLists1[d] + """); } s.push("\n");

/ Row 2 : Dynamic second Headers /

Here I need 6 spaces but pushing 5 empty strings (Due to empty Column created) s.push("","","","",""); for(let key in this.columnHeaderList){ let t; t = this.columnHeaderList[key].subcolumn; for(let key in t){ s.push(""" + t[key] + """); }

let s;
      s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; /*  Row 1 : Dynamic Headers Continuation to Static Headers */
      for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 : Dynamic second  Headers */****

**Here I need 6 spaces but pushing 5 empty strings (Due to empty Column created)**
      s.push("","","","","");
      for(let key in this.columnHeaderList){
        let t;
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
      
      console.log('s:',s);
      let columnHeader = s;

/ I'm not using the belowc code , which requires a pojo/ /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code /*

**/* I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code */**

    let jsonKeys = [];
      //"name","exampleData","fieldType","category","categoryDefinition","classification"];
      var jsonRecordsData = []; 
      console.log('length ',jsonRecordsData.length);
      let csvIterativeData;  
      let csvSeperator  
      let newLineCharacter;  
      csvSeperator = ",";  
      newLineCharacter = "\n";  
     // csvIterativeData = "";  
      csvIterativeData += columnHeader.join(csvSeperator);  
      csvIterativeData += newLineCharacter;  
      for (let i = 0; i < jsonRecordsData.length; i++) {  
        let counter = 0;  
        for (let iteratorObj in jsonKeys) {  
          let dataKey = jsonKeys[iteratorObj];  
          if (counter > 0) {  csvIterativeData += csvSeperator;  }  
          if (  jsonRecordsData[i][dataKey] !== null &&  
            jsonRecordsData[i][dataKey] !== undefined  
          ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
          } else {  csvIterativeData += '""';  
          }  
          counter++;  
        }  
        csvIterativeData += newLineCharacter;  
      }  
      console.log("csvIterativeData", csvIterativeData);  
      this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
    }     }`

}`

let s; s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; /* Row 1 : Dynamic Headers Continuation to Static Headers */ for(let d in this.columnHeaderLists1){ s.push(""" + this.columnHeaderLists1[d] + """); } s.push("\n");

/ Row 2 : Dynamic second Headers /

Here I need 6 spaces but pushing 5 empty strings (Due to empty Column created) s.push("","","","",""); for(let key in this.columnHeaderList){ let t; t = this.columnHeaderList[key].subcolumn; for(let key in t){ s.push(""" + t[key] + """); }

}
s.push("\n");

}
  
  console.log('s:',s);
  let columnHeader = s;

/ I'm not using the belowc code , which requires a pojo/ /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code /*

let jsonKeys = [];
  //"name","exampleData","fieldType","category","categoryDefinition","classification"];
  var jsonRecordsData = []; 
  console.log('length ',jsonRecordsData.length);
  let csvIterativeData;  
  let csvSeperator  
  let newLineCharacter;  
  csvSeperator = ",";  
  newLineCharacter = "\n";  
 // csvIterativeData = "";  
  csvIterativeData += columnHeader.join(csvSeperator);  
  csvIterativeData += newLineCharacter;  
  for (let i = 0; i < jsonRecordsData.length; i++) {  
    let counter = 0;  
    for (let iteratorObj in jsonKeys) {  
      let dataKey = jsonKeys[iteratorObj];  
      if (counter > 0) {  csvIterativeData += csvSeperator;  }  
      if (  jsonRecordsData[i][dataKey] !== null &&  
        jsonRecordsData[i][dataKey] !== undefined  
      ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
      } else {  csvIterativeData += '""';  
      }  
      counter++;  
    }  
    csvIterativeData += newLineCharacter;  
  }  
  console.log("csvIterativeData", csvIterativeData);  
  this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
}  

}`

let s;
      s =["Name","Example Data","Field Type","Data Category","Data Category Definition","Data Classification"]; /*  Row 1 : Dynamic Headers Continuation to Static Headers */
      for(let d in this.columnHeaderLists1){
        s.push("\"" + this.columnHeaderLists1[d] + "\"");
      }
      s.push("\n");

****/*  Row 2 : Dynamic second  Headers */****

**Here I need 6 spaces but pushing 5 empty strings (Due to empty Column created)**
      s.push("","","","","");
      for(let key in this.columnHeaderList){
        let t;
        t = this.columnHeaderList[key].subcolumn;
        for(let key in t){
          s.push("\"" + t[key] + "\"");
      }
      
    }
    s.push("\n");
   
    }
      
      console.log('s:',s);
      let columnHeader = s;
**/* I'm not using the belowc code , which requires a pojo*/  /* Our code has data from multiple queries so , it is complex to add a pojo and utilize the below code */**

    let jsonKeys = [];
      //"name","exampleData","fieldType","category","categoryDefinition","classification"];
      var jsonRecordsData = []; 
      console.log('length ',jsonRecordsData.length);
      let csvIterativeData;  
      let csvSeperator  
      let newLineCharacter;  
      csvSeperator = ",";  
      newLineCharacter = "\n";  
     // csvIterativeData = "";  
      csvIterativeData += columnHeader.join(csvSeperator);  
      csvIterativeData += newLineCharacter;  
      for (let i = 0; i < jsonRecordsData.length; i++) {  
        let counter = 0;  
        for (let iteratorObj in jsonKeys) {  
          let dataKey = jsonKeys[iteratorObj];  
          if (counter > 0) {  csvIterativeData += csvSeperator;  }  
          if (  jsonRecordsData[i][dataKey] !== null &&  
            jsonRecordsData[i][dataKey] !== undefined  
          ) {  csvIterativeData += '"' + jsonRecordsData[i][dataKey] + '"';  
          } else {  csvIterativeData += '""';  
          }  
          counter++;  
        }  
        csvIterativeData += newLineCharacter;  
      }  
      console.log("csvIterativeData", csvIterativeData);  
      this.hrefdata = "data:text/csv;charset=utf-8," + encodeURI(csvIterativeData);  
    }     }`
Source Link
Loading