Count data in Confluence

If you want to count something in your Confluence reports, like amount of projects per PM, use the following macros.

This user macro:

## @param countthis:title=Name|required=true|type=string

<script>
AJS.toInit(function() {

    var n = $('li:contains("$paramcountthis")').children('.blog-post-list:contains("No blog posts found.")').length;

    $('<tr/>').appendTo('table');
    $('<td colspan="1" class="confluenceTd"/>').text('$paramcountthis').appendTo('tr:last');
    $('<td colspan="1" class="confluenceTd"/>').text(n).appendTo('tr:last');
    
});

</script>

Will count all occurrences of the given string in a document, and append results to a table. Please note that it requires an existing 2-column table to attach to, so add a table to your page.

This user macro:

## @param pmname:title=Name(select)|required=true|type=string
## @param pmdisplayname:title=Name(display)|required=true|type=string

<script>
AJS.toInit(function() {

    var n = $('.table-wrap:contains("$parampmname")').length;

    $('<tr/>').appendTo('table');
    $('<td colspan="1" class="confluenceTd"/>').text('$parampmdisplayname').appendTo('tr:last');
    $('<td colspan="1" class="confluenceTd"/>').text(n).appendTo('tr:last');
    
});

</script>

does almost the same, but uses another variable to specify the name in the left row to put calculations next to. It also requires an existing table.

To hide the base data you’re building your calculations on, use the following user macro.

## @noparams

<script>
AJS.toInit(function() {

  $(".sectionMacroWithBorder").addClass("hidden");
        
});

</script>

If you want to show only excerpts that contain certain value, add the following user macro:

## @param pmname:title=Name(select)|required=true|type=string

<script>
AJS.toInit(function() {

  $(".sectionMacroWithBorder").not(":contains('$parampmname')").addClass("hidden");
    
});

</script>

To get an idea how to collect base data for the above macros to work, have a look at this article.

Leave a Comment