DataviewJs - Code reuse: common place for scripts

I now have quite extensive javascript functions in dataview codeblocks to clearly display my tasks. I need these functions in many places, so different notes. So far, I make do by copying the codeblock with all the functions and pasting it into another codeblock. But this makes maintenance very difficult.
Is there a place where I can store repeatedly called functions in datavieJs codeblocks, so that I can access them from many notes and codeblocks?

Wish we had … hopefully some day someone will invent it.

At DataviewJS Snippet Showcase, there are already some scripts, maybe you have some to share, too?

EDIT: Since Templater can now use CommonJS modules, I asked if these could maybe used with Dataview, too. Maybe the devs talk … some collaboration between the Templater and Dataview devs might turn out great for us all.

Thanks for the quick reply. I am not sure if my spaghetti code would be helpful here…

Perhaps I misunderstand, but couldn’t you store them in template files, and call them as needed? I’m talking the core templating utility; it’s pretty easy to use, and works well as long as you don’t need to execute the code when called.

I have looong DataviewJs Codeblocks with functions like

  • function printTable(todolist, name, captionLevel=2) {
  • function AddMyPropertiesToDataviewPageObject(page) {
  • function GetAllContainedProjectTags(pages) {

to make my own calculations, filters and compilations of my tasks. These functions are called on every run and create my task tables. So the javascript functions have to be called on every task update.

You can! I keep my functions in Files/dataviewUtils.js and refer to it like this:

var dataviewUtils = require(app.vault.adapter.basePath + "/Files/dataviewUtils.js");

        "Next Action"],
    .where(page => dataviewUtils.getNextTask(page))
    .sort(page => dataviewUtils.getNextAction(page))
    .sort(page => dataviewUtils.calculateProjectStatus(page), "desc")
    .map(page => [, 
        dataviewUtils.getNextAction(page)] )

In your required file make sure to export the function so require can see it:

function getNextTask(page) {
    for (const task of page.file.tasks) {
        if (task.completed == false) {
            return task;
    return null;
exports.getNextTask = getNextTask;

Now that’s a helpful find! Thanks for sharing, @Craig!

1 Like

I have tested it immediately. → It works!
It saves me the whole week. Oh man. Thanks. Really.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.