Last active
September 9, 2018 02:15
-
-
Save gigamonkey/c9899710ec9cca4c102c761aaa5c70e5 to your computer and use it in GitHub Desktop.
App Script script to change the link text of the link at the current cursor position to the name of the linked 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
function onInstall() { | |
onOpen(); | |
} | |
function onOpen() { | |
DocumentApp.getUi() | |
.createMenu('Fix links') | |
.addItem('Entitle link at cursor', 'entitleAtCursor') | |
.addItem('Entitle all links', 'entitleAllLinks') | |
.addToUi(); | |
} | |
function entitleGoogleDoc(url) { | |
try { | |
var doc = DocumentApp.openByUrl(url); | |
return doc.getName(); | |
} catch (err) { | |
return null; | |
} | |
} | |
function urlStripper(base) { | |
return function (url) { | |
if (url.length > base.length && url.substr(0, base.length) === base) { | |
return url.substr(base.length); | |
} else { | |
return null; | |
} | |
} | |
} | |
var entitlements = [ | |
urlStripper("https://democrats.atlassian.net/browse/"), | |
entitleGoogleDoc | |
] | |
function entitleAtCursor() { | |
var c = DocumentApp.getActiveDocument().getCursor(); | |
var offset = c.getOffset(); | |
var text = c.getElement().editAsText(); | |
var url = text.getLinkUrl(offset); | |
if (url != null) { | |
var beg = findBoundary(text, url, offset, -1); | |
var end = findBoundary(text, url, offset, 1); | |
entitleLink(text, beg, end, url); | |
} | |
} | |
function entitleAllLinks() { | |
var links = getAllLinks(); | |
Logger.log('Got ' + links.length + ' links.'); | |
for (var i = links.length - 1; i >= 0; i--) { | |
var link = links[i]; | |
Logger.log('entitling links[' + i + ']: ' + link.url); | |
entitleLink(link.text, link.start, link.end, link.url); | |
} | |
} | |
function entitleLink(text, start, end, url) { | |
var title = titleForURL(url); | |
if (title != null) { | |
text.deleteText(start, end) | |
text.insertText(start, title); | |
text.setLinkUrl(start, start + title.length - 1, url); | |
} | |
} | |
function titleForURL(url) { | |
for (var i = 0; i < entitlements.length; i++) { | |
var title = entitlements[i](url); | |
if (title != null) return title; | |
} | |
return null; | |
} | |
function findBoundary(text, url, offset, step) { | |
var o = offset; | |
while (o >= 0 && o < text.getText().length && text.getLinkUrl(o) === url) { | |
o += step; | |
} | |
return o - step; | |
} | |
function docTitle(url) { | |
try { | |
var doc = DocumentApp.openByUrl(url); | |
return doc.getName(); | |
} catch (err) { | |
return null; | |
} | |
} | |
function getAllLinks(element, links) { | |
element = element || DocumentApp.getActiveDocument().getBody(); | |
links = links || []; | |
if (element.getType() === DocumentApp.ElementType.TEXT) { | |
var textObj = element.editAsText(); | |
var text = element.getText(); | |
var current = null; | |
for (var i = 0; i < text.length; i++) { | |
var url = textObj.getLinkUrl(i); | |
if (url) { | |
if (current && current.url != url) { | |
// Url changed: close current link and save it. | |
current.end = i - 1; | |
links.push(current); | |
current = null; | |
} | |
if (!current) { | |
// In a url but no current link: start a new one. | |
current = { | |
text: element, | |
url: url, | |
start: i | |
} | |
} | |
} else { | |
if (current) { | |
// Out of url but we still have a link: close it off and save it. | |
current.end = i - 1; | |
links.push(current); | |
current = null; | |
} | |
} | |
} | |
if (current) { | |
// End of text and we still have a link: close it off and save it. | |
current.end = text.length - 1; | |
links.push(current); | |
current = null; | |
} | |
} else { | |
var n = element.getNumChildren(); | |
for (var i = 0; i < n; i++) { | |
getAllLinks(element.getChild(i), links); | |
} | |
} | |
return links; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to figure out how to use this -
getLinkUrl(offset)
to pull the linked URL from a single word in a paragraph of text within a google doc. Can you please offer me a little help?getLinkUrl(2)
retruns - Cannot find method getLinkUrl(number). in Google Apps Script IDEThank you for any help you can offer.