-2

I'm creating a csv file using javascript but I don't understand how can I insert a name for the document and also how can I insert a first row with "Name", "Langitude", "Longitude"

function createCSV() {
    console.log("arrayInfo.length", arrayInfo.length)
    if (arrayInfo.length > 0) {
        const rows = []
        for (var i = 0; i < arrayInfo.length; i++) {
            rows.push(arrayInfo[i])
        }
        console.log("rows ", rows)
        let csvContent = "";
        rows.forEach(function (rowArray) {
            let row = rowArray.join(",");
            console.log("row ", row)
            console.log("rowArray ", rowArray)
            csvContent += row + "\r\n";
        });
        console.log("csvContent ", csvContent, [csvContent])
        var csvData = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
        var csvURL = window.URL.createObjectURL(csvData);
        console.log("csvData ", csvData)
        window.open(csvURL);

    }
    else {
        console.log("no csv")
    }
}

this is the function that I use, In your opinion how can I do?

Jack23
  • 1,368
  • 7
  • 32

1 Answers1

0

I don't understand how can I insert a name for the document

You're opening a data URL in a new window. There's nothing to give a name to. There no path in the URL (just data) and you aren't creating a download so you can't suggest a filename there.

and also how can I insert a first row with "Name", "Langitude", "Longitude"

See this code:

const rows = []
for (var i = 0; i < arrayInfo.length; i++) {
    rows.push(arrayInfo[i])
}

You create an array and then you have a loop where you insert everything you want on the other lines.

Just insert the additional line you want, in the same way, first.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335