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 10-12-2020, 06:40 AM   #1
Sandwich
Junior Member
Sandwich began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Mar 2012
Device: Android
Question Customize the positive and negative number format on an integer column

I posted this as a Calibre feature request here, but I'm beginning to wonder if it's possible to do already.

Basically, I'd like to have a column for in-universe dating systems. Eg. Star Wars uses "[year] BBY" or "[year] ABY" for dating events, similar to how we have BC and AD (or BCE and ACE if you prefer) on our years.

I think the solution would have to involve 3 custom columns:
  1. Year (integer) - this would store the actual in-universe year wherein a book takes place, as either a negative or positive integer.
  2. Year Format (preset fixed values) - Different universes have different ways of labeling their years. I already covered Star Wars above; Dune uses "BG" / "AG". This column would be where we select which date format we use for each book.
  3. Chronological date (formated year, built from other columns) - I guess this would just take the integer from the Year column, combine it with the selected Year Format somehow, and show the result.

I may be overthinking this, I admit, but can anyone point me in the right direction here?
Sandwich is offline   Reply With Quote
Old 10-12-2020, 07:37 AM   #2
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,029
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Sandwich View Post
I may be overthinking this, I admit, but can anyone point me in the right direction here?
You are very close to what I would do.

I would make three custom columns:
  • The year as you described.
  • A "universe specifier", a text custom column or perhaps a column with a fixed set of values, for example "Dune" or "Star Wars".
  • A column built from other columns that chooses how to format the year number based on the universe specifier.
An example template might be:
Code:
program:		 
	universe = field('#text');
	year = raw_field('#myint');
	first_non_empty(
		if universe == 'Star Wars' then
			if year < 0 then
				strcat(floor(multiply(year, -1)), ' BBY')
			else
				strcat(year, ' ABY')
			fi
		fi,
		if universe == 'Dune' then
			if year < 0 then
				strcat(floor(multiply(year, -1)), ' BG')
			else
				strcat(year, ' AG')
			fi
		fi
	)
Ignore the lookup names in the first two lines. Those are columns I already had.

This screen capture shows the result.
Click image for larger version

Name:	Clipboard01.png
Views:	181
Size:	3.3 KB
ID:	182637

Alternatively, and just for fun, it can be made more readable using the new stored template feature. I first define a stored template called 'universe_year' that formats the year:
Code:
program:
		arguments(year, lt, gt);
		if year < 0 then
			strcat(floor(multiply(year, -1)), ' ', lt)
		else
			strcat(year, ' ', gt)
		fi
I next change the original template to use the stored template, resulting in:
Code:
program:		 
	universe = field('#text');
	year = raw_field('#myint');
	first_non_empty(
		if universe == 'Star Wars' then
			universe_year(year, 'BBY', 'ABY')
		fi,
		if universe == 'Dune' then
			universe_year(year, 'BG', 'AG')
		fi)

Last edited by chaley; 10-12-2020 at 07:44 AM. Reason: Added example universe specifiers
chaley is offline   Reply With Quote
Old 10-12-2020, 08:52 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,029
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
And for more fun, an equivalent but more compact template that uses the stored template:
Code:
program:
	universe = field('#text');
	year = raw_field('#myint');
	first_non_empty(
		test(universe == 'Star Wars', universe_year(year, 'BBY', 'ABY'), ''),
		test(universe == 'Dune', universe_year(year, 'BG', 'AG'), ''),
		'Unknown Universe'
	)
chaley is offline   Reply With Quote
Old 10-12-2020, 04:49 PM   #4
Sandwich
Junior Member
Sandwich began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Mar 2012
Device: Android
Ooh, that looks perfect!

I tried to implement it, but I've not messed around with Calibre templates before. How do I put multi-line template code into the single-line input field when editing a custom column? Or am I barking up the wrong preferences panel?
Sandwich is offline   Reply With Quote
Old 10-12-2020, 05:45 PM   #5
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,029
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Sandwich View Post
Ooh, that looks perfect!

I tried to implement it, but I've not messed around with Calibre templates before. How do I put multi-line template code into the single-line input field when editing a custom column? Or am I barking up the wrong preferences panel?
Oh, it seems I forgot to add the context-menu option to open the full template editor. I will see if I can fix that.

As for your question: copy the template and paste it into the line. Go through the process and create the column. You can then select the column in the library view, press F2 to edit it, and be presented with the full template editor.

Last edited by chaley; 10-12-2020 at 05:55 PM.
chaley is offline   Reply With Quote
Old 10-13-2020, 06:15 AM   #6
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,029
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by chaley View Post
Oh, it seems I forgot to add the context-menu option to open the full template editor. I will see if I can fix that.
Starting with the next release, when creating/editing "columns built from other columns" you can right-click on the template box and open the full template editor.
chaley is offline   Reply With Quote
Reply

Tags
chronological order, custom columns, year


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer Column Quetico Reading and Management 0 07-10-2017 12:27 AM
Method to set Date from user integer column macnab69 Library Management 9 07-07-2013 08:52 AM
Rounding to nearest integer in Custom Column - Hijri Year oelgamal Calibre 2 03-28-2013 08:12 PM
Rounding to nearest integer in Custom Column - Hijri Year oelgamal Recipes 0 03-28-2013 03:58 AM
positive/negative experience with onyx support? Clark G. Flipper Onyx Boox 69 03-19-2012 12:23 PM


All times are GMT -4. The time now is 10:11 AM.


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