DEV Community

Josua Schmid
Josua Schmid

Posted on

1

Auto-colorize Google Docs

Formatting options in Google Docs are limited. And that's good because formatting can be a lot of manual work (e.g. in Microsoft Word). But sometimes it would be nice to have some fine-grained auto-formatting feature like coloring a certain word if it occurs in the text.

Luckily there is Google Apps Script. You can apply some JavaScript code to your documents very easily, like for example to color some words selected with a Regular Expression:

function colorText() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();

  for (var i = 0; i < paragraphs.length; i++) {
    var searchResult = paragraphs[i].findText('^.*\:');

    if (searchResult) {
      var startIndex = searchResult.getStartOffset();
      var endIndex = searchResult.getEndOffsetInclusive();

      searchResult.getElement().asText().setForegroundColor(
        startIndex, endIndex, '#571f4e'
      );
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →