Sum of read bookpages

What I’m trying to do

I have a list of YAML Metadata
pages: startpage - endpage
of different chapters of a book I am working with.
My aim is, to

  • show startpage and endpage in a tableview
  • build the sum of theses pages in a third row
  • show the total amount of pages below.

Things I have tried

Here is my code:

--- 
pages:
  - 1 - 101
  - 101 - 150
  - 151 - 240
---

```dataview 
TABLE WITHOUT ID 
split(pages, "-")[0] AS Startpage,
split(pages, "-")[1] AS Endpage,
number(pages) AS Summe
FROM "currentfile" 
FLATTEN pages 
WHERE file.name = this.file.name 

TOTAL: =round(sum(number(this.pages)), 2)

It tried to use split() but the result is a datatype text instead of number. With datatype text I can only use it for the first and second row but not for building the amount of pages.

I can get the startpage by using number() as a datatype number but it is working only for the startpage as first number.
I cannot extract the second number behind the - .
The combination of number(split()) is not working.

Do you have any suggestions?

My way to handle this is to build a query doing all but the sum line, and then do that query within a dataviewjs query to calculate the sum.

So try the following in a file of its own, and see if you can’t adapt some of it to your liking.

---
pages:
- 1 - 101
- 101 - 150
- 151 - 240
---

## Original query

```dataview 
TABLE WITHOUT ID 
split(pages, "-")[0] AS Startpage,
split(pages, "-")[1] AS Endpage,
number(pages) AS Summe 
FLATTEN pages 
WHERE file.name = this.file.name
```

## Chapter query
```dataview 
TABLE WITHOUT ID start, end, number(end - start) as total
FLATTEN pages as chapter
FLATTEN list(split(chapter, "-")) as p
FLATTEN number(p[0]) as start
FLATTEN number(p[1]) as end
WHERE file.name = this.file.name
```

## With total sum

```dataviewjs 

const result = await dv.query(`
TABLE WITHOUT ID start, end, number(end - start) as total
FLATTEN pages as chapter
FLATTEN list(split(chapter, "-")) as p
FLATTEN number(p[0]) as start
FLATTEN number(p[1]) as end
WHERE file.name = this.file.name
`)

if (result.successful) {
  const total = result.value
    .values
    .map(c => c[2])
    .reduce((acc, val) => acc + val, 0)

  result.value.values.push(["",  "<span style='float: right'><strong>Total:</strong></span>", total])
  dv.table(result.value.headers,
     result.value.values)
} else
 dv.paragraph(`~~~~${ result.error }~~~~`)

```
1 Like

Thank you so much. It worked out very well.
Problem is solved.

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