DataviewJS Snippet Showcase

@a_wue Possible you haven’t updated Dataview for a while? The dateformat function is rather new. (Luxon comes with Dataview, no worries.)

(Community plugins don’t auto-update. You have to manually “Check for updates” in Settings, and perform the update by clicking “Update all” or “Update” on selected plugins.)

That did it. Thank you!

1 Like

Have you toughs about integrating standard DATAVIEW syntax for creation of the tables, and JS only for making variable that will be used in the table?

What I’m struggling now is that I have DATAVIEW queries, but I want to add just one field that needs to be calculated by Javascript.


Also is there any way we can add javascript into file like is the Obsidian.css, and just call it? I really don’t like to have bunch of script in dataview script.

Ideally, even compatible/reachable using Templater. That’d just be the thing. But no cigar, yet.

From a standpoint of code-reuse, I’d love to be able to write a few functions and call them up in both Templater and Dataview … One can dream, right?

From a standpoint of code-reuse, I’d love to be able to write a few functions and call them up in both Templater and Dataview … One can dream, right?

Any technical difficulty to load Javascript from the file located somewhere in .Obsidian/?

Have you toughs about integrating standard DATAVIEW syntax for creation of the tables, and JS only for making variable that will be used in the table?

Also any technical difficulty to execute variables that are actually JS scripts functions, from standard Dataview Syntax?

Also is there any way we can add javascript into file like is the Obsidian.css, and just call it?

You’ll find an answer to that in this thread, but unfortunately it doesn’t work on Android or iOS. The dataview script where you are importing your common code will blow up on mobile.

I am very grateful for your snippet that gives me a table with every page in my vault. Because I want to clean the vault from unused /old files, it is very useful for me.

Maybe it’s the most stupid question – but why do I get an error message, when I rename the file name from “Inbox” to anything else, like “Inhaltsverzeichnis”. It says: Dataview: Query returned 0 results.

This is the code-snippet I use:

table dateformat(file.mtime, "dd.MM.yyyy") as "bearbeitet" where file.name != this.file.name and contains(file.path, this.file.folder) sort file.mtime descending 

Thanks for any clarification!

This might have to do with caching. After renaming -Inbox TOC to Inhaltsverzeichnis, close and re-open the TOC note once, and the Dataview content should update:

Btw, I have the code slightly refined in the meantime:

table
    dateformat(file.ctime, this.dateformat) as Created,
    dateformat(file.mtime, this.dateformat) as "Last modified"
where file.name != this.file.name and contains(file.folder, this.file.folder)
sort file.mtime descending

and use a YAML frontmatter variable for the dateformat:

dateformat: "yyyy-MM-dd HH:mm" # Luxon format
2 Likes

:grinning: Yes, after closing and re-opening, everything worked out fine, thanks!

Just in case, if it doesn’t bother you: Do you happen to know, how to add a column with a moment().fromNow(); function – to show for how long a file has not been worked on? Unfortunately, I have not any programming background…

table 
dateformat(file.mtime, "dd.MM.yyyy") as "bearbeitet" 
moment().fromNow(); as "unbearbeitet seit" 
where file.name != this.file.name and contains(file.path, this.file.folder) sort file.mtime descending 

Glad it works for you!

In Dataview, you could add a new column “Untouched for” like so:

(date(now) - file.mtime) as "Untouched"

This will output rather long “human-readable” strings like “2 months, 1 weeks, 2 days, 11 hours, 2 minutes, 15 seconds, 107 ms”. If instead you wanted to show only the number of days untouched, it might require using DataviewJS.

2 Likes

Thanks, that worked out!

1 Like

I played a little … if you rather have a “nice” German display, and the “last changed” as days past, you could use the following DataviewJS code:

Show a TOC for this & subfolders, nice formatting

---
#dateformat: "yyyy-MM-dd HH:mm" # Luxon format
dateformat: "dd.MM.yyyy" # Luxon format
locale: de
---

# Inhaltsverzeichnis

```dataviewjs
var n = luxon.DateTime.now();
var format = dv.current().dateformat || 'yyyy-MM-dd';
var locale = dv.current().locale || 'en';
dv.table(
    ['Notiz', 'Angelegt', 'Letzte Änderung'],
    dv.pages()
        .where(p => p.file.name != dv.current().file.name
            && p.file.folder.includes(dv.current().file.folder))
        .sort(p => p.file.mtime, 'desc')
        .map(p => [
            p.file.link,
            p.file.ctime.setLocale(locale).toFormat(format), 
            p.file.mtime.toRelative({locale:locale,unit:'days'})])
)
```

For the “techies”: In this example I use strictly Luxon, not moment.js.

It will then look like this:

For our non-German readers

Just change the YAML to your locale and an appropriate dateformat string, like:

dateformat: "yyyy-MM-dd" # Luxon format
locale: en

and modify the header line in the dataviewjs part to use headings in your language:

```dataviewjs
…
dv.table(
    ['Note', 'Created', 'Last change'],
…

and it’ll look like this:

:smiley:

12 Likes

Thanks for all of your posts! They have been helpful in getting started. I have a beginner question, do I need to require Luxon library at some point in Obsidian? I know Moment is already installed and I can not seem to locate how to add additional JS libraries to use in code such as dataviewjs? Thanks for any guidance.
Don

1 Like

As you say, moment.js is already available, and Luxon is available in ```dataviewjs code blocks (Dataview brings it in, but not globally, use luxon.something).

Supergenial! Works like a charm - thanks a lot! :heart_eyes:
Now I enjoy my new TOC a lot. Way back, in Notion, I used a similar way to sort notes by date of modification. It just helps me to keep an eye on rarely used files.

1 Like

wow thank you so much for this, it’s exactly what I’m looking for. Though I’m not quite sure how to implement. I’ve got dataview installed (have been using it for leaflet maps).

Can I have this function just look at a folder instead of using a searchterm? Or at least how do I identify a note as being ‘People’ so that it will show up on the birthday list? Also, where do I put the dataviewjs code?

thanks so much :slight_smile:

Happy you like it!

For searchterm:, you can use all valid Dataview search terms. In this case, it looks in the folder “People” and its subfolders.

This DataviewJS code usually goes in an extra note, like “Upcoming Birthdays”, but you could it include elsewhere of course.

HTH

2 Likes

Thank you so much for this script! It’s making it much easier for me to set up my hotkeys and keeping them on the side as I figure out my workflow.

Is there an easy way to omit any of the commands not assigned with any hotkeys? (get rid of all the rows containing “-”?)

I don’t know any javascript. Managed to figure out some basics like swapping out Mac commands and removing the command ID column, but struggling to find a solution for the above.

Thanks for your kind words, appreciate it! You can use the “Commands sorted by assigned hotkey” part alone, it will only show the commands with hotkeys, not the complete command list. Or put the two parts in two notes :wink:

1 Like

Hey everyone! I just wanted to share that my plugin CustomJS is now available on the community store. It allows you to write and reuse javascript across Obsidian, and works on desktop and mobile. It pairs great with Dataview, which is my primary use case. See the Advanced Example section of the README for how I’m using it with dataview to manage my tasks.

5 Likes