Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Apps Script function to replace text in Google Slides with Google Sheet values
/**
* 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);
});
})
}
@marcyfranks
Copy link

marcyfranks commented May 22, 2022

Will this populate new slides for each row of data?

@atwellpub
Copy link
Author

atwellpub commented May 24, 2022

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.

@NiallPH
Copy link

NiallPH commented Aug 23, 2022

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