Home > OS >  Highlight Words in a text, based on a list of keywords
Highlight Words in a text, based on a list of keywords

Time:01-15

I have a google doc. There is a paragraph in the first part. Below is a table with keywords. I want to highlight words similar to the keywords in the table in the paragraph. I wrote the following script. But it was not successful.

this code part uses get keyword from the table var keywords = [];

function getKeywordsFromTable() {
  var doc = DocumentApp.getActiveDocument();
  var tables = doc.getTables();
  
  for (var i = 0; i < tables.length; i  ) {
    var table = tables[i];
    for (var j = 0; j < table.getNumRows(); j  ) {
      var row = table.getRow(j);
      for (var k = 0; k < row.getNumCells(); k  ) {
        var cell = row.getCell(k);
        var text = cell.getText();
        keywords = keywords.concat(text.split(" ")); // split the text by space and add it to the keywords array
      }
    }
  }
  
  Logger.log(keywords); // you can check the keywords array in the log
}

Try this code part using highlight keywords in paragraph

function hlKwInPara() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var text = body.getText();
  var paragraphs = text.split(" ");

  for (var i = 0; i < paragraphs.length; i  ) {
    for (var j = 0; j < keywords.length; j  ) {
      var keyword = keywords[j];
      var keywordStart = paragraphs[i].indexOf(keyword);
      if (keywordStart !== -1) {
        var keywordEnd = keywordStart   keyword.length;
        body.replaceText(keyword, "<b>"   keyword   "</b>", {matchCase: true});
      }
    }
  }
}

I will publish part of the sample file. See this link

CodePudding user response:

I believe your goal is as follows.

  • About I want to highlight words similar to the keywords in the table in the paragraph., from your showing script, I understood that you wanted to replace the texts in the paragraphs with the words in the tables. For example, you want to replace sample with <b>sample</b>.

In this case, how about the following modification?

Modification points:

  • In your script of getKeywordsFromTable(), keywords is not declared.
  • I thought that in this case, it might be able to scan each paragraph.

When these points are reflected in your script, how about the following modification?

Modified script:

function getKeywordsFromTable() {
  var keywords = [];
  var doc = DocumentApp.getActiveDocument();
  var tables = doc.getTables();
  for (var i = 0; i < tables.length; i  ) {
    var table = tables[i];
    for (var j = 0; j < table.getNumRows(); j  ) {
      var row = table.getRow(j);
      for (var k = 0; k < row.getNumCells(); k  ) {
        var cell = row.getCell(k);
        var text = cell.getText();
        keywords = keywords.concat(text.split(" ")); // split the text by space and add it to the keywords array
      }
    }
  }
  Logger.log(keywords); // you can check the keywords array in the log
  return keywords;
}

// Please run this function.
function myFunction() {
  const words = [...new Set(getKeywordsFromTable())].map(e => e.trim());
  const body = DocumentApp.getActiveDocument().getBody();
  words.forEach(t => {
    for (let i = 0; i < body.getNumChildren(); i  ) {
      const c = body.getChild(i);
      if (c.getType() != DocumentApp.ElementType.TABLE) {
        c.asParagraph().replaceText(t, "<b>"   t   "</b>");
      }
    }
  });
}
  • When this script is run, the texts in the paragraphs are replaced with the words in the tables like <b>sample</b>.

Note:

  • If your expected highlight of I want to highlight words is to change the background color of words, please test myFunction as follows.

    function myFunction() {
      const words = [...new Set(getKeywordsFromTable())].map(e => e.trim());
      const body = DocumentApp.getActiveDocument().getBody();
      words.forEach(t => {
        let word = body.findText(t);
        while (word) {
          const e = word.getElement();
          const start = word.getStartOffset();
          if (e.getParent().getParent().getType() != DocumentApp.ElementType.TABLE_CELL) {
            e.asText().setBackgroundColor(start, word.getEndOffsetInclusive(), '#00FFFF');
          }
          word = body.findText(t, word);
        }
      });
    }
    

Reference:

  • Related