Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
Script is not highlighting all repeated words, how can I fix this

I have the below script running where it needs to search the entire google doc for specific words I have chosen and highlight them within the doc. It is currently doing this however for some repeated words it does not highlight these. How could I amend the script to ensure all words are highlighted.


function highlightSentences() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  sentences.forEach(sentence => {
    const rangeElement = body.findText(sentence);
    if (rangeElement) {
      const startOffset = rangeElement.getStartOffset();
      const endOffset = rangeElement.getEndOffsetInclusive();
      const element = rangeElement.getElement();
      if (element.editAsText) {
        const textElement = element.editAsText();
        textElement.setBackgroundColor(startOffset, endOffset, "#fcfc03");
      }
    }
  });
}```
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Using this sample script and the regex, how about the following modification?

Modified script:

function highlightSentences() {
  var sentences = ["value1", "value2",,,]; // Please set your search texts.

  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const searchValue = sentences.join("|");
  var rangeElement = body.findText(searchValue);
  while (rangeElement) {
    const startOffset = rangeElement.getStartOffset();
    const endOffset = rangeElement.getEndOffsetInclusive();
    const element = rangeElement.getElement();
    const textElement = element.editAsText();
    textElement.setBackgroundColor(startOffset, endOffset, "#fcfc03");
    rangeElement = body.findText(searchValue, rangeElement);
  }
}

Reference:

  • findText(searchPattern, from)
about 4 years ago · Juan Pablo Isaza Relatório

0

Unfortunately, the findText method does only return one element, as stated in the documentation that can be read here. Nevertheless, all sentences can still be found easily using a while loop, where we keep searching for our desired sentence until the findText function does not find any more. You could adapt your code so something like:

function highlightSentences() {

  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();

  var sentences = ["search", "these"]; //An array of things to highlight

  for (var sentence of sentences){
    var rangeElement = body.findText(sentence);

    while (rangeElement != null) {

      var foundText = rangeElement.getElement().asText();

      var startOffset = rangeElement.getStartOffset();
      var endOffset = rangeElement.getEndOffsetInclusive();
    
      foundText.setBackgroundColor(startOffset, endOffset, "#fcfc03");

      rangeElement = body.findText(sentence, rangeElement);
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda