原文地址:https://www.cnblogs.com/liqinglucky/p/dataview.html
使用场景
工作中经常每周要将一周的工作汇总成周报。汇报内容会包括任务的开始结束时间和进展情况。obsidian dataview可以按照时间条件将库里的文件统计出来形成表格,并且可以自定义每一栏的标题。
被统计文件的格式要求
参考:Adding Metadata - Dataview (blacksmithgu.github.io)
由于dataview的元数据的最小单元是文件,能用到的基本元数据有
path: filename.md
folder:
name: filename
link: filename
outlinks:
inlinks:
etags: 标签
tags:
aliases:
lists:
tasks:
ctime: 5:03 下午 - 12 17, 2023
cday: 2023.12.17
mtime: 9:30 下午 - 12 17, 2023
mday: 2023.12.17
size: 289
starred: false
frontmatter:
ext: md
此外还可以自己在markdown文件中构造元数据 [1]。构造方法就是在文件开头加入
---
type: 文件类型
issueId:
theme: 任务主题
status: 完成/未完成
createTime: {{date}}
finishTime: {{date}}
tags:
- _标签_
---
我制作了一个任务文件模板.md
里面定义了以上元数据用于创建新文件。为了演示我创建两个工作文件。
工作任务1.md
---
type: 工作任务
issueId: issue-001
theme: 写项目计划
status: 完成
createTime: 2023-12-16 <<< 时间为上周
finishTime: 2023-12-16
tags:
- 项目1标签
---
工作任务2.md
--
type: 工作任务
issueId: issue-002
theme: 查资料
status: 完成
createTime: 2023-12-23 <<< 时间为本周
finishTime: 2023-12-23
tags:
- 项目1标签
- 查询标签
---
周报统计
语法参考:【Obsidian】做周记?一个dataview就够了_哔哩哔哩_bilibili
TABLE格式显示(详细:带文件名)
了解过dataview的语法后,查询本周代码:
TABLE theme as "主题", issueId, status as "现状", createTime as "启动时间", finishTime as "完成时间", createTime.weekyear as "周数", createTime.year as "年份"
// 注意:issueId没有重命名显示
where type = "工作任务"
//其他文件创建或修改日期跟本文件的周数一致的就筛选出来
where createTime.weekyear = this.file.mday.weekyear or finishTime.weekyear = this.file.mday.weekyear
//过滤出本年的文件
where createTime.year = this.file.cday.year
SORT createTime ASC
效果
查询上周代码:
TABLE theme as "主题", issueId , status as "现状", createTime as "启动时间", finishTime as "完成时间", createTime.weekyear as "周数", createTime.year as "年份"
where type = "工作任务"
where createTime.weekyear + 1 = this.file.mday.weekyear or finishTime.weekyear + 1 = this.file.mday.weekyear
where createTime.year = this.file.cday.year
SORT createTime ASC
效果
LIST格式显示(详细:带文件名)
代码:
list issueId + ": "+theme
where type = "工作任务"
where createTime.weekyear = this.file.mday.weekyear or finishTime.weekyear = this.file.mday.weekyear
where createTime.year = this.file.cday.year
SORT createTime ASC
效果:
TABLE格式显示(简单:不带文件名)
在我们汇报给其他人时,并不希望文件名这些内部信息显示出来。文件名只是作为统计的索引(key)。隐藏的方法是加上WITHOUT ID
。
代码:
TABLE WITHOUT ID
theme as "主题", issueId, status as "现状", createTime as "启动时间", finishTime as "完成时间"
where type = "工作任务"
where createTime.weekyear = this.file.mday.weekyear or finishTime.weekyear = this.file.mday.weekyear
where createTime.year = this.file.cday.year
SORT createTime ASC
效果:
LIST格式显示(简单:不带文件名)
代码:
LIST WITHOUT ID
issueId + ": " + theme
where type = "工作任务"
where createTime.weekyear = this.file.mday.weekyear or finishTime.weekyear = this.file.mday.weekyear
where createTime.year = this.file.cday.year
SORT createTime ASC
效果:
dataview的语法
语法格式:
<QUERY-TYPE> <fields>
FROM <source>
<DATA-COMMAND> <expression>
<DATA-COMMAND> <expression>
...
QUERY-TYPE 显示格式包括:
LIST
:列表
TABLE
:表格
TASK
:事项
CALENDAR
:日历
Data command:数据处理命令包括[2]
参考:obsidian插件dataview官方文档翻译 - 知乎 (zhihu.com)
- FROM like explained above.
- WHERE: Filter notes based on information inside notes, the meta data fields.
- SORT: Sorts your results depending on a field and a direction. 按一个或多个字段对所有结果进行排序。
//按照文件名升序排列
SORT file.name ASC
- GROUP BY: Bundles up several results into one result row per group. 对一个字段的所有结果进行分组。每个唯一的字段值产生一行,它有两个属性:一个对应于被分组的字段,一个是
rows
数组字段,包含所有匹配的页面。
//将文件创建时间相同的放同一组。
GROUP BY file.ctime
- LIMIT: Limits the result count of your query to the given number.
- FLATTEN: Splits up one result into multiple results based on a field or calculation. 对一个数组的每一行进行扁平化处理,在数组中的每个条目产生一个结果行。
// 将每个文献注释中的 "作者 "字段扁平化处理,使每个作者占一行。
FLATTEN file.author
显示的每一行数据项目都是以文件为最小单位,比如这里以作者名作为查表的key,可以找到表格中属于该作者的这一行的信息。
参考:
dataview介绍:可能是B站最简单易懂的Obsidian Dataview插件使用教程!帮你快速实现自动化筛选整理笔记!_哔哩哔哩_bilibili ↩︎
语法[Obsidian DataView 入门保姆级引导手册 - 知乎 (zhihu.com)](https://zhuanlan.zhihu.com/p/614881764 ↩︎