├── LICENSE ├── README.md └── csv_generator.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 AlexLibs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Client side (Serverless) csv generator 2 | ========================= 3 | 4 | 5 | ## Dependencies: 6 | None 7 | 8 | ## Supported browsers: 9 | http://caniuse.com/#feat=download 10 | 11 | ## Usage Example: 12 | 13 | http://jsbin.com/bafobowaji/1/ 14 | 15 | ### Download csv file without server. Just pass data array and choose the file name: 16 | 17 | csvGenerator = new CsvGenerator([['a', 'b', 'c'], ['', 1, 2]], 'my_csv.csv'); 18 | csvGenerator.download(true); 19 | 20 | ### If you want to create the element: 21 | 22 | csvGenerator = new CsvGenerator([['a', 'b', 'c'], ['', 1, 2]], 'my_csv.csv'); 23 | csvGenerator.getLinkElement(); 24 | 25 | ### If you want just to get href attribute of the link: 26 | 27 | csvGenerator = new CsvGenerator([['a', 'b', 'c'], ['', 1, 2]], 'my_csv.csv'); 28 | csvGenerator.getLinkElement(true); 29 | 30 | ### Formatting options 31 | 32 | // Semi colon separated 33 | csvGenerator = new CsvGenerator([['a', 'b', 'c'], ['', 1, 2]], 'my_csv.csv', ';'); 34 | 35 | // Semi colon separated, all cells quoted 36 | csvGenerator = new CsvGenerator([['a', 'b', 'c'], ['', 1, 2]], 'my_csv.csv', ';', true); 37 | 38 | ## Changes LOG: 39 | Version v2 (tag v2): get rid of jQuery from deps (thanks to [Denis Lukov's](https://github.com/NeXTs) contribution) 40 | -------------------------------------------------------------------------------- /csv_generator.js: -------------------------------------------------------------------------------- 1 | function CsvGenerator(dataArray, fileName, separator, addQuotes) { 2 | this.dataArray = dataArray; 3 | this.fileName = fileName; 4 | this.separator = separator || ','; 5 | this.addQuotes = !!addQuotes; 6 | 7 | if (this.addQuotes) { 8 | this.separator = '"' + this.separator + '"'; 9 | } 10 | 11 | this.getDownloadLink = function () { 12 | var separator = this.separator; 13 | var addQuotes = this.addQuotes; 14 | 15 | var rows = this.dataArray.map(function (row) { 16 | var rowData = row.join(separator); 17 | 18 | if (rowData.length && addQuotes) { 19 | return '"' + rowData + '"'; 20 | } 21 | 22 | return rowData; 23 | }); 24 | 25 | var type = 'data:text/csv;charset=utf-8'; 26 | var data = rows.join('\n'); 27 | 28 | if (typeof btoa === 'function') { 29 | type += ';base64'; 30 | data = btoa(data); 31 | } else { 32 | data = encodeURIComponent(data); 33 | } 34 | 35 | return this.downloadLink = this.downloadLink || type + ',' + data; 36 | }; 37 | 38 | this.getLinkElement = function (linkText) { 39 | var downloadLink = this.getDownloadLink(); 40 | var fileName = this.fileName; 41 | this.linkElement = this.linkElement || (function() { 42 | var a = document.createElement('a'); 43 | a.innerHTML = linkText || ''; 44 | a.href = downloadLink; 45 | a.download = fileName; 46 | return a; 47 | }()); 48 | return this.linkElement; 49 | }; 50 | 51 | // call with removeAfterDownload = true if you want the link to be removed after downloading 52 | this.download = function (removeAfterDownload) { 53 | var linkElement = this.getLinkElement(); 54 | linkElement.style.display = 'none'; 55 | document.body.appendChild(linkElement); 56 | linkElement.click(); 57 | if (removeAfterDownload) { 58 | document.body.removeChild(linkElement); 59 | } 60 | }; 61 | } 62 | --------------------------------------------------------------------------------