Refactor exportToCSV js function