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);
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.