The Tree of Widsdom

智慧の樹

TableSum

^do( TableSum, col1, col2 )^

Type: Macro

Purpose: Adds a 2-column ∑-table with values from previous siblings

Do you have notes that are items that cost money? Houses, pens, monster models, books? Now it is super easy to make a table of those items and their prices followed by a total row at the bottom. Nice.

Let’s say you want to make a table of certain items and their prices that ends in their TOTAL. Let’s say that the price attribute of these items is called $myPrice. And let’s say the referring expression you want to see in the table is the note’s $Name.

Now—making a section in your export that exports these items, either as compiled sub-posts or as SmartButtons, with a concluding table that summarizes their prices is easy:

  1. Just add a last sibling to the list.
  2. Enter the following text: ^do(TableSum, $Name, $myPrice)^

The headings Item and Price are already deceived for you.

Here’s an easy project:

  1. Make an agent to collect the priced items you want to include in a post or sub-post.
  2. Turn the agent off.
  3. Add a last sibling named Total cost whose sole content is ^do(TableSum, $Name, $myPrice)^.

The only args tell the macro which attributes to use—which is the referring expression and which the price:

  •  Arg1 (col1): The 1st arg is the attribute being listed in the table’s left column.
  •  Arg2 (col2): The 2nd arg is the attribute being listed in the table’s right column, which is also the number being accumulated into the final TOTAL at the bottom of the table.

Hover below to reveal BoxPress code

^action($tmpLst1(bstVAR)=collect_if(siblings, $mySum!=0, $1);$tmpLst2(bstVAR)=collect_if(siblings, $mySum!=0, $2); $tmpNum1(bstVAR)=; $tmpStr1(bstVAR)=;$tmpLst1(bstVAR).each(X){
$tmpNum2(bstVAR)=$tmpLst2(bstVAR).at($tmpNum1(bstVAR));$tmpStr1(bstVAR)=$tmpStr1(bstVAR) + "<tr><td>" + X + "</td><td class=`text-right`>$" + $tmpNum2(bstVAR).format(2) + "</td></tr>"; $tmpNum1(bstVAR)=$tmpNum1(bstVAR)+1;})^
<div class="table-responsive">
<table class="table">
<caption>^value($Name)^</caption>
<thead><tr><th>Item</th><th class=`text-right`>Cost</th></tr></thead>
<tbody>
^value($tmpStr1(bstVAR))^
<tr class="warning"><td><b>TOTAL</b></td><td class="text-right"><b>$^value(sum(siblings, $2).format(2))^</b></td></tr>
</tbody>
</table>
</div>

TableSum sample

The name of the note you are now reading is TableSum Sample. You can see the position of everything you are reading on this webpage in the Outline View image below. It shows two quite incredible facts:

Here is the actual structure of this webpage.
Here is the actual structure of this webpage.
  1. The table at the end of this webpage is actually an alias of a note whose original is the last sibling of a group of notes far, far away from this one.
  2. The alias is the child of the note TableSum Sample (the note that contains this here text).

Here is the situation of the alias’ original incarnation:

Here is the actual situation of this webpage’s final third.
Here is the actual situation of this webpage’s final third.

And here is an example of the price-carrying notes. The value is in $myPrice:

The inside of the <b>White sugar</b> note.
The inside of the White sugar note.

Here is an image of the Text View of TableSum Sample (which you are still reading):

Text View of the note you are now reading. Notice the <code>$blogExpOpt</code> settings. Actually, all the <b>[MacroName] sample</b> notes in this documentation use <b>NOTOC</b> to omit them from any possible future Table of Contents.
Text View of the note you are now reading. Notice the $blogExpOpt settings. Actually, all the [MacroName] sample notes in this documentation use NOTOC to omit them from any possible future Table of Contents.

The alias that is my child contains merely the following text:

^do(TableSum, $Name, $myPrice)^