Google apps script 检查谷歌文档文本的URL
我们有一个Google脚本,它作为附加组件运行,并将基本格式转换为基本HTML 然而,当链接是一个完整的句子时,我似乎无法检测到它们 应该找到链接的功能Google apps script 检查谷歌文档文本的URL,google-apps-script,hyperlink,google-docs,Google Apps Script,Hyperlink,Google Docs,我们有一个Google脚本,它作为附加组件运行,并将基本格式转换为基本HTML 然而,当链接是一个完整的句子时,我似乎无法检测到它们 应该找到链接的功能 function processText(item, output) { var text = item.getText(); var indices = item.getTextAttributeIndices(); Logger.log("processText. "+item+". "+text); if (indices.lengt
function processText(item, output) {
var text = item.getText();
var indices = item.getTextAttributeIndices();
Logger.log("processText. "+item+". "+text);
if (indices.length <= 1) {
var partAtts = item.getAttributes(indices[0]);
// Assuming that a whole para fully italic is a quote
if(item.isBold()) {
output.push('<b>' + text + '</b>');
}
else if(item.isItalic()) {
output.push('<blockquote>' + text + '</blockquote>');
}
else if (text.trim().indexOf('http://') > -1) {
output.push('<a href="' + text + '" rel="nofollow" class="a">' + text + '</a>');
}
else if (text.trim().indexOf('https://') > -1) {
output.push('<a href="' + text + '" rel="nofollow" class="b">' + text + '</a>');
}
else {
//using this to debug as have no idea how to run from script and use Logger.
output.push(partAtts[0]+"<<< "+text.trim().indexOf('http://')+ ", "+ text.trim().indexOf('https://')+ " (pt) "+text+". "+indices);
//output.push(text);
}
}
else {
...
函数processText(项,输出){
var text=item.getText();
var index=item.getTextAttributeIndicates();
Logger.log(“processText.+项+”+文本);
如果(索引长度-1){
输出推送(“”);
}
else if(text.trim().indexOf('https://')>-1){
输出推送(“”);
}
否则{
//使用它进行调试,因为我不知道如何从脚本运行并使用记录器。
output.push(partAtts[0]+”我不理解脚本的逻辑;它对URL和链接文本使用相同的“text”变量。Google文档不应该在文本内容中包含像http://
这样的裸链接;链接被编码为其他文本属性,并通过getLinkUrl
访问
下面是我的函数,它遍历所有文本元素,检测链接,并返回HTML格式。请注意,一个文本元素可能包含多个链接。我的测试用例是
包含一个或多个文本的句子
输出是
A sentence with a <a href="http://example.com">link</a> and <a href="https://stackoverflow.com">another link</a> and more text.
<a href="http://example.com">A full link sentence</a>
我不理解你的脚本的逻辑;它对URL和链接文本使用相同的“text”变量。谷歌文档不应该在文本内容中有像http://
这样的裸链接;链接被编码为其他文本属性,并通过getLinkUrl
访问
下面是我的函数,它遍历所有文本元素,检测链接,并返回HTML格式。请注意,一个文本元素可能包含多个链接。我的测试用例是
包含一个或多个文本的句子
输出是
A sentence with a <a href="http://example.com">link</a> and <a href="https://stackoverflow.com">another link</a> and more text.
<a href="http://example.com">A full link sentence</a>
function linkDetection() {
var body = DocumentApp.getActiveDocument().getBody();
var found = body.findElement(DocumentApp.ElementType.TEXT);
while (found) {
var elem = found.getElement();
var text = elem.getText();
var output = [];
var indices = elem.getTextAttributeIndices();
for (var i = 0; i < indices.length; i++) {
var textPart = (i == indices.length - 1 ? text.slice(indices[i]) : text.slice(indices[i], indices[i+1]));
var url = elem.getLinkUrl(indices[i]);
output.push(url ? '<a href="' + url + '">' + textPart + '</a>' : textPart);
}
Logger.log(output.join(''));
found = body.findElement(DocumentApp.ElementType.TEXT, found);
}
}