Okay So, I have a some complex google documents containing tables, and paragraphs and in the middle of these paragraph there are some hyperlinks which I want to find and highlight them I have tried many solution already provided on Stack Overflow but they are not exactly what I looking for those solution only works if doc contain direct URL instead of hyperlinks.
I have write some code which working pretty much fine. but the problem is if any hyperlink present in middle of any line or sentence so it will not targeting that hyperlink. only hyperlink which is single element or at starting of new line is getting target by this code.
here is demo file:- https://docs.google.com/document/d/16cbYg-g2pjpcBuUserhrCRfJw7VcHaXGvtmL0LxMzko/edit
code.gs
function highlightLink(){
let highlightColor = {[DocumentApp.Attribute.BACKGROUND_COLOR]: '#FFFF00'}
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let paraChild = body.getParagraphs();
paraChild.forEach(ele=>{
if(ele.getText()!= ""){
let sentence = ele.getText();
sentence.split(" ").map(item => {
let word = body.findText(item.trim())
let url = word.getElement().getLinkUrl();
if(url!= null){
word.getElement().setAttributes(highlightColor)
}
})
}
})
}
I have already tried the solution available on stack overflow but they not what exactly I am looking for.
CodePudding user response:
Modification points:
- In your script, it seems that the part of the text cannot be checked, and also, for example, when the same word is included in the paragraph, only 1st word is checked. I thought that this might be the reason for your current issue.
How about the following modification when these points are reflected in your script?
Modified script 1:
function highlightLink2() {
// let highlightColor = { [DocumentApp.Attribute.BACKGROUND_COLOR]: '#FFFF00' } // This is not used.
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let paraChild = body.getParagraphs();
paraChild.forEach(ele => {
if (ele.getText() != "") {
let sentence = ele.getText();
// --- I modified the below script.
[...new Set(sentence.split(" ").map(e => e.trim()))].forEach(t => {
let word = ele.findText(t);
while (word) {
let e = word.getElement();
let start = word.getStartOffset();
if (e.getLinkUrl(start)) {
e.asText().setBackgroundColor(start, word.getEndOffsetInclusive(), '#FFFF00');
}
word = ele.findText(t, word);
}
});
// ---
}
});
}
Modified script 2:
As another approach, the following modified script might be able to be used.
function highlightLink3() {
const body = DocumentApp.getActiveDocument().getBody();
const text = body.getText();
const words = [...new Set(text.split(/[\n ]/g).map(e => e.trim()).filter(String))];
words.forEach(t => {
let word = body.findText(t);
while (word) {
const e = word.getElement();
const start = word.getStartOffset();
if (e.getLinkUrl(start)) {
e.asText().setBackgroundColor(start, word.getEndOffsetInclusive(), '#FFFF00');
}
word = body.findText(t, word);
}
});
}
Note:
- In this modified script, your provided Document is used as a test. When you change the Document, this script might be required to be modified. Please be careful about this.