Last active
November 4, 2021 14:30
-
-
Save olehermanse/6d508b52f5db1a6a66ab75761ffb1570 to your computer and use it in GitHub Desktop.
Replace (JIRA) ticket numbers with links in a Google Doc
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
// This is a script which searches the current | |
// google doc for ticket numbers (based on a regex) | |
// and makes them into links. (To JIRA for example). | |
// It also shortens the links so they only show | |
// ticket number (removing the https... part) | |
// | |
// Edit these 2 variables to fit your needs: | |
const URL = "https://tracker.mender.io/browse/"; | |
const REGEX = "(AC|ARCHIVE|CFE|CO|COOL|ENT|EV|IBT|INF|MAR|MC|MEN|MENWEB|MM|MPPT|NW|QA|SEC|ZEN)\\-[0-9]{1,5}"; | |
// To use this script, open a Google doc, then; | |
// Tools -> Script Editor -> (Paste in script) -> Run | |
// You may be prompted for some permissions | |
function addLinksToDoc() { | |
var body = DocumentApp.getActiveDocument().getBody(); | |
visit(body); | |
} | |
function visit(element) { | |
insertLinks(element); | |
if (element.getNumChildren === undefined){ | |
return; | |
} | |
for (let x = 0; x < element.getNumChildren(); x++) { | |
let child = element.getChild(x); | |
visit(child); | |
} | |
} | |
function insertLinks(element) { | |
// console.log("Visited: " + element.getType()); | |
if (element.getType() != "TEXT") { | |
return; | |
} | |
let match = element.findText(REGEX); | |
while (match) | |
{ | |
console.log(element.getText()); | |
let x = match.getStartOffset(); | |
let y = match.getEndOffsetInclusive(); | |
let ticket = element.getText().slice(x, y+1); | |
console.log("Creating link to: " + ticket); | |
element.setLinkUrl(x, y, URL + ticket); | |
if (x >= URL.length) { | |
if (element.getText().startsWith(URL, x-URL.length)) { | |
element.deleteText(x - URL.length, x - 1); | |
} | |
} | |
match = element.findText(REGEX, match); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment