Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
Google App Scripts Highlight Selected Text

I'm writing an app script and I'm trying to make a function that highlights a particular piece of text. The result of the code is that I'm highlighting the entire line instead of just the text.

Before:

enter image description here

After:

enter image description here

What I want:

enter image description here

Here is my code:

function myFun4(){
  var selection = DocumentApp.getActiveDocument().getSelection();
  if(selection){
    var el = selection.getRangeElements();
    Logger.log(el[0].getElement().getAttributes());
    var el0 = el[0].getElement();
    Logger.log(el0.asText());
    var highlightStyle = {};
    highlightStyle[DocumentApp.Attribute.BOLD] = true;
    highlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#FFFF00';
    el0.setAttributes(highlightStyle);   
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe your goal as follows.

  • You want to highlight the selected text on Google Document.

In this case, how about the following modification?

Modified script:

When you use this script, please select a text on Google Document and run the script.

function myFun4(){
  var selection = DocumentApp.getActiveDocument().getSelection();
  if(selection){
    selection.getRangeElements().forEach(e => {
      var range = [e.getStartOffset(), e.getEndOffsetInclusive()];
      e.getElement().asText()
        .setBackgroundColor(...range, '#FFFF00')
        .setBold(...range, true);
    });
  }
}
  • In this modification, the background color and bold are reflected to the selected text using the methods of setBackgroundColor(startOffset, endOffsetInclusive, color) and setBold(startOffset, endOffsetInclusive, bold).

  • In your script, the style of paragraph is changed. I thought that this might be the reason of your issue.

References:

  • setBackgroundColor(startOffset, endOffsetInclusive, color)
  • setBold(startOffset, endOffsetInclusive, bold)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda