jQueryプラグインのtable2csvを使って<table>のデータをcsvファイルをダウンロードする機能を作成しています。
ダウンロードはできましたがcsvファイルの一番最初にカンマが入ってしまいそのカンマだけ無くしたいです。
どのように改修すればよろしいでしょうか。
下記の部分を改修したいのですがどのように変更すればよいか思いつきません。
宜しくお願い致します。
if (i !== numCols - 1) {
output += options.separator;
} else {
output += options.newline;
}
JavaScript
1(function ($) { 2 "use strict"; 3 const optionsDefaults = { 4 /* action='download' options */ 5 filename: "table.csv", 6 7 /* action='output' options */ 8 appendTo: "body", 9 10 /* general options */ 11 separator: ",", 12 newline: "\n", 13 quoteFields: true, 14 trimContent: true, 15 excludeColumns: "", 16 excludeRows: "" 17 }; 18 19 let options = {}; 20 21 function quote(text) { 22 return "\"" + text.replace(/"/g, "\"\"") + "\""; 23 } 24 25 // taken from http://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server 26 function download(filename, text) { 27 const element = document.createElement("a"); 28 element.setAttribute("href", "data:text/plain;charset=utf-8,%EF%BB%BF,\ufeff" + encodeURIComponent(text)); 29 element.setAttribute("download", filename); 30 31 element.style.display = "none"; 32 document.body.appendChild(element); 33 34 element.click(); 35 36 document.body.removeChild(element); 37 } 38 39 function convert(table) { 40 let output = ""; 41 42 const rows = table.find("tr").not(options.excludeRows); 43 44 const numCols = rows.first().find("td,th").filter(":visible").not(options.excludeColumns).length; 45 46 rows.each(function (ignore, elem) { 47 $(elem).find("td,th").filter(":visible").not(options.excludeColumns) 48 .each(function (i, col) { 49 const column = $(col); 50 51 // Strip whitespaces 52 const content = options.trimContent 53 ? $.trim(column.text()) 54 : column.text(); 55 56 output += options.quoteFields 57 ? quote(content) 58 : content; 59 if (i !== numCols - 1) { 60 output += options.separator; 61 } else { 62 output += options.newline; 63 } 64 }); 65 }); 66 67 return output; 68 } 69 70 $.fn.table2csv = function (action, opt) { 71 if (typeof action === "object") { 72 opt = action; 73 action = "download"; 74 } else if (action === undefined) { 75 action = "download"; 76 } 77 78 // type checking 79 if (typeof action !== "string") { 80 throw new Error("\"action\" argument must be a string"); 81 } 82 if (opt !== undefined && typeof opt !== "object") { 83 throw new Error("\"options\" argument must be an object"); 84 } 85 86 options = $.extend({}, optionsDefaults, opt); 87 88 const table = this.filter("table"); // TODO use $.each 89 90 if (table.length <= 0) { 91 throw new Error("table2csv must be called on a <table> element"); 92 } 93 94 if (table.length > 1) { 95 throw new Error("converting multiple table elements at once is not supported yet"); 96 } 97 98 let csv = convert(table); 99 100 switch (action) { 101 case "download": 102 download(options.filename, csv); 103 break; 104 case "output": 105 $(options.appendTo).append($("<pre>").text(csv)); 106 break; 107 case "return": 108 return csv; 109 default: 110 throw new Error("\"action\" argument must be one of the supported action strings"); 111 } 112 113 return this; 114 }; 115 116}(jQuery)); 117
あなたの回答
tips
プレビュー