DataviewJs - Code reuse: common place for scripts

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");

dv.table(
    [
        "Page", 
        "", 
        "Next Action"],
    dv.pages("")
    .where(page => dataviewUtils.getNextTask(page))
    .sort(page => dataviewUtils.getNextAction(page))
    .sort(page => dataviewUtils.calculateProjectStatus(page), "desc")
    .map(page => [
        page.file.link, 
        dataviewUtils.calculateProjectStatus(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;
45 Likes