View Single Post
Old 09-14-2025, 11:29 AM   #180
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,529
Karma: 8075938
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
14 Sept 2025 (in calibre 8.10.100)
  • A "with" statement in the template language that for the duration of the associated code block changes the "current book" to the one specified by the book id.
  • A formatter function selected_books() that returns the book ids of the currently selected books
  • A formatter function selected_column() that returns the lookup name of the column containing the selected cell.
  • A formatter function sort_book_ids() that sorts the books specified by book_ids.
  • A formatter function show_dialog() that opens a dialog to display plain text or html.
  • Add check boxes to the template tester to control "run as you type" and to restrict test runs to the first selected book. These are very useful if generating reports using selected books.

Here is an example using several of the features described above. It generates a report of book sizes for the selected books.
Code:
program: 
	ids = sort_book_ids(selected_books(), 'series', 1, 'title', 1);
	res = '<style> th, td {padding: 2px;}</style> <h2>Book Size Report</h2><p><table>';
	total = 0;

	def table_row(title, series, size):
		return strcat('<tr><td>', title, '</td>', 
						 '<td>', series, '</td>',
						 '<td>', if size !=# 0 then human_readable(size) else '0' fi, '</td>',
						 '</tr>', character('newline'))
	fed;

	for id in ids:
		with id:
			s = booksize();
			total = total + s;
			res = strcat(res, table_row($title, $series, s))
		htiw
	rof;
	res = strcat(res, table_row('TOTAL', '', total));
	res = strcat(res, '</table>');
	show_dialog(res)

Last edited by chaley; 09-19-2025 at 04:51 AM.
chaley is offline   Reply With Quote