Answers:
我对使用Google Apps脚本进行了一些研究,并提出了适合我需要的脚本。我希望它对其他人也有用。
function onOpen() {
DocumentApp.getUi().createAddonMenu()
.addItem('Make URLs Clickable', 'makeUrlsClickable')
.addToUi();
}
function makeUrlsClickable() {
var urlRegex = 'http[s]?:\/\/[^ ]+';
var body = DocumentApp.getActiveDocument().getBody();
var urlElement = body.findText(urlRegex);
while (urlElement != null) {
var urlText = urlElement.getElement().asText();
var startOffset = urlElement.getStartOffset();
var endOffset = urlElement.getEndOffsetInclusive();
urlText.setLinkUrl(startOffset, endOffset, getOnlyUrl(urlText.getText()));
urlElement = body.findText(urlRegex, urlElement);
}
}
function getOnlyUrl(text) {
var startOffset = text.indexOf('http');
var endOffset = text.indexOf(' ', startOffset);
if (endOffset === -1) {
endOffset = text.length;
}
return text.substring(startOffset, endOffset);
}