Quantcast
Channel: 簡睿隨筆
Viewing all articles
Browse latest Browse all 897

[Obs#30] 更多的Dataview: 動態查詢筆記資料

$
0
0

查詢語法與YAML簡介

Dataview提供類似資料庫的結構化查詢語言(Structured Query Language,SQL)的簡化語法,讓我們可以查詢筆記的詮釋資料(Metadata),將筆記的即時動態透過簡單的語法就能顯示出來。

Dataview使用的詮釋資料主要來自筆記開頭的YAML區域與檔案資訊:
– YAML區:筆記開頭以三個減號夾住的區域,用來定義該篇筆記的屬性(詮釋資料,Metadata)
– YAML欄位:固定欄位有aliases與tags,其他我們可自行定義
– 欄位的值不要使用中括號和井號
– 欄位的值若有多個,用中括號夾住
– YAML輸入完成後,檢視預覽面板是否可正確解析,若顯示(INVALID YAML)則否必須修正

```dataview
[TABLE|LIST|TASK] field1, field2, ..., fieldN FROM #tag or "folder" or [[link]]
WHERE somefield = somevalue
COMMAND argument
COMMAND argument
...
COMMAND argument
```

相關連結

增強功能

  1. 查詢來源(from)增加連結 [[link]]
    • from [[Link]]: 連結到我的筆記 (Backlink)
      • list from [[note]] // List all notes which link to [[note]]
    • from outgoing([[Link]]): 我的筆記向外連結的筆記
      • list from outgoing([[note]]) // List all notes that [[note]] links to
  2. Group by
  3. Flatten
  4. 可使用 as 中文欄名了

範例

Todo list/進度管理

1. 特定標籤+Emoji
```dataview
table flag as 標示,file.ctime as 建檔時間
from #project-todo
```
2. Group by

用Group by分群。下列用status分成4個群組。分群後產生的陣列是rows。

```dataview
table status, rows.file.name as 檔名, rows.file.ctime as 建檔時間
from #project-todo
where status
group by status
```
3. Flatten

把多個資料攤平逐一顯示。例如一本書有3名作者則顯示成3筆記錄。

```dataview
table member,status,flag
from #project-todo
sort file.name
flatten member
```

contains(含有)

▼ 檔名含有特定字串(Obsidian)的筆記

```dataview
list from ""
where contains(file.name, "Obsidian")
```

▼ 找出按〔Ctrl+V〕貼入的圖檔

```dataview
list from ""
where contains(file.name, "Pasted ")
```

反向/內部連結

▼ 反向連結:指向from的筆記

```dataview
table file.ctime as "建檔日期"
from [[SmartGit Notes]]
```

▼ 向外連結:from裡使用到的筆記

```dataview
table file.ctime as "建檔日期"
from outgoing([[SmartGit Notes]])
```

日期比對

  • now, today, yesterday, tomorrow等只能放在date()裡
  • 日期與日期比較
```
where file.mtime >= date(yesterday)
```
  • 3天內修改的筆記: 使用dur函數
```
where date(today) - file.mtime <= dur(3 days) 
```
  • 範例將#project-todo改成 “” 就可查詢整個儲存庫。
1. 今日修改的檔案
```dataview
table file.mtime 
from #project-todo
WHERE file.mtime >= date(today) - dur(1 day)
```
2. 三天內建立的筆記
```dataview
table file.ctime
from #project-todo
where file.ctime >= date(today) - dur(3 days) 
sort file.mtime desc
```
3. 今年新建立的筆記
```dataview
table file.ctime
from #project-todo
where file.ctime.year = date(today).year
```
4. 建檔迄今過了多少時間
```dataview
table file.ctime, date(now)-(file.ctime) as 迄今時數
from #project-todo
sort date(today)-(file.ctime)
```

教學影片

##您可能也會有興趣的類似文章


Viewing all articles
Browse latest Browse all 897

Trending Articles