Example: Stock Exchange Price
You can use the custom macro functionality to create a macro that allows users to display stock exchange prices on a Confluence page.
The example below assumes you already have a REST API authentication token for the StockData REST API stored in a Script Variable with the name STOCKDATA_API_KEY
.
- Select the Settings cog in the top right-hand corner of the screen.
- Select Macros under ScriptRunner.
- Select Create Custom Macro.
- Fill out the fields that appear:
- Macro Name: Stock Exchange Price.
- Description: Use this macro for a stock exchange price to appear on a Confluence page.
- Enabled (radio button): Select Enabled.
- Body Type: None.
- Output Type: Block
Script to Execute: Enter the script for the macro here.
groovydef exchange_symbol = parameters['Stock Exchange Symbol'] def url = "https://api.stockdata.org/v1/data/quote?symbols=${exchange_symbol}&api_token=${STOCKDATA_API_KEY}" def resp = get(url).asObject(Map) if (resp.status > 300) { return "<p>Sorry, we couldn't fetch the stock details for ${exchange_symbol}</p>" } def body = resp.body as Map<List> def data = body.data as List<Map> if (!data) { return "<p>Sorry, we couldn't fetch the stock details for ${exchange_symbol}</p>" } def stock_price = "${data[0].price} ${data[0].currency}" def stock_name = "${data[0].name} (${data[0].ticker})" return """ <p>${stock_name} ${stock_price}</p> """
- Select Add Parameter.
- Type: String
- Name: Stock Exchange Symbol
- Description: Enter the symbol of the stock that you would like to see the exchange price for.
- Required: Do not tick the box
- Hidden: Do not tick the box
Select Add.
When users add this macro to a Confluence page, they will enter something like NASDAQ:AMZN for Amazon.com.
- Select Save.
Result: You can now use this macro on a Confluence page. For help with using the macro, check out the Use Macros section of the Macros documentation.
This is what the macro looks when rendered on a Confluence page:
You can use multiple instances of it in a table, which would look like this:
If the user enters an incorrect stock code symbol in the macro editor, they will receive an error message in the output. You can see this error message in the image above.