Inline JS Query: Tasks from Files Created within the Week

What I’m Trying to Do:

Trying to create an inline JS query to:

  1. Pull tasks from all files located in folder “1. Daily Notes”.
  2. Filter to only contain tasks that are unchecked, that have the metadata fields [urgent:: true] and [important:: true], and are from files created within the past week from the current date.
  3. Sort tasks by the “created” shorthand field.

Essentially, I am trying to write the following DQL into an inline JS query:

TASK
FROM "1. Daily Notes"
WHERE !completed AND file.cday > (date(now) - dur(1w)) AND (urgent AND important)
SORT created

It being an inline JS query is the important part here.

Admittedly, I have no experience with JS and have been entirely going off of references and forum posts. Figured it was high time that I posted here myself.

This is as far as I got:

$=dv.taskList(dv.pages('"1. Daily Notes"').file.tasks.where(t => !t.completed).where(t => t.creationDate - dv.current().date, "YYYY-MM-DD", -7))

Regards.

What date related properties do you have in your daily notes? Do you have a field called date or creationDate or created?

Your queries use either of these in addition to file.cday, which all together makes me confused and suspect you’re just using some AI to generate these queries.

As far as date related properties go, my daily note titles are always formatted as “YYYY-MM-DD”. I suppose it wouldn’t be difficult to have a date field though.

Your queries use either of these in addition to file.cday , which all together makes me confused and suspect you’re just using some AI to generate these queries.

That would just be me running out of brain juice after throwing whatever date related data commands I could find on the internet at my query.

The tricky part of this query was that after you’ve done .file.tasks you’re only dealing with the tasks metadata, and then it seems you’ve not got access to the file-part of it anymore. I’m not sure if this is to be considered a bug or not, as I kind of felt it should be still available. I need to look into that later on.

However, it can be circumvented by sorting and filtering on the date before we filter on the task themselves. The dataviewjs query we then end up looks like this, and this is the variant I suggest using for further adapting and modifications:

```dataviewjs
const endOfLastWeek = moment("2024-03-07").add(-7, "days").format("YYYY-MM-DD");

const result = dv.pages('"1. Daily Notes"')
  .where(p => p.file.day && p.file.day.toISODate() > endOfLastWeek)
  .sort(p => p.file.day)
  .file.tasks
  .where(t => 
    !t.completed &&
    t.urgent && 
    t.important);
  
if ( result.successful )
  dv.taskList(result, false);
else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~");
```

Here we pre-calculate the end of last week, before we enter the actual query, so we don’t have to repeat that for every row. The query then proceed to only include those having a defined file.day which are within the last week. After that it narrows the rows down to the tasks, and filters out anything not completed, but urgent and important. The final step, which I do strongly suggest keeping is to output the tasklist if all went well, and an error message if something went wrong.

To arrive at the wanted inline dataviewjs variant, we just need to concatenate all of the above lines, and if you’ve kept the semicolons where I strategically placed in the query above, it’s just a matter of removing extra newlines and space, and you’ll arrive at this line:

`$= const endOfLastWeek = moment("2024-03-07").add(-7, "days").format("YYYY-MM-DD"); const result = dv.pages('"ForumStuff/f77/f77895"').where(p => p.file.day && p.file.day.toISODate() > endOfLastWeek).sort(p => p.file.day).file.tasks.where(t => !t.completed && t.urgent && t.important); if ( result.successful ) dv.taskList(result, false); else dv.paragraph("~~~~\n" + result.error + "\n~~~~") `

Hope that is meeting your requirements

Yeah, that got it going as I wanted it. Thanks!

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