js 脚本
module.exports = async (params) => {
const { app } = params;
// ================= 配置区域 =================
// 请在此处填写你要扫描的文件夹路径(大小写敏感)
// 例如:"项目管理/软基底项目" 或 "Work/Projects"
// 如果留空 "",则会扫描整个仓库(慎用!)
const TARGET_FOLDER_PATH = "002-project/000-任务规划/003-任务总览";
// ===========================================
// 获取仓库中所有的 Markdown 文件
const allFiles = app.vault.getMarkdownFiles();
// 筛选出属于指定文件夹下的文件
// f.path 包含完整路径,我们检查它是否以指定文件夹路径开头
const targetFiles = allFiles.filter(f => f.path.startsWith(TARGET_FOLDER_PATH));
if (targetFiles.length === 0) {
new Notice(`❌ 未在文件夹 "${TARGET_FOLDER_PATH}" 下找到任何 Markdown 文件。请检查路径配置。`);
return;
}
// 提示开始
new Notice(`开始处理:正在扫描 ${targetFiles.length} 个文件...`);
let processedCount = 0;
// 遍历每一个文件进行处理
for (const file of targetFiles) {
try {
// 1. 读取文件内容
const fileContent = await app.vault.read(file);
// 2. 统计任务数量
// 匹配所有任务 (- [ ] 或 - [x] 等)
const allTasksMatch = fileContent.match(/- \[[^\]]\]/g);
const totalCount = allTasksMatch ? allTasksMatch.length : 0;
// 匹配已完成任务 (- [x] 或 - [X])
const completedTasksMatch = fileContent.match(/- \[[xX]\]/g);
const completedCount = completedTasksMatch ? completedTasksMatch.length : 0;
// 如果该文件没有任何任务,你可以选择跳过更新,或者依然强制写入 0
// 这里设定:只要是目标文件夹的文件,都进行更新,方便统一查看
// 3. 更新元数据 (Frontmatter)
await app.fileManager.processFrontMatter(file, (frontmatter) => {
// 只有当数字确实发生变化时才更新,避免不必要的文件修改记录(可选优化)
if (frontmatter["总计"] !== totalCount || frontmatter["完成"] !== completedCount) {
frontmatter["总计"] = totalCount;
frontmatter["完成"] = completedCount;
processedCount++;
}
});
} catch (error) {
console.error(`处理文件 ${file.path} 时出错:`, error);
}
}
// 4. 完成提示
if (processedCount > 0) {
new Notice(`✅ 处理完成!共更新了 ${processedCount} 个文件的任务数据。`);
} else {
new Notice(`✅ 扫描完成。数据已是最新的,无需更新。`);
}
}
yaml 格式
---
总计: 5
完成: 1
每天: 1
单位: 节
优先级: 🟥
tags: 进度
状态: DOING
aliases:
---
dataview 代码
```dataview
table without id "[" + aliases + "](" + file.name + ")" + "<br>🎯 " + 每天 + " " + 单位 + " ⏳ " + 完成 + "/" + 总计 +"" as 项目, "<progress max=100 value=" + round((完成/总计)*100) + "></progress>" + round(完成/总计*100) + "%" + "<br>" + 优先级 + " 余 " + round(总计 - 完成)+ " " + 单位 + ",需 " + round((总计 - 完成)/每天) + " 天" +"" as 进度
from "002-project/000-任务规划/003-任务总览" and #进度
where contains(状态,"DOING")
sort 优先级,file.name asc
```