Formatting Google Docs Hyperlink in the middle of a paragraph

48 views
Skip to first unread message

Al

unread,
Aug 21, 2019, 9:41:12 AM8/21/19
to google-apps-sc...@googlegroups.com
I tried to write a script to apply a custom formatting for hyperlinks in a document.
The issue I encountered was that it does not format hyperlinks in the middle of paragraphs.
It only functions when the link is applied for the whole paragraph.

I hope someone can point out what I missed or confirm if what I'm trying to do is not yet possible. Thanks!

Sample Document Content:

This is a test paragraph with a link in the middle of it.

This is a test paragraph with a link on the whole sentence.


Expected Result:

This is a test paragraph with a link in the middle of it.
This is a test paragraph with a link on the whole sentence.

Actual Result:
 This is a test paragraph with a link in the middle of it.
This is a test paragraph with a link on the whole sentence.

Script: 
function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();
  var childNum = body.getNumChildren();
  
  var linkStyle = {};
  linkStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = "#FF0000";
  linkStyle[DocumentApp.Attribute.UNDERLINE] = true;
  
  
  for(var i=0; i<childNum; i++){
    var par = body.getChild(i);
    var link = par.asText().getLinkUrl();
    Logger.log(link);
    
    if(link !=null){
      par.setAttributes(linkStyle);
    }
  }

}


Tanaike

unread,
Aug 22, 2019, 1:21:18 AM8/22/19
to Google Apps Script Community
  • You want to set the custom format for the linked texts on Google Document.
  • There is the case that the linked texts are put to the middle of the paragraph.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

In this modification, the all characters of the texts in each paragraph are checked, and the offset of the linked texts is retrieved. Then, the custom format is set to the texts using the retrieved offset.

Modified script:
From:
for(var i=0; i<childNum; i++){
 
var par = body.getChild(i);
 
var link = par.asText().getLinkUrl();
 
Logger.log(link);
 
 
if(link !=null){
    par
.setAttributes(linkStyle);
 
}
}


To:
for (var i = 0; i < childNum; i++) {
 
var par = body.getChild(i);
 
var text = par.asText();
 
var t = text.getText();
 
var temp = {};
 
for (var j = 0; j < t.length; j++) {
   
var link = text.getLinkUrl(j);
   
if (!("start" in temp) && link) {
      temp
.start = j;
   
} else if ("start" in temp && !("end" in temp) && !link || j == t.length - 1) {
      temp
.end = j;
   
}
   
if ("start" in temp && "end" in temp) {
      text
.setAttributes(temp.start, temp.end, linkStyle)
      temp
= {};
   
}
 
}
}


References:

If I misunderstood your issue and this was not the result you want, I apologize.

Al

unread,
Aug 22, 2019, 2:46:13 AM8/22/19
to Google Apps Script Community
Thank you. This was very great help.
Reply all
Reply to author
Forward
0 new messages