s4
changeset 997:f7cd4528926b
Warning message for BOM fixed
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Mon, 17 Oct 2022 17:11:04 +0859 |
parents | a50f5b0b2c3d |
children | f73eece61ee4 |
files | s4-main.js |
diffstat | 1 files changed, 17 insertions(+), 4 deletions(-) [+] |
line diff
1.1 --- a/s4-main.js Sun Oct 16 07:56:34 2022 +0859 1.2 +++ b/s4-main.js Mon Oct 17 17:11:04 2022 +0859 1.3 @@ -816,12 +816,13 @@ 1.4 quizwarnVisible = true; 1.5 } 1.6 } 1.7 - function downloadFile(filename, content) { 1.8 + function downloadFile(filename, content, BOM) { 1.9 let bom = new Uint8Array([0xEF, 0xBB, 0xBF]); 1.10 - let str = new Blob([bom, content], {type: "text/csv"}); 1.11 + let str = new Blob(BOM ? [bom, content] : [content], 1.12 + {type: "text/csv"}); 1.13 var uri = URL.createObjectURL(str); 1.14 let a = document.createElement("a"); 1.15 - a.download = filename; 1.16 + a.download = (BOM ? "BOM-" : "")+filename; 1.17 a.href = uri; 1.18 document.body.appendChild(a); 1.19 a.click(); 1.20 @@ -829,6 +830,7 @@ 1.21 } 1.22 function getTextContentCSV_1(e) { 1.23 let blogtbl = document.querySelector("table.blog_replies"); 1.24 + let needBOM = e.ctrlKey; 1.25 if (!blogtbl) return; 1.26 let trw = blogtbl.querySelector("tr.warn"), a; 1.27 if (trw && (a=trw.querySelector("th>a"))) { 1.28 @@ -843,6 +845,16 @@ 1.29 } 1.30 } 1.31 } 1.32 + if (navigator.userAgent.match(/Windows/)) { 1.33 + if (!e.ctrlKey && !e.shiftKey && !window.confirm(`Excelで読ませるCSVの場合はBOMが必要です。 1.34 +その場合は一度キャンセルして Ctrl キーを押しながらボタンクリックして下さい。 1.35 +逆にExcel以外(GoogleスプレッドシートやLibreOfficeや他のツール)で読む場合はBOMをつけるとファイルの1行目の先頭にゴミのようなものが見える場合あるのでそのときは除去する必要があります。 1.36 +今後もBOM不要の場合はShiftキーを押しながらクリックして下さい。 1.37 +If you feed this CSV into Microsoft Excel, consider adding BOM sequence that can be prepended by pressing Control key with click. 1.38 +In this case, click CSVget with ctrl key after Cancel this dialog. 1.39 +If you never need BOM, press Shift key with click.`)) 1.40 + return; 1.41 + } 1.42 outcsv = [] 1.43 for (let row of blogtbl.querySelectorAll("tr[id]")) { 1.44 let tds = row.querySelectorAll("td"), 1.45 @@ -860,7 +872,7 @@ 1.46 let line = new CSV(outcsv, {header:true}).encode(), 1.47 fn = myurl.replace(/.*\?/, "").replaceAll("+", "-") 1.48 .replace(/#.*/, "").replace("-n:all", ""); 1.49 - downloadFile(fn+".csv", line); 1.50 + downloadFile(fn+".csv", line, needBOM); 1.51 } 1.52 function getTextContentCSV(e) { 1.53 if (!document.getElementById("csvminjs")) { 1.54 @@ -913,6 +925,7 @@ 1.55 btn.type = "button"; 1.56 btn.title = `見えている書き込みをCSVで取得します 1.57 全件表示されていることを確認してから利用して下さい。 1.58 +Excelで利用する場合は Ctrl を押しながらクリックして下さい。 1.59 Get seen TEXT content as CSV.`; 1.60 btn.addEventListener("click", getTextContentCSV, false); 1.61 let artlink = td.querySelector('a[accesskey="f"]');