Last active
June 2, 2021 19:35
-
-
Save browlry/24a75f0e5f49f582a8248af81c1d74f2 to your computer and use it in GitHub Desktop.
Google Apps Script to automatically show or hide slides in Google Slides using comma-separated dates in the Speaker Notes
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
// Create a separate Google Slides presentation to hold the inactive slides and paste the URL here: | |
inactiveSlidesUrl = "https://docs.google.com/presentation/d/ABABABABABABABABABABABABABABABABA0/edit" | |
// In your Google Slides presentation, | |
// go to Utilities > Script Editor and paste this code. | |
// | |
// In the Speaker Notes for each slide, | |
// list the dates that slide should be toggled on or off, | |
// in MM/dd/yyyy format, | |
// separated by commas. | |
// | |
// - Correct: 01/01/2020,01/02/2020 | |
// - Wrong: 1/1/2020,1/2/2020 | |
// - Wrong: 1/1/20,1/1/20 | |
// - Wrong: 01/01/2020-01/02/2020 | |
// - Correct: 01/01/2020,01/02/2020,01/01/2021,01/02/2021 | |
// - Wrong: 01/01/2020-01/02/2020,01/01/2021-01/02/2021 | |
// | |
// The script will run each night and move slides | |
// from your presentation to the inactive slides presentation | |
// if today's date is in the Speaker Notes. | |
// | |
// The script will also run each morning and move slides | |
// from the inactive slides presentation to your presentation | |
// if today's date is in the Speaker Notes. | |
// | |
// When the script moves a slide, | |
// today's date is removed from the speaker notes. | |
// | |
// Add a trigger to your script to run the following function every day between 10 PM and 11 PM: | |
function turnOffSlides() { | |
var presentation = SlidesApp.getActivePresentation(); | |
var inactiveSlides = SlidesApp.openByUrl(inactiveSlidesUrl) | |
moveSlidesByDateInSpeakerNotes(presentation,inactiveSlides); | |
} | |
// Add a trigger to your script to run the following function every day between 12 AM and 1 AM: | |
function turnOnSlides() { | |
var presentation = SlidesApp.getActivePresentation(); | |
var inactiveSlides = SlidesApp.openByUrl(inactiveSlidesUrl) | |
moveSlidesByDateInSpeakerNotes(inactiveSlides,presentation); | |
} | |
function moveSlidesByDateInSpeakerNotes(source,destination) { | |
var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy"); // get today's date as a string in MM/dd/yyyy format | |
var slides = source.getSlides(); | |
for (var i = 0; i < slides.length; i = i + 1) { // go through each slide in the presentation | |
var slide = slides[i]; | |
var speakerNotes = slide.getNotesPage().getSpeakerNotesShape().getText(); // get the speaker notes | |
var speakerNotesText = speakerNotes.asString().replace(/\s/g,""); // remove any whitespace | |
if (speakerNotesText === "" || speakerNotesText === undefined) { // if the Speaker Notes are blank, continue to the next slide. | |
continue; | |
} | |
var dates = speakerNotesText.split(","); // use the commas to split the Speaker Notes string into an array of strings | |
var badDate = getBadString(dates,/[0-1][0-9]\/[0-3][0-9]\/[0-9]{4}/) // make sure the dates are in MM/dd/yyyy format | |
if (badDate !== null) { | |
throw "Invalid date on slide #" + (i + 1) + ": '" + badDate + "'. Date must be in MM/dd/yyyy format."; | |
} | |
for (var j = 0; j < dates.length; j = j + 1) { // go through each date | |
if (dates[j] === today) { // if the date matches today's date: | |
dates.splice(j,1); // - remove the date from the list | |
speakerNotes.setText(dates.join()) // - put the modified list of dates back in the speaker notes for that slide | |
destination.appendSlide(slide); // - move the slide to the destination presentation | |
slide.remove(); // - remove the slide from the source presentation | |
break; // - don't check any more dates for this slide; go to the next slide. | |
} | |
} | |
} | |
} | |
function getBadString(strings,regexPattern) { | |
for (var j = 0; j < strings.length; j = j + 1) { | |
if (!regexPattern.test(strings[j])) { | |
return strings[j]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is useful code, thanks for sharing it.