Creating a Table in the Description

Creating tables in the description is possible thanks to Atlassian's wiki markup, but how would we create a table programmatically using SIL? Find out by reading below!

Background

You can create a table in the description or any custom field that has been set to render wiki markup. For instructions on configuring a custom field to render wiki markup, see this Atlassian documentation below:

Configuring renderers

For this tutorial, we will be using this Atlassian documentation on using text formatting notation for a table. Please refer to the Atlassian documentation below:

Text Formatting Notation Help - Tables 

As you can see from the Atlassian website, the notation for creating a table is fairly simple.

Code Example

The idea here is to replace the text as seen above to a SIL script that creates the formatting for us. For example:

string [] headers = {"heading 1", "heading 2", "heading 3"}; string [] data = {"col A1","col A2","col A3","col B1","col B2", "col B3"};   // create header string table = "||" + replace(headers, "|", "||") + "||\n";   number tableWidth = size(headers); number dataSize = size(data); number remainder = 0;   if ((dataSize % tableWidth) != 0) {     throw "Data must be equal to the headers!"; }   // add values to table for (int i=0; i < dataSize; i++) {     table += "|" + data[i];     remainder = tableWidth % (i + 1);     if (remainder == 0 && i != 0) {         table += "|\n";     } }   table += "|";   runnerLog(table);

Code Breakdown - Headers

SIL normally stores arrays with elements separated by pipes. The following line of code:

string [] headers = {"heading 1", "heading 2", "heading 3"};


Would be stored like this in memory:

heading 1|heading 2|heading 3


Since we know what our headers look like in memory, we can convert the pipes so that they look like the example from Atlassian. We also add some extra pipes on both sides.

Our output for the table now looks like this:

Code Breakdown - Error Catching

What happens if the columns of data do not match the headers? At best, this means that the columns will not look right. At worst, the data will be wrong. For this, use the modulo operator % to see if dataSize is equally divisible by tableWidth.

Code Breakdown - Looping

Here we loop through the data. Anytime the modulo operator is zero, we know that we are at the end of the line and can put in a new line character.

Additional Help

Need help implementing this script? Talk to me directly to me by clicking on the bot on this page.

Related articles

Filter by label

There are no items with the selected labels at this time.

Peacock