![]() |
#1 |
Junior Member
![]() Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
|
Stuck on If, then, else
Hello - very new to Calibre, but very familiar with data manipulation. I am trying to create a custom field using if-then-else. I have spent days searching and cannot figure out the correct syntax.
Here is what I am trying to solve for: IF {universe} isempty THEN concatenate({universe},{book#}) ELSE concatenate ("1",{author},{saga},{Book#} Any help is greatly appreciated!! |
![]() |
![]() |
![]() |
#2 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Assuming you meant not isempty on the IF, that "universe" is a custom column with the lookup key #universe, that "saga" is a custom column with the lookup name #saga, and "book#" is the calibre book id, I think you want something like this in General Program Mode: Code:
program: if $#universe then strcat($#universe, $id) else strcat('1', $authors, $#saga, $id) fi |
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Junior Member
![]() Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
|
Thank you Chaley! This has gotten me so much closer to my goal! I would never have figured out closing with 'fi'.
Here are the field names (assuming you are asking for the Lookup name vs Column Header): #universe, #saga, #book_num, authors, title You are correct - the true statement is when universe is NOT blank. Mis-type on my end. My library is primarily fantasy novels. Some of the sets (e.g. Forgotten Realms) are sorted by their book order, the rest I want sorted by author and saga or author and title. Does Calibre allow one to pull out the author last name? Or at least use a string that has last name first? This field will be ultimately used as a sort. Here are examples of what I am trying to do: Universe sample: Universe = Forgotten Realms Book# = 26 Desired Result = Forgotten Realms26 Book w Saga Universe = (blank) Author = Terry Brooks Saga = Saga of Shannara Book# = 12 Title = Bearer of the Black Staff Desired Result = Brooks, TerrySaga of Shannara12 Book w no Saga Universe = (blank) Author = Terry Brooks Saga = (blank) Book# = (blank) Title = Daughter of Darkness Desired Result = Brooks, TerryDaughter of Darkness Does the syntax here follow Excel rules, where once you get past the first criteria, you no longer have to reference it? In other words, can I just perform an ELSEIF where Saga is NOT BLANK or do I still need to reference the Universe criteria? Thank you for any assistance you can offer! |
![]() |
![]() |
![]() |
#4 |
Junior Member
![]() Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
|
Hey Chaley!
I figured out the last name sort by using the following: program: if $#universe then strcat($#universe, $#book_num) else strcat('1', $author_sort, $#book_num) fi I have tried various combinations for 'else if', but keep getting errors. I'm really close. If you can help me with this last bit of syntax, it would be greatly appreciated! |
![]() |
![]() |
![]() |
#5 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
This does what your examples show, with the exception that I added commas so I could read the output. Note that your examples didn't show the '1' so I removed it.
The lines at the top are there so I could test it. I don't have your columns so I simulated them using variables. You can remove these lines and use the column lookup names directly. The field author_sort contains the value calibre uses to sort. If there is more than one author, this template chooses the first author in the author_sort list. The template language syntax is closer to visual basic than to excel. The General Program Mode documentation might help. This should be enough to get you going. Code:
program: # universe = $#universe; # universe = 'Forgotten Realms'; universe = ''; # book_num = $#book_num book_num = 26; # sage = $#saga # saga = 'Saga of Shannara'; saga = ''; # title = $title title = 'Bearer of the Black Staff'; # author_sort = $#author_sort author_sort = 'Brooks, Terry'; if universe then strcat(universe, ', ', book_num) elif saga then strcat(list_item(author_sort, 0, '&'), ', ', saga, ', ', $id) else strcat(list_item(author_sort, 0, '&'), ', ', title) fi |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Junior Member
![]() Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
|
Chaley - THANK YOU!!!
I had to make some minor mods to get it to work, but it works! program: if $#universe then strcat($#universe, $#book_num) elif $#saga then strcat(list_item($author_sort, 0, '&'), $#saga, $#book_num) else strcat(list_item($author_sort, 0, '&'), $title) fi What programming language is this? I know VBA and SQL. I'd like to get more familiar with this. One last question if I may - the book number is not sorting correctly. I have a list that the system sorts 1, 10, 11, 2... I cannot figure out what the format is to add preceding zeros. Much appreciated!! |
![]() |
![]() |
![]() |
#7 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Note also that you can write templates in pure Python 3, should that be of interest. The documentation tells you how to do it. You would need to look at the calibre database api. Quote:
Code:
program: output = ''; for i in range(40, 0, -5): output = output & format_number(i, '03d') & ',' rof; list_sort(output, 0, ',') |
||
![]() |
![]() |
![]() |
#8 |
Junior Member
![]() Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
|
THANK YOU!
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Stuck on 5.4.3.3 | VelvetElvis | Calibre Companion | 2 | 05-10-2020 01:38 AM |
Stuck | BeccaPrice | Writers' Corner | 3 | 05-27-2013 11:03 AM |
i am stuck | me too | Workshop | 14 | 07-25-2012 02:38 AM |
K3 got stuck | wyndslash | Amazon Kindle | 18 | 10-16-2011 08:16 PM |