Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Confluence User Macro - parse a table

Matteo Gubellini _SoftComply_
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.
February 10, 2021

Hi guys,

I started dabbling in user macros and I'm having some issues with parsing the content of a table.

Basically my macro is a wrapper around another one (page properties report) which outputs an html table. I'm using .split with </tr> but if I try to output the parsed content, it looks like Confluence renders the HTML. So I end up with what I think is the bare value of a cell, but in reality it contains a lot more garbage such as spans, etc.

E.g. in the page I see the text "draft", but if I measure the length of the string is like 335...

Any suggestion?

Please note that I know very little of coding...

Thanks!

2 answers

0 votes
Matteo Gubellini _SoftComply_
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.
February 10, 2021

Hi Bill,

The body is rendered, but I didn't know you could escape the output.

This is a snippet:

#set($myTrainingBody=$body.split("</thead>")) ## Isolate table headers

#foreach ($string in $myTrainingBody)
#set($xxx = $myTrainingTemp.add($string))
##<p> $string </p>
#end

##$myTrainingTemp

#set($myTrainingLine=$myTrainingTemp.get(0)) ## Get headers
##Line $myTrainingLine

#set($myTrainingRow=$myTrainingLine.split("</th>"))
#foreach($data in $myTrainingRow)
#set($xxx = $myTrainingPageProperties.add($data))
#set($j = $j + 1)
#end
#set($xxx=$myTrainingMatrix.add($myTrainingPageProperties))
#foreach($point in [1..$j])
#set($xxx = $myTrainingPageProperties.remove(0))
#end
#set($j = 0)

#set($myTrainingLine=$myTrainingTemp.get(1)) ## Disregard headers
##Line $myTrainingLine

#set($myTrainingRow=$myTrainingLine.split("</tr>"))

#foreach ($string in $myTrainingRow)
#set($xxx = $myTrainingRowT.add($string))
##<p>String $string </p>
#end

#foreach($entry in $myTrainingRowT)
#set($line = $myTrainingRowT.get($i))
#set($i = $i + 1)
#set($tempLine=$line.split("</td>"))
#foreach($data in $tempLine)
#set($xxx = $myTrainingPageProperties.add($data))
#set($j = $j + 1)
#end
#set($xxx=$myTrainingMatrix.add($myTrainingPageProperties))
##<p>Page Properties $myTrainingPageProperties</p>
#foreach($point in [1..$j])
#set($xxx = $myTrainingPageProperties.remove(0))
#end
#set($j = 0)
#end

_____________________________

When I access one of the data in $myTrainingMatrix, although the output may be something just like "Draft", the length of the string is like 301.

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.
February 10, 2021

Well it would be best to post your code if possible, and then a screencap of what is happening (and did you set the output to be escaped or rendered).

I will give you one warning though. I have created a wrapper macro around the page properties macro. It displayed OK on the page, but I was unable to pull the value with page properties report macro (I think there a bug was filed for this).

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.
February 10, 2021

So you are nesting the page properties macro inside your user macro?

My suggestion is to write a wrapper macro around the page properties macro (i,e,, your user macro uses parameters to configure the macro. Then you do not need to parse it.

Take a look at this article I wrote to see if this concept would work for you: Getting-Started-with-User-Macros-Writing-a-Wrapper-Macro The idea it to come at the issue orthogonality.

Matteo Gubellini _SoftComply_
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.
February 10, 2021

Hi Bill,

I read your article, but how can I then use the output of the page properties macro? I need to pick values in certain cells and based on their value trigger some behavior.

Thanks.

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.
February 10, 2021

Well the user would select or enter the value when inserting the macro. At that point, that setting is saved to a variable that you can use elsewhere in the code.

So in my macro code, the user parameter is set here:

## @param Priority:title=Priority|type=enum|enumValues=Critical,High,Medium,Low

Now you can use this value in its variable form 

$paramPriority
Matteo Gubellini _SoftComply_
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.
February 11, 2021

Hi Bill,

I should have explained the situation a bit better, my bad.

The Page Properties Report macro pulls certain values from pages in another space.

I need to get the values reported by the macro in a certain column and manipulate them. I don't really need a pure wrapper, as the inputs to the Properties Report macro don't change.

Thanks for your help

Matteo

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.
February 11, 2021

Sorry I missed that. Let me think about it. You should be able to pull the values as java objects,. i.e., not use the report macro at all.

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.
February 11, 2021

Take a look at this question here. It should point you in the right direction as far as searching for a solution

How-to-access-a-page-property-via-macro 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events