View Single Post
Old 05-10-2025, 02:41 PM   #15
DJH352
Member
DJH352 began at the beginning.
 
DJH352's Avatar
 
Posts: 10
Karma: 10
Join Date: Apr 2025
Location: UK
Device: Kindle Paperwhite
Quote:
Originally Posted by chaley View Post
That is an excellent start.

Here is the converted template. It returns the maximum series index for the series of the book. Somehow the indenting was lost in @dunhill's post, which is corrected here.
Code:
python:
def evaluate(book, context):
	db = context.db.new_api
	# Count the books in the series. First get the series name
	series = book.get('series')
	if series:
		# The book is in a series. Get all the books in that series
		series_id = db.get_item_id('series', series, case_sensitive=True)
		books = db.books_for_field('series', series_id)
		# Get the highest series index
		max_dex = -9999
		for b in books:
			ser = db.field_for('series_index', b)
			if ser > max_dex:
				max_dex = ser
		# Convert the max index to a string.
		x = str(int(max_dex) if max_dex.is_integer() else max_dex)
	else:
		x = ''
	return x
This template could be used in a custom column but it might be too slow because it operates on all the books all the time. It can be used on demand in Action chains, possibly in a Single Field Edit action that operates on all the books in a series.

This is fantastic, thank you so much.

I added an empty book to the Jack Reacher series and calibre immediately populated the column for that book with the maximum index number. There was no lag (1,350 books)

After a re-start all books in the series were updated. When I deleted the empty book and restarted calibre, the field automatically updated again

Thank you to everyone who has help me with my query
Attached Thumbnails
Click image for larger version

Name:	Calibre Series 2025-05-10 192026.jpg
Views:	32
Size:	57.5 KB
ID:	215599   Click image for larger version

Name:	Calibre Series Update 2025-05-10 192026.jpg
Views:	35
Size:	45.1 KB
ID:	215600  
DJH352 is offline   Reply With Quote