Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 01-17-2021, 01:22 AM   #1
Quuzuu
Junior Member
Quuzuu began at the beginning.
 
Posts: 9
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, 03:49 AM   #2
Quuzuu
Junior Member
Quuzuu began at the beginning.
 
Posts: 9
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 03:52 AM.
Quuzuu is offline   Reply With Quote
Old 01-17-2021, 04:05 AM   #3
Quuzuu
Junior Member
Quuzuu began at the beginning.
 
Posts: 9
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, 10:18 AM   #4
chaley
Grumpy old git
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: 9,560
Karma: 3125810
Join Date: Jan 2010
Location: UK
Device: Reader
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 07:00 PM. Reason: Correct mistaken function name
chaley is offline   Reply With Quote
Old 01-17-2021, 05:35 PM   #5
Quuzuu
Junior Member
Quuzuu began at the beginning.
 
Posts: 9
Karma: 10
Join Date: Dec 2020
Device: Kindle iOS App
Thanks so much!
Quuzuu is offline   Reply With Quote
Reply

Tags
template

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:49 AM
Need help in Custom Columns, template syntax, and rounding numbers dennocoil Calibre 0 07-10-2017 04:24 PM
Need more help with searching multiple columns at once beckyt Library Management 3 06-19-2016 07:35 PM
Request: template-making assistance for column built from other columns iienderii Library Management 9 04-04-2016 11:27 PM
Can I generate sum of column or columns? GeekyGal Calibre 5 09-29-2011 08:32 PM


All times are GMT -4. The time now is 09:02 AM.


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