Skip to content

Instantly share code, notes, and snippets.

@WebInspectInc
Created January 24, 2024 15:00
Show Gist options
  • Save WebInspectInc/27b5bf5c82feb461970651a4fc9cecda to your computer and use it in GitHub Desktop.
Save WebInspectInc/27b5bf5c82feb461970651a4fc9cecda to your computer and use it in GitHub Desktop.
Summarizing books read with DataviewJS in Obsidian
```dataviewjs
const query = 'outgoing([[]]) AND #books';
const total =
dv
.pages(query)
.filter(item => item
.hasOwnProperty('pages'))
.values
.reduce((sum, item) => sum + item.pages, 0);
const booknum = dv.pages(query).length;
let rating =
dv
.pages(query)
.filter(item => item
.hasOwnProperty('rating'))
.values
.reduce((sum, item) => sum + item.rating, 0);
rating = (rating / booknum).toFixed(2);
dv.paragraph(`Number of books: ${booknum}`);
dv.paragraph(`Average rating: ${rating}`);
dv.paragraph(`Total pages read: ${total}`);
```
@WebInspectInc
Copy link
Author

I include this script in a note titled "Books read in YYYY", and I link to any book that I completed reading in said year.

Each book note includes a #books tag, and at least two properties: rating (0-5) and pages. This script will then output something like:

Number of books: 4
Average rating: 3.5
Total pages read: 2432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment