Define a variable in DataviewJS and then call it on the page

I’m exploring the possibility that I can dynamically update the priority of tasks (using tags), but the tag will be generated by dataview.

I have a due date which I compare to today’s date. If the due date is less than 7 days away, I give a variable pTag a value of “#high”, and similar for “#medium” and “#low”. This is all working within my DataviewJS codeblock (I get correct values for pTag when console.log).

My problem is that I cannot seem to access the value of pTag in an inline dataview (either $=... or =....

Am I completely insane or is there no way to get the value from the codeblock outside of the codeblock?

Help (if I had hair, I’d be pulling it out)

Thanks

possibly this plugin gives a workaround?

@cfritze MetaEdit is possible, but I’d prefer not to bring another plugin into the mix.

It seems odd that my variable is clearly changing within the DataviewJS, as I manipulate the due date (defined in YAML front matter).

So, it is really a question of whether the variables defined within a DataviewJS codeblock are ‘ring-fenced’ and not accessible outside of the codeblock. Which seems odd, because we can clearly move data into it.

Maybe I’m just missing the right syntax for the inline dataview call. I’ve tried everything I could think of, but it may just be that I’m not seeing it.

Actually, I’m not sure MetaEdit would work. All of the examples seem to suggest that you need a ‘trigger’ (button) to call the update action. What I’m trying to achieve is something that updates automatically based on comparison of the dates, without the need for user interaction.

It’s all good. And your script helps me too!

I’ve removed the previous post, as I am finding that there are a number of issues:

  1. The updating is very inconsistent. If I change the due date in the YAML, the pTag variable sometimes doesn’t update at all. Other times it happens almost immediately. I’ve tried adjusting the refresh for dataview, but this doesn’t seem to have much effect.

  2. While (when it works) it is really nice there is limited value because the dynamic value does not work in other dataviews. I was hoping to have auto-updating priorities for tasks (as they get closer the their priority goes up), but if this cannot be seen in a list view (seeing all of the tasks) then it is not really viable.

The script is below, in case you still have a use for it. Maybe someone else will chime in with some recommendations.


	const {update} = this.app.plugins.plugins["metaedit"].api
	let pTag = ""
	let dueDate = this.current().due
	let mLimit = dv.luxon.DateTime.now().plus({days: 7}).toFormat('yyyy-MM-dd')
	let lowLimit = dv.luxon.DateTime.now().plus({days: 21}).toFormat('yyyy-MM-dd')

	if(dueDate>=mLimit){
		update('pTag', 'medium', this.current().file.path)
	}
	if(dueDate<=mLimit){
		update('pTag', 'high', this.current().file.path)
	}
	if(dueDate>=lowLimit){
		update('pTag', 'low', this.current().file.path)
	}

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