1/** 2@namespace Converts JSON to CSV. 3 4Compress with: http://jscompress.com/ 5*/ 6(function (window) { 7 "use strict"; 8 /** 9 Default constructor 10 */ 11 var _CSV = function (JSONData, headers, options) { 12 if (typeof JSONData === 'undefined') 13 return; 14 15 if (typeof options === 'undefined'){ 16 options = {}; 17 } 18 19 var csvData = typeof JSONData != 'object' ? JSON.parse(settings.JSONData) : JSONData, 20 csvHeaders, 21 csvEncoding = 'data:text/csv;charset=utf-8,', 22 csvOutput = "", 23 csvRows = [], 24 BREAK = '\r\n', 25 DELIMITER = options.separator ? options.separator : ',', 26 FILENAME = options.fileName ? options.fileName : "quiz_questions_export.csv"; 27 28 // Get and Write the headers 29 csvHeaders = Object.keys(csvData[0]); 30 csvOutput += headers.join(',') + BREAK; 31 32 for (var i = 0; i < csvData.length; i++) { 33 var rowElements = []; 34 for(var k = 0; k < csvHeaders.length; k++) { 35 rowElements.push(csvData[i][csvHeaders[k]]); 36 } // Write the row array based on the headers 37 csvRows.push(rowElements.join(DELIMITER)); 38 } 39 40 csvOutput += csvRows.join(BREAK); 41 42 // Initiate Download 43 var a = document.createElement("a"); 44 45 if (navigator.msSaveBlob) { // IE10 46 navigator.msSaveBlob(new Blob([csvOutput], { type: "text/csv" }), FILENAME); 47 } else if ('download' in a) { //html5 A[download] 48 a.href = csvEncoding + encodeURIComponent(csvOutput); 49 a.download = FILENAME; 50 document.body.appendChild(a); 51 setTimeout(function() { 52 a.click(); 53 document.body.removeChild(a); 54 }, 66); 55 } else if (document.execCommand) { // Other version of IE 56 var oWin = window.open("about:blank", "_blank"); 57 oWin.document.write(csvOutput); 58 oWin.document.close(); 59 oWin.document.execCommand('SaveAs', true, FILENAME); 60 oWin.close(); 61 } else { 62 alert("Support for your specific browser hasn't been created yet, please check back later."); 63 } 64 }; 65 66 window.CSVExport = _CSV; 67 68})(window); 69 70// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys 71if (!Object.keys) { 72 Object.keys = (function() { 73 'use strict'; 74 var hasOwnProperty = Object.prototype.hasOwnProperty, 75 hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), 76 dontEnums = [ 77 'toString', 78 'toLocaleString', 79 'valueOf', 80 'hasOwnProperty', 81 'isPrototypeOf', 82 'propertyIsEnumerable', 83 'constructor' 84 ], 85 dontEnumsLength = dontEnums.length; 86 87 return function(obj) { 88 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) { 89 throw new TypeError('Object.keys called on non-object'); 90 } 91 92 var result = [], prop, i; 93 94 for (prop in obj) { 95 if (hasOwnProperty.call(obj, prop)) { 96 result.push(prop); 97 } 98 } 99 100 if (hasDontEnumBug) { 101 for (i = 0; i < dontEnumsLength; i++) { 102 if (hasOwnProperty.call(obj, dontEnums[i])) { 103 result.push(dontEnums[i]); 104 } 105 } 106 } 107 return result; 108 }; 109 }()); 110} 111 112var emitXmlHeader = function (headers) { 113 var headerRow = '<ss:Row>\n'; 114 for (var colName in headers) { 115 headerRow += ' <ss:Cell>\n'; 116 headerRow += ' <ss:Data ss:Type="String">'; 117 headerRow += colName + '</ss:Data>\n'; 118 headerRow += ' </ss:Cell>\n'; 119 } 120 headerRow += '</ss:Row>\n'; 121 return '<?xml version="1.0"?>\n' + 122 '<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\n' + 123 'xmlns:o="urn:schemas-microsoft-com:office:office"\n' + 124 'xmlns:x="urn:schemas-microsoft-com:office:excel"\n' + 125 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"\n' + 126 'xmlns:html="http://www.w3.org/TR/REC-html40">\n' + 127 '<ss:Worksheet ss:Name="Quiz questions">\n' + 128 '<ss:Table>\n\n' + headerRow; 129}; 130 131var emitXmlFooter = function() { 132 return '\n</ss:Table>\n' + 133 '</ss:Worksheet>\n' + 134 '</ss:Workbook>\n'; 135}; 136 137var jsonToSsXml = function (jsonObject, headers) { 138 var row; 139 var col; 140 var xml; 141 var data = typeof jsonObject != "object" ? JSON.parse(jsonObject) : jsonObject; 142 143 xml = emitXmlHeader(headers); 144 145 for (row = 0; row < data.length; row++) { 146 xml += '<ss:Row>\n'; 147 148 for (col in data[row]) { 149 xml += '<ss:Cell>\n'; 150 xml += '<ss:Data ss:Type="' + headers[col] + '">'; 151 xml += data[row][col] + '</ss:Data>\n'; 152 xml += '</ss:Cell>\n'; 153 } 154 155 xml += '</ss:Row>\n'; 156 } 157 158 xml += emitXmlFooter(); 159 return xml; 160}; 161 162xlsExporter = function (content, filename, contentType) { 163 var contentMimeType; 164 165 if (typeof contentType === 'undefined'){ 166 contentType = 'xls'; 167 } 168 switch(contentType){ 169 case 'xls': 170 contentMimeType = 'application/vnd.ms-excel'; 171 break; 172 case 'xlsx': 173 contentMimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; 174 break; 175 default: 176 contentMimeType = 'application/vnd.ms-excel'; 177 break; 178 } 179 180 var a = document.getElementById('downloadFile'); 181 var blob = new Blob([content], {
182 'type': contentMimeType 183 }); 184 a.href = window.URL.createObjectURL(blob); 185 a.download = filename + '.' + contentType; 186 a.click(); 187 window.URL.revokeObjectURL(a.href); 188};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.