Quote:
Originally Posted by Joanna
In my library, I have a custom column #pages (the number of pages as estimated by the Count Pages plugin) and a custom column #progress (which is a number, entered manually based on the information from my Kindle: if the Kindle shows 54%, I enter 54 etc.).
I would like to have:
a) a column that shows the number of pages read,
b) a column that shows the number of pages left.
The rough formulas would be:
a) #progress/100 x #pages
b) (100-#progress)/100 x #pages
which, translated into the Calibre template language, look as follows:
a) {#calc:'multiply(divide(field('#progress'), 100), field('#pages'))'}
b) {#calc:'multiply(divide(subtract(100, field('#progress')), 100), field('#pages'))'}
This seems to render correct results. Still, I have some doubts and questions:
1) I don't quite get the very beginning: do I have to put the name of field (#calc: )? It doesn't seem to matter which field I put there (still not quite getting the syntax  ).
|
There are three distinct template language syntaxes:
Single Function Mode (SFM),
Template Program Mode (TPM), and
General Program Mode (GPM). The three share some features and syntax.
You are using TPM, which is designed to look like SFM while adding some of the capability of GPM. In particular, both SFM and TPM let you specify a column that can be used later in the template. In SFM the value of the column becomes the first argument to the (one) function. In TPM the "$" variable is replaced with the column's value. As such you could write your "pages read" (I think) template (in TPM) as
Code:
{#progress:'multiply(divide($, 100), field('#pages'))'}
and your "pages left" template as
Code:
{#progress:'multiply(divide(subtract(100, $), 100), field('#pages'))'}
However, given the complexity of the templates I would use GPM instead of TPM in order to make the templates easier to read and easier to maintain. One possible GPM template for "pages read" would be:
Code:
program:
progress = field('#progress');
percent = divide(progress, 100);
multiply(field('#pages'), percent);
This template returns the value of the last function -- the "multiply".
Quote:
Originally Posted by Joanna
2) What function should I use so that the calculations are made only if #progress is not empty, and otherwise the target column is left blank?
|
We get one solution by extending the above GPM template:
Code:
program:
progress = field('#progress');
percent = divide(progress, 100);
read = multiply(field('#pages'), percent);
test(progress, read, '');
Note that this tests for #progress being empty, not being equal to zero.
Quote:
Originally Posted by Joanna
3) Is it possible to sort such a calculated column in an ascending/descending order like a regular numerical column, and if so, how to achieve that? At the moment, sorting is kind of alphabetical, based on the first digit, then the second digit etc. while ignoring the order of magnitude, i.e. 40 and 400 would be between 4 and 5.
|
Change the "Sort/search column by" value for the column to "Number". See the attached screen capture.