Last active
May 2, 2024 05:21
-
-
Save atwellpub/9e217c49e840e3d9709dfbf847b5fa62 to your computer and use it in GitHub Desktop.
Google Apps Script function to replace text in Google Slides with Google Sheet values
This file contains 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
/** | |
* Function to replace text in Google Slides with Google Sheet values | |
* @reference https://hudsonatwell.co/2020/10/03/how-to-use-google-slides-to-autogenerate-featured-images/ | |
*/ | |
function generate_featured_image() { | |
/* get spreadsheet from public view link */ | |
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/1necmbNPUsGJ3fwNiFpgNLbtH6c2RmJDwIQCPuhAfA7s/edit"; //make sure this includes the '/edit at the end | |
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl); | |
/* load active slide deck */ | |
var deck = SlidesApp.getActivePresentation(); | |
/* load google sheet tab from spreadsheet */ | |
var sheet = ss.getSheetByName('Sheet1'); | |
/* get cell values from sheet */ | |
var title = sheet.getRange('A2').getValue(); /* get title from cell A2 */ | |
var subtitle = sheet.getRange('B2').getValue(); /* get subtitle from cell B2 */ | |
var category = sheet.getRange('C2').getValue(); /* get category from cell C2 */ | |
var excerpt = sheet.getRange('D2').getValue(); /* get excerpt from cell D2 */ | |
var slides = deck.getSlides(); | |
/* log our variables just in case we need to check the apps script logs */ | |
Logger.log(ss) | |
Logger.log(sheet) | |
Logger.log(title) | |
Logger.log(subtitle) | |
Logger.log(category) | |
Logger.log(excerpt) | |
/* loop through slide deck slides and replace tokens with variable values */ | |
slides.forEach(function(slide){ | |
var shapes = (slide.getShapes()); | |
shapes.forEach(function(shape){ | |
shape.getText().replaceAllText('{{title}}',title); | |
shape.getText().replaceAllText('{{subtitle}}',subtitle); | |
shape.getText().replaceAllText('{{category}}',category); | |
shape.getText().replaceAllText('{{excerpt}}',excerpt); | |
}); | |
}) | |
} |
Hi @marcyfranks
If I remember correctly, it will only iterate through current slides and replace the tokens with the values. I don't think it will create new slides.
How would you replace text within a table? Been trying but it looks like you'd need to iterate through each slide, then each table, then each cell?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will this populate new slides for each row of data?