Francesco

My fix for XLS Export when comma is present in data

Recommended Posts

Hi, we used client side export chart functionality for creating an excel report. However when exported data contains commas, generated excel got broken (rows with commas have more columns than header).

So...digging in fusioncharts.js code we did this trick:

Quote

k = function() {
                        var c = 0;
                        Oa = [];
                        Na.replace(/[^\r\n]+/g, function(a) {
                            Oa[c] = [];
                            // FB fix for exporting data with comma inside
                            /*
                            Oa[c] = a.split(",").map(function(c) {
                                return c.replace(/"/g, "")
                            });

                            */
                            Oa[c] = splitCsv(a).map(function(c) {
                                return c.replace(/"/g, "")
                            });

                            
                            c += 1
                        })
                    },

..using stackoverflow splt function below:

Quote

function splitCsv(str) {
  return str.split(',').reduce((accum,curr)=>{
    if(accum.isConcatting) {
      accum.soFar[accum.soFar.length-1] += ','+curr
    } else {
      accum.soFar.push(curr)
    }
    if(curr.split('"').length % 2 == 0) {
      accum.isConcatting= !accum.isConcatting
    }
    return accum;
  },{soFar:[],isConcatting:false}).soFar
}

Our problem seems to be solved right now B)

...but is there any side effect we don't know? May I have broken something else?

Regards,

Francesco

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now