I'm using the code below to search for URLs in a Google Doc and turn them into active links. It works beautifully, except that the link cuts off the last character of the URL (e.g. "https://link.com/123" gets linked as "https://link.com/12").
function updateLinks() {
var linkRegex = "https?:\/\/[^\\s]*";
//Open active doc
var body = DocumentApp.getActiveDocument().getBody();
//Find URLs
var link = body.findText(linkRegex);
//Loop through the body finding texts matching the search pattern
while (link != null) {
// Get the link as an object
var linkElement = link.getElement().asText();
// Get the positions of start and end
var start = link.getStartOffset();
var end = link.getEndOffsetInclusive();
//slice only the link out of it
var correctLink = linkElement.getText().slice(start, end);
// Format link
linkElement.setLinkUrl(start, end, correctLink);
// Find next
link = body.findText(linkRegex, link);
}
}
I'm sure this is simple, but I'm a complete novice and would appreciate any advice. Thanks in advance!