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');
}
});
});
}
If you want to change all the text from
#E04935
to#000000
, you can try the below snippet:Snippet
Explanation
The reason your code was not working is because you were trying to use the
getForegroundColor()
method on an object of typeTextRange
while this method is expected to be called from an object of typeTextStyle
.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
Apps Script Editing & Styling Text Slides;
Apps Script Class TextStyle - getForegroundColor().