0

I created a code that can export a file via excel the code is working fine without error but my problem is the excel file has a lot of spaces can. enter image description here

as you can see in the image row 123 data is put in column 7 not in column

heres my code for exporting the data

export  function download() {
var header = [];
var finalData = []
var group = [
    { "group_name": "123" },
    { "group_name": "123b" },
    { "group_name": "123ef" },
    { "group_name": "Accounts Payable" },
    { "group_name": "ADG JET TEAM" },
    { "group_name": "001 Approval" }
]
var member = [
    {"001 Approval": "083817 - Ranjeet Kumar (ranjeet.kumar3@concentrix.com)" },
    { "001 Approval": "C01747 - Abid Shaikh (abid.shaikh1@concentrix.com)"},
    { "001 Approval": "C01747 - Abid Shaikh (abid.shaikh1@concentrix.com)"},
    { "123b": "C01747 - Abid Shaikh (abid.shaikh1@concentrix.com)"},
    {
        "123ef": "C01747 - Abid Shaikh (abid.shaikh1@concentrix.com)"
    }
]

group.forEach(data=>{
    header.push(data.group_name)
})
finalData.push(header)
header.forEach(headerData=>{
    var temp = []
    member.forEach(memberData=>{
        if (headerData === Object.keys(memberData)[0]){
            temp.push(memberData[Object.keys(memberData)[0]])
        }else{
            temp.push("")
        }
    })
    finalData.push(temp)
})
exportToCsv('export.csv', finalData)}

the exportToexcel code is from here https://jsfiddle.net/jossef/m3rrLzk0/

Corioste
  • 65
  • 8

1 Answers1

0

Open your CSV in a text editor and and check the output, what are you using as separator and delimiter. This can be a CSV bad format (fields not surrounded by quotes, for ex), or a problem with excel configuration (csv delimiters and separators).

If you have data like that

field1, field2, field 3 supercool, this is a phrase, ops

It can be a problem, it should be something similar to

"field1", "field2", "field 3 supercool", "this is a phrase, ops"

In addition, try to open your csv with Google Sheets (docs), which will try to recognize the delimiters and separators automatically. See if it works.

A common problem for that is that your CSV is saparated by spaces or commas, but phrase can have a space and a comma which will be interpreted as a separator, and the whole document will break.

It can be useful to take a look at that link: Write a string containing commas and double quotes to CSV

Felippe Regazio
  • 189
  • 2
  • 4