2017-10-19 1 views
0

Wir haben ein Google-Skript, das als Add-On ausgeführt wird und grundlegende Formatierungen in einfaches HTML konvertiert.Prüfen Sie den Text des Google-Dokuments auf die URL

Allerdings kann ich nicht scheinen Links zu erkennen, wenn sie einen vollständigen Satz sind.

Funktion, die Links finden sollte;

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 { 
... 

Ausgänge -

<p>A sentence with a <a href="https://www.theguardian.com/politics/2017/oct/19/brexit-talks-uk-must-prepare-to-leave-without-deal-say-former-ministers" class="c">link</a></p> 
<p>undefined<<< -1, -1 (pt) A full link sentence. 0</p> 

Dies ist, wie der Text in der Google Doc aussieht.

enter image description here

Jede Hilfe sehr geschätzt. Wirklich aus meiner Tiefe hier. Auch wenn es nur darum geht, mir das aus dem Skript-Editor zu helfen. dh wählen Sie ein Dokument aus, damit ich die Protokollausgabe sehen und meine Versuchs- und Fehlerausgabe erhöhen kann!

Antwort

1

Ich verstehe die Logik Ihres Skripts nicht; Es verwendet dieselbe "Text" -Variable für die URL und den Linktext. Google-Dokumente dürfen keine blossen Links wie http:// im Textinhalt enthalten. Die Links sind als andere Textattribute codiert und werden mit getLinkUrl aufgerufen.

Hier ist meine Funktion, die alle Textelemente durchläuft, Links erkennt und das HTML-Format zurückgibt. Beachten Sie, dass ein Textelement mehrere Links enthalten kann. Mein Testfall ist

Ein Satz mit einem link und another link und mehr Text.

A full link sentence

und der Ausgang ist

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> 

Die while-Schleife Textelemente übergeht; dann geht die for-Schleife über Textattributindizes. Die textPart ist der Teil des Textes zwischen zwei Indizes; die url ist, was auch immer dieser Teil verbunden ist (möglicherweise null, wenn es kein Link ist). Jeder Teil wird an das Array output gesendet, gegebenenfalls mit Formatierung des Links. Das Array ist verbunden und protokolliert.

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); 
    } 
} 
Verwandte Themen