Forked from atwellpub/Google App Script - Replace Slide Content Using Google Sheet Data.js
Created
October 14, 2020 20:23
-
-
Save schlos/d7b6b6d40766b7f2a5fb4415eab684bb 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 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
/** | |
* 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