I’ll try explaining my use case a bit:
This is a book database, with each note representing one book, audiobook, podcast, eBook, graphic novel, etc.
And then I have a Gallery note, that shows all the books I’ve read. The Gallery note includes several dv.paragraph
lines that say things like dv.paragraph("# This year, I've read " + BookCount + " books and I've listened to " + AudioCount + " audiobooks (of which " + dv.pages('"Book Log/2022"').where(p => p.Medium == "Podcast").length + " were podcasts), for an overall " + (BookCount + AudioCount) + " books and audiobooks read.")
So far no problem. But if I want to do this: dv.paragraph("# I've read " + pagesum + " pages this year. And I've listened to " + (hoursum.toFixed(1)) + " hours.")
I need to have a function for pagesum
that somehow differentiates between the mediums with pages (i.e. books, ebooks, graphic novels, etc.) and those with hours (i.e. audiobooks and podcasts).
So far this would have been solved because the two categories had different length indicators, i.e. the ones with pages had the variable Pages:
and the ones with hours had Hours:
. Now I streamlined it so it all works with Length
(which is a little confusing since length
with a lower case l
is a built in function, I’m seeing, which is bad on my part).
This works. However, I just started reading graphic novels again, and would like to create a new medium type for that. And here comes my question, because the only solution for this is to go through all my Gallery notes, there are several (one for each year, plus some others), and add something like .where(p => p.Medium == "GraphicNovel")
for it.
My idea for transcluding would have made it easier since I’d only have to add the .where(p => p.Medium == "Podcast")
in one note and it would be transferred to the other instances.
I’m not sure if I explained it better this time, or just reiterated the same thing over again. Let me know if this helps.
I’m open to suggestions on how to make it better 
This is the whole codeblock:
```dataviewjs
let BookCount = (dv.pages('"Book Log/2022"').where(p => p.Medium == "Book" || p.Medium == "eBook").length);
let AudioCount = (dv.pages('"Book Log/2022"').where(p => p.Medium == "Audiobook" || p.Medium == "Podcast").length);
let pagesum = 0;
for(let i = 0; i < dv.pages('"Book Log/2022"').where(p => p.Medium == "Book" || p.Medium == "eBook").length; i++) {
if(dv.pages('"Book Log/2022"').where(p => p.Medium == "Book" || p.Medium == "eBook")[i].Length) {
pagesum += dv.pages('"Book Log/2022"').where(p => p.Medium == "Book" || p.Medium == "eBook")[i].Length;
}
}
let hoursum = 0;
for(let i = 0; i < dv.pages('"Book Log/2022"').where(p => p.Medium == "Audiobook" || p.Medium == "Podcast").length;i++) {
if(dv.pages('"Book Log/2022"').where(p => p.Medium == "Audiobook" || p.Medium == "Podcast")[i].Length) {
hoursum += dv.pages('"Book Log/2022"').where(p => p.Medium == "Audiobook" || p.Medium == "Podcast")[i].Length;
}
}
let Grade = 0;
for(let i = 0; i < dv.pages('"Book Log/2022"').length;i++) {
if(dv.pages('"Book Log/2022"')[i].Rating) {
Grade += dv.pages('"Book Log/2022"')[i].Rating;
}
}
let AvgRating = (Grade / (dv.pages('"Book Log/2022"').where(p => p.Medium == "Book" || p.Medium == "eBook" || p.Medium == "Audiobook" || p.Medium == "Podcast").length))
let today = DateTime.now().toFormat("ooo");
dv.paragraph("# This year, I've read " + BookCount + " books and I've listened to " + AudioCount + " audiobooks (of which " + dv.pages('"Book Log/2022"').where(p => p.Medium == "Podcast").length + " were podcasts), for an overall " + (BookCount + AudioCount) + " books and audiobooks read.")
dv.paragraph("# I've read " + pagesum + " pages this year. And I've listened to " + (hoursum.toFixed(1)) + " hours.")
dv.paragraph("# The average rating for the overall " + (BookCount + AudioCount) + " books and audiobooks is " + AvgRating.toFixed(1) + " out of 10.")
dv.paragraph("# Average book length: " + (pagesum / BookCount).toFixed() + " pages")
dv.paragraph("# Average audiobook length: " + (hoursum / AudioCount).toFixed(1) + " hours")
dv.paragraph("# Average pages read per day: " + (pagesum / today).toFixed(1) + " pages")
Any ideas and advice appreciated,
Thanks!