DATAVIEWJS - List of tasks containing backlinks from a specific directory

Hello community,

I am desperately trying to find a way to list tasks that have backlinks to notes from a directory named “97_Projet”.

WHY:
To separate my tasks by project, I have not used tags but a backlink to a note describing the project. All of my projects are described in notes grouped in the “97_PROJETS” directory.

OBJECTIVE:
List tasks by grouping them by project.

EXAMPLES OF TASKS related to “PROJET_X”:

  • [[PROJET_X]] - #TASK - Desinens atratum prima per atratum. [[UNKNOWN]]
    [created:: 2024-09-08] [due:: 2024-09-08]

This task is linked to a note describing “PROJET_X” located in the “97_PROJETS” directory.
This task is also linked to another note in the directory containing information about the individual “UNKNOWN” requesting the task “98_USER”.

ATTEMPT WITH DATAVIEWJS:

// DataviewJS script to list uncompleted tasks with backlinks to the "97_PROJETS" folder
dv.header(2, "Tâches non terminées par projet");

let tasks = dv.pages()
    .file.tasks
    .where(t => !t.completed && t.due) // Only include tasks that are not completed and have a due date
    .where(t => t.links && t.links.some(link => link.path.startsWith("97_PROJETS"))) // Check if the task contains backlinks and one of them points to "97_PROJETS"

// Sort tasks by due date
let sortedTasks = tasks.sort(t => t.due);

// Group tasks by project (backlink in the text pointing to the "97_PROJETS" folder)
let groupedTasks = {};
for (let task of sortedTasks) {
    let projectLink = task.links.find(link => link.path.startsWith("97_PROJETS")).path;
    if (!groupedTasks[projectLink]) {
        groupedTasks[projectLink] = [];
    }
    groupedTasks[projectLink].push(task);
}

// Loop through the grouped tasks
for (let project in groupedTasks) {
    dv.header(3, `Projet : ${project}`);
    dv.taskList(groupedTasks[project]);
}

HELP :pray:

Solution :

```dataviewjs
// 1. Récupération des Notes du Répertoire "97_PROJETS"
let projectNotes = dv.pages('"97_PROJETS"').map(p => p.file.name);

// 2. Récupération et Filtrage des Tâches
// 2.1 Accède à toutes les tâches de toutes les pages (notes) dans la base de données
let tasks = dv.pages()
    .file.tasks
    // 2.2 Filtre les tâches pour inclure uniquement celles ayant au moins un lien
    .where(t => t.text && t.text.includes("[["))
    // 2.3 Filtre encore pour ne garder que les tâches contenant le tag #TASK
    .where(t => t.text.includes("#TASK"));

// 3. Filtrage des Tâches Liées aux Notes dans "97_PROJETS"
// 3.1 Filtre les tâches pour ne garder que celles qui sont liées à des notes contenues dans projectNotes
let filteredTasks = tasks.filter(t => {
    let links = t.text.match(/\[\[(.*?)\]\]/g) || [];
    return links.some(link => {
        let noteName = link.replace(/^\[\[|\]\]$/g, '');
        return projectNotes.includes(noteName);
    });
});

// 4. Regroupement des Tâches par Note
// 4.1 Initialise un objet "groupedTasks" pour stocker les tâches regroupées par note
let groupedTasks = {};
for (let task of filteredTasks) {
    let links = task.text.match(/\[\[(.*?)\]\]/g) || [];
    let notes = links.map(link => link.replace(/^\[\[|\]\]$/g, ''));

    // 4.2 Incrémente les compteurs de tâches complètes et non complètes
    for (let note of notes) {
        if (projectNotes.includes(note)) {
            if (!groupedTasks[note]) {
                groupedTasks[note] = { completed: 0, uncompleted: 0 };
            }
            if (task.completed) {
                groupedTasks[note].completed++;
            } else {
                groupedTasks[note].uncompleted++;
            }
        }
    }
}

// 5. Affichage sous forme d'un tableau à 4 colonnes
// 5.1 Prépare les données pour le tableau
let tableData = Object.entries(groupedTasks).map(([note, counts]) => [
    note,
    counts.uncompleted,
    counts.completed,
    counts.uncompleted + counts.completed // Total des tâches
]);

// 5.2 Trie les données par nom de note
tableData.sort((a, b) => a[0].localeCompare(b[0]));

// 5.3 Affiche le tableau avec les colonnes demandées
dv.table(["Note", "Tâches non complètes", "Tâches complètes", "Total des tâches"], tableData);

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