-1

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>
  • Please don't ask us to provide you code. Instead, try something and come back with a [mcve] of your code. – evolutionxbox Jul 22 '22 at 15:11
  • Probably the most straightforward path here is to generate a CSV as these can be easily loaded in Excel. There are plenty of existing questions about CSV generation. Try something and if it doesn't work, then ask a specific question about the issue you're having. Here's the solution I'd try first: https://stackoverflow.com/a/68146412/14357 – spender Jul 22 '22 at 15:16
  • @evolutionxbox Thank you for helping me understand the stackoverflow etiquette. This was my first question, and I have revised to show what I tried and the console error that I am receiving. – user19602105 Jul 22 '22 at 15:31

2 Answers2

0

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
acolchagoff
  • 1,926
  • 3
  • 18
  • 30
0

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();
    }