Last active
January 28, 2022 17:03
-
-
Save maxdemaio/154c60ccf344e78ae659c645397520f1 to your computer and use it in GitHub Desktop.
Given a table id attribute, this script will convert said table into Jira table format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Quick and simple export target #table_id into a jiraTable | |
function download_table_as_jiraTable( | |
table_id, | |
separator = "|", | |
headerSeparator = "||" | |
) { | |
// Select rows from table_id | |
var rows = document.querySelectorAll("table#" + table_id + " tr"); | |
// Construct jiraTable | |
var jiraTable = []; | |
for (var i = 0; i < rows.length; i++) { | |
var row = [], | |
cols = rows[i].querySelectorAll("td, th"); | |
for (var j = 0; j < cols.length; j++) { | |
var data = cols[j].innerText; | |
// Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-jiraTable) | |
data = data.replace(/"/g, '""'); | |
// Push escaped string | |
if (i == 0 && j == cols.length - 1) { | |
row.push(headerSeparator + data + headerSeparator); | |
} else if (i == 0 && j != cols.length - 1) { | |
row.push(headerSeparator + data); | |
} else if (j == cols.length - 1) { | |
row.push(separator + data + separator); | |
} else { | |
row.push(separator + data); | |
} | |
} | |
jiraTable.push(row); | |
} | |
var jiraTable_string = jiraTable.join("\n"); | |
// Remove commas from string version of array | |
jiraTable_string = jiraTable_string.replace(/,/g, ""); | |
// Download it | |
var filename = | |
"export_" + table_id + "_" + new Date().toLocaleDateString() + ".txt"; | |
var link = document.createElement("a"); | |
link.style.display = "none"; | |
link.setAttribute("target", "_blank"); | |
link.setAttribute( | |
"href", | |
"data:text/jiraTable;charset=utf-8," + encodeURIComponent(jiraTable_string) | |
); | |
link.setAttribute("download", filename); | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment