Using a user macro multiple times on a page

Reto Eggenberger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 14, 2018

Hi,

I have the folloing user macro, which uses javascript to calculate the EAN13 check digit.

<div id="result"></div>

<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#result').text('$paramEAN13'+getLastEan13Digit('$paramEAN13'));
});

function getLastEan13Digit(ean) {
if (!ean || ean.length !== 12) {return 'ERROR';};
const multiply = [1, 3];
let total = 0;
ean.split('').forEach((letter, index) => {
total += parseInt(letter, 10) * multiply[index % 2];
})
const base10Superior = Math.ceil(total / 10) * 10;
return base10Superior - total;
};
</script>

This works absolutely fine.

But if I use the same macro more than once on a page, it only renders the first one.

Thanks for any help

Reto

3 answers

1 accepted

0 votes
Answer accepted
Reto Eggenberger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2018

I didn't get it to work with a user macro and javascript. But we also have scriptrunner. And there it was easy:

def bodyvalue = body;
long tot = 0;

if (bodyvalue.length() == 12) {
for (int i = 0; i < 12; i++) {
tot = tot + (Long.parseLong(String.valueOf(bodyvalue.charAt(i))) * (i % 2 == 0 ? 1 : 3));
}
bodyvalue = tot % 10 == 0 ? "0" : "" +(10-(tot % 10));
} else {
bodyvalue = ' falsche Anzahl Ziffern '+bodyvalue.length();
}

body + bodyvalue;

This is my prototype and it works already fine. 

0 votes
Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2018

I think the issue is the <script> construct. You are essential trying to add the same script multiple times to the page's <script> section. Not sure how that is going to work.

Can you restructure using VTL and Java objects?

Reto Eggenberger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2018

Yes, your right. I solved it with scriptrunner (which I'm more comfortble with ...).

0 votes
AnnWorley
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 17, 2018

Hi Reto,

What version of Confluence does the macro work in? When I save it, I get an error message

Screen Shot 2018-08-17 at 11.09.24 AM.png

I am not a user macro veteran so please let me know if I am missing something in the configuration:

Screen Shot 2018-08-17 at 11.11.41 AM.png

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2018

That is an error you get when you have no parameters defined. For this case, you need the following statement:

## @noparams

But it looks like his macro has one parameter: EAN13?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events