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
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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, your right. I solved it with scriptrunner (which I'm more comfortble with ...).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Reto,
What version of Confluence does the macro work in? When I save it, I get an error message
I am not a user macro veteran so please let me know if I am missing something in the configuration:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.