1

Currently I am trying to create a csv file using encodeURI(csvContent) and window.open as a means of downloading the file. This is the same workflow for other CSV downloads in the application so I am trying to attempt to keep it consistent.

Problem: I have been attempting to handle a specific case in a string that is supposed to fit into one of the cells that result from the csv download. There is are a couple of line breaks in the string that I am having a very difficult time trying to target to replace with a regex pattern. These new line breaks cause my Numbers app to create misaligned cells because it treated the new line as it's own row.

The way it looks like in the object containing the string is

  • description : "Sf01 - Bad Sensor - CO2 undefined↵↵Replace CO2 Sensor"

The way it looks while console logging is

  • Sf01 - Bad Sensor - CO2 undefined

    Replace CO2 Sensor

I have tried a couple of regex patterns found on Stackoverflow such as

  • '/↵↵/'
  • '/\r?\n|\r/g'

However when I console log the result of the replace with those patterns the new line still seems to persist (verified it by trying to download the csv and opening in Numbers and console logging statements).

Targeted Solution: Sf01 - Bad Sensor - CO2 undefined. Replace CO2 Sensor

Some of the Stack links I explored

     lineArray.push("data:text/csv;charset=utf-8, Filter Type: " + filterType);
        lineArray.push("Name, Description, Status, Type, Time, Building, Level, Estimated Savings, Estimated Cost")
        data.forEach(function(infoObject, index){
            var importantInfo = [];
            importantInfo.push(infoObject['activity_name']);
            console.log(infoObject['description']);
            var description = infoObject['description'].replace('/↵↵/', '. ');
            // var description = infoObject['description'].replace('/\r?\n|\r/g', ' ');
            console.log(description);
            importantInfo.push(description);
            importantInfo.push(infoObject['activity_status']);
            importantInfo.push(infoObject['activity_type']);
            importantInfo.push(infoObject['activity_date']);
            importantInfo.push(infoObject['building']);
            importantInfo.push(infoObject['activity_level']);
            importantInfo.push(infoObject['estimated_savings']);
            importantInfo.push(infoObject['estimated_cost']);
            var line = importantInfo.join(", ");
            lineArray.push(line);
        });
        var csvContent = lineArray.join("\n");
        var encodedUri = encodeURI(csvContent);
        window.open(encodedUri);

Thanks for reading. I would appreciate any help or insight!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Sung Kim
  • 11
  • 1

0 Answers0