Google Slides API- How to change text color for all shapes of a certain color

1.5k views Asked by At

I have 11 files with over 140 slides each, and none of the shapes are tied to a theme/master. My goal was the change the master font and also replace all the red text (there is SO much red text) with black text.

I successfully updated the master font (thanks to https://gist.github.com/dsottimano/20a50daded2128b4c86acb430cecba67), but have come up short in trying to write something for ForegoundColor.

I tried to adapt this code and cannot make it work: https://mashe.hawksey.info/2017/10/changing-the-color-of-all-links-in-google-slides-with-google-apps-script/

I need to replace all text formatted with foregroundcolor hex #e04935 with hex #000000.

Appreciate any tips on making this work!

Here is what I have done so far:

//original from: https://mashe.hawksey.info/2017/10/changing-the-color-of-all-links-in-google-slides-with-google-apps-script/
/**
 * @OnlyCurrentDoc
 */
 
function changeTextColorforShapes(){
  var deck = SlidesApp.getActivePresentation();
  var slides = deck.getSlides();
  slides.forEach(function(slide) {
    // https://developers.google.com/apps-script/reference/slides/slide#getPageElements()
    var elements = slide.getPageElements();
    elements.forEach(function(element){
      // https://developers.google.com/apps-script/reference/slides/page-element#getPageElementType()
      var type = element.getPageElementType();
      // Text boxes are 'SHAPES' (this code doesn't handle table cells)
      if (type == "SHAPE"){
        // https://developers.google.com/apps-script/reference/slides/text-range#getTextStyle()
        var textStyle = element.asShape().getText().getForegroundColor();
        // https://developers.google.com/apps-script/reference/slides/text-style#hasLink()
        // Returns true if text is color #e04935 (https://www.color-hex.com/color/e04935) and changes text to color #ffffff (black)
        if (ForegroundColor('#e04935')
          textStyle.setForegroundColor('#ffffff');
      }
    });
  });
}
1

There are 1 answers

0
ale13 On

If you want to change all the text from #E04935 to #000000, you can try the below snippet:

Snippet

// Copyright 2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0

function changeColor() {
   var presentation = SlidesApp.getActivePresentation();
   var slides = presentation.getSlides();
   for (let i = 0; i < slides.length; i++) {
      var elements = slides[i].getPageElements();
      for (let j = 0; j < elements.length; j++)
         if (elements[j].asShape().getText().getTextStyle().getForegroundColor().asRgbColor().asHexString() == '#E04935')
            elements[j].asShape().getText().getTextStyle().setForegroundColor('#000000');

   }
}

Explanation

The reason your code was not working is because you were trying to use the getForegroundColor() method on an object of type TextRange while this method is expected to be called from an object of type TextStyle.

So in order to test exactly if the color of the text is the wanted one, you will have to retrieve first the color as a RGB color in order to get the HEX value of it.

Reference