View Single Post
Old 04-12-2023, 07:28 PM   #1074
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,205
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
You have one of two approaches:
  • One Formulas Action: You replace the formula you have above with this one:
    Code:
    program:
        report = '';
        for book_id in from_selection('id'):
            report = strcat(
                report,
                character('newline'),
                book_field(book_id, 'author'), ' - ', book_field(book_id, 'title'), character('newline'),
                'Chapter Count: ', book_raw_field(book_id, '#chaptercount'), character('newline'),
                'Page Count: ', book_raw_field(book_id, '#pagecount'),character('newline'),
                'Average Pages per Chapter: ', format_number((divide(book_raw_field(book_id, '#pagecount'), book_raw_field(book_id, '#chaptercount'))), '{0:5.2f}'),
                )
        rof;
        report
    As you can see it uses for loop to traverse all selected books (from_selection function). It can access metadata for books using the book_field() and book_raw_field() functions, both of which needs the book_id.

    This might be slightly slower than the second approach, I've never done any bench-marking and not interested in doing it, because the speed is fine for me.


  • Two Actions Chain (Chain Variables + Formulas): If you don't want to dabble in plugin specific functions like from_selection, book_field ...etc, do the following:

    First add a Chain Variables action, then add a variable called report and remember to tick the iterate checkbox, then add this template:

    Code:
    program:
        report = globals(report);
        strcat(
            report,
            character('newline'),
            $author, ' - ', $title, character('newline'),
            'Chapter Count: ', $$#chaptercount, character('newline'),
            'Page Count: ', $$#pagecount,character('newline'),
            'Average Pages per Chapter: ', format_number((divide($$#pagecount, $$#chaptercount)), '{0:5.2f}')
            )
    Now add a Formulas Action with the following template:

    Code:
    program:
        globals(report)

N.B This is the same approach I talked about when I told you to narrow down the books scope using templates scope. So if you have an action that needs to act on a subset of selected books, you filter the books ids you want to act upon using one of the two above techniques, and feed the list of book_ids to the action via template scopes.

Last edited by capink; 04-12-2023 at 07:31 PM.
capink is offline   Reply With Quote