{"id":12442,"date":"2025-04-14T14:22:07","date_gmt":"2025-04-14T20:22:07","guid":{"rendered":"https:\/\/www.mrc-productivity.com\/techblog\/?post_type=ht_kb&#038;p=12442"},"modified":"2025-04-17T11:18:59","modified_gmt":"2025-04-17T17:18:59","slug":"m-power-reports-adding-auto-sum-capabilities-to-the-excel-output","status":"publish","type":"ht_kb","link":"https:\/\/www.mrc-productivity.com\/techblog\/?ht_kb=m-power-reports-adding-auto-sum-capabilities-to-the-excel-output","title":{"rendered":"m-Power Reports: Adding auto sum capabilities to the Excel output"},"content":{"rendered":"\n<p>Excel output is a very popular choice among users for exporting report data from m-Power. When an Excel output is made from an m-Power report, the total line values in the generated output are raw values from the query rather than an Excel formula.<\/p>\n\n\n\n<p>For developers who want their total lines in the Excel output to auto sum via the <a href=\"https:\/\/support.microsoft.com\/en-us\/office\/sum-function-043e1c7d-7726-4e80-8f32-07b23e057f89\" data-type=\"link\" data-id=\"https:\/\/support.microsoft.com\/en-us\/office\/sum-function-043e1c7d-7726-4e80-8f32-07b23e057f89\"><code>=SUM(range)<\/code> function<\/a> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>=SUM(A2:A10)<\/code><\/pre>\n\n\n\n<p>this techblog post will show how to accomplish this using the m-Painter editor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Requirements<\/h2>\n\n\n\n<p>The following points are required setup for this functionality to work in your m-Power report:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Detail Level must be selected at runtime. <\/li>\n\n\n\n<li>The report template must be a Regular Report.<\/li>\n\n\n\n<li>Formatted Excel (outfmt=16) must be the output format chosen for Excel. <\/li>\n\n\n\n<li>The report may optionally have one selected subtotal dimension at runtime &#8211; that being <strong>Subtotal Level 1*<\/strong>. <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-danger\">*The auto sum functionality outlined in this post is not supported for reports with multiple dimension fields. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Walkthrough<\/h2>\n\n\n\n<p>Once the report is created, open m-Painter and go to the HTML source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1 &#8211; Initialize Freemarker variables<\/h3>\n\n\n\n<p>Find the comment <code>&lt;!-- END_REPORT_TABLE_HEADER --&gt;<\/code> and below this, copy and paste in the following Freemarker variables as shown <strong>in bold<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- END_REPORT_TABLE_HEADER --&gt;\n<strong>&lt;!--&lt;#assign rowCounter = 0&gt;--&gt;\n&lt;!--&lt;#assign firstSubtotal='Y'&gt;--&gt;\n&lt;!--&lt;#assign detailOnly='Y'&gt;--&gt;<\/strong>\n&lt;!-- &lt;#list report as section&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>These lines will initialize three Freemarker variables that will be needed for the auto sum functionality to work. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2 &#8211; Add Freemarker to the Detail Level<\/h3>\n\n\n\n<p>Find where the detail level is printing out, which in contained entirely within these comments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- &lt;#list section.detailRows as row&gt; --&gt;\n...\n...\n&lt;!-- &lt;\/#list&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>Within this block, add the following code <strong>(in bold)<\/strong> right AFTER the detail row prints, which would immediately follow the <code>&lt;tr class=\"detailBackground\" &gt;...&lt;\/tr&gt; <\/code>block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- &lt;#list section.detailRows as row&gt; --&gt;\n      &lt;tr id=\"level_&#91;DETAIL]\" class=\"level-row\"&gt;\n        &lt;td class=\"rsd\" align=\"center\" colspan=\"6\"&gt;Detail Level&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr class=\"detailBackground\" data-level=\"detail\"&gt;\n        &lt;td class=\"ten text-right\" nowrap=\"nowrap\"&gt;${row.CNUM?html}&lt;\/td&gt;\n        &lt;td class=\"ten text-right\" nowrap=\"nowrap\"&gt;${row.PNUM?html}&lt;\/td&gt;\n        &lt;td class=\"ten text-right\" nowrap=\"nowrap\"&gt;${row.MTDSALES?html}&lt;\/td&gt;\n        &lt;td class=\"ten text-right\" nowrap=\"nowrap\"&gt;${row.MTDCOST?html}&lt;\/td&gt;\n      &lt;\/tr&gt;\n<strong>   &lt;!--&lt;#if rowCounter = 0&gt;\n         &lt;#assign dataRange='=sum(C2:'\/&gt;\n         &lt;#assign rowCounter = 2&gt;\n   &lt;\/#if&gt;\n        &lt;#assign rowCounter = rowCounter + 1&gt;--&gt;      \n<\/strong>&lt;!-- &lt;\/#list&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>This counter calculation logic will find the amount of detail rows returned within the given section of the report in order to define the appropriate cell range (ex. C2:C100) for the SUM() function in Excel. <\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>The most important thing to notice with this code is various ranges for the SUM function are hardcoded to use column &#8216;C&#8217;. This is done because my MTDSALES field, which is being summed for Excel, will be the third column in the export. Meaning, it will be the &#8216;C&#8217; column. <\/p>\n\n\n\n<p>Further steps will also use column &#8216;C&#8217; for defining ranges.<\/p>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon\">It is entirely possible to repeat the auto sum logic for my MTDCOST field, which is the fourth column in my m-Power report. You may duplicate this auto sum logic and refer to &#8216;D&#8217; or whatever letter as needed to correspond to the same column in the Excel export.<\/p>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3 &#8211; Add Freemarker to the Subtotal Level<\/h3>\n\n\n\n<p>Find where the Subtotal Level 1 is printing out, which is contained within these comments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- &lt;#if subTotal1.dsp != 0&gt; --&gt;\n...\n...\n&lt;!--&lt;\/#if&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>Within this block, add the following code (<strong>in bold<\/strong>) right BEFORE the <code>&lt;tr class=\"sub1Background data-level=\"1\"&gt;<\/code> tag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- &lt;#if subTotal1.dsp != 0&gt; --&gt;\n\n  &lt;tr id=\"level_&#91;SHIPYEAR]\" class=\"level-row\"&gt;\n     &lt;td class=\"rsd\" align=\"center\" colspan=\"6\"&gt;Subtotal Level 1 by                          &#91;SHIPYEAR]&lt;\/td&gt;\n  &lt;\/tr&gt;\n\n <strong> &lt;!--&lt;#assign endOfDetailRange=rowCounter-1&gt;\n      &lt;#assign begOfNextDetailRange=rowCounter+1&gt;\n      &lt;#assign dataRange=dataRange+'C'+endOfDetailRange+',C'+begOfNextDetailRange+':'&gt;\n      &lt;#assign rowCounter = rowCounter + 1&gt;\n      &lt;#assign detailOnly='N'&gt;---&gt;\n\n  &lt;!--&lt;#if firstSubtotal='Y'&gt;\n      &lt;#assign subTotalDataRange='=sum(C2:C'+endOfDetailRange+')'&gt;\n  &lt;#else&gt;\n      &lt;#assign subTotalDataRange='=sum(C'+begOfNextRange+':C'+endOfDetailRange+')'&gt;\n  &lt;\/#if&gt;\n   \n  &lt;#assign begOfNextRange=endOfDetailRange+2&gt;--&gt;<\/strong>\n                                      \n&lt;tr class=\"sub1Background\" data-level=\"1\"&gt;\t                                    \t\t         &lt;td class=\"s1 text-right\"&gt;${subTotal1.CNUM?html}&lt;\/td&gt;            \t\t  &lt;td class=\"s1t\"&gt;${lastRow.PNUM?html}&lt;\/td&gt;\n &lt;td class=\"s1 text-right\"&gt;\n  <strong> &lt;!--&lt;#if page.outfmt==16&gt;--&gt;\n     ${subTotalDataRange}\n   &lt;!--&lt;#else&gt;--&gt;<\/strong>\n     ${subTotal1.MTDSALES?html}\n   <strong>&lt;!--&lt;\/#if&gt;--&gt;\n<\/strong> &lt;\/td&gt;\n &lt;td class=\"s1 text-right\"&gt;${subTotal1.MTDCOST?html}&lt;\/td&gt;\n <strong>&lt;!--&lt;#assign subTotalDataRange=''&gt;\t\n     &lt;#assign firstSubtotal='N'&gt;--&gt;<\/strong>\n&lt;\/tr&gt;\n\n&lt;!--&lt;\/#if&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>These various lines of code are summed up here:<\/p>\n\n\n\n<p><code>endOfDetailRange<\/code> &#8211; Gets the row number from the last detail line before the subtotal prints out.<\/p>\n\n\n\n<p><code>begOfNextDetailRange <\/code>&#8211; Defines the row number from the first detail line after the subtotal has printed.<\/p>\n\n\n\n<p><strong> <\/strong><code>dataRange <\/code>&#8211; concatenates the two previous variables to create the range needed for the <code>=SUM()<\/code> function that will be computed for the Grand Total level (not shown yet), based on the details.<\/p>\n\n\n\n<p>What follows after is logic summarized as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>IF the subtotal total line being printed is the first subtotal line of the report &#8211;\n<ul class=\"wp-block-list\">\n<li>Then the range for the SUM function is the first detail row, C2, combined with the end of the detail range.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>ELSE when the subtotal total line being printed is NOT the first subtotal line of the report &#8211;\n<ul class=\"wp-block-list\">\n<li>Then the range for the SUM function is the previous range&#8217;s last detail row plus two (skipping the subtotal row that prints). <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Lastly, there is code to print out the computed auto sum function only when the user exports to Formatted Excel (outfmt=16). If not exporting to Excel, then the original value from the presentation layer will be printed in the cell.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4 &#8211; Add Freemarker to the Grand Total Level<\/h3>\n\n\n\n<p>Find where the Grand Total level is printing out, which is contained within these comments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- &lt;#if grandTotal.dsp != 0&gt; --&gt;\n...\n...\n&lt;!-- &lt;\/#if&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>Within this block, add the following code (<strong>in bold<\/strong>) inside the table cell (<code>&lt;td&gt;..&lt;\/td&gt;<\/code>) where the value being auto summed is printing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n&lt;!-- &lt;#if grandTotal.dsp != 0&gt; --&gt;\n\n   &lt;tr id=\"level_&#91;GRAND]\" class=\"level-row\"&gt;\n     &lt;td class=\"rsd\" align=\"center\" colspan=\"6\"&gt;Grand Total Level&lt;\/td&gt;\n   &lt;\/tr&gt;\n\n   &lt;tr class=\"grandBackground\" data-level=\"0\"&gt;\n     &lt;td class=\"grand text-right\"&gt;\n       Grand Total\n     &lt;\/td&gt;\n     &lt;td class=\"grand text-right\"&gt;\n     &lt;\/td&gt;\n     &lt;td class=\"grand text-right\"&gt;\n       <strong>&lt;!--&lt;#if page.outfmt==16&gt;--&gt;\n         &lt;!--&lt;#if detailOnly='N'&gt;--&gt;\n           ${dataRange?keep_before_last(\",\")})\n         &lt;!--&lt;#else&gt;--&gt;\n           =sum(C2:C${rowCounter-1})\n         &lt;!--&lt;\/#if&gt;--&gt;\n       &lt;!--&lt;#else&gt;--&gt;<\/strong>\n         ${grandTotal.MTDSALES?html}\n      <strong> &lt;!--&lt;\/#if&gt;--&gt;<\/strong>\n     &lt;\/td&gt;\n     &lt;td class=\"grand text-right\"&gt;\n       ${grandTotal.MTDCOST?html}\n     &lt;\/td&gt;\n  &lt;\/tr&gt;\n\n&lt;!-- &lt;\/#if&gt; --&gt;<\/code><\/pre>\n\n\n\n<p>The parts in bold can be interpreted like this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>IF the report is exported to Formatted Excel (outfmt=16) then\n<ul class=\"wp-block-list\">\n<li>IF this is only a detail report &#8212; no subtotals &#8212; assign the =SUM function on the Grand Total to the entire detail range ex. (C2:C300).<\/li>\n\n\n\n<li>ELSE assign the SUM function on the Grand Total line to the collection of detail ranges between the subtotal lines (ex. =SUM(C2:C84,C86:C144,C146:C227,C229:C300)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>ELSE when the report is <strong>not <\/strong>exported to Formatted Excel (outfmt=16) then print the original field value in the presentation.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5 &#8211; Set the Excel button to Formatted Excel (outfmt=16)<\/h3>\n\n\n\n<p>Make sure if you have not already, to set your &#8216;Excel&#8217; option to Formatted Excel. The autosum functionality will only be applicable for outfmt=16 and not the other Excel options. <\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69f5b396d3787&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69f5b396d3787\" class=\"wp-block-image size-large wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"\/techblog\/images\/autosum_formattedexcelbutton.jpg\" alt=\"\"\/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>After saving m-Painter you may now test and run the application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6 &#8211; Runtime<\/h3>\n\n\n\n<p>At runtime, the report when ran to the browser (HTML) will look absolutely no different &#8211; as if it was a stock report.<\/p>\n\n\n\n<p>When Exported to Formatted Excel, both options will produce the auto sum functionality:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Detail Level only (with Grand Total):<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"\/techblog\/images\/autosum_detailandgrand.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Detail Level and Subtotal Level 1 (with Grand Total). <\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"\/techblog\/images\/autosum_detail_sub_grand1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"\/techblog\/images\/autosum_detail_sub_grand2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Formatting the Numeric Value<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>In order to specify the edit\/format code of the auto sum cell, add the following attribute to the respective &lt;th> column(s) in the m-Painter source:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;th data-col=\"MTDSALES\" class=\"text-right\" <strong>data-number-pattern=\"$#,##0.00\"<\/strong>><\/code><\/pre>\n\n\n\n<p>Other example of format codes include: &#8220;#,###&#8221;, &#8220;#,##0&#8221;, &#8220;$#,##0&#8221;, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Note<\/h2>\n<\/div><\/div>\n\n\n\n<p>This technical blog article is intended for advanced technical users. The custom FreeMarker code provided is meant to introduce and guide users on how they might implement this functionality. Any additional assistance beyond the information provided here is outside the scope of normal hotline support. If you require further help, please contact mrc support to discuss potential consulting services.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Excel output is a very popular choice among users for exporting report data from m-Power. When an Excel output is made from an m-Power report, the total line values in the generated output are raw values from the query rather than an Excel formula. For developers who want their total&#8230;<\/p>\n","protected":false},"author":12,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[],"ht-kb-tag":[],"class_list":["post-12442","ht_kb","type-ht_kb","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/12442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12442"}],"version-history":[{"count":25,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/12442\/revisions"}],"predecessor-version":[{"id":12474,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=\/wp\/v2\/ht-kb\/12442\/revisions\/12474"}],"wp:attachment":[{"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12442"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fht-kb-category&post=12442"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.mrc-productivity.com\/techblog\/index.php?rest_route=%2Fwp%2Fv2%2Fht-kb-tag&post=12442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}