dataview-table: Pull notes from 2 different tags and populate 2-column table based on tags #2388
-
Hello, I am trying to create a table as follows: I have two sets of notes. For each note, there exists a partner-note in the other set with a slightly different name. These sets are in different folders, and contain unique tags. Additionally, they also share a tag which is unique to these files,
There might be entries in there which do not have a counterpart in the other folder - but those do not have the respective tags. So I suppose I want to perform the following steps:
The entries within the table should then link to the respective notes. Can someone explain to me how this would be done? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
With a lot of pain and ChatGPT, I have arrived at this: ```dataviewjs
// Fetch pages from the experiments and reports directories
const experiments = dv.pages('"experiments"').where(p => p.tags && p.tags.includes("experiment_report"));
const reports = dv.pages('"report/exp_analyses"').where(p => p.tags && p.tags.includes("experiment_report"));
// Create maps to store file paths and cleaned names
const experimentMap = new Map();
const reportMap = new Map();
// Helper function to clean names
function cleanName(fileName, prefix) {
return fileName.replace(/^[\_]+/, "").replace(new RegExp(`^${prefix}_`), ""); // Remove leading underscores and prefix
}
// Populate experimentMap
for (const exp of experiments) {
const cleanedName = cleanName(exp.file.name, "exp");
experimentMap.set(cleanedName, exp.file.path);
}
// Populate reportMap
for (const rep of reports) {
const cleanedName = cleanName(rep.file.name, "rep");
reportMap.set(cleanedName, rep.file.path);
}
// Get all unique base names from both maps
const allBaseNames = new Set([...experimentMap.keys(), ...reportMap.keys()]);
// Generate table rows
const rows = Array.from(allBaseNames).map(name => {
const experimentPath = experimentMap.get(name);
const reportPath = reportMap.get(name);
// Create file links or show '/' if the file doesn't exist
const experimentLink = experimentPath ? `[[${experimentPath}|${name}]]` : "/";
const reportLink = reportPath ? `[[${reportPath}|${name}]]` : "/";
// Debugging information
console.log(`Name: ${name}, Experiment Path: ${experimentPath}, Report Path: ${reportPath}`);
console.log(`Experiment Link: ${experimentLink}, Report Link: ${reportLink}`);
return [experimentLink, reportLink];
});
// Display the table
dv.table(["Experiment", "Report"], rows);
``` which successfully renders to if a match is found, and to when a counterpart is not found |
Beta Was this translation helpful? Give feedback.
With a lot of pain and ChatGPT, I have arrived at this: