Forked from dsottimano/gist:20a50daded2128b4c86acb430cecba67
          
        
    
          Last active
          June 8, 2024 02:28 
        
      - 
      
- 
        Save tpetrzilkaCD/c2c990138d8924e22398da826cbaa910 to your computer and use it in GitHub Desktop. 
    Google slides - change font for every slide using apps script
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | //script adapted by @dsottimano - source> https://gist.github.com/dsottimano/20a50daded2128b4c86acb430cecba67 | |
| //original from: https://stackoverflow.com/questions/52569689/clear-text-formatting-in-slides-using-apps-script | |
| //credit to https://stackoverflow.com/users/7108653/tanaike | |
| function onOpen() { | |
| SlidesApp.getUi() | |
| .createMenu('Custom Menu') | |
| .addItem('Change Master Font', 'changeFont') | |
| .addToUi(); | |
| } | |
| function getFont() { | |
| var ui = SlidesApp.getUi(); | |
| var result = ui.prompt( | |
| 'Replace all fonts', | |
| 'Please enter the exact name of the font you want to use on every slide:', | |
| ui.ButtonSet.OK_CANCEL); | |
| if (result.getSelectedButton() == ui.Button.OK) { | |
| return result.getResponseText(); | |
| } else { | |
| return false | |
| } | |
| } | |
| function toDefault(text,font) { | |
| if (text.getRange(0,1).asString().charCodeAt(0) != 10) { | |
| var style = text.getTextStyle(); | |
| return style.setFontFamily(font); | |
| } | |
| return null; | |
| } | |
| function changeFont() { | |
| var font = getFont(); | |
| var slideDeck = SlidesApp.getActivePresentation(); | |
| var pageElements = []; | |
| var slide = slideDeck.getSlides().forEach(function(s) { | |
| pageElements.push(s.getPageElements()); | |
| }) | |
| pageElements = pageElements.flat(Infinity); | |
| pageElements.forEach(function(e) { | |
| if (e.getPageElementType() == "SHAPE") { | |
| var text = e.asShape().getText(); | |
| toDefault(text,font); | |
| } else if (e.getPageElementType() == "TABLE") { | |
| var table = e.asTable(); | |
| try { | |
| for (var row = 0; row < table.getNumRows(); row++) { | |
| for (var col = 0; col < table.getNumColumns(); col++) { | |
| var text = table.getCell(row, col).getText(); | |
| toDefault(text,font); | |
| } | |
| } | |
| } catch (err) { | |
| Logger.log(err); | |
| Logger.log(text.asString()); | |
| //Logger.log(table.getTitle()) | |
| } | |
| } | |
| }); | |
| } | |
| Array.prototype.flat || Object.defineProperty(Array.prototype, "flat", { | |
| configurable: !0, | |
| value: function r() { | |
| var t = isNaN(arguments[0]) ? 1 : Number(arguments[0]); | |
| return t ? Array.prototype.reduce.call(this, function (a, e) { | |
| return Array.isArray(e) ? a.push.apply(a, r.call(e, t - 1)) : a.push(e), a | |
| }, []) : Array.prototype.slice.call(this) | |
| }, | |
| writable: !0 | |
| }), Array.prototype.flatMap || Object.defineProperty(Array.prototype, "flatMap", { | |
| configurable: !0, | |
| value: function (r) { | |
| return Array.prototype.map.apply(this, arguments).flat() | |
| }, | |
| writable: !0 | |
| }) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
added try catch error handling in case of merged table cells in slides for which the font cannot be changed via appscript - others suggested to used advanced slides API calls to change such cells..