0

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');
      }
    });
  });
}
  • Can you provide the detail of `but have come up short in trying to write something for ForegoundColor.` and `I tried to adapt this code and cannot make it work:`? And I cannot understand about `I need to replace all text formatted with foregroundcolor hex #e04935 with hex #000000.`. Can I ask you about the detail of it? – Tanaike Oct 07 '20 at 22:02
  • My slides have red text throughout (all hex #e04935) and I need to change all that text to black. I thought I could use the framework for this code, because it changes the color of all the text with links. I tried this and I have NO IDEA what I am doing: var textStyle = element.asShape().getText().getForegroundColor(); if ForegroundColor('#e04935'){ textStyle.setForegroundColor('#000000'); – Tina Zappile Oct 08 '20 at 00:05
  • Thank you for replying. What is `var textStyle = element.asShape().getText().getForegroundColor(); if ForegroundColor('#e04935'){ textStyle.setForegroundColor('#000000');`? Can you show your current script? If you can do, please add it to your question? – Tanaike Oct 08 '20 at 00:15
  • I added what I have so far to my question. Clearly I have no idea what I am doing – Tina Zappile Oct 08 '20 at 15:24
  • Thank you for replying. I noticed that an answer has already been posted. I would like to respect the existing answer. I think that it will resolve your issue. – Tanaike Oct 08 '20 at 23:01

1 Answers1

1

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

ale13
  • 5,679
  • 3
  • 10
  • 25