Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 01-17-2021, 12:22 AM   #1
Quuzuu
Member
Quuzuu began at the beginning.
 
Posts: 11
Karma: 10
Join Date: Dec 2020
Device: Kindle iOS App
Template: sum of multiple integers from other columns

Suppose in an attempt to better rate my books, I created 4 custom columns: 'story', 'characters', 'writing', 'ending', each an integer between 1-10.

Then I'd like to add another column called 'total_score', which is the sum of the above. How should I build the template?

(General Program Mode is preferred, if possible)

Thanks!!
Quuzuu is offline   Reply With Quote
Old 01-17-2021, 02:49 AM   #2
Quuzuu
Member
Quuzuu began at the beginning.
 
Posts: 11
Karma: 10
Join Date: Dec 2020
Device: Kindle iOS App
I tried something like:
program: field(#'story') + field(#'characters') + field(#'writing') + field(#'ending')
which didn't work.

I also tried program: add(x, y), but it only accept 2 arguments, which could work, but seems a little messy.

Last edited by Quuzuu; 01-17-2021 at 02:52 AM.
Quuzuu is offline   Reply With Quote
Advert
Old 01-17-2021, 03:05 AM   #3
Quuzuu
Member
Quuzuu began at the beginning.
 
Posts: 11
Karma: 10
Join Date: Dec 2020
Device: Kindle iOS App
Update: my current solution (wonder if there's a cleaner way):

program:
r = 0;
r = add(r, field('#story'));
r = add(r, field('#characters'));
r = add(r, field('#writing'));
r = add(r, field('#ending'));
floor(r);
Quuzuu is offline   Reply With Quote
Old 01-17-2021, 09:18 AM   #4
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,352
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Thumbs up

In the next version of calibre (5.10 probably) add() and multiply() accept a variable number of arguments. Thus, once released your solution can be written as
Code:
program:
    floor(add(field('#story'), field('#characters'), field('#writing'), field('#ending')))
To head off questions: I changed add() and multiply() because the operations are commutative, giving the same answer regardless of order of evaluation, while subtract and divide are not. I don't want to worry about guaranteeing the order of evaluation within the function.

Last edited by chaley; 01-17-2021 at 06:00 PM. Reason: Correct mistaken function name
chaley is offline   Reply With Quote
Old 01-17-2021, 04:35 PM   #5
Quuzuu
Member
Quuzuu began at the beginning.
 
Posts: 11
Karma: 10
Join Date: Dec 2020
Device: Kindle iOS App
Thanks so much!
Quuzuu is offline   Reply With Quote
Advert
Reply

Tags
template


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Showing template-computed columns as [rating] stars isn't possible? kaoruAngel Library Management 2 11-06-2017 08:49 AM
Need help in Custom Columns, template syntax, and rounding numbers dennocoil Calibre 0 07-10-2017 03:24 PM
Need more help with searching multiple columns at once beckyt Library Management 3 06-19-2016 06:35 PM
Request: template-making assistance for column built from other columns iienderii Library Management 9 04-04-2016 10:27 PM
Can I generate sum of column or columns? GeekyGal Calibre 5 09-29-2011 07:32 PM


All times are GMT -4. The time now is 11:50 AM.


MobileRead.com is a privately owned, operated and funded community.