Home > Software design >  Javascript Array of Objects to Excel
Javascript Array of Objects to Excel

Time:07-23

I would like to export a javascript array of objects into excel or even a database, something similar to https://jsfiddle.net/3an24jmw/25/

I tried to use the jsfiddle.net code above, and am getting a console error:

Uncaught TypeError: RowItem.forEach is not a function at Array.forEach () at exportToCsv at HTMLButtonElement.onclick

My array and javascript looks something like this:

var items = [
   {
      title: 'title1',
      youtube: 'youtubevideoid',
      audioaddress: "audio1.mp3",
      year: '2022',
      month: '07',
      day: '22',
      time: 'AM',
      speaker: 'speaker1'
   },
   {
      title: 'title2',
      youtube: 'youtubevideoid2',
      audioaddress: "audio2.mp3",
      year: '2022',
      month: '07',
      day: '22',
      time: 'PM',
      speaker: 'speaker2'
   },
];

//Download CSV of Javascript Array
    exportToCsv = function() {
      var CsvString = "";
      items.forEach(function(RowItem, RowIndex) {
        RowItem.forEach(function(ColItem, ColIndex) {
          CsvString  = ColItem   ',';
        });
        CsvString  = "\r\n";
      });
      CsvString = "data:application/csv,"   encodeURIComponent(CsvString);
     var x = document.createElement("A");
     x.setAttribute("href", CsvString );
     x.setAttribute("download","items.csv");
     document.body.appendChild(x);
     x.click();
    }

And the html button code looks like this:

<button onclick="exportToCsv()">export to CSV</button>

CodePudding user response:

you could write a script that formats your object in a csv format and then save it to a file, so if you did something similar to the following:

var items = [
   {
      title: 'title1',
      youtube: 'youtubevideoid',
      audioaddress: "audio1.mp3",
      year: '2022',
      month: '07',
      day: '22',
      time: 'AM',
      speaker: 'speaker1'
   },
   {
      title: 'title2',
      youtube: 'youtubevideoid2',
      audioaddress: "audio2.mp3",
      year: '2022',
      month: '07',
      day: '22',
      time: 'PM',
      speaker: 'speaker2'
   },
];

for(var i = 0; i < items.length; i  ){
  for(item in items[i]){
    process.stdout.write(items[i][item]);
    process.stdout.write(', ');
  }
  process.stdout.write("\n");
}

then run this with node similar to as follows:

node test.js > blah.csv

CodePudding user response:

I know this isn't the best solution ever, but this is how I have managed to get the results I was looking for. Here is the jsfiddle of the solution: https://jsfiddle.net/7w42xk5q/

I made a new array and converted the objects into nested arrays.

Here is the javascript I added after the "items" array and before the "exportToCsv" function:

var itemsgrouped = [
];

for(var i = 0; i < items.length; i  ){
    itemsgrouped.push(
        [
        items[i].title,
        items[i].youtube,
        items[i].audioaddress,
        items[i].year,
        items[i].month,
        items[i].day,
        items[i].time,
        items[i].speaker
      ]
    )
}

Then I changed the "exporttoCSV" function to be for the "itemsgrouped" array instead of the "items" array.

//Download CSV of Javascript Array
    exportToCsv = function() {
      var CsvString = "";
      itemsgrouped.forEach(function(RowItem, RowIndex) {
        RowItem.forEach(function(ColItem, ColIndex) {
          CsvString  = ColItem   ',';
        });
        CsvString  = "\r\n";
      });
      CsvString = "data:application/csv,"   encodeURIComponent(CsvString);
     var x = document.createElement("A");
     x.setAttribute("href", CsvString );
     x.setAttribute("download","itemsgrouped.csv");
     document.body.appendChild(x);
     x.click();
    }
  • Related