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 03-05-2016, 12:28 AM   #1
asogn
Junior Member
asogn began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Feb 2011
Device: kindle
How to build custom column based on other columne (if - then statement)

Hi there people

Background.
I have a Kindle 2/3 and I am planning to let the Kindle Collection plugin handle my collections.

The way I use kindle is that as soon as I am finished reading books in one collection I rename it on the kindle to x_NAME.... or X_NAME where NAME is the original collection name.
This works fine and I can set the plugin to not do anything with collections starting with x_

However, the plugin will try to recreate the original collection when I run it again.

So to handle this I am planning to also cater for the x_ named collection.
To do that, I use the plugin to read all existing collections existing on kindle into a custom column called kindlecollections.
Then I have another custom column called collectionname with following template
{#universe:'ifempty($, field('author_sort'))'}{series:| - |}
The template works the way I want. But does not check anything in #kindlecollections

What I would like is for the #collectionname to check if #kindlecollections start with x_ or X_
If it does, copy the content of #kindlecollections to #collectionname
If it doesn't, use {#universe:'ifempty($, field('author_sort'))'}{series:| - |} to populate #collectionname

I have been playing with the template, but can't seem to get my head around it.
Anyone able to help me?
Or is it another way to do what I want?

Cheers, Atle
asogn is offline   Reply With Quote
Old 03-05-2016, 01:59 AM   #2
asogn
Junior Member
asogn began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Feb 2011
Device: kindle
OK, was able to figure it out. Had some problems due to apostrophe, but following works for me:
{#kindlecollections:'contains($,'^x_*',field('#kin dlecollections'),"{#universe:'ifempty($, field('author_sort'))'}{series:| - |}")'}

Cheers, Atle
asogn is offline   Reply With Quote
Advert
Old 03-05-2016, 05:00 AM   #3
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by asogn View Post
OK, was able to figure it out. Had some problems due to apostrophe, but following works for me:
Code:
{#kindlecollections:'contains($,'^x_*',field('#kindlecollections'),"{#universe:'ifempty($, field('author_sort'))'}{series:| - |}")'}
Cheers, Atle
That template will at some point not work, depending on the values in #universe or series. The problem: bracketed expressions are macros, evaluated before the template is parsed. Thus if #universe or series can contain a character that can affect parsing, most notably " or ' but other characters as well, then the template will fail. Also, nesting Template Program Mode expressions is problematic.

If you want to nest templates or TPM expressions then you should use General Program Mode where no macro processing takes place. They are also easier to read when dealing with this level of complexity. Something like:
Code:
program:
u = ifempty(field('#universe'), field('author_sort')); 
s = finish_formatting(field('series'), '', ' - ', '');
kc = field('#kindlecollections');
contains(kc, '^x_*', kc, strcat(u, s))
I can't test it because I don't have your columns.

FWIW: I suspect that the regexp you use in the 'contains' isn't what you really want. That regexp looks for a leading x followed by zero or more underscores. I thing you want '^x_', which is a leading x followed by an underscore.
chaley is offline   Reply With Quote
Old 03-05-2016, 11:17 AM   #4
asogn
Junior Member
asogn began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Feb 2011
Device: kindle
Quote:
Originally Posted by chaley View Post
That template will at some point not work, depending on the values in #universe or series. The problem: bracketed expressions are macros, evaluated before the template is parsed. Thus if #universe or series can contain a character that can affect parsing, most notably " or ' but other characters as well, then the template will fail. Also, nesting Template Program Mode expressions is problematic.

If you want to nest templates or TPM expressions then you should use General Program Mode where no macro processing takes place. They are also easier to read when dealing with this level of complexity. Something like:
Code:
program:
u = ifempty(field('#universe'), field('author_sort')); 
s = finish_formatting(field('series'), '', ' - ', '');
kc = field('#kindlecollections');
contains(kc, '^x_*', kc, strcat(u, s))
I can't test it because I don't have your columns.

FWIW: I suspect that the regexp you use in the 'contains' isn't what you really want. That regexp looks for a leading x followed by zero or more underscores. I thing you want '^x_', which is a leading x followed by an underscore.
This works like a charm. Changed the regex to be correct (i.e. '^x_').
And it looks much simpler to use program: instead.
So when I have time I will expand this to also strip out quotes. Currently that is not a problem since I have changed all my series to not include quotes....
Turns out that Kindle is not so happy with quotes (or the kindle collections plugin is not so happy)

Next step is to verify that everything works properly and not screw up my kindle on the first run.

Thanks a lot

Cheers, Atle
asogn is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Column Build from other Columns Tanjamuse Library Management 7 10-16-2014 01:28 AM
REQ: Template for custom column to calc # of Pgs based on traditional counts jecilop Library Management 15 09-13-2014 07:04 PM
Looking for how to create a custom column based on a tag kaufman Library Management 10 02-12-2014 11:15 AM
Custom column returns value based on value of another custom column? calvin-c Calibre 3 09-14-2013 02:24 PM
Using "Build column from other column" custom-column type HFC3 Library Management 3 07-07-2013 03:11 AM


All times are GMT -4. The time now is 04:44 AM.


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