1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| const {createButton} = app.plugins.plugins["buttons"] const groups = dv.pages(`"Codes"`).groupBy(p => p.file.folder) //不想展示的文件夹 const folderArr = ["MOD","后端","python","本地","git"] let cdata = [] dv.el("b", createButton({ app, el: this.container, args: {name: '导出',class:'tiny'}, clickOverride: {click: downloadCSV, params: [cdata,"data.csv"]} }) ); for (let group of groups) { if (group.key!="Codes" && folderArr.filter(f => group.key.includes(f)).length == 0) { dv.paragraph(group.key); let tableData = dv.table(["Name","key points","阅读次数"], group.rows .sort(k => k.file.name, 'asc') .map(function(k){ cdata.push([ k.file.path.replace(/Codes\/|\.md/g, ''), k["key-points"]?k["key-points"].toString():" " ]) return [k.file.link, k["key-points"]?k["key-points"].toString():" ", k["times-of-view"]] })); } } console.log(cdata) // 将二维数组转换为 CSV 格式的字符串 function arrayToCSV(data) { return data.map(row => { return row.map(cell => { // 如果单元格包含逗号、换行符或双引号,需要用双引号包裹 if (typeof cell === "string" && (cell.includes(",") || cell.includes("\n") || cell.includes('"'))) { return `"${cell.replace(/"/g, '""')}"`; // 替换双引号为两个双引号 } return cell; }).join(","); // 用逗号分隔单元格 }).join("\n"); // 用换行符分隔行 } // 下载 CSV 文件 function downloadCSV(data, filename = "data.csv") { const csvString = arrayToCSV(data); const blob = new Blob([csvString], { type: "text/csv;charset=utf-8;" }); const link = document.createElement("a"); if (link.download !== undefined) { // 特性检测 // 创建一个 blob URL const url = URL.createObjectURL(blob); link.setAttribute("href", url); link.setAttribute("download", filename); link.style.visibility = "hidden"; document.body.appendChild(link); link.click(); document.body.removeChild(link); } } //downloadCSV(cdata)
|