![]() |
#1 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
A custom column built from other columns: simple math operations
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 ![]() 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? 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. |
![]() |
![]() |
![]() |
#2 | |||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
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'))'} Code:
{#progress:'multiply(divide(subtract(100, $), 100), field('#pages'))'} Code:
program: progress = field('#progress'); percent = divide(progress, 100); multiply(field('#pages'), percent); Quote:
Code:
program: progress = field('#progress'); percent = divide(progress, 100); read = multiply(field('#pages'), percent); test(progress, read, ''); Quote:
|
|||
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
@chaley, thank you for so much your help! GPM does look much less confusing.
So now I am using your template for "Pages read" and a similar one for "Pages left": Code:
program: progress = field('#progress'); percent = divide(progress, 100); pagesread = multiply(field('#pages'), percent); test(progress, pagesread, ''); Code:
program: progress = field('#progress'); left = subtract(100, progress); percent = divide(left, 100); pagesleft = multiply(percent, field('#pages')); test(progress, pagesleft, ''); This has not worked right in my previous TPM template either. Not sure what the reason is, though. There is also some problem with sorting: I've chosen sorting by numbers in the column dialog but now when I try to sort, nothing seems to happen. |
![]() |
![]() |
![]() |
#4 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
Last edited by chaley; 06-05-2016 at 10:38 AM. |
||
![]() |
![]() |
![]() |
#5 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
Thanks! I thought it must have had something to do with the comma! Glad it works now.
One more question: I realized that for a couple of books (audiobooks, in fact) I don't have the number of pages entered. How to get 'blank' results for those books? I.e. how to have something similar to your Code:
test(progress, pagesread, ''); I'm pretty sure the sorting doesn't work, though. Are you absolutely sure it works for you? Based on the screenshot, it looks like it certainly does but theoretically, you could have sorted on myint first, by coincidence, and then the order would not have had to change. No idea what's wrong with my sorting. When I switch the sorting method to Text or Date, sorting works (obviously, producing bizarre results). When I switch to Numbers, nothing happens whatsoever. // EDIT I've checked it with another library and another Calibre installation - same results ![]() Last edited by Joanna; 06-05-2016 at 11:12 AM. |
![]() |
![]() |
Advert | |
|
![]() |
#6 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
One complexity: raw_field distinguishes between empty fields and fields containing an empty value by returning the string "None" in the first case. This matters because the value for #pages is now fetched using raw_field. The easiest solution is to check #pages in the first argument to "test", but this would need to be done using "field" instead of "raw_field". Something like: Code:
program: progress = field('#myint2'); left = subtract(100, progress); percent = divide(left, 100); pagesleft = multiply(percent, raw_field('#myint')); test(and(progress, field('#myint')), pagesleft, ''); Quote:
Be sure that you are running the latest calibre. There were some problems with sorting in the mid calibre 2 releases. If you wish I am willing to look at your metadata.db to see if I can determine why it isn't sorting. Zip it up and send it to calibre2 at charles dot haleys dot org. Send only the metadata.db, not the library. Note that I will see the contents of your library. If that is a problem (and I can imagine that it might be) then don't send it. ![]() |
||
![]() |
![]() |
![]() |
#7 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
Thanks! Now everything (but sorting) works perfectly
![]() I'm running the almost-latest Calibre (2.57.1), so it's probably not that. I'd be more than happy to supply you with my metadata.db, if you're willing to have a look at that ![]() ![]() |
![]() |
![]() |
![]() |
#8 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
I don't have any plugins installed. Given your problems, it is possible that some plugin you have installed is interfering with sorting. You might try starting calibre from the command line using (something like) Code:
calibre --ignore-plugins Finally, there are some "tweaks" (Preferences / Tweaks) that affect sorting. You should check if you have set any of them by searching for the word "sort" in the tweaks editor. |
|
![]() |
![]() |
![]() |
#9 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
@chaley, thanks for checking!
Now I'm really puzzled. Is there any way the problem could be somewhere outside of Calibre (my computer configuration, locale, anything)? I'm asking this as I've just made a fresh installation of Calibre Portable, no plugins/tweaks whatsoever, default settings only. I've opened the library - and sorting still won't work, which is really strange. |
![]() |
![]() |
![]() |
#10 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Locale is a fine suspect. Try changing the tweak "locale_for_sorting" to "en" and see if anything changes.
What OS/version are you running? I am on Win10/64. |
![]() |
![]() |
![]() |
#11 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Also, do run calibre in debug mode. It could be that something is going wrong inside calibre that it can tell us about.
|
![]() |
![]() |
![]() |
#12 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
Tried changing the tweak, to no avail. I am on Win7 Pro 64-bit, Polish version.
|
![]() |
![]() |
![]() |
#13 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
I've run Calibre in debug mode (both my normal installation and the new one). It doesn't look like anything interesting has come up
![]() Code:
calibre Debug log calibre 2.57.1 Portable embedded-python: True is64bit: False Windows-7-6.1.7601-SP1 Windows ('32bit', 'WindowsPE') 32bit process running on 64bit windows ('Windows', '7', '6.1.7601') Python 2.7.9 Windows: ('7', '6.1.7601', 'SP1', 'Multiprocessor Free') Successfully initialized third party plugins: Extract ISBN && ADLIBRIS_SE && Unplugged && Modify ePub && User Category && ADLIBRIS_NO && Search The Internet && Resize Cover && Quick Preferences && Generate Cover && BOL_NL && Barnes & Noble && Job Spy && BOL_DE && LubimyCzytac && Favourites Menu && MultiColumnSearch && Reading List && Open With && View Manager && Count Pages && Author Book Count Hierarchy && Author Book Count && Walk Search History && Goodreads && catawiki && Quality Check && Manage Series && biblioNETka.pl && Find Duplicates Starting up... Calibre, and hence Job Spy, was gracefully shut down last time? True Last time daemon started: never Last time daemon failed: never Total daemon starts inception_to_date: 0 Total daemon failures inception-to-date: 0 Job Spy has finished initialization... [0] un-optimized number of undesirable characters accumulated: 759 calibre_plugins.multi_column_search.ui:211: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal [1] optimized number of undesirable characters accumulated: 660 Started up in 9.14 seconds with 2421 books Code:
calibre Debug log calibre 2.58 Portable embedded-python: True is64bit: False Windows-7-6.1.7601-SP1 Windows ('32bit', 'WindowsPE') 32bit process running on 64bit windows ('Windows', '7', '6.1.7601') Python 2.7.9 Windows: ('7', '6.1.7601', 'SP1', 'Multiprocessor Free') Starting up... Started up in 5.37 seconds with 1 books |
![]() |
![]() |
![]() |
#14 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 199
Karma: 76476
Join Date: Feb 2012
Location: Poland
Device: none
|
I've tried sorting on my other computer: same results. The computer runs on Windows 8.1 64-bit (also the Polish language version).
|
![]() |
![]() |
![]() |
#15 |
null operator (he/him)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 21,733
Karma: 29711016
Join Date: Mar 2012
Location: Sydney Australia
Device: none
|
@Joanna - maybe it's something in Windows->Regional Settings->Advanced Settings->Numbers. FWIW - mine are:
BR Last edited by BetterRed; 06-05-2016 at 06:31 PM. |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Request: template-making assistance for column built from other columns | iienderii | Library Management | 9 | 04-04-2016 10:27 PM |
Crash when adding a custom (built) column | MidwestJen | Library Management | 13 | 12-06-2014 03:52 PM |
Custom Column Build from other Columns | Tanjamuse | Library Management | 7 | 10-16-2014 01:28 AM |
Date custom column built from a Y/N one? | glos | Library Management | 3 | 08-25-2013 12:48 PM |
Custom yes/no column built from long text column | Philantrop | Library Management | 7 | 03-23-2013 07:44 PM |