Search code examples
excelangularangular-directivelinefeed

Remove blank row while pasting data from Excel to Angular Kendo UI grid


I am using the following example: excel-paste

Please look at the excel-paste.directive.ts Once the data is pasted, I need to validate and save data to database. When I paste data from Excel to the grid, in the directive it uses line feed to split and adds an extra blank row at the bottom. I tried replacing line feed with carriage return using '\r\n', but that does the same.

const rows = data.split('\n');

Please guide if there is a way to remove that empty line


Solution

  • You can use the Array.prototype.pop() function, to remove the last element of the array.

    const rows = data.split('\n');
    rows.pop();
    

    Alternatively, if you wanted to do it all in one line you could use the Array.prototype.slice() function to copy all elements except for the last from the array.

    const rows = data.split('\n').slice(0, -1);