random uuid in user maco
Asked by Ren? Kray:

I want to write a Confluence user macro, which should get a unique uuid. Currently I fail to randomly generate this UUID.  
The format should look like "#c199f52d-9724-4911-ad47-479e937d3b88".  
I guess I can solve this, if someone shows me how I can access the java function Math.random(); from my user macro.


Answers:
author: Davin Studer
body: This is how I do it. I was never able to find a good way to generate a random number or find a way to get at Math.random() but this seems to work well.

```
#set( $id = $action.dateFormatter.calendar.timeInMillis )
```

Even when the user macro is used multiple times per page I find that I get different mills. So for velocity I reference it this way ...

```
<div id="mydiv-$id"></div>
```

and then I can target it with JavaScript this way.

```
<script>
var id = '$id';

ASJ.toInit(function(){
    var stuff = AJS.$('#' + id).html();
});
</script>
```

comments: author: Ren? Kray
body: Thanks for your help. I will test it on occasion. For now my hash solution (see above) works fine for me.


author: Lion
body: why I can not get time in 8.2.1? i copy the line in my macro and change nothing, but I get a $id result .


author: Lion
body: It's a bug , shit, wasted my 2 hours.

<https://jira.atlassian.com/browse/CONFSERVER-82741>

BTW, confluence does not give a solution to distinct many macro instances of the same macro definition in the same page , No one has this demand?

BTW, #set( $id = $action.dateFormatter.calendar.get(14) ) is better in this scenario.


author: Bill Bailey
body: For my user macros, I don't worry about that. Confluence will generate what it needs. Even if I am wrapping an existing macro, I delete that reference. It works fine.

comments: author: Ren? Kray
body: I want to modify the rendered results via JavaScript so I need a html object id for reference the right object on the page if a user used the macro multiple times on the same page.


author: Bill Bailey
body: Hmm, seems a bit hard. You could create your own ID, but you would then need to increment it each time, which complicated. And seems like a brittle solution to rely on javascript to modify an object based on ID that could change.

Is there a way to do it within the user macro so that it is dynamic?

Maybe sharing a bit more detail on what you are trying to do would help?


author: Ren? Kray
body: I have now solved this by forming a hash over all input parameters. This should be sufficient for my purposes.


author: C_ G_
body: [@Ren? Kray](/t5/user/viewprofilepage/user-id/4151620) could you explain or paste your macro solution to help me get started?

I'm still looking for a solution to generate a unique ID on a page


author: Ren? Kray
body: Hi Cyrus:

If I remember correctly, I was looking for a solution for the following macro.

In the end, I used id="$paramtitle" as "unique" id. For me it was unique enough, but for a general purpose, I would suggest finding a different solution.

Regards, Ren?

```
## Macro title: roadmap_panel
## Macro has a body: Y
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: Ren? Kray
## Date created: 2021-03-23
## Installed by: Ren? Kray
## @param title:title=Title|type=string|required=true|default=Title for this panel
## @param o1:title=O1|type=boolean|required=true|default=false
## @param o1ext:title=O1 Ext|type=string|required=false|default=
## @param o2:title=O2|type=boolean|required=true|default=false
## @param o2ext:title=O2 Ext|type=string|required=false|default=
## @param o3:title=O3|type=boolean|required=true|default=false
## @param o3ext:title=O3 Ext|type=string|required=false|default=
## @param health:title=Health|type=boolean|required=true|default=false

<style>
div.roadmap_panel{
    border: solid grey 1px;
    border-radius: 5px;
    background-color: #ddd;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 3px 5px 0 rgba(0, 0, 0, 0.19);
}
div.roadmap_panel div.roadmap_panel_title{
    padding: 5px 5px 5px 10px;
    border-radius: 5px;
    font-weight: bold;
}
div.roadmap_panel div.roadmap_panel_title span.button{
    padding: 2px 5px 2px 5px;
    float: right;
}
div.roadmap_panel div.roadmap_panel_title span.status{
    padding: 2px 5px 2px 5px;
    border-radius: 3px;
    border: solid black 1px;
    color: white;
    font-size: 80%;
    float: right;
    margin-right: 5px;
}
div.roadmap_panel div.roadmap_panel_body{
    background-color: white;
    padding: 5px;
    border-radius: 0 0 5px 5px;
    border-top: solid #999 1px;
    display: none;
}
</style>
<div class="roadmap_panel" id="$paramtitle">
<div onclick="roadmap_panel_show_hide('$paramtitle')" class="roadmap_panel_title">
<span class="button">+</span>
#if ($paramhealth==true)
<span class="status" style="background-color: green;">Health</span>
#end
#if ($paramo3==true)
<span class="status" style="background-color: red;">
O3
#if ( "$paramo3ext" != "" )
- $paramo3ext
#end
</span>
#end
#if ($paramo2==true)
<span class="status" style="background-color: red;">
O2
#if ( "$paramo2ext" != "" )
- $paramo2ext
#end
</span>
#end
#if ($paramo1==true)
<span class="status" style="background-color: red;">
O1
#if ( "$paramo1ext" != "" )
- $paramo1ext
#end
</span>
#end
<p style="margin:0">$paramtitle</p>
</div>
<div class="roadmap_panel_body">
$body
</div>
</div>
<script>
function roadmap_panel_show_hide(id) {
    panel=document.getElementById(id);
    button=panel.getElementsByTagName("SPAN")[0];
    body=panel.getElementsByTagName("DIV")[1];
    if(body.style.getPropertyValue('display')=="block"){
        body.style.setProperty('display','none');
        button.innerHTML="+";
    }else{
        body.style.setProperty('display','block');
        button.innerHTML="-";
    }
}
</script>
```


author: Lion
body: I think it's not enough, When i want to include the same macro in one page many times, this script will only render once.


Tags: confluence-server

server

user-macro