I am working on a program that parses a CSV files and then moves the respective data into different arrays that will make a spreadsheet out of them. However, my spreadsheet has extra blank rows with one cell each. Upon further inspection, I have found these extra cells contain a single newline character each. I have filtered my arrays so that they do not contain any newline characters, yet the problem still persists. I am doing this in conjunction with AngularJS, and while I doubt that is causing the problem, I did not have this issue before I implemented Angular.
Has anyone had anything like this before? With dynamic creation of newline characters where they shouldn't be.
Here is some of my code:
headers = breakText[0];
function isDefined(element, index, array){
return (element !== undefined || element !=='\n');
//I tried to stop the newlines here, but this did nothing
};
//new header file including ONLY desired titles; no undefines
$scope.clearHeaders = headers.filter(isDefined);
$scope.clearPatients = [];
for(var i=1; i<breakText.length; i++){
if breakText[0] {
$scope.clearPatients.push(breakText[i].filter(isDefined));
}
};
breakText was a 2D array containing undefined areas where I had deleted stuff. So the above code is creating a new header array and another, 2D array to hold an array of data for each person, all without undefined spaces. These arrays contain blanks ""
as well as data, but nothing with \n
or \r\n
.
Here is part of the AngularJS implementation:
$scope.columns = $scope.clearHeaders;
$scope.cells = {};
$scope.values = $scope.clearPatients.map(function(c,row){
return c.map(function(data){
return {
content: data,
color: $scope.makeColors(data)
};
});
});
We have tried using a replace function to replace \n
with ''
and disallow it to return the data if it is ''
but this causes issues since some of our initial data is also blank. It also does not stop the creation of the new cell.
We have also tried wrapping it in something such as if(data !== '\n')
but that changed nothing.
Edit1:
You can see it working live here, yet it is designed to work with this small CSV file that can be downloaded here. The text below the table after you upload a file shows what each cell is bound to, and if you click and edit the cells, you can see the content dynamically changing. By clicking on the extra cells between the rows, you can see that they contain a newline character.
I just tried creating a new filter function, where it would check for a newline in the clearPatients
array, and then filter everything into a new array, sans-newline. This new array was then called to be in the table rather than the one with newlines. Yet this threw many errors that could not be determined of their source.
Edit2:
After Mike P's answer and some other input, it was determined that the best way to fix this problem would be to swap the line var allTextLines = $scope.csv.split(/\r\n|\n/);
with the regex /\r?\n/
Thanks for your help!